help me out with this question on arrays

• 08-10-2002
datainjector
help me out with this question on arrays
Okay i just want to ask u guys if i am on the rihgt track....

Question).Use a double-subscript array to solve the following problem.A company has four salespeople (1to4) who sell five different products ( 1 to 5).Once a day, each salesperson passes in a slip for each different type of product sold.Each slip contains:

1. The salesperson number
2. The product number.
3.The total dollar value of that product sold that day.

Thus, each salesperson passes between 0 and 5 sales slip per day.Assume that the information from all of the slips for last month are available.Write a program that will read all this information for the last month's sales and summarize the total sales by salesperson by product.All totals should be stored in the double-subscript array sales.After processing all the information for last month, print the result in a tabular format with each of the colums representing a particular salesperson and each of the rows representing a particular product.Cross total each row to get the total sales of each product for the last month; cross total each colum n to get the total sales by salesperson for last month.Your tabular printout should include these cross totals to right of the of the totaled rows and the bottom of the totaledcolumns.

Well if u guys just explain how the layout would look like that would help alot and here is my code ...Not tried the question yet but just printed the arrays out.......
Code:

```#include <stdio.h> #include <stdlib.h> int main() {  int sales[5][4]= {23,45,32,12,34,56,75,64,37,32,79,70,83,48,52,35,19,76,43,24};  int i, j;  printf ("                Sales Person Number\n"         "                [0]  [1]  [2]  [3]\n");  for ( i =0; i <= 4; i++ ){  printf ("Product_Price[%d] ", i );     for ( j =0; j <= 3; j++ )         printf ("%-5d", sales[i][j] );         printf ( "\n" );         }       system("PAUSE");       return 0; }```
Well in this code i just tried some stuff out..I just wanted to know that do i understand the question is the code below doing alil bit what they are talking about though it might not work but i guess u should know what i am trying to do here ??
Code:

```#include <stdio.h> #include <stdlib.h> int sales_by_product(  int[] [4] ); int sales_by_person (  int[] [4] ); int main() {  int a[5][4]= {23,45,32,12,34,56,75,64,37,32,79,70,83,48,52,35,19,76,43,24};  int sales [2][4]={0};  int i, j;  printf ("                Sales Person Number\n"         "                [0]  [1]  [2]  [3]\n");  for ( i =0; i <= 4; i++ ){  printf ("Product_Price[%d] ", i );     for ( j =0; j <= 3; j++ )         printf ("%-5d", a[i][j] );         printf ( "\n" );         }   for ( i=0; i = 0; i++ ){       for ( j=0; j <=3; j++ ) {       sales[i][j]=sales_by_product( a[3][4] );       printf ("%d", sales[i][j] );       }             }       system("PAUSE");       return 0; } int sales_by_product (  int product[] [4] ) {  int row, column;  int cnt;  for ( row = 0; row <=0; row++ ){     for ( column= 0; column <= 3; column++ )     return ++product[row][column];     } }```

Thanks alot
• 08-11-2002
Salem
Well you certainly have the use of 2D arrays pretty much sorted out, even the passing of 2D arrays to functions.

1. Use some #defines for the array dimensions
#define N_SALES 4
#define N_PRODUCTS 5

Then you have
Code:

```int sales[N_SALES][N_PRODUCTS]; for ( s = 0 ; s < N_SALES ; s++ ) {   for ( p = 0 ; p < N_PRODUCTS; p++ ) {     // do stuff with sales[s][p]   } }```
Makes it easier to read, and MUCH easier to change when you employ a new salesperson, or add a new product line

> sales[i][j]=sales_by_product( a[3][4] );
Given your declaration of this function, the correct call would be
sales[i][j]=sales_by_product( a );

> Okay i just want to ask u guys if i am on the rihgt track
Go for it :)
• 08-11-2002
datainjector
well i have tried alil....
Code:

```#include <stdio.h> #include <stdlib.h> #define A_SIZE 5 #define P_SIZE 4 int sales_by_product(  int[] [P_SIZE], int , int ); int sales_by_person (  int[] [P_SIZE], int, int ); int main() {  int a[5][4]= {23,45,32,12,34,56,75,64,37,32,79,70,83,48,52,35,19,76,43,24};  int sales [2][4]={0};  int i, j;  printf ("                Sales Person Number\n"         "                [0]  [1]  [2]  [3]\n");  for ( i =0; i <= A_SIZE-1; i++ ){  printf ("Product_Price[%d] ", i );     for ( j =0; j <= P_SIZE-1; j++ )         printf ("%-5d", a[i][j] );         printf ( "\n" );         }       for ( j=0; j <=3; j++ ) {       sales[0][j]=sales_by_product( a, P_SIZE, A_SIZE );       printf ("%d ", sales[0][j] );               }       system("PAUSE");       return 0; } int sales_by_product (  int product[] [P_SIZE], int SIZE_P, int SIZE_A ) {  int row, column;  int cnt=0, sum=0;     for ( row =0; row <= SIZE_A-1; row++ ){     for ( column= 0; column <= SIZE_P-1; column++ )     sum += product[row][column];     return sum;             } }```
Salem thanks for reminding me how usefull #define is .Know i now i am on the right track ...thats the good new but the bad new is ...Well c i want to calculate the total of column 0,1,2,3 and 4 for all the rows ..well i am only getting the awnser for one row and why is that ...Well is it my function i dont c any problem with it??? Help me out to spot the problem guys

Thanks alot
• 08-11-2002
Hammer
You need to indent your code properly, then you would see the error (or at least one of them, I haven't looked for more).

Here is one of your functions reformatted. I won't tell you the mistake, it speaks for itself. :)
Code:

```int sales_by_product(int product[][P_SIZE], int SIZE_P, int SIZE_A) {     int row, column;     int cnt = 0, sum = 0;     for (row = 0; row <= SIZE_A - 1; row++)     {         for (column = 0; column <= SIZE_P - 1; column++)             sum += product[row][column];         return(sum);     } }```
• 08-11-2002
datainjector
hammes it still not working
hammer i dint c the error and my dam code is not working i am so ........ed:mad:
• 08-11-2002
Hammer
Re: hammes it still not working
Quote:

Originally posted by datainjector
hammer i dint c the error and my dam code is not working i am so ........ed:mad:
OK, look at where the return statement is sitting (in the code I posted). Is it in the correct place?
• 08-11-2002
datainjector
hammer ....
Well the return statment seems fair to me .. i did take close look at the code.. but i can still find that buggy bug seems today is ma bad day:o
• 08-11-2002
Hammer
The return statement is within the outer loop. This means that you process only one row, then return.

Maybe you meant something more like this:
Code:

```for (row = 0; row <= SIZE_A - 1; row++) {         for (column = 0; column <= SIZE_P - 1; column++)         {             sum += product[row][column];         } } return(sum);```
This doesn't fix your problem though. Look at this:
>sales[0][j] = sales_by_product(a, P_SIZE, A_SIZE);
Each time you call the sales_by_product() function you are passing it the same parameters, which means you are always going to get the same answer, despite the fact you're doing it within a loop.

And lastly, as a matter of formatting, if you're trying to sum up the rows, then really you should show the total value on the end of each row as opposed to the bottom. You would then end up with something like this:
Code:

```                Sales Person Number                 [0]  [1]  [2]  [3] Product_Price[0] 23  45  32  12  112 Product_Price[1] 34  56  75  64  229 Product_Price[2] 37  32  79  70  218 Product_Price[3] 83  48  52  35  218 Product_Price[4] 19  76  43  24  162```
This can be achieved by changing the loop in main to something like this, and negating the need for the seperate function.:
Code:

```for (i = 0; i <= A_SIZE - 1; i++) {     sum = 0;     printf("Product_Price[%d] ", i);     for (j = 0; j <= P_SIZE - 1; j++)     {             printf("%-5d", a[i][j]);             sum += a[i][j];     }     printf ("%d\n", sum); }```
If I'm way off base here with what you want, please forgive me! ;)
• 08-11-2002
datainjector
thanks alot buddy
hey thanks alot ..Wow since u helped me out with ths stuff after i finish this dam program ..i am gonna make a turtle graphic program ;) Wish me luck man :)
• 08-11-2002
datainjector
umm... almost done hammer
Code:

```#include <stdio.h> #include <stdlib.h> #define A_SIZE 5 #define P_SIZE 4 int main() {  int a[5][4]= {23,45,32,12,34,56,75,64,37,32,79,70,83,48,52,35,19,76,43,24};  int sales [2][4]={0};  int i, j, cnt=0,b,sum=0, sum2=0,row,column,x;  printf ("                Sales Person Number\n"         "                [0]  [1]  [2]  [3]\n");  for ( i =0; i <= A_SIZE-1; i++ ){  printf ("Product_Price[%d] ", i );  sum =0;     for ( j =0; j <= P_SIZE-1; j++ )     {  printf ("%-5d", a[i][j] );         sum += a[i][j];     }     for ( b =0; b <= 3; b++ )         sales[0][b]= sum;     printf ("%d ", sales[0][3]);   printf ("\n");   /*Find sales per person*/   for ( column =0; column <= P_SIZE-1; column++ )   {   sum2=0;     for ( row = 0; row = A_SIZE-1; row++ )     sum2+= a[row][column];   }   for ( x = 0; x <= 3; x++ )       sales[1][x] = sum2;   printf ("%d", sales[1][3] );   }       system("PAUSE");       return 0; }```
Thanks to u hammer i am almost completed my work 90 percent :D Well but i still have some problem i thing the algorithim ... the one to add each person product together ....Thanks alot
• 08-12-2002
Hammer
To print the totals of the sales by person, you need to loop through each of the rows in the array. Don't do this within the loop you already have (like you're trying to do).

Here's what you'll need:
Code:

```    for (column = 0; column < P_SIZE; column++)     {         sum2 = 0;         for (row = 0; row < A_SIZE; row++)                 sum2 += a[row][column];         printf ("%-5d", sum2);             } /* Output from this loop: 196  257  281  205 */```