| View previous topic :: View next topic |
| Author |
Message |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Wed Jun 18, 2008 7:59 am Post subject: [Help] Started C# and trying to make unending Average Calc |
|
|
Like The title says,
I started learning C# Today and I am trying to make an unending Average calculator.
This Is my code so far:
| Code: |
using System;
public class NadavProgram
{
public static void Main()
{
char more;
double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11;
double numOfGrades = 4;
double average;
Console.WriteLine("Enter The First Grade:");
num1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Grade:");
num2 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter The Third Grade:");
num3 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter The Fourth Grade:");
num4 = double.Parse(Console.ReadLine());
average = (num1 + num2 + num3 + num4) /numOfGrades;
Console.WriteLine("Your Grades Average: {0}", average);
End:
Console.WriteLine("Would You like to add more Grades to the avarage?");
Console.WriteLine("If You Do, Type y for yes, or n for no. ");
more = char.Parse(Console.ReadLine());
if (more == 'y') {
Console.WriteLine("Enter The Fifth Grade:");
num5 = double.Parse(Console.ReadLine());
numOfGrades++;
}
else if (more == 'n')
{
Console.WriteLine("Ok.");
Console.WriteLine("Your Average stays {0}", average);
goto End;
}
else
{
Console.WriteLine("Invalid input.");
goto End;
}
}
}
|
Don't know how to make it unending.
Help pl0x?
Don't write the code just tell me how to. |
|
| Back to top |
|
 |
dvdcowboy Grandmaster Cheater
Reputation: 0
Joined: 11 Nov 2006 Posts: 738
|
Posted: Wed Jun 18, 2008 8:32 am Post subject: |
|
|
unending? you mean make it so that it asks you to create another average?
if so/ label's and goto's |
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Wed Jun 18, 2008 8:40 am Post subject: |
|
|
Yes this is what I mean.
Tho I have manged to write it myself:
| Code: |
using System;
public class NadavsAverageCalculator
{
public static void Main()
{
Console.WriteLine("||*********************************************||");
Console.WriteLine("||This Is Nadav's Program to Calculate Averages||");
Console.WriteLine("||*********************************************||");
char more;
double num1, num2;
double numOfGrades = 2;
double average;
double totalGrades;
double temp;
Console.WriteLine("Enter The First Grade:");
num1 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter The Second Grade:");
num2 = double.Parse(Console.ReadLine());
totalGrades = num1 + num2;
chicken:
Console.WriteLine("");
Console.WriteLine("Would You like to add another grade?");
Console.WriteLine("Type y for yes, or n for no");
more = char.Parse(Console.ReadLine());
if (more == 'y')
{
Console.WriteLine("Enter The next grade:");
temp = double.Parse(Console.ReadLine());
totalGrades = totalGrades + temp;
numOfGrades = numOfGrades + 1;
average = totalGrades / numOfGrades;
Console.WriteLine("Your Average is {0}.", average);
goto chicken;
}
else if (more == 'n')
{
Console.WriteLine("Very Well.");
average = totalGrades / numOfGrades;
Console.WriteLine("Your Average is {0}.", average);
goto chicken;
}
else
{
Console.WriteLine("Invalid input.");
goto chicken;
}
}
}
|
How can I make it more efficient? |
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Wed Jun 18, 2008 8:44 am Post subject: |
|
|
Not exactly what you were hoping for, but I guess the concept is portable - use -1 to end the input
| Code: | @echo off
Title The ~CALCULATOR~
set count=0
set grade=0
set total=0
:start
cls
echo ==================
echo Enter grade(total: %total%):
set /p grade=
if %grade% == -1 goto end
set /a total+=%grade%
set /a count+=1
goto start
:end
echo
cls
set /a iavg = %total%/%count%
set /a favg = %total%%%count%
echo Average: %iavg% and %favg%/%count%
echo.
pause > nul |
|
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Wed Jun 18, 2008 8:48 am Post subject: |
|
|
| DoomsDay wrote: | Not exactly what you were hoping for, but I guess the concept is portable - use -1 to end the input
| Code: | @echo off
Title The ~CALCULATOR~
set count=0
set grade=0
set total=0
:start
cls
echo ==================
echo Enter grade(total: %total%):
set /p grade=
if %grade% == -1 goto end
set /a total+=%grade%
set /a count+=1
goto start
:end
echo
cls
set /a iavg = %total%/%count%
set /a favg = %total%%%count%
echo Average: %iavg% and %favg%/%count%
echo.
pause > nul |
|
Just to clear it up,
what language is that?  |
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Wed Jun 18, 2008 8:50 am Post subject: |
|
|
| nadavafuta wrote: | | DoomsDay wrote: | Not exactly what you were hoping for, but I guess the concept is portable - use -1 to end the input
|
Just to clear it up,
what language is that?  | Batch... |
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Wed Jun 18, 2008 8:53 am Post subject: |
|
|
| DoomsDay wrote: | | nadavafuta wrote: | | DoomsDay wrote: | Not exactly what you were hoping for, but I guess the concept is portable - use -1 to end the input
|
Just to clear it up,
what language is that?  | Batch... |
oh, ok... lolz.
Sorry for my stupidity, just started this whole thing. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Jun 18, 2008 9:02 am Post subject: |
|
|
Ew...goto. Use a do while loop. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
SkinnyQ Master Cheater
Reputation: 0
Joined: 16 Jun 2006 Posts: 411
|
Posted: Wed Jun 18, 2008 9:07 am Post subject: |
|
|
| oib111 wrote: | | Ew...goto. Use a do while loop. |
Well, can you write the part with the loop?
Haven't learned this loop yet. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Jun 18, 2008 9:12 am Post subject: |
|
|
I program in C++ so I don't know the syntax in C#, but here's a link explaining it.
http://www.softsteel.co.uk/tutorials/cSharp/lesson9.html
The way you would implement this is you would make a bool type variable and keep looping while it equals true. And when the user inputs that it doesn't want to put in another grade you just set it to false. _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
dvdcowboy Grandmaster Cheater
Reputation: 0
Joined: 11 Nov 2006 Posts: 738
|
Posted: Wed Jun 18, 2008 9:21 am Post subject: |
|
|
or you could write a class and then refer to it?
for example
URLHarvestor URLHarvestor = New URLHarvestor
Last edited by dvdcowboy on Fri Jul 31, 2009 1:29 pm; edited 1 time in total |
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Jun 18, 2008 10:41 am Post subject: |
|
|
A while loop is like an if statement except once everything in the loop is done executing, it'll check back at the beginning to see if the statement is still correct. If so, it'll repeat.
smileplease:
He's just learning. You can't expect him to know everything overnight. Now please leave your spam in the random spam section. _________________
|
|
| Back to top |
|
 |
DoomsDay Grandmaster Cheater
Reputation: 0
Joined: 06 Jan 2007 Posts: 768 Location: %HomePath%
|
Posted: Wed Jun 18, 2008 11:56 am Post subject: |
|
|
See if the following works for you(never used C#, created using your code...) | Code: | int sum = 0;
int num = 0;
int count = 0;
double avg = 0;
do{
Console.WriteLine("\nEnter Grade(total:"+sum+")");
num = int.pharse(Console.ReadLine);
if (num != -1){
sum += num;
count++;
}
} while (num != -1)
if (count > 0)
{
avg = sum/count;
Console.WriteLine("\n\nAverage: " + avg);
} |
|
|
| Back to top |
|
 |
Estx Expert Cheater
Reputation: 0
Joined: 04 Mar 2008 Posts: 172
|
Posted: Wed Jun 18, 2008 12:00 pm Post subject: |
|
|
Example only, don't expect you to use it. Nor should you. Just take hint from it and study how the code works. If you're having trouble understanding something highlight the code and press F1.
| Code: | using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool bRepeat = true;
double[] grades = new double[0];
int gradeCount = 1;
while(bRepeat)
{
Array.Resize(ref grades, gradeCount);
Console.Write("Grade #{0}: ", gradeCount);
grades[gradeCount - 1] = double.Parse(Console.ReadLine());
Console.WriteLine("New average: {0}\n", CalcAverage(grades));
gradeCount++;
}
}
static double CalcAverage(double[] array)
{
double result = 0.0;
for (int i = 0; i < array.Length; i++)
{ result += array[i]; }
result = result / array.Length;
return result;
}
}
}
|
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Jun 18, 2008 3:35 pm Post subject: |
|
|
| Estx wrote: | Example only, don't expect you to use it. Nor should you. Just take hint from it and study how the code works. If you're having trouble understanding something highlight the code and press F1.
| Code: | using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool bRepeat = true;
double[] grades = new double[0];
int gradeCount = 1;
while(bRepeat)
{
Array.Resize(ref grades, gradeCount);
Console.Write("Grade #{0}: ", gradeCount);
grades[gradeCount - 1] = double.Parse(Console.ReadLine());
Console.WriteLine("New average: {0}\n", CalcAverage(grades));
gradeCount++;
}
}
static double CalcAverage(double[] array)
{
double result = 0.0;
for (int i = 0; i < array.Length; i++)
{ result += array[i]; }
result = result / array.Length;
return result;
}
}
}
|
|
Generic Lists are faster than resizing Arrays.
This is how I would do it:
| Code: |
//using System.Collections.Generic;
//...
List<int> toAverage = new List<int>();
string input = String.Empty; int buffer; int currentAverage;
while ((input = Console.ReadLine()) != "stop")
{
if (!int.TryParse(input, out buffer))
{
Console.WriteLine("Invalid response.");
Console.WriteLine("Enter next number, or \"stop\" to exit.");
continue;
}
toAverage.Add(buffer);
currentAverage = CalculateAverage(toAverage);
Console.WriteLine("Current average: " + currentAverage.ToString());
Console.WriteLine("Enter next number, or \"stop\" to exit.");
}
Console.ReadLine();
}
private int CalculateAverage(List<int> values)
{
int sum = 0;
for (int i = 0; i < values.Count; i++) sum += values[i];
return (sum / values.Count);
}
|
Edit:
| Quote: | | Ew...goto. Use a do while loop. |
o_O
Why a do-while? I prefer pre-test loops. _________________
|
|
| Back to top |
|
 |
|