School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
You're Browsing As A Guest! Register Now...
Become an Expert!

Join 353,816 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,501 people online right now.Registration is fast and FREE... Join Now!



Arrays

52 Weeks of Code Challenge: WPF
Week #10 of the 52 Weeks of Code Challenge is WPF. If you're a .NET programmer, you should give it a shot. Click Here!

Arrays Rate Topic: -----

#1 roinjoin  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Posted 24 September 2009 - 11:02 PM

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


#2 modi123_1  Icon User is offline

  • Suiter #2
  • Icon

Reputation: 115
  • View blog
  • Posts: 2,171
  • Joined: 12-June 08


Dream Kudos: 150

Re: Arrays

Posted 25 September 2009 - 08: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
  • Icon

Reputation: 79
  • View blog
  • Posts: 209
  • Joined: 24-August 09


Dream Kudos: 50

Re: Arrays

Posted 25 September 2009 - 09: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 - 10:01 AM

Was This Post Helpful? 1
  • +
  • -

#4 roinjoin  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Re: Arrays

Posted 25 September 2009 - 05: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
  • Icon

Reputation: 79
  • View blog
  • Posts: 209
  • Joined: 24-August 09


Dream Kudos: 50

Re: Arrays

Posted 25 September 2009 - 09:33 PM

did you use the if condition as i said? was your prob solved?
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month