# 2D Array Problem

Page 1 of 1

## 3 Replies - 948 Views - Last Post: 08 January 2010 - 03:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=148846&amp;s=3463473a8783327d46119ff2e89b4aef&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kdogg727

Reputation: 0
• Posts: 9
• Joined: 12-December 08

# 2D Array Problem

Posted 08 January 2010 - 08:18 AM

I have the sum of the rows displaying, but I'm trying to figure out how t display the sum of each column now.

```namespace _DArrayProject
{
class Program
{
static void Main(string[] args)
{
int[,] numbers = { { 13, 25, 64, 100, 12, 7 }, { 5, 28, 20, 34, 16, 46 }, { 7, 13, 6, 12, 42, 35 }, { 21, 32, 18, 17, 76, 42 }, { 100, 6, 7, 8, 17, 12 } };

Console.WriteLine("Sum of Array");
Console.WriteLine();

for (int row = 0; row < 5; row++)
{
int sum = 0;
for (int column = 0; column < 6; column++)
{

Console.Write(numbers[row, column] + "\t" );
sum += numbers[row,column];

}
Console.Write("\t" + sum);
Console.WriteLine();
}
```

This post has been edited by kdogg727: 08 January 2010 - 08:34 AM

Is This A Good Question/Topic? 0

## Replies To: 2D Array Problem

Reputation: 66
• Posts: 560
• Joined: 27-October 09

## Re: 2D Array Problem

Posted 08 January 2010 - 08:31 AM

Would love to try and help out, but could you please put your code like so just to give me a fighting chance at understanding it properly. Also you are supposed to put your question in the body of the post not in the title or header. Just re-iterate it with slight clarification as to what the actual problem is, or the error, or whatever the real problem is.

### #3 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: 2D Array Problem

Posted 08 January 2010 - 11:34 AM

```			for (int i = 0; i < 6; i++) {
int colSum = 0;
for (int j = 0; j < 5; j++) {
colSum += numbers[j, i];
}
Console.Write( colSum + "\t" );

}
Console.WriteLine();

```

just reverse the rows and cols essentially

This post has been edited by SwiftStriker00: 08 January 2010 - 11:36 AM

Reputation: 66
• Posts: 560
• Joined: 27-October 09

## Re: 2D Array Problem

Posted 08 January 2010 - 03:42 PM

```namespace _DArrayProject
{

class Program
{

static void Main(string[] args)
{

int[,] numbers = { { 13, 25, 64, 100, 12, 7 }, { 5, 28, 20, 34, 16, 46 }, { 7, 13, 6, 12, 42, 35 }, { 21, 32, 18, 17, 76, 42 }, { 100, 6, 7, 8, 17, 12 } };

Console.WriteLine("Sum of Array");
Console.WriteLine();

for (int column = 0; column < 5; column++)
{

int sum = 0;
for (int row = 0; row < 6; row++)
{

Console.Write(numbers[row, column] + "\t" );
sum += numbers[row,column];

} // for (int row = 0; row < 6; row++)

Console.Write("\t" + sum);
Console.WriteLine();

} // for (int column = 0; column < 5; column++)

} // static void Main(string[] args)
} // class Program
} // namespace _DArrayProject

```

Like SwiftStiker00 said you just had the columns and rows reversed. Hope this helps out. Just posted with your variables for clarification purposes.