Build arrays A & B, add values in both, and display in array C

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 9648 Views - Last Post: 28 October 2007 - 07:30 AM Rate Topic: -----

#1 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Build arrays A & B, add values in both, and display in array C

Posted 23 October 2007 - 07:08 AM

Hi, I am trying to put together a little program that will take input from the user which will build 2 arrays. int m = arrayA, int n = arrayB. And then sum the integer values from these arrays and display them in a 3rd array, arrayC.

arrayA 8 12 3 4 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

So far, not so good...my code is not much to go on, I need a push in the right direction. Thanks.

 class MyMethods
	{
//	  ======================================================
		public static void Get(out int value, int m, int n)
		{
			string numString;
			do
			{
				Console.WriteLine("Please enter a number: ");
				Console.WriteLine();
				numString = Console.ReadLine();
				value = int.Parse(numString);
			} while ((m>=0) && (n>=0));
		}// Get
//	  =======
		public static void BuildArray(int m, int n, ref int[] arrayA, ref int[] arrayB)
		{
			arrayA = new int[m];
			arrayB = new int[n];
		}// BuildArray
//	  ==============
		public static void FillArray(int m, int n, int[] arrayA, int[] arrayB, int minDigit, int maxDigit, int randomDigit, int randomNumber)
		{
			int ii;
			int jj;
			for (ii = 0; ii < m; ii++)
				for (jj = 0; jj < n; jj++)
					arrayA[ii] = ii;
					arrayB[jj] = jj;

			randomDigit = randomNumber.Next(m, n);
			Console.WriteLine(randomDigit);
			ii++;
			jj++;
		}


	}//class MyMethods
//  ==================


Is This A Good Question/Topic? 0
  • +

Replies To: Build arrays A & B, add values in both, and display in array C

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 23 October 2007 - 09:00 AM

FillArray confuses me. Are you trying to fill an array with random numbers? Is randomNumber and instance of System.Random? If so, Next(int,int) is an invalid method call.

Where's arrayC and where are you generating it?
Was This Post Helpful? 0
  • +
  • -

#3 Binary_Ninja  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 24-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 24 October 2007 - 02:44 AM

View PostCrazyJ, on 23 Oct, 2007 - 07:08 AM, said:

Hi, I am trying to put together a little program that will take input from the user which will build 2 arrays. int m = arrayA, int n = arrayB. And then sum the integer values from these arrays and display them in a 3rd array, arrayC.

arrayA 8 12 3 4 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

So far, not so good...my code is not much to go on, I need a push in the right direction. Thanks.

 class MyMethods
	{
//	  ======================================================
		public static void Get(out int value, int m, int n)
		{
			string numString;
			do
			{
				Console.WriteLine("Please enter a number: ");
				Console.WriteLine();
				numString = Console.ReadLine();
				value = int.Parse(numString);
			} while ((m>=0) && (n>=0));
		}// Get
//	  =======
		public static void BuildArray(int m, int n, ref int[] arrayA, ref int[] arrayB)
		{
			arrayA = new int[m];
			arrayB = new int[n];
		}// BuildArray
//	  ==============
		public static void FillArray(int m, int n, int[] arrayA, int[] arrayB, int minDigit, int maxDigit, int randomDigit, int randomNumber)
		{
			int ii;
			int jj;
			for (ii = 0; ii < m; ii++)
				for (jj = 0; jj < n; jj++)
					arrayA[ii] = ii;
					arrayB[jj] = jj;

			randomDigit = randomNumber.Next(m, n);
			Console.WriteLine(randomDigit);
			ii++;
			jj++;
		}


	}//class MyMethods
//  ==================

Hi, ok i think i know what you are trying to do here, arnt you trying to add the first element of the first array to the first element of the second array and store it in the third?
Was This Post Helpful? 0
  • +
  • -

#4 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 24 October 2007 - 11:23 AM

Thanks for the response. Sorry for the confusion, this should explain my program much better:
namespace SumArrays_3
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("This program will accept input from the user which will ");
			Console.WriteLine("be used to build 2 arrays.");
			Console.WriteLine();
			Console.WriteLine("The 1st integer entered will determine the number of");
			Console.WriteLine("elements in array A.");
			Console.WriteLine();
			Console.WriteLine("The 2nd integer entered will determine the number of");
			Console.WriteLine("elements in array B.");
			Console.WriteLine();
			Console.WriteLine("A Minimum and a Maximum number must then be entered");
			Console.WriteLine("signifying the range of random numbers used to fill");
			Console.WriteLine("arrays A and B.");
			Console.WriteLine();
			Console.WriteLine("===============================================");
			Console.WriteLine("Array C will display the sum of arrays A and B.");
			Console.WriteLine("===============================================");
			Console.ReadLine();
		}// Main
//	  ========
	}//class Program
//  ================
}// namespace SumArrays_3


public static void ArrayInput(out int value, int m, int n)
		{
			string numString;

			do
			{
//			  =================
				Console.WriteLine("Please enter a number for Array A: ");
				int m = int.Parse(Console.ReadLine());
				int[] arrayA = new int[m];
				Console.WriteLine();
//			  ====================
				Console.WriteLine("Please enter a number for Array B: ");
				int n = int.Parse(Console.ReadLine());
				int[] arrayB = new int[n];
				Console.WriteLine();
//			  ====================
			}

//		  ==========================
			Random randomNumber = new Random();
//		  ===================================
			Console.WriteLine("Enter the minimum number.");
			numString = Console.ReadLine();
			int minDigit = int.Parse(numString);
//		  ====================================
			Console.WriteLine("Enter the maximum number.");
			numString = System.Console.ReadLine();
			int maxDigit = int.Parse(numString);
			Console.WriteLine();
//		  ====================

		}

	}//class MyMethods
//  ==================


Again, sorry for the confusion, thanks!

View PostBinary_Ninja, on 24 Oct, 2007 - 02:44 AM, said:

View PostCrazyJ, on 23 Oct, 2007 - 07:08 AM, said:

Hi, I am trying to put together a little program that will take input from the user which will build 2 arrays. int m = arrayA, int n = arrayB. And then sum the integer values from these arrays and display them in a 3rd array, arrayC.

arrayA 8 12 3 4 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

So far, not so good...my code is not much to go on, I need a push in the right direction. Thanks.

 class MyMethods
	{
//	  ======================================================
		public static void Get(out int value, int m, int n)
		{
			string numString;
			do
			{
				Console.WriteLine("Please enter a number: ");
				Console.WriteLine();
				numString = Console.ReadLine();
				value = int.Parse(numString);
			} while ((m>=0) && (n>=0));
		}// Get
//	  =======
		public static void BuildArray(int m, int n, ref int[] arrayA, ref int[] arrayB)
		{
			arrayA = new int[m];
			arrayB = new int[n];
		}// BuildArray
//	  ==============
		public static void FillArray(int m, int n, int[] arrayA, int[] arrayB, int minDigit, int maxDigit, int randomDigit, int randomNumber)
		{
			int ii;
			int jj;
			for (ii = 0; ii < m; ii++)
				for (jj = 0; jj < n; jj++)
					arrayA[ii] = ii;
					arrayB[jj] = jj;

			randomDigit = randomNumber.Next(m, n);
			Console.WriteLine(randomDigit);
			ii++;
			jj++;
		}


	}//class MyMethods
//  ==================

Hi, ok i think i know what you are trying to do here, arnt you trying to add the first element of the first array to the first element of the second array and store it in the third?

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 24 October 2007 - 12:03 PM

Sorry, FillArray is still incomprehensible. :blink:

Here's a large part of your solution. Hope this will inspire you to write the rest.

int [] GetRandomFilledArray(int arraySize, int valueMax, int valueMin) {
   int valueRange = valueMax - valueMin;
   Random rnd = new Random();
   int [] newArray = new int[arraySize];
   for(int i=0; i<arraySize; i++) {
	  newArray[i] = rnd.Next(valueRange) + valueMin;
   }
   return newArray;
}

Was This Post Helpful? 0
  • +
  • -

#6 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 24 October 2007 - 12:30 PM

DOH! :crazy: I see what you mean. I posted my code from a previous attempt rather than my most recent, sorry! Thanks for the shove in the right direction though, really.

View Postbaavgai, on 24 Oct, 2007 - 12:03 PM, said:

Sorry, FillArray is still incomprehensible. :blink:

Here's a large part of your solution. Hope this will inspire you to write the rest.

int [] GetRandomFilledArray(int arraySize, int valueMax, int valueMin) {
   int valueRange = valueMax - valueMin;
   Random rnd = new Random();
   int [] newArray = new int[arraySize];
   for(int i=0; i<arraySize; i++) {
	  newArray[i] = rnd.Next(valueRange) + valueMin;
   }
   return newArray;
}

Was This Post Helpful? 0
  • +
  • -

#7 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 24 October 2007 - 05:15 PM

Okay, here is what I've got. My program will fill arrayA, I need to find out if I can use the same method to fill arrayB. Then I am having trouble with how to design arrayC so it will display the sum of all values in A and B. Also, how can I display all three of the arrays elements horizontally? Thanks! :)
 static void Main(string[] args)
		{
			int m;
			int n;
			string numString;
			
			Console.WriteLine("This program will accept input from the user which will ");
			Console.WriteLine("be used to build 2 arrays.");
			Console.WriteLine();
			Console.WriteLine("The 1st integer entered will determine the number of");
			Console.WriteLine("elements in array A.");
			Console.WriteLine();
			Console.WriteLine("The 2nd integer entered will determine the number of");
			Console.WriteLine("elements in array B.");
			Console.WriteLine();
			Console.WriteLine("A Minimum and a Maximum number must then be entered");
			Console.WriteLine("signifying the range of random numbers used to fill");
			Console.WriteLine("arrays A and B.");
			Console.WriteLine();
			Console.WriteLine("===============================================");
			Console.WriteLine("Array C will display the sum of arrays A and B.");
			Console.WriteLine("===============================================");
			Console.WriteLine();
//		  ===================
			MyMethods.InputA(out m);
			MyMethods.InputB(out n);

//		  ====================
			int ii;
			int randomDigit;
//		  ==========================
			Random randomNumber = new Random();
//		  ===================================
			Console.WriteLine("Enter the minimum number.");
			numString = Console.ReadLine();
			int minValue = int.Parse(numString);
//		  ====================================
			Console.WriteLine("Enter the maximum number.");
			numString = System.Console.ReadLine();
			int maxValue = int.Parse(numString);
			Console.WriteLine();
//		  ====================
		   
			ii = 0;
			while (ii < m)
			{
				randomDigit = randomNumber.Next(minValue, maxValue);
				Console.WriteLine(randomDigit);

				ii++;
			}  // while
			Console.ReadLine();
//		  ===========


and the Methods
 public static int[] InputA(out int m)
		{
	  
			Console.Write("Please enter the size for Array A: ");
			m = int.Parse(Console.ReadLine());
			int[] arrayA = new int[m];
			Console.WriteLine();
			return arrayA;

		}// method InputA
//	  =================

		public static int[] InputB(out int n)
		{
			 Console.WriteLine("Please enter the size for Array B: ");
			 n = int.Parse(Console.ReadLine());
			 int[] arrayB = new int[n];
			 Console.WriteLine();
			 return arrayB;
		}// method InputB
//	  =====================


Was This Post Helpful? 0
  • +
  • -

#8 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 25 October 2007 - 05:02 AM

Hello, is anyone out there??? I am struggling with figuring out how to add 2 arrays, displaying them in a third array. My last post contains my code for arrayA and arrayB. I would like the result to be...

arrayA 8 12 3 4 10 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

Please advise, I have been racking my brain trying to figure this one out. I understand that its possible to iterate through an array using the FOR EACH loop, but I cant see how I can implement this in my code. (see code in previous posts) Thank you. :blink:

CrazyJ

This post has been edited by CrazyJ: 25 October 2007 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#9 Binary_Ninja  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 24-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 25 October 2007 - 06:47 AM

View PostCrazyJ, on 25 Oct, 2007 - 05:02 AM, said:

Hello, is anyone out there??? I am struggling with figuring out how to add 2 arrays, displaying them in a third array. My last post contains my code for arrayA and arrayB. I would like the result to be...

arrayA 8 12 3 4 10 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

Please advise, I have been racking my brain trying to figure this one out. I understand that its possible to iterate through an array using the FOR EACH loop, but I cant see how I can implement this in my code. (see code in previous posts) Thank you. :blink:

CrazyJ

Hello,so you want the user to enter the numbers and the as many numbers as they want in each array? or
you just want want to use this?arrayA 8 12 3 4 10 6 3
arrayB 22 3 6 6.As for the FOR EACH loop, it is used to loop through the elements in a array, but you cannot add elements.Ithink you are after something like ArrayC[0]=ArrayA[0]+ArrayB[0];
ArrayC[1]=ArrayA[1]+ArrayB[0];
and so on.
Was This Post Helpful? 0
  • +
  • -

#10 CrazyJ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 15-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 25 October 2007 - 07:05 AM

I want the user to be able to enter as many values as they want in each array. This poses an issue if I must add them as you suggested, adding each element such as [0] through [12].
Here is my code, all done but this final, important segment....adding the values and displaying in arrayC! Thank you for the response Ninja, I've been patiently waiting...
  static void Main(string[] args)
		{
			int m;
			int n;
			string numString;
			
			Console.WriteLine("This program will accept input from the user which will ");
			Console.WriteLine("be used to build 2 arrays.");
			Console.WriteLine();
			Console.WriteLine("The 1st integer entered will determine the number of");
			Console.WriteLine("elements in array A.");
			Console.WriteLine();
			Console.WriteLine("The 2nd integer entered will determine the number of");
			Console.WriteLine("elements in array B.");
			Console.WriteLine();
			Console.WriteLine("A Minimum and a Maximum number must then be entered");
			Console.WriteLine("signifying the range of random numbers used to fill");
			Console.WriteLine("arrays A and B.");
			Console.WriteLine();
			Console.WriteLine("===============================================");
			Console.WriteLine("Array C will display the sum of arrays A and B.");
			Console.WriteLine("===============================================");
			Console.WriteLine();
//		  ===================
			MyMethods.InputA(out m);
			MyMethods.InputB(out n);

//		  ====================
			int ii;
			int jj;
			int randomDigit;
//		  ==========================
			Random randomNumber = new Random();
//		  ===================================
			Console.WriteLine("Enter the minimum number: ");
			numString = Console.ReadLine();
			int minValue = int.Parse(numString);
//		  ====================================
			Console.WriteLine("Enter the maximum number: ");
			numString = System.Console.ReadLine();
			int maxValue = int.Parse(numString);
			Console.WriteLine();
//		  ====================

			Console.WriteLine("Array A: ");
			ii = 0;
			while (ii < m)
		   {
				randomDigit = randomNumber.Next(minValue, maxValue);
				Console.Write(randomDigit + " ");

			ii++;

			}  // while
//		  ===========
			Console.WriteLine();
//		  ====================
			Console.WriteLine("Array B: ");
			jj = 0;
			while (jj < n)
			{
				randomDigit = randomNumber.Next(minValue, maxValue);
				Console.Write(randomDigit + " ");

				jj++;

			}  // while
			
			Console.ReadLine();
		}// Main
//	  ========


AND Methods

class MyMethods
	{
		public static int[] InputA(out int m)
		{
			Console.Write("Please enter the size for Array A: ");
			m = int.Parse(Console.ReadLine());
			int[] arrayA = new int[m];
			Console.WriteLine();
			return arrayA;
			
		}// method InputA
//	  =================

		public static int[] InputB(out int n)
		{
			 Console.WriteLine("Please enter the size for Array B: ");
			 n = int.Parse(Console.ReadLine());
			 int[] arrayB = new int[n];
			 Console.WriteLine();
			 return arrayB;

		}// method InputB
//	  =====================
		public static void ArrayC()
		{
			int sum = randomDigit + randomDigit;

			int[] arrayC = new int[sum];

		}// method ArrayC
//	  =================

	}//class MyMethods
//  ==================


PLEASE HELP...anyone?!

View PostBinary_Ninja, on 25 Oct, 2007 - 06:47 AM, said:

View PostCrazyJ, on 25 Oct, 2007 - 05:02 AM, said:

Hello, is anyone out there??? I am struggling with figuring out how to add 2 arrays, displaying them in a third array. My last post contains my code for arrayA and arrayB. I would like the result to be...

arrayA 8 12 3 4 10 6 3
arrayB 22 3 6 6
arrayC 30 15 9 10 6 3

Please advise, I have been racking my brain trying to figure this one out. I understand that its possible to iterate through an array using the FOR EACH loop, but I cant see how I can implement this in my code. (see code in previous posts) Thank you. :blink:

CrazyJ

Hello,so you want the user to enter the numbers and the as many numbers as they want in each array? or
you just want want to use this?arrayA 8 12 3 4 10 6 3
arrayB 22 3 6 6.As for the FOR EACH loop, it is used to loop through the elements in a array, but you cannot add elements.Ithink you are after something like ArrayC[0]=ArrayA[0]+ArrayB[0];
ArrayC[1]=ArrayA[1]+ArrayB[0];
and so on.

Was This Post Helpful? 0
  • +
  • -

#11 Binary_Ninja  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 24-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 25 October 2007 - 07:31 AM

Ok, this is how you display the ArrayC

foreach(int x in ArrayC)
{
Console.WriteLine("{0}", x);
}

Now the problem is how to add them.
Was This Post Helpful? 0
  • +
  • -

#12 guahguahmonster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 209
  • Joined: 29-August 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 26 October 2007 - 07:56 PM

A crucial problem you shall have to address here is the length of the arrays and what to do if they are not equal. If Array A has 8 elements and Array B has only 7, arrayC[7] = arrayA[7] + arrayB[7]; won't make you happy. I notice you have defined behavior that if one array is longer, the longer values are kept on the end of the array. Ok.

I would write code to find which array is longer, then copy it to another array. Then, loop through elements of the shorter array and add them one by one to the corresponding element of the longer array.

How's that sound?

-----

Please also note the following code that you posted:
			Console.WriteLine("Array A: ");
			ii = 0;
			while (ii < m)
		   {
				randomDigit = randomNumber.Next(minValue, maxValue);
				Console.Write(randomDigit + " ");

			ii++;

			}  // while
//		  ===========
			Console.WriteLine();
//		  ====================
			Console.WriteLine("Array B: ");
			jj = 0;
			while (jj < n)
			{
				randomDigit = randomNumber.Next(minValue, maxValue);
				Console.Write(randomDigit + " ");

				jj++;

			}  // while



To me, it looked you wanted to set those values in Array A and B, but this is ineffective, for you do not set these values in array A nor B. In fact, you do not even HAVE an array A or B. Sure, your inputA and inputB methods return an int array, but you do not assign anything to the return value, so it just gets lost in nowhere. In addition, that code probably won't run because you did not declare the type of randomDigit (And while I'm here, why is it a randomDigit? what if minValue=10 and maxValue=99? Then they're not digits...)

This post has been edited by guahguahmonster: 26 October 2007 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 27 October 2007 - 04:36 AM

You still not done with this? Please, post when you get it, I'm curious. There aren't enough C# posts, anyway. ;)

A foreach is a complete red herring, ignore it. You shouldn't use one in any form of solution for this problem.

With the code I posted before, you're about ten lines away from solution. The fundamental problem is understanding arrays and loops.

I answered the first part of the problem, arrays filled with random numbers of a given length. guahguahmonster gave an overview of the second part, filling a third array by combining the values of two others.

Another hint, initializing arrayC should look something like:
int [] arrayC = new int[(arrayA.Length()>arrayB.Length()) ? arrayA.Length() : arrayB.Length()];


Ok, that's probably overly quirky syntax; figure out better. So, where are you stuck?
Was This Post Helpful? 0
  • +
  • -

#14 Binary_Ninja  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 24-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 27 October 2007 - 06:03 AM

I was looking at it, and im stuck at, what if 1 array is bigger that the other? exactly the code you put up there baavgai.

This post has been edited by Binary_Ninja: 27 October 2007 - 06:03 AM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Build arrays A & B, add values in both, and display in array C

Posted 27 October 2007 - 08:56 AM

View PostBinary_Ninja, on 27 Oct, 2007 - 09:03 AM, said:

I was looking at it, and im stuck at, what if 1 array is bigger that the other? exactly the code you put up there baavgai.


Two options for this.

Option One, loop through the smallest doing normal addition. Then, loop from the smallest + 1 to the end of the longest and only insert the value of the largest. That is actually the fastest and cleanest, but I suspect the next option will be more common.

Option Two, loop through to the largest number. Check to make sure if the arrayA or arrayB is out of range. If they aren't out of range, proceed normally. If one is, just feed the one that isn't to arrayC. There are many variants on this check feed, but the logic will be consistent.

There, explained but resisted the urge to code. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2