-
March 19th, 2012, 07:47 AM
#1
Need Help with C# program
Hi I am very new to C#. In an attempt to create a small program that asks the user to input grades and then outputs the grade average, I ran into some issues. This is what I have so far.
I know that it is no where near done, and probably wrong. I would just need to know how to assign the grades A,B,C,D,F and give them values for the calculations. Can anyone help in this matter?
Thanks
Tom
string[] grades = new String[5];
int index = 0;
//loop to have the user input arrays values dynamically
for (index = 0; index <= 4; index++)
{
Console.WriteLine("Enter of your grade " + (index + 1) + ": ");
grades[index] = Console.ReadLine();
}
// loop to print out array values
Console.WriteLine("\nThese are the grades you entered.");
Console.WriteLine("Your Grade average is" index++ / 5);
for (index = 0; index <= 4; index++)
{
Console.WriteLine(grades[index] + "\n");
}
Console.WriteLine("\nPress [Enter] key to exit.");
Console.ReadLine();
}
-
March 19th, 2012, 09:03 AM
#2
Re: Need Help with C# program
You should lookup the double.Parse(string s) and double.TryParse(string) functions.
Then you could use a double to add new values and just divide by 5 after the for loop.
Code:
int index = 0;
double d = 0;
//loop to have the user input arrays values dynamically
for (index = 0; index <= 4; index++)
{
Console.WriteLine("Enter of your grade " + (index + 1) + ": ");
grades[index] = Console.ReadLine();
d += double.TryParse(...);
}
// loop to print out array values
Console.WriteLine("\nThese are the grades you entered.");
//Console.WriteLine("Your Grade average is" index++ / 5);
Console.WriteLine("Your Grade average is" d / 5);
...
Check what would happen if the TryParse fails, what would be added to d?
Hope it helps.
-
March 19th, 2012, 04:02 PM
#3
Re: Need Help with C# program
Thank you very much nikel. I am definatley going to try this tomorrow. I decided to let the user enter grades from 0% to 100% as supposed to A, B... If you could give some advice on how to error trap for blank values, letters, and over 100% values to output invalid entry, please try again
-
March 19th, 2012, 04:31 PM
#4
Re: Need Help with C# program
Also remember that if:
index = 3
then
index++ / 5 is three fifths
and
++index / 5 is four fifths.
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
March 19th, 2012, 07:08 PM
#5
Re: Need Help with C# program
Below is a complete working class in your name TomTom that implements part of the problem statement.
Take time to figure out how I use my variables and their scopes.
Also see that I have used a format specifier {0} in my Console.WriteLine() statements.
Code:
using System;
using System.Text;
namespace TomTom1985
{
class TomTom1985
{
public static void Main(string[] args)
{
// grades average
int average = 0;
// grades are stored as string in this local variables
string[] grades = new string[5];
// special characters to exclude in our consoleInput variable
char[] specialChar = {' ', ','};
// prompt user to enter grades and read input
Console.Write("Enter of your grades seperated by commas/spaces: ");
string consoleInput = Console.ReadLine();// initializing string consoleInpt
// output a new line
Console.WriteLine();
// remove special characters
grades = RemoveSpecialChars(consoleInput, specialChar);
// convert grades to int value type
int[] intGrades = new int[5];
for (int i = 0; i < grades.Length; i++)
{
intGrades[i] = Convert.ToInt32(grades[i]);
}
Console.Write("You enterd the following grades: ");
// loop to print out array values
for (int index = 0; index < grades.Length; index++)
{
Console.Write("{0} ", grades[index]);
}
// output a new line
Console.WriteLine();
// compute and output average
average = Average(intGrades);
Console.WriteLine("Grade Average is: {0}", average);
}
public static int Average(int[] values )
{
// assume total is zero
int total = 0;
for (int valuesIndex = 0; valuesIndex < values.Length; valuesIndex++)
{
total += values[valuesIndex];
}
return total/(values.Length);// return average
}
public static string[] RemoveSpecialChars(string input, char[] targets)
{
// maximum value to return
int maxReturn = 5;
// Split the input string up using the target
// characters as the delimiters.
string[] subStrings = input.Split(targets, maxReturn, StringSplitOptions.RemoveEmptyEntries);
// output will contain the eventual output information.
return subStrings;
}
}
}
Try to implement the other possibilities and share with me.
---------------------------------------------------------------------------------------------------------------------------
Lastly I'm learning too... Do continue to share your problems with me as I'd do same to promote our learning. Thanks.
Last edited by chillaxzino; March 19th, 2012 at 07:10 PM.
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
|