-
September 9th, 2010, 04:11 PM
#1
Having trouble writing program to find average of numbers
Hello all.
I have an assignment in which I'm required to find the average of a sequence of nonnegative numbers entered by the user, where the user enters a negative number to terminate the input. I've gotten the termination to work successfully, but I'm having trouble calculating the average.
As you can see, my code takes the next number in the sequence and divides it by the number of terms that have been used at that point. Unfortunately that isn't what I want. If only there was some way I could set the first entered number equal to a certain variable, the second to another, and so on. Can anyone give me a suggestion? Below is my code.
Code:
static void Main(string[] args)
{
double input = 0, average = 0;
int numberOfTerms = 1;
Console.WriteLine("To end this program, enter a negative number and press any key.");
while (input >= 0)
{
Console.Write("Enter a number in your sequence: ");
input = Double.Parse(Console.ReadLine());
if (input > 0)
{
average = input / numberOfTerms;
}
numberOfTerms = numberOfTerms + 1;
Console.WriteLine("The average so far is {0}.", average);
}
Console.ReadKey(true);
}
}
Thanks!
-
September 9th, 2010, 06:32 PM
#2
Re: Having trouble writing program to find average of numbers
You need to keep the current total and divide that by the number of terms. So near the top, you need to define another variable:
double total = 0.0;
Then in the loop, after the user has entered the input, you need something like:
total += input;
Then to calculate the average, you need to change the line you have to:
average = total / numberOfTerms;
Try that, you're almost there.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
September 9th, 2010, 07:30 PM
#3
Re: Having trouble writing program to find average of numbers
you would also what to change the line where you check input >0 to total >0
Always use [code][/code] tags when posting code.
-
September 9th, 2010, 07:49 PM
#4
Re: Having trouble writing program to find average of numbers
DataMiser, I think that he is just checking for "end of user input" in that statement. He states that a -ve number indicates that the user has finished entering numbers, so I think that line is ok... However the two checks "> 0" and ">= 0" that he has, should be the same, depending on exactly what is required.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
September 19th, 2010, 12:26 AM
#5
Re: Having trouble writing program to find average of numbers
static void Main(string[] args)
{
double input = 0;
double total = 0;
int numberOfTerms = 1;
Console.WriteLine("To end this program, enter a negative number and press any key.");
while (input >= 0)
{
Console.Write("Enter a number in your sequence: ");
input = Double.Parse(Console.ReadLine());
if (input > 0)
{
total += input;
numberOfTerms += 1;
}
double average = total / numberOfTerms;
Console.WriteLine("The average so far is {0}.", average);
}
Console.ReadKey(true);
}
}
-
September 21st, 2010, 01:51 AM
#6
Re: Having trouble writing program to find average of numbers
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|