COSC 1436 Programming Assignment 2 Programming Assignment 2 Refactoring is the process of taking existing code and improving its design without changing the functionality. In this programming assignment, you are going to take your code from Programming Assignment 1 and refactor it so that it uses functions. The program should still do the following: Get the name of a student Get the name of three assignments Get the grade of three assignments as integers Calculates the average of the three assignments as a double Displays the name of the student with his/her average to one decimal place Displays the names and grades of the assignments neatly as a table. However, you will now need to create 5 functions. You should write a function for each of the following: Get the name of the student Get the name of the three assignments Get the grade of each of the three assignments Get the average of the three assignments Display the grade information to the student including the student’s name, average, and list of grades As you can see, you basically need a function for each task your program performs. You will have a total of 6 functions including main. The main function should do nothing but create needed variables to pass to the functions and then call the functions in the correct order to produce the desired output. Make sure you use good programming style. Comment every variable, calculation, and function. Use self-documenting names for variables and functions. You should be able to reuse most of your code that you wrote for Programming Assignment 1. You should use arrays for the assignment names and the assignment grades. The code will need to be moved into the appropriate function. Some minor changes may need to be made to account for passing data into the function and returning a value from the function. Name your file PA2_lastName_firstName.cpp, replacing lastName with your actual last name and firstName with your actual first name.
COSC 1436 Programming Assignment 2 Notice that the possible output for a sample run will look the same as it did for Programming Assignment 1: Enter student first and last name: Roy Biv
Enter the name of assignment 1: Homework 1 Enter the name of assignment 2: Programming Lab 1 Enter the name of assignment 3: Exam 1 Enter the grade for Homework 1: 95 Enter the grade for Programming Lab 1: 88 Enter the grade for Exam 1: 100 The average for Roy Biv is 94.3 Here are your grades: Homework 1: 95 Programming Lab 1: 88 Exam 1: 100 Thank you for playing.
Code
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
#define NUM_ASSIGNMENTS 3
string getName();
void getAssignments(string []);
void getGrades(int [],string []);
void displayInfo(int [],string [],double,string);
double getAverage(int []);
int main()
{
string name;
string assignments[NUM_ASSIGNMENTS];
int grades[NUM_ASSIGNMENTS];
double avg;
name=getName();
getAssignments(assignments);
getGrades(grades,assignments);
avg=getAverage(grades);
displayInfo(grades,assignments,avg,name);
}
string getName()
{
string name;
cout<<"Enter student first and last name:
";
getline(cin,name);
return name;
}
void getAssignments(string assi[])
{
cout<<endl;
for(int i=0;i<NUM_ASSIGNMENTS;i++)
{
cout<<"Enter the name of
assignment "<<(i+1)<<" :";
getline(cin,assi[i]);
}
}
void getGrades(int grades[],string assi[])
{
cout<<endl;
for(int i=0;i<NUM_ASSIGNMENTS;i++)
{
cout<<"Enter the grade for
"<<assi[i]<<" :";
cin>>grades[i];
}
}
double getAverage(int grades[])
{
double sum=0;
for(int i=0;i<NUM_ASSIGNMENTS;i++)
sum+=grades[i];
return sum/NUM_ASSIGNMENTS;
}
void displayInfo(int grd[],string assi[],double avg,string
name)
{
cout<<endl;
cout<<fixed<<setprecision(1);
cout<<"The average for "<<name<<" is
"<<avg<<endl;
cout<<"\nHere are your
grades:"<<endl;
for(int i=0;i<NUM_ASSIGNMENTS;i++)
cout<<assi[i]<<":
"<<grd[i]<<endl;
cout<<"\n\nThank you for
playing."<<endl;
}
output
If you have any query regarding the code
please ask me in the comment i am here for help you. Please do not
direct thumbs down just ask if you have any query. And if you like
my work then please appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.