2

I am trying to assign user input into an array; however, the program below only picks up on the first element in each line of input. The ultimate goal of this program is to find the diagonal sums of integers and return the absolute value of their difference.

Example input (note that the first number gives the number of rows and columns (square array):

Input: 3

11 2 4

4 5 6

10 8 -12

Output: Expected = 15 Actual = 10

I realize that the issue lies in the way that the array is setup. If I print the array out I get: 111555999

Any hints/help would be very appreciated.

int main() {
    int n, i, c, multi_array[200][200], sum1 = 0, sum2 = 0;

    scanf("%i", &n);   //N = number of rows and number of columns (square 2D array)

    for (i = 0; i < n; i++) {
        for (c = 0; c < n; c++) {
            scanf("%d ", &multi_array[c][i]);   //enter integers to store in array
        }
    }

    for (i = 0; i != n; i++) {
        sum1 += multi_array[i][i];    //add up top left to bottom right diagonal
    }

    for (i = 0; i != n; i++) {
        sum2 += multi_array[i][n-i];    //add up top right to bottom left diagonal
    }
    printf("%i", abs(sum1 - sum2));    //print absolute value of the difference between diagonals
    return 0;
}
4
  • Please indent your code. So your question is how to read a 2D array, right? Commented Nov 19, 2015 at 20:06
  • It should be scanf("%d ", &multi_array[i][c]); instead. Commented Nov 19, 2015 at 20:08
  • @LuizEduardoF. has a point. That way you read in column major order. Is that what you want? But that's not the only problem... Commented Nov 19, 2015 at 20:09
  • 1
    Your indexing calculation is wrong for the second diagonal is wrong. Change sum2 += multi_array[i][n-i]; to sum2 += multi_array[i][n-i-1];. Commented Nov 19, 2015 at 20:14

2 Answers 2

4

Your major problem is here, where you go out of bounds:

for (i = 0; i != n; i++) {
  sum2 += multi_array[i][n - i]; // when i is 0, th
}

When i = 0, you are accessing multi_array[0][3], which is out of bounds when N = 3.

So change it to this:

multi_array[i][n - i - 1]

You should read your array like this:

for (i = 0; i < n; i++) {
  for (c = 0; c < n; c++) {
    scanf(" %d ", &multi_array[i][c]);
  }
}

since C stored its arrays in row-major order. What you have stores the array in column-major order. It's not wrong, but it's something you do only if you really have to.


Finally, change again the input part of your code to this:

scanf("%d", &n);

for (i = 0; i < n; i++) {
    for (c = 0; c < n; c++) {
        scanf("%d", &multi_array[i][c]);
    }
}

so that you have to input exactly what you need to. With your initial code I have to type an extra random number when I had completed the input process.


Last but not least, I am posting the whole code, where I have wrote some extra printf()'s, which are actually for the programmer, so that he can see step-by-step if his code is acting as expected or not.

#include <stdio.h>
#include <stdlib.h>     /* abs */

int main() {
    int n, i, c, multi_array[200][200], sum1 = 0, sum2 = 0;

    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        for (c = 0; c < n; c++) {
            scanf("%d", &multi_array[i][c]);
        }
    }

    for (i = 0; i < n; i++) {
        for (c = 0; c < n; c++) {
            printf("|%d|", multi_array[i][c]);
        }
        printf("\n");
    }

    for (i = 0; i != n; i++) {
        sum1 += multi_array[i][i];
    }
    printf("sum1 is %d\n", sum1);

    for (i = 0; i != n; i++) {
        sum2 += multi_array[i][n - i - 1];
    }
    printf("sum2 is %d\n", sum2);

    printf("%i", abs(sum1 - sum2));
    return 0;
}

Output:

3
11 2 4
4 5 6
10 8 -12
|11||2||4|
|4||5||6|
|10||8||-12|
sum1 is 4
sum2 is 19
15
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for the help! This is the second time where small issues threw me for a loop. New to C and Stack Overflow. The help here has been tremendous already. Again, thank you.
@JoeyGrant if you treat SO good, it will will treat you good². Glad that I helped!
3

You are clearly going out of bounds here:

   for (i = 0; i != n; i++) {
        sum2 += multi_array[i][n-i];    //add up top right to bottom left diagonal
    }

When i is equal to 0 the expression n-i will be equal to n, but the range of the array is from 0 to n-1. The code will read uninitialized values and cause undefined behavior.

The second array index should be 1 less.

3 Comments

Yeah C++ keyword, I saw your answer, but then you deleted it, so I couldn't comment that what you were saying was correct. So I posted mine. Then you un-deleted it. However, now I have expanded my answer, so I will not delete it, I hope that you see why! +1 for going there first.
@gsamaras Sure, there is nothing wrong with duplicate answers in general.
Well duplicates are not good when it comes to answers. The future reader wants to see fast what he needs to see, not double read things. If it wasn't for my answer been expanding I would have deleted it. Happy for you not getting it wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.