|
-
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
|