0

Hi im new at stackoverflow and also beginner into c and trying to solve this problem My code is

#include <stdio.h>

int main(){
    int sales[5][4];
    int salesPersons, products=0;
    int value=0;

    printf("Enter salesperon number: ");
    scanf("%d", &salesPersons);
    
    while(salesPersons != 0){
        printf("Enter product number: ");
        scanf("%d", &products);
        printf("Enter value: ");
        scanf("%d", &value);

        if(salesPersons >= 1 && products >= 1 && products < 6 && value >= 0)
            sales[products - 1][salesPersons - 1] += value;
        else
            printf("Invalid input!\n");
        printf("Enter salesperson number (Enter 0 to exit inputting): ");
        scanf("%d", &salesPersons);
    }

    printf("%7s%7s%7s%7s%7s%7s%7s\n", "N", "P1", "P2", "P3", "P4", "P5", "TOTAL");
    int totalProduct[5] = {0}, totalPerson[4] = {0};
    for (int row = 0; row < 5; row++) {
        printf("%7d", row + 1);
            for (int column = 0; column < 5; column++) {
                printf("%7d", sales[row][column]);
                totalProduct[row] += sales[row][column];
                totalPerson[column] += sales[row][column];
        }
        printf("%7d\n", totalProduct[row]);
    }
    printf("%7s%7d%7d%7d%7d%7d\n", "TOTAL", totalPerson[0],totalPerson[1],totalPerson[2],totalPerson[3],totalPerson[4]);

    return 0;
}

I tested with only 1 salesperson, product, and value. But turns out the output printing number it shouldnt printed. Anyone know how to fix this?

Enter salesperon number: 1
Enter product number: 2
Enter value: 3
Enter salesperson number (Enter 0 to exit inputting): 0
      N     P1     P2     P3     P4     P5  TOTAL 
      11928634432  2208215775231      0    1971944432139
      2    197      0-1490587977  32766-14905879781313824304
      3-1490587978  327661928639853  22082-34104450497062219
      4-341044504  327421928639776  22082      01587650096
      5      0      01928638656  22082-1490587712438073026
  TOTAL97002147  8759016138243  99012-1377788055
Jardel Lucca
  • 1,115
  • 3
  • 10
  • 19
MJee
  • 11
  • 3
  • 2
    `int sales[5][4];` ==> `int sales[5][4] = {{0}};` The expression `sales[products - 1][salesPersons - 1] += value;` invokes *undefined behavior* when the content of `sales[products - 1][salesPersons - 1]` is indeterminate (which yours is, as it was declared and defined, but never given any initial state). – WhozCraig Oct 11 '22 at 02:48
  • 1
    @WhozCraig: `sales[products - 1][salesPersons - 1] += value;` does not have undefined behavior due to `sales[products - 1][salesPersons - 1]` having an indeterminate value. Having an indeterminate value simply means some unspecified value is used. That is not undefined behavior; “undefined behavior” means nothing about the behavior is specified by the C standard. But, in this case, the standard specifies part of the behavior, which is that an unpsecified value is used, and the computation otherwise proceeds as usual. – Eric Postpischil Oct 11 '22 at 05:12
  • @WhozCraig, `int sales[5][4] = {0}` works as well – tstanisl Oct 11 '22 at 10:36

1 Answers1

1

As pointed out in the comments section, variables are not being initialized. They would be automatically initialized to zero if they were global or static (see this). You can initialized them in the same way you did for totalPerson, that is, totalPerson[4] = {0};. Note this is the same as totalPerson[4] = {0,};, only the first element is initialized explicitly. When any element of the array is explicitly initialized, all other elements are zero initialized by default (see this). So doing totalPerson[4] = {1}; would initialize it to [1, 0, 0, 0]. Some people prefer the totalPerson[4] = {1,}; notation because of this.

Apart from initialization, there's still another issue: you are allocating only 4 bytes for arrays in which you are trying to use 5 bytes. As arrays are zero-indexed in C, accesses such as the following:

totalPerson[column] += sales[row][column];

Will cause error when column is 4, since indexing with 4 is access to the 5th element of the array, and both totalPerson and sales have only 4 bytes allocated in that dimension.

So change the following line:

int sales[5][4];

With:

int sales[5][5] = {0};

And:

int totalProduct[5] = {0}, totalPerson[4] = {0};

With:

int totalProduct[5] = {0}, totalPerson[5] = {0};

So the program will work as expected:

$ ./program 
Enter salesperon number: 1
Enter product number: 1
Enter value: 10
Enter salesperson number (Enter 0 to exit inputting): 2
Enter product number: 1
Enter value: 20
Enter salesperson number (Enter 0 to exit inputting): 1
Enter product number: 5
Enter value: 50
Enter salesperson number (Enter 0 to exit inputting): 0
      N     P1     P2     P3     P4     P5  TOTAL
      1     10     20      0      0      0     30  
      2      0      0      0      0      0      0   
      3      0      0      0      0      0      0   
      4      0      0      0      0      0      0   
      5     50      0      0      0      0     50  
  TOTAL     60     20      0      0      0   
Jardel Lucca
  • 1,115
  • 3
  • 10
  • 19