SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Barbara Dolye
Chapter:
Data Types And Expressions
Exercise:
Programming Exercises
Question:8 | ISBN:9781285096261 | Edition: 4

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

1 0

Discussions

Post the discussion to improve the above solution.