how to sum all the columns in 2d cells..

This is a discussion on how to sum all the columns in 2d cells.. within the C Programming forums, part of the General Programming Boards category; i tried to do this its not working.. Code: for(kndex=0;kndex<cols;kndex++){ for(index=0;index<rows;index++){ sumc[kndex]=sumc[kndex]+matrix[kndex][index]; } } for(index=0;index<cols;index++){ printf("%d",sumc[index]); }...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to sum all the columns in 2d cells..

    i tried to do this its not working..
    Code:
    for(kndex=0;kndex<cols;kndex++){
    
       for(index=0;index<rows;index++){
          sumc[kndex]=sumc[kndex]+matrix[kndex][index];
    
       }
    
    }
    
    for(index=0;index<cols;index++){
     printf("%d",sumc[index]);
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if i get a 2d array like this
    1 1 1
    2 2 2

    it needs
    to put in the arr aray 3 3 3
    (summing all the of the columns)
    i tried this code
    i cant see the error
    cols is the number of column
    rows is the number of rows
    Code:
    for (index = 0; index < rows; index++)
       {
          for (kndex = 0; kndex < cols; kndex++)
          {
              
             arr[index]=arr[index]+matrix[kndex][index];
    	  }
    
       }
    
    for (kndex = 0; kndex < cols; kndex++)
          {
    printf("%d ",arr[kndex]);
    }

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why don't you do as laserlight has already suggested (elsewhere), and post the smallest compilable bit of code that you can, rather than these little snippets without any context at all?

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when my
    matrix array is
    1 1 1
    2 2 2

    it prints me
    3 3 3 1

    why??

    Code:
    for (kndex = 0; kndex < cols; kndex++)
       {
          for (index = 0; index < rows; index++)
          {
              
             arr[kndex]=arr[kndex]+matrix[index][kndex];
    	  }
    
       }
    
    for (kndex = 0; kndex < cols; kndex++)
          {
    printf("%d ",arr[kndex]);
    }

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    stupid error
    solved it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to check if all the cells in the 2D array ..
    By transgalactic2 in forum C Programming
    Replies: 49
    Last Post: 01-03-2009, 12:43 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 07:40 AM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM
  5. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 08:19 AM

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