c help arrays

This is a discussion on c help arrays within the C Programming forums, part of the General Programming Boards category; I am a newbie trying create an array that will sum the row and columns. I have been able to ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    c help arrays

    I am a newbie trying create an array that will sum the row and columns. I have been able to get them to display,but not total. Any help would be great.

    Code:
    #include <stdio.h>
    void main(void)
    {
      int val[5][5] = {8,3,9,0,10,
                       3,5,17,1,1,
                      2,8,6,23,1,
    	       15,7,3,2,9,
    	       6,14,2,6,0};
      void display (int nums[5][5]);  /* function prototype */
    
      display(val);
    }
    void display(int nums[5][5])
    
    {
      int row_num, col_num;
      for (row_num = 0; row_num <5; ++row_num)
      {
        for(col_num = 0; col_num <5; ++col_num)
          printf("%5d",nums[row_num][col_num]);
        printf("\n");
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    So want every part of the array to be sumed with all the other parts, if not can you say it again, i didnt really understand.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    C help arrays

    I need to know how to total all the rows and columns in the arrays

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something like this.
    Code:
    #include <stdio.h>
    
    void display(int nums[5][5])
    {
       int r, c;
       for ( r = 0; r < 5; ++r )
       {
          int sum = 0;
          for ( c = 0; c < 5; ++c )
          {
             printf("%5d ", nums[r][c]);
             sum += nums[r][c];
          }
          printf("sum = %d\n", sum);
       }
    }
    
    int main(void)
    {
       int val[5][5] =
       {
          {8,3,9,0,10},
          {3,5,17,1,1},
          {2,8,6,23,1},
          {15,7,3,2,9},
          {6,14,2,6,0},
       };
       display(val);
       return 0;
    }
    
    /* my output
        8     3     9     0    10 sum = 30
        3     5    17     1     1 sum = 27
        2     8     6    23     1 sum = 40
       15     7     3     2     9 sum = 36
        6    14     2     6     0 sum = 28
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    Hi,

    I have made some changes to your program, read my comments.

    Code:
    #include <stdio.h>
    
    int display (int nums[5][5]);  /*This function will return the total*/ 
    
    int main(void)   /*main should ALWAYS return an integer*/
    {
      int val[5][5] = {8,3,9,0,10,
                              3,5,17,1,1,
                              2,8,6,23,1,
    	          15,7,3,2,9,
    	          6,14,2,6,0};
      int total;
      
      total=display(val);   /*Returned back total from display*/
      
      printf("\nTotal is:%d",total);
      
      return 0;      /*Since main was defined as int main(void) */ 
    
    
    }
    
    int display(int nums[5][5])  /*This line was causing trouble*/
    
    {
      int row_num, col_num;
      int total=0;         /*This variable will hold your total*/
    
      for (row_num = 0; row_num <5; ++row_num)
      {
        for(col_num = 0; col_num <5; ++col_num)
        {
          
            printf("%5d",nums[row_num][col_num]);
    
            total = total + nums[row_num][col_num]; /*total calc */
        
       }
     }
      
     return total;   /*Returning total back to main*/
    }
    Make sure you go through this program and understand what's going on.
    Last edited by sahil_m; 11-08-2005 at 09:40 AM.
    Thanks,
    Sahil

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    c help arrays

    Hi Sahil I am getting the folowing errors. I am using VB to compile.

    compiling...
    summ arrayhelp.cpp
    c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(25) : error C2556: 'void __cdecl display(int [][5])' : overloaded function differs only by return type from 'int __cdecl display(int [][5])'
    c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(3) : see declaration of 'display'
    c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(25) : error C2371: 'display' : redefinition; different basic types
    c:\program files\microsoft visual studio\myprojects\prob2-float\summ arrayhelp.cpp(3) : see declaration of 'display'
    Error executing cl.exe.

    summ arrayhelp.obj - 2 error(s), 0 warning(s)

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    New Delhi
    Posts
    40
    Oh!, I am sorry I made a typo, but you should have figured that out yourself, the function prototype and the definition dont match, I am fixing that right now.
    Thanks,
    Sahil

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    thanks, I didn't catch it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21