Program #4 Write a complete C++ program, including a good comment in each functi
ID: 3706970 • Letter: P
Question
Program #4
Write a complete C++ program, including a good comment in each function and in the main program, to do the following:
Outline: The main program will read in three integers representing a student's scores on three tests. The main program will call a function to determine if these three scores are valid. If the scores are valid, the main program will call other functions to classify the scores in various ways. Repeat this process for the entire set of data, then print some counters.
Details (a description of each function and then the main program):
Write a function introduction which will not receive any parameters (and will not return an answer). This function will print a description of the program. It will also explain how to signal the end of the set of data.
Write a function aretheyvalid which will receive three integer parameters representing three test scores. The function will determine whether or not the three scores are valid— each in the range from 0 - 100.
For each score, if the score is invalid, the function will print it, with a message explaining why it is invalid--too big or too small. If all three scores are valid, the function will return a signal to the main program saying this is a valid set of scores; if any score is invalid (or two or more are invalid), the function will return a signal saying this is an invalid set of scores. NOTE: A Boolean variable—a variable with data type bool--is perfect for the answer to be returned by the function.
Write a function aretheysame which will receive three integer parameters, each a value from 0 to 100. The function will compare these three values and print one of the following four messages: (1) all three numbers are the same; (2) two numbers are the same and the third is larger (there are three ways this can happen); (3) two numbers are the same and the third is smaller (also three ways for this to occur); (4) all three numbers are different. The function will print, but it will not return a value.
Write a function roundscore which will receive one integer parameter, a number from 0 to 100. This function will round the integer to the nearest 10; round up for 5 to 9, and round down for 0 to 4. The function will return the rounded score to the calling program.
EXAMPLES: If you send the function 54, it will return 50; if you send 70, it will return 70; if you send 88, it will return 90; if you send 75, it will return 80.
Main Program:
The main program will call the function introduction one time, to start things off. After this, in a loop, the main program will read in three numbers. The main program will print the numbers as soon as they are read in.
The main program will call the function aretheyvalid to determine if this group is valid.
The main program will print the result of the function call (and add to counters—see below).
If the function aretheyvalid says it is a valid group, the main program will then process this set of data. However, if the function aretheyvalid says the group is not valid, the main program will simply go back to read the next group of data values. Be sure to skip a few lines on the output display.
If the group is valid, first the main program will send the original three numbers to a function called aretheysame. Then the main program will send the numbers one at a time to another function called roundscore, producing three rounded scores. (There will be three calls to roundscore.) The main program will print each original number and what it was rounded to. Finally, the main program will send the three rounded scores to the function aretheysame again. How is this call different from the first call to this function?
The main program will continue this process for the entire set of test scores. You decide how to determine the end of the entire data set. Be sure to explain your choice to the user (in the introduction function) and in a comment.
When the main program runs out of groups (your choice of how to decide when the main program has run out of data), the main program will print the final values of three counters it has been keeping track of. The three counters are: the total number of groups processed, how many groups were valid, and how many groups were invalid.
Use three separate counters. Do NOT compute one from the others.
DATA: You will be judged on the quality of your data. Include at least 20 groups of various types. Include each possible comparison message at least 3-4 times, and be sure to cover the various ways each message can occur. Include at least 5 invalid groups (spread them through the entire set of data-not grouped together), some with the first grade invalid, some with the second or third invalid, and some with more than one invalid grade. Have several cases where the original grades are different but the rounded grades are equal.
You are allowed (but not required) to use a data file for the input to the program; you are also allowed to use a file for output, sending the prompts to the screen, but the rest of the output to this file. If you include a data file, be sure to print a copy of it.
STYLE: Each function should have a good comment explaining its role in the program and what parameter(s) it will receive.
Explanation / Answer
#include<stdio.h>
#include<math.h>
#include<iomanip.h>
#include<conio.h>
void introduction()
{
cout<<" Program will read in three integers representing a student's scores On three tests.";
cout<<" After that it will determine if these three scores are valid or invalid";
cout<<" If the scores are valid, Then it will check the comparison between the scores for same or different";
cout<<" various ways. If valid then roundup the data and again comparison will perform";
cout<<" Finally it will show number of groups, number of valid groups and number of invalid groups";
}
unsigned int aretheyvalid(int x,int y, int z)
{
unsigned int opt;
if(x>=0 && x<=100 && y>=0 && y<=100 && z>=0 && z<=100) // VALIDITY CHECK OF SCORES
{
cout<<endl<<"VALID SET OF SCORES";
opt=1;
}
else
{
cout<<endl<<"INVALID SET OF SCORES"; // COMPARISON OF SCORES IN DIFFERENT VIEWS
if(x<0)
cout<<endl<<"FIRST SCORE IS TOO SMALL..." ;
if(y<0)
cout<<endl<<"SECOND SCORE IS TOO SMALL...";
if(z<0)
cout<<endl<<"THIRD SCORE IS TOO SMALL...";
if(x>100)
cout<<endl<<"FIRST SCORE IS TOO BIG..." ;
if(y>100)
cout<<endl<<"SECOND SCORE IS TOO BIG...";
if(z>100)
cout<<endl<<"THIRD SCORE IS TOO BIG...";
opt=0;
}
return(opt); // RETURNING THE RESULT OF VALIDITY CHECK
}
void aretheysame(int x, int y,int z)
{
if(x==y && y==z) // COMPARE THE EQUALITY OF NUMBERS
cout<<endl<<"ALL THREE ARE SAME SCORES";
else
if(x==y && y>z) // COMPARE THE SCORES FOR 2 SAME OR NOT
cout<<endl<<"FIRST & SECOND SCORES SAME AND THIRD IS SMALLER";
else
if(x==y && y<z)
cout<<endl<<"FIRST & SECOND SCORES SAME AND THIRD IS GREATER";
else
if(x==z && z>y)
cout<<endl<<"FIRST & THIRD SCORES SAME AND SECOND IS SMALLER";
else
if(x==z && z<y)
cout<<endl<<"FIRST & THIRD SCORES SAME AND SECOND IS GREATER";
else
if(y==z && z>x)
cout<<endl<<"SECOND & THIRD SCORES SAME AND FIRST IS SMALLER";
else
if(y==z && z<x)
cout<<endl<<"SECOND & THIRD SCORES SAME AND FIRST IS GREATER";
else
if(x!=y && x!=z && y!=z) // COMPARISON FOR DIFFERENT SCORES
cout<<endl<<"ALL THREE ARE DIFFERENT";
}
int roundscore(int x)
{ int z;
if(x%10 < 5) // ROUND DOWN THE NUMBER
z=(x-(x%10));
else
z=(x+(10-(x%10))); // ROUND UP THE NUMBER
cout<<endl<<z;
return(z);
}
void main()
{
static count,nov,noiv; /
int x,y,z,p,q,r,choice;
unsigned int opt;
clrscr();
introduction(); // INTRODUCTION FUNCTION CALL
while(1)
{
clrscr();
count++; // COUNTING NUMBER OF GROUPS
cout<<"Enter three scores[0 to 100]"; // ENTERING OF THREE SCORES
cin>>x>>y>>z;
cout<<endl<<"ENTERED SCORES ARE "<<x<<" "<<y<<" "<<z;
opt=aretheyvalid(x,y,z); // CHECK VALID SCORES OR NOT
if(opt==1)
{
nov++; // COUNTING NUMBER OF VALID GROUPS
aretheysame(x,y,z); // COMPARISON OF SAME NUMBERS OR DIFFERENT
p=roundscore(x);
q=roundscore(y);
r=roundscore(z);
cout<<endl<<"ENTERED SCORES"<<" "<<"ROUNDED SCORES";
cout<<endl<<endl<<x<<" "<<p;
cout<<endl<<endl<<y<<" "<<q;
cout<<endl<<endl<<z<<" "<<r;
cout<<endl<<"AFTER ROUNDED UP";
aretheysame(p,q,r); // COMPARISON OF SAME NUMBERS OR DIFFERENT AFTER ROUNDED UP
}
else
noiv++; // COUNTING NUMBER OF INVALID GROUPS
cout<<" ENTER 0 to EXIT & 1 to CONTINUE"; // DISPLAYING THE CHOICE TO CONTINUE OR NOT FOR ENTERING MORE GROUPS
cin>>choice;
if(choice==0)
break;
else
if(choice==1)
continue;
else
cout<<endl<<"SORRY INVALID CHOICE";
}
cout<<" TOTAL NUMBER OF GROUPS :"<<count;
cout<<" TOTAL NUMBER OF VALID GROUPS"<<nov;
cout<<" TOTAL NUMBER OF INVALID GROUPS"<<noiv;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.