Write a program that computes a weighted average giving the following weights.
Homework: 10%
Projects: 35%
Quizzes: 10%
Exams: 30%
Final Exam: 15%
Do a compile-time initialization with the following values:
Homework: 97; Projects: 82; Quizzes: 60; Exams: 75; Final Exam 80. Display
all values, including the weights, appropriately labeled and formatted. Rerun
the application with different values.
Explanation:
The following program is used to computes the weighted average for the given weights and the values.
Another values also commented in the main method. Uncomment the comments and test the program with different values.
Program:
// Create a class named WeightedAverage
class WeightedAverage
{
// Create a main method to run the program
static void Main(string[] args)
{
// Declare and initilize the variables.
int HomeworkWeightPercent = 10;
int ProjectsWeightPercent = 35;
int QuizzesWeightPercent = 10;
int ExamsWeightPercent = 30;
int FinalExamWeightPercent = 15;
// Declare the variables to store the values.
int Homework = 97;
int Projects = 82;
int Quizzes = 60;
int Exams = 75;
int FinalExam = 80;
// other values
/*
int Homework=78;
int Projects=68;
int Quizzes=83;
int Exams= 91;
int FinalExam=74;
*/
// Calculate the total
double weightTotal = (HomeworkWeightPercent * Homework) +
(ProjectsWeightPercent * Projects) +
(QuizzesWeightPercent * Quizzes) +
(ExamsWeightPercent * Exams) +
(FinalExamWeightPercent * FinalExam);
// Calculate the average
double weightedAverage = weightTotal / (HomeworkWeightPercent + ProjectsWeightPercent +
QuizzesWeightPercent + ExamsWeightPercent + FinalExamWeightPercent);
// Display the values and weights
Console.WriteLine("Homework value:\t{0}\tHomework Weight:{1}%", Homework, HomeworkWeightPercent);
Console.WriteLine("Projects value:\t{0}\tProjects Weight:{1}%", Projects, ProjectsWeightPercent);
Console.WriteLine("Quizzes value:\t{0}\tQuizzes Weight:\t{1}%", Quizzes, QuizzesWeightPercent);
Console.WriteLine("Exams value:\t{0}\tExams Weight:\t{1}%", Exams, ExamsWeightPercent);
Console.WriteLine("FinalExam value:{0}\tFinalExam Weight:{1}%", FinalExam, FinalExamWeightPercent);
// Display the weighted average.
Console.WriteLine("\nWeighted Average: {0:00.00}", weightedAverage);
Console.ReadKey();
}
}
Output:
Homework value: 97 Homework Weight:10%
Projects value: 82 Projects Weight:35%
Quizzes value: 60 Quizzes Weight: 10%
Exams value: 75 Exams Weight: 30%
FinalExam value:80 FinalExam Weight:15%
Weighted Average: 78.90