1

I've figured out how to do most of what I need. I realize that the variables used in the for loops will not be reachable outside of the loop, but I need to display the sum of the integers the user puts in. So,

step 1: Asks user for input of number of integers.

Step 2: Runs through getting each integer.

Step 3: Then displays all input.

And step 4: should have the sum of step 3...and that's

where my issue is.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dynamic_Entry
{
    class Program
    {
        static void Main()
        {
            Console.Write("How many integers are in your list? ");
            int k = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[k];
            int sum = 0;

            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("Please enter an integer: ");
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int n = 0; n < a.Length; n++)
            {
                Console.WriteLine("{0, 5}", a[n]);
            }
            Console.WriteLine("-----");

            sum += [] a;
            Console.Write("{0, 5}", sum);
            Console.ReadLine();
        }
    }
}

Any help on how to get the sum from outside the loop? If I put the hyphen line inside the last loop, it'll keep putting lines after every number...I only need the line at the end, with the sum under it. Thanks!

5
  • put the addition to sum into the for loop Commented Aug 2, 2013 at 5:39
  • You can declare int sum = 0; up top, then sum += a[i]; as you read them in. Commented Aug 2, 2013 at 5:44
  • I can't add the addition into the loop, because then it makes the output wrong. The final sum is displayed after the hyphen line, which is outside of the loop. If the hyphen line was inside the loop, I'd get a line after every integer entered. Commented Aug 2, 2013 at 5:49
  • 1
    Why does that make the output wrong? Commented Aug 2, 2013 at 6:25
  • Because it had to be a certain format. Having a line under every integer entered was not the format the output was supposed to have. The program displays all integers in a neat row, then has the hyphen line, then displays the sum. Commented Aug 2, 2013 at 6:45

2 Answers 2

3

Since you can used linq, replace the sum with bellow

 Console.WriteLine("-----");
    sum = a.Sum(); 
 Console.Write("{0, 5}", sum);
Sign up to request clarification or add additional context in comments.

11 Comments

This is EXACTLY what I needed. Can you explain how the array a still keeps the value of the array while it is in the different loops?
Same answer I gave before this answer.
U are just iterating the array not changing the value of the array. That's why Same values are being kept. Very common sense
Hi Atish Dipongkor, sorry to duplicate your answer, because when i submit the answer the time, your answer not yet show out, cause just 4 min different.
Atish, your original answer was to use Array.Sum, which did not exist. a.Sum did, and that is what Harris posted first.
|
2

You may use

a.Sum() 

method of Linq.

And your code should look like following

static void Main()
        {
            Console.Write("How many integers are in your list? ");
            int k = Convert.ToInt32(Console.ReadLine());
            int[] a = new int[k];
            int sum = 0;

            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("Please enter an integer: ");
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            for (int n = 0; n < a.Length; n++)
            {
                Console.WriteLine("{0, 5}", a[n]);
            }
            Console.WriteLine("-----");

            Console.Write("{0, 5}", a.Sum());
            Console.ReadLine();
        }

4 Comments

I don't have a Apply.Sum option in my Visual Studio. This is a console application, btw.
I didn't tell u to use Apply.Sum(). I told u to use Array.Sum(). Your accepted answer was given after my answer. What's the problem with my answer. Seems something wrong here
I didn't have Array.Sum, simple misspelling. In other words, when I tried your code, it didn't work. When I tried Harris', it did. No hard feelings.
Man ..... Array.Sum() is a method of LINQ. That means you can use Sum() method with your Array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.