locked
insert the sum of items of an array into another array RRS feed

  • Question

  • I've an array, suppose

    I need to sum up the consecutive the terms of the array and insert into another array. 
    I got the sum of the consecutive items of the array but is having problem inserting into 
    another array.

    input = [1,2,3,4,5]  

    output = [3,5,7,9]

    int count = 0;
    int nextCount = 1;
    int[] input = new int[5];
    
    for (int i = 0; i < 5; i++)
    {
    	Console.WriteLine("Please enter the input " + i);
    	input[i] = int.Parse(Console.ReadLine());
    }
    
     while (count < input.Length-1)
    {
    	int sum = 0;
    	Console.Write(input[count]);
    	Console.Write(input[nextCount]);
    	Console.WriteLine(Environment.NewLine);
    	sum = input[count] + input[nextCount];
    	count++;
    	nextCount++;
    	// how to insert the sum value into another array
    }

    How to insert the sum value into another array?

    Thanks!

    Tuesday, February 10, 2015 2:40 PM

Answers

  • This looks like homework so I'll provide suggestions but not the actual code. Think about how you might solve this problem logically.  You take the first and second element, add them together and then store that into a separate array

    ASSUME: input is an arbitrary array of values
    Create an output array that is Input.Length - 1 in size
    For each element in output array 
       Get the element at the same index and index + 1 and sum them
       Store the sum in the output array at the current index

    When the loop finishes you'll have the sums in an output array that you can then do any additional work on.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Kristin Xie Tuesday, February 24, 2015 10:13 AM
    Tuesday, February 10, 2015 2:57 PM

All replies

  • you can add variable "i" before "while" and create a new array, and after the "nextCount++" you can insert the "sum variable"

    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    • Proposed as answer by Bouzid Mohamed Wednesday, February 11, 2015 9:27 PM
    Tuesday, February 10, 2015 2:53 PM
  • This looks like homework so I'll provide suggestions but not the actual code. Think about how you might solve this problem logically.  You take the first and second element, add them together and then store that into a separate array

    ASSUME: input is an arbitrary array of values
    Create an output array that is Input.Length - 1 in size
    For each element in output array 
       Get the element at the same index and index + 1 and sum them
       Store the sum in the output array at the current index

    When the loop finishes you'll have the sums in an output array that you can then do any additional work on.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Kristin Xie Tuesday, February 24, 2015 10:13 AM
    Tuesday, February 10, 2015 2:57 PM
  • Thanks for the prompt reply.

    This is not a homework, but only a small part(sample) of a project.

    I tried the following approach but getting very weird output. I can not figure out where I am doing wrong.

    // added an intermediate array
    int[] = new int[input.Length-1];
    
     while (count < input.Length-1)
    {
    	int sum = 0;
    	Console.Write(input[count]);
    	Console.Write(input[nextCount]);	
    	sum = input[count] + input[nextCount];
    	count++;
    	nextCount++;
    	//added loop to insert value of sum in the array
    	for (int k = 0; k < bridgeArray.Length; k++)
    	{
    		bridgeArray[k] = sum;		
    		break;
    	}
    }
    Thanks!

    Tuesday, February 10, 2015 3:18 PM
  •  // This code will produce a List of integer 

              int [] input = new int []{ 1, 2, 3, 4, 5 };

                List<int> output = new List<int>();
                for (int i = 0; i < input.Length; i++)
                {
                    if(i!=0)
                    {
                        int total = input[i - 1] + input[i];
                        output.Add(total);
                    }
                }


    • Edited by Val10 Tuesday, February 10, 2015 3:42 PM
    Tuesday, February 10, 2015 3:42 PM
  • Hi Scala,

    namespace sumMSDN
    {
        class Program
        {
            static void Main(string[] args)
            {
                int count = 0;
                int nextCount = 1;
    
                int[] input = new int[5];
    
    
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("Please enter the input " + i);
                    input[i] = int.Parse(Console.ReadLine());
                }
    
                int[] output = new int[5];
                int j = 0;
    
                while (count < input.Length - 1)
                {
                    
                    int sum = 0;
                    Console.Write(input[count]);
                    Console.Write(input[nextCount]);
                    Console.WriteLine(Environment.NewLine);
                    sum = input[count] + input[nextCount];
                    output[j] = sum;
                    count++;
                    j++;
                    nextCount++;
                    // how to insert the sum value into another array
                }
    
                //just for test
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("output table " + output[i]);
                    
                }
                Console.ReadLine();
    
            }
        }
    }
    


    Mark as answer or vote as helpful if you find it useful | Ammar Zaied [MCP]

    Tuesday, February 10, 2015 3:49 PM
  • The code isn't valid.  I'm also not sure where your bridgeArray is coming from.  Since it appears you already have the input I'll skip over that.

    1. Name the intermediary array when you declare it (i.e. totals)
    2. Replace the while with a simple for loop using an index (not foreach)
    3. Remove the sum, count and nextCount variables as you don't need them
    4. Sum the inputs and store in the totals array (totals[index] = input[index] + input[index + 1])
    5. Remove the for loop as it does nothing
    6. Optionally print out the input values and the result of the summation

    Tuesday, February 10, 2015 3:57 PM
  • Hello Guys, thanks for the suggestions.
    I changed the code, but still can not get the value in the output array. The output array value is [0 1 2 3]. Why is it so? Output should be [3 5 7 9]

    static void Main(string[] args)
            {
                int[] input = new int[5];
                List<int> output = new List<int>();
    
                for (int i = 0; i < 5; i++)
                {
                    Console.WriteLine("Please enter the input " + i);
                    input[i] = int.Parse(Console.ReadLine());
                }            
                
                for (int k = 0; k < input.Length-1; k++)
                {
                    //Console.Write(input[k]);
                    //Console.Write(input[k+1]);
                    int total = input[k + 1] + input[k];
                    output.Add(k);
                    //Console.Write(total);
                                                   
                }
     
                Console.WriteLine(Environment.NewLine);
                foreach (int item in output)
                {
                    Console.WriteLine(item);
    		// output is 0 1 2 3  why is it???
                }
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
    Why the output array outside the loop displays 0 1 2 3, it should contain the total obtained from the upper for loop?

    Tuesday, February 10, 2015 5:03 PM
  • The line that is assigning the value to output is using k (the index) not total (the sum). Change the line.

    output.Add(total);

    Also, don't use hard coded indice in your code. Your input array specifies its size so all the for loops should be using that when you want to enumerate it.

    Tuesday, February 10, 2015 5:11 PM
  • Scala : does this help ? 
        static void Main(string[] args)
            {
                int[] a = { 1,2,3,4,5};//your array read from console
                var result = Enumerable.Zip(a, a, (x, y) => x + (x + 1)).Take(a.Length - 1).ToArray()  ;
                foreach (int item in result) Console.WriteLine(item);
                
            }       

    Tuesday, February 10, 2015 5:30 PM