Thread: Multi-Array Problem: Calculate Sum of Rows and Columns (Beginner)

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    4

    Multi-Array Problem: Calculate Sum of Rows and Columns (Beginner)

    Hello,

    First time poster and learning about arrays.

    The problem asks to build a 5x5 matrix and then to calculate the sum of the rows and columns.

    The code (pasted below) works well for single digits, but when I input the number '10,' the program goes haywire. (Note: an input of 100, 20, 30, etc works....)

    Any reason why my arrays aren't reading the number '10'? Appreciate your comments!

    Thanks.

    Code:
    #include <stdio.h>
    
    
    #define N 5
    
    
    int main(){
        
        int mat[N][N] = {0};
        int ch, col, row, i, sum = 0;
        
        
            for (row = 0; row < N; row++){
             printf("\nEnter row %d: ", row + 1);
            for (col = 0; col < N; col++){
                scanf(" %d", &ch);
                if (ch != '\n'){
                mat[row][col] = ch;
                }
                else
                    break;
            }
        
            }
        
        printf("\n\n");
        printf("Row totals:");
        
        for (row = 0; row < N; row++)
            for (col = 0; col < N; col++){
           
            i = mat[row][col];
            sum += i;
                if (col == 4){
                    printf(" %d", sum);
                    sum = 0;
                    break;
                }
                
        }
        
        printf("\n\n");
        printf("Column totals:");
        
        for (col = 0; col < N; col++)
            for (row = 0; row < N; row++){
                
                i = mat[row][col];
                sum += i;
                if (row == 4){
                    printf(" %d", sum);
                    sum = 0;
                    break;
                }
                
            }
    
    
        
         printf("\n\n");
        
        return 0;
    Last edited by SanFi; 1 Week Ago at 07:52 PM.

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    490
    You are breaking the loop when the input value == '\n'. The numerical value of '\n' is (commonly) 10. I would just get rid of that since you don't really want to break the loop early anyway.
    Code:
    #include <stdio.h>
     
    #define N 5
     
    int main() {
        int mat[N][N] = {{0}};
     
        for (int row = 0; row < N; row++) {
            printf("\nEnter row %d: ", row + 1);
            for (int col = 0; col < N; col++)
                scanf("%d", &mat[row][col]);
        }
     
        printf("\n\nRow totals:");
        for (int row = 0; row < N; row++) {
            int sum = 0;
            for (int col = 0; col < N; col++)
                sum += mat[row][col];
            printf(" %d", sum);
        }
     
        printf("\n\nColumn totals:");
        for (int col = 0; col < N; col++) {
            int sum = 0;
            for (int row = 0; row < N; row++)
                sum += mat[row][col];
            printf(" %d", sum);
        }
        printf("\n\n");
          
        return 0;
    }
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    4
    Thanks! Also, your code is obviously much cleaner, and I learned a couple of things from it.

    Just out of curiosity: would you prefer to use scanf over getchar when dealing with numbers only? For example, would the use of getchar to register numerical inputs be recommended in this program?

    Quote Originally Posted by john.c View Post
    You are breaking the loop when the input value == '\n'. The numerical value of '\n' is (commonly) 10. I would just get rid of that since you don't really want to break the loop early anyway.
    Code:
    #include <stdio.h>
     
    #define N 5
     
    int main() {
        int mat[N][N] = {{0}};
     
        for (int row = 0; row < N; row++) {
            printf("\nEnter row %d: ", row + 1);
            for (int col = 0; col < N; col++)
                scanf("%d", &mat[row][col]);
        }
     
        printf("\n\nRow totals:");
        for (int row = 0; row < N; row++) {
            int sum = 0;
            for (int col = 0; col < N; col++)
                sum += mat[row][col];
            printf(" %d", sum);
        }
     
        printf("\n\nColumn totals:");
        for (int col = 0; col < N; col++) {
            int sum = 0;
            for (int row = 0; row < N; row++)
                sum += mat[row][col];
            printf(" %d", sum);
        }
        printf("\n\n");
          
        return 0;
    }

  4. #4
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    490
    Quote Originally Posted by SanFi View Post
    would you prefer to use scanf over getchar when dealing with numbers only? For example, would the use of getchar to register numerical inputs be recommended in this program?
    scanf is more appropriate than getchar for numerical input. getchar reads a character at a time, so you would need to build up a multi-digit number one character at a time, something like:
    Code:
    #include <stdio.h>
    #include <ctype.h>  // isdigit and friends
     
    int main()
    {
        int n = 0;  // the integer we are building up
     
        printf("Enter a integer: ");
     
        // getchar returns a character code as an int
        for (int c; isdigit(c = getchar()); )
        {
            // Multiply n by 10 to shift its decimal digits over.
            // Subtract the character code for '0' from c
            // to get it's numerical digit value, and add it to n.
     
            n = n * 10 + (c - '0');
        }
     
        printf("\nn: %d\n", n);
     
        return 0;
    }
    Note that in the above program if you enter an integer larger than MAX_INT (for 32-bits it's 2147483647) then n will overflow and give the wrong result. This is generally the case for scanf, too.

    Probably the best option for reading an integer is to read a line of input as a string and then parse it for an the integer with strtol. This allows better error detection (particularly out-of-range errors which scanf doesn't generally detect at all). It's a little involved to check all the possible errors, though.
    Let him who is not come to logic be plagued with continuous and everlasting filth.
    - John of Salisbury, 1160

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array counting Rows and Columns:
    By jocdrew21 in forum C Programming
    Replies: 7
    Last Post: 07-21-2013, 02:07 PM
  2. Swapping Rows and Columns in a 2D array
    By xxshankar in forum C Programming
    Replies: 2
    Last Post: 03-11-2010, 03:40 PM
  3. Addition of the rows and columns in an array.
    By Turtal in forum C Programming
    Replies: 4
    Last Post: 11-14-2006, 09:00 PM
  4. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  5. printing rows and columns
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 08-03-2003, 05:42 PM

Tags for this Thread