Search code examples
arrayscsumlinevar

Can't seem to sum multiple array lines to multiple int variables in c language


I'm a first grade student atm. and I'm struggling to finish my "homework" with c language. Right now I'm trying to apply 4 array lines sum to 4 int variables and here's what I've achieved so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int dvimatisMas[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16} };
    int eiluciuSum[4];

for(int i = 0; i < 4; i++){

    for(int x = 0; x < 4; x++){

        for(int j = 0; j < 4; j++){

            eiluciuSum[i] += dvimatisMas[x][j];

        }

    }

}

printf("suma 1 eil: %d\n", eiluciuSum[1]);
printf("suma 2 eil: %d\n", eiluciuSum[2]);
printf("suma 3 eil: %d\n", eiluciuSum[3]);
printf("suma 4 eil: %d\n", eiluciuSum[4]);

int min = 0;
int max = 0;

}

it simply gives out a bunch of answers even though there should be just 4. As you can see in the code I've tried to correct this by writing 4 separate prints and specifying each bracket for them so yeah I obviously get 4 answers like I should but they aren't correct and still I shouldn't be specifying all of that anyway.

If I write something more "simple" I get the desired result:

int dvimatisMas[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16} };
int eilute1 = 0;
int eilute2 = 0;
int eilute3 = 0;
int eilute4 = 0;

for(int i = 0; i < 4; i++){

    eilute1 += dvimatisMas[0][i];
    eilute2 += dvimatisMas[1][i];
    eilute3 += dvimatisMas[2][i];
    eilute4 += dvimatisMas[3][i];

}

printf("suma 1 eil: %d\n", eilute1);
printf("suma 2 eil: %d\n", eilute2);
printf("suma 3 eil: %d\n", eilute3);
printf("suma 4 eil: %d\n", eilute4);

But the problem is that I also have to print out the smallest number out of 'eilute' as well as the biggest. But I can't get to that point since I need 'eilute[]' (I called it 'eiluciuSum[]' in the first code) to be expanded by the code and to use it in an 'if' statement like "if(eilute[i] < 0){ min += eilute[i]; printf("smallest: %d", min)" etc. and thats how I'm supposed to do it (well at the very least something like that instead of bunch of complicated equations and I mean it is more short and 'professional' am I right?).

If I managed to explain my situation understandably could someone help and explain on what I'm doing wrong in the first provided code?

Thank you in advance.


Solution

  • The final version of your code is at the bottom.

    Firstly, you don't need the variable x. Cut out the second for loop.

    for(int i = 0; i < 4; i++) {
    
        for(int j = 0; j < 4; j++) {
    
            eiluciuSum[i] += dvimatisMas[i][j];
    
        }
    
    }
    

    Secondly, the problem is that the eiluciuSum array is not initialized to zero. It already contains a garbage value in it from whatever was stored in that memory address before. You should replace its definition with this:

    int eiluciuSum[4] = { 0 };
    

    Then just put the printf statement after the inner loop like this:

    for (int i = 0; i < 4; i++) {
    
        for (int j = 0; j < 4; j++) {
    
            eiluciuSum[i] += dvimatisMas[i][j];
    
        }
    
        printf("%d\n", eiluciuSum[i]);
    
    }
    

    And it should work fine without the 4 printf statements below.

    Thirdly, in regards to finding the maximum/minimum values, all you need to do is set the max/min variables to the first element in the array and then loop through the array, looking for values larger/smaller than whatever is the current largest/smallest, and switch those values.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int dvimatisMas[4][4] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13, 14, 15, 16} };
        int eiluciuSum[4] = { 0 }; // initialize all elements of array to 0
    
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                eiluciuSum[i] += dvimatisMas[i][j];
            }
            printf("%d\n", eiluciuSum[i]);
        }
    
        int min = eiluciuSum[0]; // assume the first sum is the smallest
        int max = eiluciuSum[0]; // assume the first sum is the largest
    
        // loop through all elements of array and if you encounter one bigger/smaller
        // than the current max/min, then make those the current max/min values
        for (int i = 0; i < 4; i++) {
            if (eiluciuSum[i] < min)
                min = eiluciuSum[i];
            if (eiluciuSum[i] > max)
                max = eiluciuSum[i];
        }
        printf("The smallest sum is %d\n", min);
        printf("The largest sum is %d\n", max);
    }