Assignment 5 Functions and If statements Due Thursday, October 19, 2017 For each
ID: 3596134 • Letter: A
Question
Assignment 5 Functions and If statements Due Thursday, October 19, 2017 For each of the programs assigned below, submit the following: • Copy of the source code (.c ) saved as an electronic attachment Assignment 5 located in Canvas under Assignments. • Word docx file with screen captures of sample executions for all cases presented in problems. 1. Write a program • to prompt the user to enter the length of a shape and the height of that same shape. • Next, prompt the user to enter the type of shape they would like to calculate the area of: a rectangle(have them enter a character R) or a triangle(have them enter a character T). • Using an if structure, call a function called AreaRect if the user entered R. AreaRect should accept two inputs(length and height) and should return the area to main. • Else Call a function called AreaTri. AreaTri should accept two inputs(length and height) and should return the area to main. • Using a second if structure, print “the area of the rectangle is %lf” if the user entered R • Else print “the area of the triangle is %lf” Run with sample data: h=4.3, l=6.4, shape= T and h=3.7, l=7.2, shape=R 2. Write a program to calculate a student’s grade based on three test scores, using the guidelines below: • Write a function readScores and call it three times to read three test scores (between 0 and 100), using data type float for the scores. • Call a function calculateGrade to determine the student’s grade from the three test scores using the algorithm below. The function calculateGrade receives the three scores and returns the grade as a character. Implement the algorithm using nested statements within a given range rather than merely simple if statements. The grade is not printed from this function. These are the guidelines: o If the average score is 90% or more, the grade is A. o If the average score is 70% or more and less than 90%, check the third score. If the third score is more than 90%, the grade is A; otherwise the grade is B. o If the average score is 50% or more and less than 70%, check the average of the second and third scores. If the average of the two is greater than 70%, the grade is C; otherwise the grade is D. o If the average score is less than 50% then the grade is F. • Print the student’s grade from function main (or if you prefer, from a printResults function). Test with the following sets of scores: a. Test 1 score: 74 Test 2 score: 82 Test 3 score: 92 b. Test 1 score: 62 Test 2 score: 84 Test 3 score: 73 c. Test 1 score: 52 Test 2 score: 64 Test 3 score: 85 d. Test 1 score: 54 Test 2 score: 62 Test 3 score: 61
Explanation / Answer
// question 1
#include <iostream> // Header file for basic input and uotput operations
using namespace std;
//Declaration of function
float AreaRec(float , float); // To calculate area of rectangle
float AreaTri (float, float); // To calculate area of traingle
int main ()
{
char shape; // for taking input whether traingle or rectangle
float lenght, height; // declaring ltenght and height
cout<<"Enter the height of the shape: ";
cin>>height;
cout<<"Enter the Lenght of the shape: ";
cin>>lenght;
cout<<" Enter the shape of the object R for rectangle and T for triangle: ";
cin>>shape; // inputing shape
if (shape == 'T') // checkinf if input is T or R
cout<<" Area of triangle is: " << AreaTri(height,lenght);
else if (shape == 'R')
cout<<" Area of triangle is: " <<AreaRec(height,lenght);
else // if input is other than T and R it will show appropriate messge and terminate
{
cout<<"You have entered wrong Choice: "<< shape << " Please see options";
cout<<"Exiting program";
return 0;
}
cout<<endl;
}
// DEfination of functions
float AreaRec(float ht, float len)
{
return (len*ht); // calculating and returning area of rectrangle
}
float AreaTri( float ht, float len)
{
return ((ht*len)/2); // calculating and returning area of traingle
}
//Output of the code
dps@Machine:~/Documents/C++$ ./area
Enter the height of the shape: 4.3
Enter the Lenght of the shape: 6.4
Enter the shape of the object
R for rectangle and
T for triangle: T
Area of triangle is: 13.76
dps@Machine:~/Documents/C++$ ./area
Enter the height of the shape: 3.7
Enter the Lenght of the shape: 7.2
Enter the shape of the object
R for rectangle and
T for triangle: R
Area of triangle is: 26.64
//Question 2
#include <iostream> //for basic input output
using namespace std;
// function to input scores
float readScores (){
float score;
cout<<"Enter the scores of test (0-100): ";
cin>>score;
return (score);
}
//function to calculate grades
char calculateGrade ( float first, float second, float third) {
float per = (((first+second+third)/3)); // declaring and calculating average
// comapring marks and returning equailant grade
if (per >= 90)
return('A');
else if (per >=70){ // Nested If else
if (third > 90)
return ('A');
else
return ('B');
}
else if (per >=50){
int avg= ((second+third)/2);
if ( avg > 70 )
return ('C');
else
return ('D');
}
else if (per < 50 )
return 'F';
}
// FUnction for printing
void printResults (float first, float second, float third, char grade )
{
cout<<" Test 1 Score: "<<first <<" Test 2 score: "<<second <<" Test 3 score: "<<third;
cout<<" The GRADE for your marks is: "<<grade;
cout<<endl;
}
int main ()
{
float one, two, three; // declaring 3 variables to store values
// Readdin and storing values
> two =readScores();
three = readScores();
// storing values in variable
char grade= calculateGrade(one, two, three);
// calling print function
printResults(one, two, three, grade );
return 0;
}
//output of the code
dps@Machine:~/Documents/C++$ ./grade
Enter the scores of test (0-100): 74
Enter the scores of test (0-100): 82
Enter the scores of test (0-100): 92
Test 1 Score: 74 Test 2 score: 82 Test 3 score: 92
The GRADE for your marks is: A
dps@Machine:~/Documents/C++$ ./grade
Enter the scores of test (0-100): 62
Enter the scores of test (0-100): 84
Enter the scores of test (0-100): 73
Test 1 Score: 62 Test 2 score: 84 Test 3 score: 73
The GRADE for your marks is: B
dps@Machine:~/Documents/C++$ ./grade
Enter the scores of test (0-100): 52
Enter the scores of test (0-100): 64
Enter the scores of test (0-100): 85
Test 1 Score: 52 Test 2 score: 64 Test 3 score: 85
The GRADE for your marks is: C