#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Summation of elements in two-dimensional array


    Here's the code I am trying to use:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int a[5][5], row, col, sum1 = 0, sum2 = 0;
    
    	for (row = 0; row < 5; row++) {
    		printf("Enter row %d: ", row + 1);
    		for (col = 0; col < 5; col++) {
    			scanf("%d", &a[row][col]);
    		}
    	}
    	printf("\nRow totals: ");
    	for (row = 0; row < 5; row++) {
    		sum1 += a[row][col];
    		printf(" %d", sum1);
    	}
    	printf("\nColumn totals: ");
    	for (col = 0; col < 5; col++) {
    		sum2 += a[row][col];
    		printf(" %d", sum2);
    	}
    	return 0;
    }
    There are 5 rows and 5 columns and input will be provided for each of them. The program is then supposed to print out the sum of all the elements in row 1 and then the sum of elements of row 2 and so on, in a single line, all separated by a space. After that, it should print out the sum of all the elements in column 1 and then the sum of elements of column 2 and so on, in another single line, all separated by a space. But when input is provided, it prints out numbers which shouldn't be there. What is exactly wrong with this code?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    When you try to print the row and column totals you need to use two loops, similar to your entry loop. Don't forget to reset sum after you print it.

    Jim

    Comments on this post

    • rodiongork agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by jimblumberg
    When you try to print the row and column totals you need to use two loops, similar to your entry loop. Don't forget to reset sum after you print it.

    Jim
    I am not sure how to incorporate the resetting sum in the middle of the two loops. Could you provide a demonstration?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Show your current code.

    What have you tried?


    Jim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by jimblumberg
    Show your current code.

    What have you tried?


    Jim
    I am not sure how this all fits in:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int a[5][5], row, col, sum1 = 0, sum2 = 0;
    
    	for (row = 0; row < 5; row++) {
    		printf("Enter row %d: ", row + 1);
    		for (col = 0; col < 5; col++) {
    			scanf("%d", &a[row][col]);
    		}
    	}
    	printf("\nRow totals: ");
    	for (row = 0; row < 5; row++) {
    		for (col = 0; col < 5; col++) {
    			sum1 += a[row][col];
    			printf(" %d", sum1);
    		}
    	}
    	printf("\nColumn totals: ");
    	for (col = 0; col < 5; col++) {
    		for (row = 0; row < 5; row++) {
    			sum2 += a[row][col];
    			printf(" %d", sum2);
    		}
    	}
    	return 0;
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Your printf() statements should probably be outside the inner loop, followed by setting sum to zero.

    Jim

    Comments on this post

    • arman.khandaker agrees
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by jimblumberg
    Your printf() statements should probably be outside the inner loop, followed by setting sum to zero.

    Jim
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int a[5][5], row, col, sum1 = 0, sum2 = 0;
    
    	for (row = 0; row < 5; row++) {
    		printf("Enter row %d: ", row + 1);
    		for (col = 0; col < 5; col++) {
    			scanf("%d", &a[row][col]);
    		}
    	}
    	printf("\nRow totals: ");
    	for (row = 0; row < 5; row++) {
    		for (col = 0; col < 5; col++) {
    			sum1 += a[row][col];
    		}
    		printf(" %d", sum1);
    		sum1 = 0;
    	}
    		
    	printf("\nColumn totals: ");
    	for (col = 0; col < 5; col++) {
    		for (row = 0; row < 5; row++) {
    			sum2 += a[row][col];
    		}
    		printf(" %d", sum2);
    		sum2 = 0; 
    	}
    	return 0;
    }
    The above code worked like a charm! Thanks a lot sir! :D

IMN logo majestic logo threadwatch logo seochat tools logo