Thread: 5x5 Array Problem - Any assistance appreciated...

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

    5x5 Array Problem - Any assistance appreciated...

    Writing a program that contains a 5x5 array and then prints the row totas and the column totals. The row total output is fine. But the output of the column total is wrong. The total after each individual column is added to the variable is output rather than the actually final sum of all the columns.

    Code:
    #include <stdio.h>
    
     
    intmain ()
    {
    int a[5][5];
    int row_tot = 0, i = 0, j = 0, col_tot= 0;
    
    for (i = 0; i < 5; i++){
    
    printf("Enter row %d: ", i + 1);
    
    for (j = 0;j < 5; j++){
    scanf("%d", &a[i][j]);
    }
    }
    printf("\n\nRow totals: ");
    for (i = 0; i < 5; i++){
    row_tot= 0;
    
    for (j = 0; j < 5; j++){
    row_tot+= a[i][j];
    printf("%d ", row_tot);
    }
    }
    
    printf("\nColumn totals: ");
    
    for (j = 0; j < 5; j++){
    col_tot = 0;
    
    for (i = 0; i < 5; i++) {
    col_tot += a[i][j];
    printf("%d ", col_tot);
    }
    }
    
    return 0;
    }
    

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First in future please post code as plain text.

    Next your problem seems to be the placement of your printf() in your loops. Move the printf() to after the inner for() loop, instead of inside the inner loop.

    The row total output is fine.
    Not really it has the same problem as the column printout. You should only have 5 totals, your code produces much more data than that (5 times as many entries than what is required).

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    I apologise, new to the site. The output from the row totals is correct though e.g.:

    R1: 1 2 1 2 1
    R2: 1 2 2 2 2
    R3: 3 3 3 3 3
    R4: 4 4 4 4 4
    R5: 5 5 5 5 5

    Row totals: 7 9 15 20 25
    Column totals: 14 16 15 16 15


    Is there any particular reason why i'm still getting the correct answer for the row totals?
    Thanks for the help

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    This is the output I got from your program:

    Code:
    Row totals: 1 4 9 11 21 1 4 9 11 31 1 4 9 11 41 1 4 9 11 51 1 4 9 11 61 
    Column totals: 1 2 3 4 5 3 6 9 12 15 5 10 15 20 25 2 4 6 8 10 10 30 60 100 150
    And the output from the program after the suggested changes:
    Code:
    Row totals: 21 31 41 51 61 
    Column totals: 5 15 25 10 150
    This is the values I used for the array:

    Code:
       int a[5][5] = {{1,3,5,2,10},{1,3,5,2,20},{1,3,5,2,30},{1,3,5,2,40},{1,3,5,2,50}};
    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array as function parameter problem, help appreciated.
    By somekid413 in forum C Programming
    Replies: 4
    Last Post: 01-17-2010, 04:34 PM
  2. Replies: 26
    Last Post: 10-24-2009, 11:40 PM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 12:06 PM
  4. reverse an array - help appreciated
    By Vireyda in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2004, 12:52 PM
  5. Array assistance
    By BungleSpice in forum C Programming
    Replies: 4
    Last Post: 03-13-2004, 01:34 AM