You are here:

C/C Programming

Advertisement


Question
QUESTION: I have a program where i have to use 2-Dimensional array using function. 1) Print normal table with region and hardcoded numbers. 2) Print another table with the sum of the numbers given in table.
3) Print the largest number among the region, and print region name.< this is what im having troubles.




#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define ROW 3
#define COLUMN 6
#define MAX 20

/*prototypes*/
void printregion(char region [] [MAX], int table [] [COLUMN]);
void printadd(char region [] [MAX], int table [] [COLUMN]);
void printcompare(int table [] [COLUMN],char region [] [MAX]);

int main()
{

int table[ROW][COLUMN] = {{6,15,14,21,25,23},
        {3,12,19,25,35,15},
        {4,11,11,51,29,27}};

char region[ROW][MAX] = {"Kitchener", "Waterloo", "Guelph"};


printregion(region, table);
printadd(region,table);
printcompare(table,region);


return 0;
}

void printregion(char region [] [MAX], int table [] [COLUMN])
{
int row, column ;          /* counter */
  printf("\n");
     printf("Region\t       May\tJun\tJul\tAug\tSept\tOct\tTotal\t\n");
        printf("_____________________________________________________________________\n");

for(row=0;row<ROW;row++)       
{
  for (column  =  0 ;  column <  MAX ;  column++)     
     printf("%c", region[ row ] [ column ]);  /*print out the region*/
        if(row==2)
         printf("      ");
         printf("\t");
         for (column  =  0 ;  column <  COLUMN ;  column++)     
         printf("%d\t", table[ row ] [ column ]);   /*print out the numbers*/       
         printf("\n");          
   }  /*close for loop*/          
} /*close function*/


  

void printadd(char region [] [MAX], int table [] [COLUMN])

{
int row, column;   
  int sum=0;          /* counter */
     printf("\n");
         printf("Region\t       May\tJun\tJul\tAug\tSept\tOct\tTotal\t\n");
         printf("_____________________________________________________________________\n");

for (row=0;row<ROW;row++)       
  {
     for ( column  =  0 ;  column <  MAX ;  column++)     
        printf("%c", region[ row ] [ column ]);   /*print out the region*/
         if(row==2)
         printf("      ");
         printf("\t");


for (column  =  0 ;  column <  COLUMN ;  column++)         
  printf("%d\t", table[ row ] [ column ]);   /*prints out the table numbers*/
  for (column =0; column<COLUMN; column++)
     sum+=table[row] [column];   /*add all columns*/
        printf("%d",sum);      /*print out the sum*/
         sum=0;   /*to reset answer back to zero for next row sum */
         printf("\n");          
  
}/* closes for loop*/          

}/*closes function*/


void printcompare(int table [] [COLUMN],char region [] [MAX])

  /* Assumes that there is at least one element */

 {
    int a,b,max;
  
    for (a=0;a<ROW;++a)
        for(b=0;b<COLUMN;++b)
         if (table[a][b]>max)
         max=table[a][b];
        
         printf("The maximum array value is %d.\n",max);
         
  }

ANSWER: Hello Alex.

The problem you are having with printing the maximum occurs because the variable max in the printcompare function is not initialized to anything. It has an unpredictable value which is very likely larger than anything in your table and so the condition (table[a][b]>max) is never true.

Add this line:
max = table[0][0];
just before the first for loop, so it looks like this:

  int a,b,max;
  max = table[0][0];

  for (a=0;a<ROW;++a)

and the function will be correct.

Best regards

Zlatko

---------- FOLLOW-UP ----------

QUESTION: I tried what you did i fixed it and got

void printcompare(int table [] [COLUMN],char region [] [MAX])

  /* Assumes that there is at least one element */

{
int a,b,max;
 max = table[0][0];

 for (a=0;a<ROW;++a)
{
        for(b=0;b<COLUMN;++b)    
         if (table[a][b]>max)          
         max=table[a][b];

        }

         
printf("\nThe maximum array value is %d.\n",max);
         
  }


Its right because its getting from the hard-codding, but i want it from getting the sum, which is not picking up, and here is an example picture.

http://img828.imageshack.us/img828/35/resultl.png

Output
Output  
ANSWER: Hi Alex

The sums calculated in printadd are not actually part of the table so they won't be seen by the printcompare function. The printcompare looks only at the table. You could put the sums into the table as they are calculated, but to do so, you will need to make the table bigger by 1 column.

The easiest way to do that is declare the table like this:
int table[ROW][COLUMN+1] = {{6,15,14,21,25,23},
   {3,12,19,25,35,15},
   {4,11,11,51,29,27}};

Unfortunately, that means you will also have to declare all your functions to take a table of COLUMN+1 columns, like this:
/*prototypes*/
void printregion(char region [] [MAX], int table [] [COLUMN+1]);
void printadd(char region [] [MAX], int table [] [COLUMN+1]);
void printcompare(int table [] [COLUMN+1],char region [] [MAX]);
Don't forget to make the actual functions match the prototypes.

You could use another define to make the code neater. I'll leave that up to you.

In printadd, once you know the sum, store it in the table like this:
  printf("%d",sum);          /*print out the sum*/
  table[row][COLUMN]=sum; /* store the sum */
  sum=0;  /*to reset answer back to zero for next row sum */


In printcompare, have the inner for loop go up to and including the COLUMN index, like this:
for(b=0;b<=COLUMN;++b)

I think you are lucky that your printouts are lined up so neatly. On my system, they are skewed. I think the printf("%c" is behaving differently on my system. I see from your screenshot, that you are using Linux. I'm using Windows and the Microsoft compiler. Anyway, I suggest you use printf("%s". You will find it more compact. I've put the complete program below, including the changed printf. You can see how much shorter the code is.

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define ROW 3
#define COLUMN 6
#define MAX 20

/*prototypes*/
void printregion(char region [] [MAX], int table [] [COLUMN+1]);
void printadd(char region [] [MAX], int table [] [COLUMN+1]);
void printcompare(int table [] [COLUMN+1],char region [] [MAX]);

int main()
{

   int table[ROW][COLUMN+1] = {{6,15,14,21,25,23},
   {3,12,19,25,35,15},
   {4,11,11,51,29,27}};

   char region[ROW][MAX] = {"Kitchener", "Waterloo", "Guelph"};


   printregion(region, table);
   printadd(region,table);
   printcompare(table,region);


   return 0;
}

void printregion(char region [] [MAX], int table [] [COLUMN+1])
{
   int row, column ;          /* counter */
   printf("\n");
   printf("Region\t       May\tJun\tJul\tAug\tSept\tOct\tTotal\t\n");
   printf("_____________________________________________________________________\n");

   for(row=0;row<ROW;row++)
   {
       printf("%-15s", region[row]);

       for (column  =  0 ;  column <  COLUMN ;  column++)
         printf("%d\t", table[ row ] [ column ]);   /*print out the numbers*/
       printf("\n");
   }  /*close for loop*/
} /*close function*/




void printadd(char region [] [MAX], int table [] [COLUMN+1])

{
   int row, column;
   int sum=0;          /* counter */
   printf("\n");
   printf("Region\t       May\tJun\tJul\tAug\tSept\tOct\tTotal\t\n");
   printf("_____________________________________________________________________\n");

   for (row=0;row<ROW;row++)
   {
       printf("%-15s", region[row]);

       for (column  =  0 ;  column <  COLUMN ;  column++)
         printf("%d\t", table[ row ] [ column ]);   /*prints out the table numbers*/
       for (column =0; column<COLUMN; column++)
         sum+=table[row] [column];       /*add all columns*/
       printf("%d",sum);          /*print out the sum*/
       table[row][COLUMN]=sum;
       sum=0;  /*to reset answer back to zero for next row sum */
       printf("\n");

   }/* closes for loop*/

}/*closes function*/


void printcompare(int table [] [COLUMN+1],char region [] [MAX])

/* Assumes that there is at least one element */

{
   int a,b,max;
   max = table[0][0];

   for (a=0;a<ROW;++a)
       for(b=0;b<=COLUMN;++b)
         if (table[a][b]>max)
         max=table[a][b];

   printf("The maximum array value is %d.\n",max);

}


---------- FOLLOW-UP ----------

QUESTION: I appreciate what you did, and notice the big change, i just wanted to know what this does

sum+=table[row] [column];       
      printf("%d",sum);          
      table[row][COLUMN]=sum; /*this one*/

Also, I'm trying to configure the compare function to add the string that relates with the 133 output. I tried multiple ways, but nothing, and I'm learning a lot more from this then my teacher. the +Column he never taught us a way doing this, which is sad.

Answer
Hello Alex

I'm glad I'm being helpful to you. There are many ways to solve any problem and I don't know which topics your instructor is emphasizing, so remember that any advice I give you may go against your instructor's intent. All I know is that you wanted the sums in the table so that the printcompare picks out the largest element in the table, including the possibility of picking out one of the sums. For that reason I made the table wider by one column so that the table was COLUMN+1 elements wide. The largest column index would then be the number defined as COLUMN.

In the printadd function, you calculate the sums of each row. Once you have the sum of a row, it is a good opportunity to store the sum in the table and that is the point of the line
table[row][COLUMN]=sum;
It stores the sum of a particular row into the table at the last column position.

As I said, there are many ways to solve a problem. If your table contains only positive numbers, then of course the maximum element will be one of the sums. There is no way for an original table element to be the maximum because the sum for a particular row will always be greater than any element in the corresponding row. So, another way to solve the problem, if you assume only positive numbers in the original table, is to have the printadd function return the maximum sum that it found, instead of storing all the sums in the table. Sometimes one approach is clearly better than another, but often there are many good candidates.

I don't understand what you mean by "configure the compare function to add the string". Do you mean that you want to pass some text in as a parameter? Try to explain to me the end result that you want, and I'll tell you how to do it. Show me one of the ways you tried. Often, when I see someone's attempt, I can get a general idea of what that person is trying to do.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.