I have the project that this one is based off of, I will paste at the end. Creat
ID: 3573069 • Letter: I
Question
I have the project that this one is based off of, I will paste at the end.
Create a project/program named Ch_6_HW. The program should use the code from your Ch_5_HW as the starting point (just copy/paste your code) and do the following while using the selection structures as detailed below. The parts in blue are the new additions/changes:
NOTE: make sure you include a function declaration/prototype for each function above the main function. Make sure you place the function definition after the main function.
Prompt the user for the number of students grades will be entered for.
Using an if/else statement, check that the number of students is greater than 0 and display an error message if they are not. Alternatively continue processing if they are good.
Prompt the user for the number of grades that will be entered for each student.
Using an if/else statement, check that the number of grades to be entered for each student is greater than 0 and display an error message if it is not. Alternatively continue processing if it is good.
i.In a prompt the user for the number of points each assignment is worth.
In a function named pts_poss, pass one integer for the number of the assignment, get the input in the function, and return that input to the calling location as a double.
Using an if/else, check that the number of points returned is greater than or equal to 0. If not don’t increment the loop counter, otherwise increment the counter and add the number of points to an accumulator variable.
ii.After possible points have been entered for all grades print out the total points possible.
iii.In a go through the number of students
Nested in the above loop use a for loop to allow each of the grades to be entered for the specific student. The grade should be entered through a call to a function named ent_grd which receives two integers (one for student number and one for the grade number) and returns a double which is the grade entered.
Using an if/else statement, check that the grade entered is greater than or equal to 0. If it is then add it to an accumulator variable for the student’s grades, otherwise display an error message and decrement the counter variable to pass through the loop again and reenter the grade.
When all grades have been entered for that student, print out his/her average by making a call to a function named find_avg which receives two doubles (total points the student scored and total points possible) and returns a double which is the student’s average.
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int numGrades, numStudents;
double assignments = 0;
int main()
{
//Prompt the user for the number of students
cout << "Please enter the number of students: " << endl;
cin >> numStudents;
if (numStudents <= 0)
cout << "Error: The number of students must be greater than 0 " << endl;
else
{
//prompt the user for the number of grades
cout << "Enter the number of grades for each student: " << endl;
cin >> numGrades;
cout << endl;
if (numGrades <= 0)
cout << "Error, the number of grades must be greater than 0" << endl;
else
{
//use a loop to enter points for assignments
double grades;
double total = 0.0;
//prompt the user for number of points for each assignment
int numSteps;
numSteps = 1;
while (numSteps <= numGrades)
{
cout << "How many points is assignment " << numSteps << " worth? " << endl;
cin >> grades;
if (grades < 0)
{
cout << "Error, grade must be positive." << endl;
numSteps--;
}
else
{
total = total + grades;
numSteps++;
}
}
cout << "The " << numGrades << " assignments are worth a total of " << total << " points." << endl;
int numSteps2;
numSteps2 = 1;
int numSteps3;
double grades2;
{
do
{
double total2 = 0.0;
for (numSteps3 = 1; numSteps3 <= numGrades; numSteps3++)
{
cout << "Enter Student " << numSteps2 << "'s grade for assignment " << numSteps3 << endl;
cin >> grades2;
if (grades2 < 0)
{
cout << "Error, grade must be positive " << endl;
numSteps3--;
}
else
{
total2 = total2 + grades2;
}
}
cout << "The average for student " << numSteps2 << " is " << ((total2 / total) * 100) << endl;
numSteps2++;
//loop until number equals previously inputted amount
} while (numSteps2 <= numStudents);
}
}
}
system("PAUSE");
return 0;
}
Explanation / Answer
PROGRAM CODE:
/*
* Ch_6_HW.cpp
*
* Created on: 06-Dec-2016
* Author: kasturi
*/
//#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int numGrades, numStudents;
double assignments = 0;
double pts_poss(int num_assignments);
double ent_grd(int student_num, int grad_num);
double find_avg(double Student_points, double totalPoints);
int main()
{
//Prompt the user for the number of students
cout << "Please enter the number of students: " << endl;
cin >> numStudents;
if (numStudents <= 0)
cout << "Error: The number of students must be greater than 0 " << endl;
else
{
//prompt the user for the number of grades
cout << "Enter the number of grades for each student: " << endl;
cin >> numGrades;
cout << endl;
if (numGrades <= 0)
cout << "Error, the number of grades must be greater than 0" << endl;
else
{
double grades;
double total = 0.0;
//prompt the user for number of points for each assignment
int numSteps;
numSteps = 1;
while (numSteps <= numGrades)
{
grades = ent_grd(numStudents,numSteps);
if (grades < 0)
{
cout << "Error, grade must be positive." << endl;
numSteps--;
}
else
{
total = total + grades;
numSteps++;
}
}
cout << "The " << numGrades << " assignments are worth a total of " << total << " points." << endl;
assignments = total;
pts_poss(numGrades);
}
}
system("PAUSE");
return 0;
}
double pts_poss(int numAssignments)
{
int numSteps2;
numSteps2 = 1;
int numSteps3;
double grades2;
do
{
double total2 = 0.0;
for (numSteps3 = 1; numSteps3 <= numAssignments; numSteps3++)
{
cout << "Enter Student " << numSteps2 << "'s grade for assignment " << numSteps3 << endl;
cin >> grades2;
if (grades2 < 0)
{
cout << "Error, grade must be positive " << endl;
numSteps3--;
}
else
{
total2 = total2 + grades2;
}
}
cout << "The average for student " << numSteps2 << " is " << find_avg(total2, assignments) << endl;
numSteps2++;
//loop until number equals previously inputted amount
} while (numSteps2 <= numStudents);
return numAssignments;
}
double ent_grd(int student_num, int grad_num)
{
double grade = 0.0;
cout << "How many points is assignment " << grad_num << " worth? " << endl;
cin>>grade;
return grade;
}
double find_avg(double Student_points, double totalPoints)
{
return Student_points/totalPoints*100;
}
OUTPUT:
Please enter the number of students:
1
Enter the number of grades for each student:
4
How many points is assignment 1 worth?
100
How many points is assignment 2 worth?
100
How many points is assignment 3 worth?
100
How many points is assignment 4 worth?
100
The 4 assignments are worth a total of 400 points.
Enter Student 1's grade for assignment 1
80
Enter Student 1's grade for assignment 2
80
Enter Student 1's grade for assignment 3
80
Enter Student 1's grade for assignment 4
80
The average for student 1 is 80