Thread: Why does my two-dimensional array print out incorrect values?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    5

    Why does my two-dimensional array print out incorrect values?

    I'm trying to print the sum of columns 1, 4 and 6. When I input 30 values into the 2d array, only the fist column is has the correct output.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int i, j, sum1=0, sum2=0, sum3=0;
        int arr[5][6];
    
    
        for(i=0; i < 5; i++){
            for(j=0; j<6; j++){
                printf("Enter a value: ");
                scanf("%d",&arr[i][j]);
                ;
            }
        }
    
    
        printf("Elements of 2D array:- \n");
       for(i=0; i<5; i++) {
          for(j=0; j<6; j++) {
             printf("%d ", arr[i][j]);
             if(j==5){
                printf("\n");
             }
          }
       }
    
    
    //this functions calculate the sum of the 1st column//
        for(i=0; i<1; i++){
            for(j=0; j<6; j++){
               sum1 += arr[j][i];
            }
        printf("Sum of 1st column is: %d\n", sum1);
            sum1=0;
       }
    
    
    
    
    //this functions calculate the sum of the 4th column//
       for(i=0; i<1; i++){
            for(j=3; j<6; j++){
               sum2 = arr[j][i] + sum2;
               }
        printf("Sum of 4th column is: %d\n", sum2);
            sum2=0;
       }
    
    
    //this functions calculate the sum of the 6th column//
        for(i=0; i<1; i++){
               for(j=5; j<6; j++){
               sum3 = arr[j][i] + sum3;
               }
        printf("Sum of 6th column is: %d\n", sum3);
            sum3=0;
       }
    
    
        return 0;
    }
    Attached Images Attached Images Why does my two-dimensional array print out incorrect values?-array-png 

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    627
    Code:
    #include <stdio.h>
     
    #define ROWS 5
    #define COLS 6
     
    int main()
    {
        int arr[ROWS][COLS];
        for (int r = 0; r < ROWS; r++)
            for (int c = 0; c < COLS; c++) {
                printf("Enter a value: ");
                scanf("%d", &arr[r][c]);
            }
     
        printf("Elements of 2D array:-\n");
        for (int r = 0; r < ROWS; r++) {
            for (int c = 0; c < COLS; c++)
                printf("%3d ", arr[r][c]);
            printf("\n");
        }
     
        int sum = 0;
        for (int r = 0; r < ROWS; r++)
            sum += arr[r][0];
        printf("Sum of 1st column is: %d\n", sum);
     
        sum = 0;
        for (int r = 0; r < ROWS; r++)
            sum += arr[r][3];
        printf("Sum of 4th column is: %d\n", sum);
     
        sum = 0;
        for (int r = 0; r < ROWS; r++)
            sum += arr[r][5];
        printf("Sum of 6th column is: %d\n", sum);
     
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    5
    Thank you bro, I appreciate your help!
    It finally work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two dimensional array not changing values
    By YayIguess in forum C Programming
    Replies: 1
    Last Post: 08-31-2015, 01:04 PM
  2. incorrect values in array
    By johnmerlino in forum C Programming
    Replies: 1
    Last Post: 04-06-2014, 08:36 PM
  3. incorrect print array help
    By stlninja7 in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2012, 09:25 PM
  4. print 2-dimensional array
    By marcoesteves in forum C Programming
    Replies: 3
    Last Post: 11-25-2010, 11:23 AM
  5. How do I print out a two dimensional array of strings?
    By nellosmomishot in forum C Programming
    Replies: 6
    Last Post: 11-23-2008, 03:47 PM

Tags for this Thread