4 Replies - 525 Views - Last Post: 25 September 2009 - 10:33 PM Rate Topic: -----

#1 roinjoin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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();
			}



ModEdit: Fix your code tags

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • 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/
Was This Post Helpful? 0
  • +
  • -

#3 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Arrays

Posted 25 September 2009 - 10:49 AM

View Postroinjoin, 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>)
//do addition



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

Was This Post Helpful? 1
  • +
  • -

#4 roinjoin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1