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

1. ## 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. 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]);
}```

3. 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?

4. 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]);
}```

5. stupid error
solved it