Pretty new to C#.
I want to put certain functions into a method to be able to call them when needed (i.e. InputLine, InputInt, InputDouble). However, when I tried to set it up within FindAverage() It runs through the function but is not what I want. There are variables that have been inputed by a user and I want to be able to use those inputs to get the average. However, I'm not sure where to go about to do this. Any help would be greatly appreciated.
Line 102 - 163 is what I want to be able to have a general function to use in FindAverage. I've tried to use
static double FindAverage (string prompt, int i)
{
double Average = 0;
double Grade = 0;
double Grades = 0;
double Sum = 0;
int NoOfItems = InputInt (prompt, i);
for (i = 0; i<= NoOfItems; i = i +1)
{
Grade = InputDouble ("Enter grade",0);
Grades = 0;
Sum = Grade + Grades;
Average = Sum / NoOfItems;
}
return Average;
}
but it didn't work out too well.
The code below is my code which runs perfectly fine with the intended output as well.
using System;
namespace GradeCalc2
{
class Grades
{
static string InputLine (string prompt, int i)
{
Console.Write (prompt, i);
return Console.ReadLine ();
}
static int InputInt (string prompt, int i)
{
string nStr = InputLine (prompt, i).Trim ();
return int.Parse (nStr);
}
static double InputDouble (string prompt, int i)
{
string nStr = InputLine (prompt, i).Trim ();
return double.Parse (nStr);
}
static double FindAverage (double Exam, double Lab, double Hw, double Project, double Participation)
{
double number = 0;
double Average;
double Sum;
double grade;
double grades = 0;
{
grade = 0;
Sum = grade + grades;
Average = Sum / number;
return Average;
}
}
static void Main ()
{
int ExamWeight = 0;
int LabWeight = 0;
int HwWeight = 0;
int ProjectWeight = 0;
int ParticipationWeight = 0;
do {
ExamWeight = InputInt ("Enter the weight for exams: ", ExamWeight);
Console.WriteLine ("You entered {0} for exams.", ExamWeight);
LabWeight = InputInt ("Enter the weight for labs: ", LabWeight);
Console.WriteLine ("You entered {0} for lab.", LabWeight);
HwWeight = InputInt ("Enter the weight for homework: ", HwWeight);
Console.WriteLine ("You entered {0} for homework.", HwWeight);
ProjectWeight = InputInt ("Enter the weight for projects: ", HwWeight);
Console.WriteLine ("You entered {0} for Projects.", ProjectWeight);
ParticipationWeight = InputInt ("Enter the weight for participation: ", ParticipationWeight);
Console.WriteLine ("You entered {0} for participation.", ParticipationWeight);
} while (ExamWeight + LabWeight + HwWeight + ProjectWeight + ParticipationWeight !=100);
double Grade = 0;
int ExamNumber = 0;
int LabNumber = 0;
int HwNumber = 0;
int ProjectNumber = 0;
int ParticipationNumber = 0;
int ExamSum = 0;
int LabSum = 0;
int HwSum = 0;
int ProjectSum = 0;
int ParticipationSum = 0;
int i;
double ExamAverage = 0;
double LabAverage = 0;
double HwAverage = 0;
double ProjectAverage = 0;
double ParticipationAverage = 0;
double ExamGrade = 0;
double LabGrade = 0;
double HwGrade = 0;
double ProjectGrade = 0;
double ParticipationGrade = 0;
{
{
{
ExamNumber = InputInt ("Please enter the number of exams taken.", ExamNumber);
Console.WriteLine ("You entered {0} for the number of exams taken.", ExamNumber);
LabNumber = InputInt ("Please enter the number of labs done.", LabNumber);
Console.WriteLine ("You entered {0} for the number of labs done.", LabNumber);
HwNumber = InputInt ("Please enter the number of homework done.", HwNumber);
Console.WriteLine ("You entered {0} for the number of homework done.", HwNumber);
ProjectNumber = InputInt ("Please enter the number of projects done.", ProjectNumber);
Console.WriteLine ("You entered {0} for the number of projects done.", ProjectNumber);
ParticipationNumber = InputInt ("Please enter the number of times you have participated.", ParticipationNumber);
Console.WriteLine ("You entered {0} for the number of times participated done.", ParticipationNumber);
for (i = 1; i <= ExamNumber; i = i +1) {
ExamSum = InputInt ("Please enter your grade for exam {0}: ", i) + ExamSum;
Console.WriteLine ("Total exam points: {0}", ExamSum);
}
ExamAverage = ExamSum / ExamNumber;
ExamGrade = ExamAverage;
Console.WriteLine ("Average Exam Grade: {0}", ExamGrade);
for (i = 1; i <= LabNumber; i = i +1) {
LabSum = InputInt ("Please enter your grade for lab {0}: ", i) + LabSum;
Console.WriteLine ("Total lab points: {0}", LabSum);
}
LabAverage = LabSum / LabNumber;
LabGrade = LabAverage;
Console.WriteLine ("Average Lab Grade: {0}", LabGrade);
for (i = 1; i <= HwNumber; i = i +1) {
HwSum = InputInt ("Please enter your grade for homework {0}: ", i) + HwSum;
Console.WriteLine ("Total homework points: {0}", HwSum);
}
HwAverage = HwSum / HwNumber;
HwGrade = HwAverage;
Console.WriteLine ("Average Homework Grade: {0}", HwGrade);
for (i = 1; i <= ProjectNumber; i = i +1) {
ProjectSum = InputInt ("Please enter your grade for project {0}: ", i) + ProjectSum;
Console.WriteLine ("Total project points: {0}", ProjectSum);
}
ProjectAverage = ProjectSum / ProjectNumber;
ProjectGrade = ProjectAverage;
Console.WriteLine ("Average Project Grade: {0}", ProjectGrade);
for (i = 1; i <= ParticipationNumber; i = i +1) {
ParticipationSum = InputInt ("Please enter your grade for participation {0}: ", i) + ParticipationSum;
Console.WriteLine ("Total participation points: {0}", ParticipationSum);
}
ParticipationAverage = ParticipationSum / ParticipationNumber;
ParticipationGrade = ParticipationAverage;
Console.WriteLine ("Average Participation Grade: {0}", ParticipationGrade);
}
Grade = (ExamGrade * ExamWeight + LabGrade * LabWeight + HwGrade * HwWeight + ProjectGrade * ProjectWeight + ParticipationGrade * ParticipationWeight) / 100;
{
Console.WriteLine ("Your grade is {0}%", Grade);
if (Grade >= 93.0)
Console.WriteLine ("Your grade is higher than a 93% therefore you are getting an A");
else if ((Grade > 90.0) && (Grade < 93.0))
Console.WriteLine ("Your grade is greater than a 90% and less than a 93% therefore you are getting an A-");
else if ((Grade > 87.0) && (Grade < 90.0))
Console.WriteLine ("Your grade is greater than a 87% and less than a 90% therefore you are getting an B+");
else if ((Grade > 83.0) && (Grade < 87.0))
Console.WriteLine ("Your grade is greater than a 83% and less than a 87% therefore you are getting an B");
else if ((Grade > 80.0) && (Grade < 83.0))
Console.WriteLine ("Your grade is greater than a 80% and less than a 83% therefore you are getting an B-");
else if ((Grade > 77.0) && (Grade < 80.0))
Console.WriteLine ("Your grade is greater than a 77% and less than a 80% therefore you are getting an C+");
else if ((Grade > 73.0) && (Grade < 77.0))
Console.WriteLine ("Your grade is greater than a 73% and less than a 77% therefore you are getting an C");
else if ((Grade > 70.0) && (Grade < 73.0))
Console.WriteLine ("Your grade is greater than a 70% and less than a 73% therefore you are getting an C-");
else if ((Grade > 67.0) && (Grade < 70.0))
Console.WriteLine ("Your grade is greater than a 67% and less than a 70% therefore you are getting an D+");
else if ((Grade > 60.0) && (Grade < 67.0))
Console.WriteLine ("Your grade is greater than a 60% and less than a 67% therefore you are getting an D");
else if (Grade < 60.0)
Console.WriteLine ("Your grade is lower than a 60% therefore you are getting an F");
}
}
}
}
}
}
Sorry about the title. I had that problem earlier but was able to fix it and I prematurely posted this thread without updating the title.

New Topic/Question
Reply



MultiQuote





|