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

  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
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,534
    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, 01:43 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08: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, 09:19 AM