Thread: sum rows & columns in array

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    sum rows & columns in array

    Howdy,

    I need to take as input bi-dimensional array and summarize its columns (each column separately) & rows (each row separately).

    This is what I wrote so far:

    Code:
     
    
    #include<stdio.h>
    
    #define ROW 2
    
    #define COL 2
    
    void sumrz(int a[][COL],int *sum_row, int *sum_col);
    
    void main ()
    
    {
    
    int a[ROW][COL],i,j,*sum_row,*sum_col;
    
              for (i=0;i<ROW ;i++)
    
                        for (j=0;j<COL;j++)
    
                        scanf ("%d", &a[i][j]);
    
     
    
               sumrz(a,sum_col,sum_row);
    
     
    
     printf ("array is: \n");
    
              for (i=0;i<ROW;i++)
    
                        for(j=0;j<COL;j++)
    
                        {
    
                        printf("%d",a[i][j]);
    
                        printf ("\n");
    
                        }
    
    }
    
     
    
     
    
    void sumrz(int a[][COL],int *sum_row, int *sum_col)
    
    {
    
                int i,j;
    
     
    
                        for (i=0;i<ROW;i++)
    
                        *sum_row =0;
    
                        for(j=0;j<COL;j++)
    
                        *sum_row+=a[i][j];
    
                        printf ("row sum is %d\n", *sum_row);
    
     
    
                        for (j=0;j<COL;j++)
    
                        *sum_col=0;
    
                        for (i=0;i<ROW;i++)
    
                        *sum_col+=a[i][j];
    
                        printf ("column sum is %d\n", *sum_col);
    
     
    
    }
    It’s clear to me I misuse pointers. Will you help me right it correctly? Are ther any others false? Progarm does't give output.




    Thanx!



    Ronen

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just think about the problem:
    Code:
    sum rows:
        count from zero to row size -1
            sum[thisrownumber] += thisrowofthearray[thiscell];
    Do the same for columns. Basicly, your problem is you need an array which is as big as the total number of rows, so you can store the sum of each row in it. (Or, don't store it, and just display it instead. Then do the same for rows.

    An example:
    Code:
    int array[2][2], rowtotals[2];
    
    sumrows( array, rowtotals );
    I won't do more than the pesudocode here, becuase that already does basicly all the work for you, not that you didn't make an effort.

    Oh, and main returns an int, and you're just using pointers without actually making them point to something:
    Code:
    int a[ROW][COL],i,j,*sum_row,*sum_col;
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That's some poor indentation - so hard to see where the code flow really is.

    I think you meant to do this in main
    Code:
    int a[ROW][COL],i,j,sum_row,sum_col;
    // your code
    sumrz(a,&sum_col,&sum_row);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    sum rows & columns in array

    the problem is I'm not sure how do I treat the pointers. How do I link between them & my result of sum of the rows & columns.
    Please explain it to me with the code of this program, this way I'll hopefully grab it better.

    cheers!

    Ronen

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well following sumrz(a,sum_col,sum_row);

    Try
    printf( "sums=%d,%d\n", sum_col, sum_row);

    Everything else looks OK at first glance
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    I get these 2 warnings from compiler:
    warning C4700: local variable 'sum_col' used without having been initialized
    warning C4700: local variable 'sum_row' used without having been initialized

    it was also before your comment.
    other compiler respond with odd numbers in 'sum_col' & 'sum_rowl'

    Ronen

  7. #7
    Quote Originally Posted by ronenk
    I need to take as input bi-dimensional array and summarize its columns (each column separately) & rows (each row separately).

    This is what I wrote so far:
    • Code reindented
    • misuse of pointers fixed

    Code:
     
    #include<stdio.h>
    
    #define ROW 2
    #define COL 2
    
    void    sumrz (int a[][COL],
                   int *sum_row,
                   int *sum_col);
    
    void main ()
    {
       int     a[ROW][COL],
               i,
               j,
              sum_row,
              sum_col;
    
       for (i = 0; i < ROW; i++)
    
          for (j = 0; j < COL; j++)
    
             scanf ("%d", &a[i][j]);
    
       sumrz (a, &sum_col, &sum_row);
    
       printf ("array is: \n");
    
       for (i = 0; i < ROW; i++)
    
          for (j = 0; j < COL; j++)
          {
             printf ("%d", a[i][j]);
             printf ("\n");
          }
    }
    
    void sumrz (int a[][COL],
                int *sum_row,
                int *sum_col)
    {
       int     i,
               j;
    
       for (i = 0; i < ROW; i++)
    
          *sum_row = 0;
    
       for (j = 0; j < COL; j++)
    
          *sum_row += a[i][j];
    
       printf ("row sum is %d\n", *sum_row);
    
       for (j = 0; j < COL; j++)
    
          *sum_col = 0;
    
       for (i = 0; i < ROW; i++)
    
          *sum_col += a[i][j];
    
       printf ("column sum is %d\n", *sum_col);
    }
    In addition, you seem to have a serious problem with missing { }'s, making your algorithm to run in a strange way. I suggest you always use the curly braces whatever the number of lines in the code structure (real-life shows that this number natural trend is growth).

    An extra detail. I recommend that you define the variables on a unique line for each of them. Factorization is attractive, but it makes the code hard to maintain and to modularize. It also breaks my personnal C-rule:

    The scope of an object shall be reduced to minimum.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    now i got it riht.

    Thanx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Weird errors.
    By Desolation in forum C++ Programming
    Replies: 20
    Last Post: 05-09-2007, 01:10 PM
  3. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM