# Arrays

Page 1 of 1

## 4 Replies - 814 Views - Last Post: 25 September 2009 - 10:33 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=127985&amp;s=203446b97e3e48fd396c308dbe073d86&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 roinjoin

Reputation: 0
• Posts: 2
• Joined: 24-September 09

# Arrays

Posted 25 September 2009 - 12:02 AM

Need to loop through two loops and add the values together.
The first element in the first loop must be added to the first element in the second loop.
The combined values must be in a third array. This is the Sum array.

```			int[][] arr = new int[2][];
int[] Sum;

arr[0] = new int[5] { 11, 32, 15, 17, 92 };
arr[1] = new int[4] { 2, 4, 6, 8 };

//Display the array elements
for (int i = 0; i < arr.Length; i++)
{
Sum = arr[0][i] + arr[1][i];
System.Console.WriteLine("Sum of the two Arrays: {0}", Sum);
System.Console.WriteLine("Element({0}); ", i);

for (int j = 0; j<arr[i].Length; j ++)
{
System.Console.WriteLine("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}

System.Console.WriteLine();
}

```

Is This A Good Question/Topic? 0

## Replies To: Arrays

### #2 modi123_1

• Suitor #2

Reputation: 13859
• Posts: 55,340
• Joined: 12-June 08

## Re: Arrays

Posted 25 September 2009 - 09:21 AM

Okay.. is the first element of the first array supposed to be added to each element in the second or just the first element/

### #3 aks29921

• D.I.C Regular

Reputation: 116
• Posts: 345
• Joined: 24-August 09

## Re: Arrays

Posted 25 September 2009 - 10:49 AM

roinjoin, on 24 Sep, 2009 - 11:02 PM, said:

Need to loop through two loops and add the values together.
The first element in the first loop must be added to the first element in the second loop.
The combined values must be in a third array. This is the Sum array.

```			int[][] arr = new int[2][];
int[] Sum;

arr[0] = new int[5] { 11, 32, 15, 17, 92 };
arr[1] = new int[4] { 2, 4, 6, 8 };

//Display the array elements
for (int i = 0; i < arr.Length; i++)
{
Sum = arr[0][i] + arr[1][i];
System.Console.WriteLine("Sum of the two Arrays: {0}", Sum);
System.Console.WriteLine("Element({0}); ", i);

for (int j = 0; j<arr[i].Length; j ++)
{
System.Console.WriteLine("{0}{1}", arr[i][j], j == (arr[i].Length - 1) ? "" : " ");
}

System.Console.WriteLine();
}

```

i am afraid you are not very clear in what you want to ask..

however, what i can infer is that you have a loop within a loop and you want the values of both of them to be incremented together, i.e. if outer loop variable is 1, inner is 1(then add values of both and store result in c[1]), next time outer variable is 2 and inner one is also 2 and so on).

if that's all you have to do then simply put a condition in the inner loop
```if (<inner loop variable>==<outer loop variable>)

```

and yes, don't forget to close your [code] tag from the next time

This post has been edited by aks29921: 25 September 2009 - 11:01 AM

### #4 roinjoin

Reputation: 0
• Posts: 2
• Joined: 24-September 09

## Re: Arrays

Posted 25 September 2009 - 06:29 PM

Yes .. the first element of the first array has to be added to the first element of the second array. This combined value should then be stored as the first element of the Sum array. Then the second element of the first must be combined with the second element of the second array. This value must then be stored as the second value in the sum array. So the corressponding elements in the respective arrays must be added and stored in the Sum array

### #5 aks29921

• D.I.C Regular

Reputation: 116
• Posts: 345
• Joined: 24-August 09

## Re: Arrays

Posted 25 September 2009 - 10:33 PM

did you use the if condition as i said? was your prob solved?