cant figure out how to calculate averages

using System; namespace MyApp { internal class Program { static void Main(string[] args) { Intro(); double test1 = score1("Please enter the score of the first test"); while (test1 > -999) { test1 = score1("Please enter the score of your test"); } double total = grade(test1); letter(total); Ending(); } public static void Intro() { Console.Title = ("EX38"); Console.WindowHeight = 15; Console.WindowWidth = 60; Console.ForegroundColor = ConsoleColor.Green; } public static double score1(string s) { Console.WriteLine(s); double test1 = Convert.ToDouble(Console.ReadLine()); return test1; } public static double grade(double test1) { double total = (test1+999) / 5; Console.WriteLine("The average of the grades is {0:f1}", total); return total; } public static void letter(double total) { if (total > 89) { Console.WriteLine("This is an A"); } if (total < 89 && total > 79) { Console.WriteLine("This is an B"); } if (total < 79 && total > 69) { Console.WriteLine("This is an C"); } if (total < 69 && total > 59) { Console.WriteLine("This is an D"); } if (total < 59) { Console.WriteLine("This is an F"); } } public static void Ending() { Console.SetCursorPosition(Console.WindowWidth / 2 - 15, Console.WindowHeight - 2); Console.WriteLine("Press enter twice to close window."); Console.ReadLine(); } } }
26 Replies
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
I am trying to make a console application that allows the user to enter in multiple test scores and if -999 is entered it will show show the average of the scores
Angius
Angius10mo ago
And which part doesn't work?
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
its more about me not knowing what to do because currently it will just display -198 This is a F because it doesnt calculate any numbers entered prior to the -999
Angius
Angius10mo ago
You're never tracking the amount of items entered So you cannot calculate an average
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
I dont know how i would do that
Angius
Angius10mo ago
You can have a variable that tracks it That increments with each loop
int count = 0;
while (...)
{
count++;
// ...
}
int count = 0;
while (...)
{
count++;
// ...
}
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
so like this? Intro(); double test1 = score1("Please enter the score of the first test"); int count = 0; while (test1 > -999) { test1 = score1("Please enter the score of your test"); count++; } double total = grade(test1); letter(total); Ending();
Angius
Angius10mo ago
Yes, that's how you would track the amount of numbers entered You'd also have to use it later, though Also, you're not accumulating the numbers entered anywhere, it seems Your score() method returns a number You assign it to a variable But it's always the same variable That you keep overwriting
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
so how would i fix it?
Angius
Angius10mo ago
You could use a list of numbers Or you could keep adding to some variable Then you would just divide that sum by the amount of numbers entered And you get your average
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
alright ill try to impement it
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
would this work?
No description
Angius
Angius10mo ago
You never change testtotal inside of the loop, and instead create a testtotal2 variable that exists only inside of the loop So no, probably not
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
i tested it out and it still doesnt work
No description
Angius
Angius10mo ago
Well, what does your grade() method do?
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
public static double grade(double testtotal2, int count) { double total = (testtotal2 + 999) / count; Console.WriteLine("The average of the grades is {0:f1}", total); return total; }
Angius
Angius10mo ago
Why are you adding 999 to your total?
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
to cancel out the -999 because to stop the loop i use -999
Angius
Angius10mo ago
? When you enter -999 the loop stops It doesn't add -999 to the total
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
i think it does
No description
Angius
Angius10mo ago
Ah Well You're adding testtotal + test1 to testtotal2 for some reason Since testtotal never changes, you keep overwriting the value in testtotal2 instead of adding to it
double total = 0.0;
int count = 0;
while(...)
{
count++;
total = total + score1(...);
}
double total = 0.0;
int count = 0;
while(...)
{
count++;
total = total + score1(...);
}
You need to be adding to the same variable Incrementing it by consecutive values
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
like this???
No description
Angius
Angius10mo ago
Almost You're not adding the result of the first test to it No idea why your first test is some special case that's outside of the loop tbh But hey, it's a choice you made
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
im just lost rn
Angius
Angius10mo ago
I can see that, yes You have a lot of code for what is just two things: 1. Sum of numbers 2. Amount of numbers In short, you want this:
double total = 0; // sum of all numbers
int count = 0; // amount of numbers
while (...)
{
count++;
total += GetNumberSomehow();
}
double average = total / count;
Console.WriteLine($"Average is");
double total = 0; // sum of all numbers
int count = 0; // amount of numbers
while (...)
{
count++;
total += GetNumberSomehow();
}
double average = total / count;
Console.WriteLine($"Average is");
zǝɥɔsdɹoʇ
zǝɥɔsdɹoʇOP10mo ago
Thank you so much
Want results from more Discord servers?
Add your server