Write a program that gives the user a menu of 8 options. The options are: Read i
ID: 3772047 • Letter: W
Question
Write a program that gives the user a menu of 8 options. The options are:
Read in 10 scores (integers) from the user.
Read in 10 scores from a file, scores.txt.
Print the 10 scores.
Print the highest score.
Print the lowest score.
Print the mean (average) of the 10 scores.
Print a score based on an entry or row# and show how many scores are higher.
Exit the program.
Both the size of the array (a constant) and the array itself must be declared locally inside of int main(). You should initialize the array to some value (probably 0) in case the user chooses options 3 - 7 before 1 or 2. Options 1 or 2 will overwrite any value previously stored in the array.
Each option in the menu must simply call a function. Each function must at minimum pass in the array and the size of the array. You must list all function prototypes above int main () and all function definitions must appear after int main(). You will find a few helpful programs in Gaddis text - Chapter 8 that provide examples on how to declare and process homogenous data with 1-D arrays as well as the set-up of some useful array functions.
You may use the input file, provided here => scores.txt or you may create your own. You do not need to turn in your input file. Please note that I will be using this version of the scores.txt when testing your program.
Provide appropriate input validation where necessary, use meaningful identifier names and DO NOT declare any variables globally.
Here is a sample run of the program that you may use as a guide in helping set-up the interface and also provides a few check figures...
If your program does not compile, it will not be graded. HINT: I am thinking seven to eight array processing functions with procedures or algorithms that address the menu choice should be adequate. Use a do while loop with a switch statement. You should call the relevant functions from each case. Compile your code often. Only write a small portion of code, let's say a single function for a specific menu option, check that it still compiles before moving on. This way when you get a syntax error, you can be fairly certain the error is in the part you just wrote.
Modify the program that you wrote above "Score Analysis" so it includes two more menu options and write the necessary procedures specified below in two additional functions:
Explanation / Answer
Here is the complete program
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include<sys/types.h>
using namespace std;
void readFile(int *a, int s);
void readScores(int *a, int s);
void printAllScores(int *a, int s);
void printHighest(int *a, int s);
void printLowest(int *a, int s);
void average(int *a, int s);
void printOneScore(int *a, int s);
int readMenu();
int getpid();
int main() {
clock_t tStart = clock();
bool flag=true;
int size=10;
int a[10]={0,0,0,0,0,0,0,0,0,0};
int option;
do
{
option=readMenu();
switch (option)
{
case 1:
readScores(a,size);
break;
case 2:
readFile(a,size);
break;
case 3:
printAllScores(a,size);
break;
case 4:
printHighest(a,size);
break;
case 5:
printLowest(a,size);
break;
case 6:
average(a,size);
break;
case 7:
printOneScore(a,size);
break;
case 8:
flag=false;
break;
default:
cout <<" Invalid Option ";
break;
}
}
while (flag);
cout << "Array processing test now concluded. Exiting program ..... ";
float time=(double)(clock() - tStart)/CLOCKS_PER_SEC;
cout <<" Execution Time: " << time<<" ";
return 0;
}
void readFile(int *a, int s)
{
int i,p;
ifstream myfile;
myfile.open("scores.txt");
for(i=0;i<s;i++)
{
if(myfile >>p)
a[i]=p;
else cout <<" There is some problem with the file. Cannot Read Full data ";
}
cout <<" File Read Complete";
}
void readScores(int *a, int s)
{
int i=0;
for(i=0;i<s;i++)
{
cout << "Enter score#"<<i+1<<": ";
cin >>a[i];
}
cout << " Scores Read. Please select option 3 to view scores ";
}
void printAllScores(int *a, int s)
{
int i=0;
for(i=0;i<s;i++)
{
cout << "Score#"<<i+1<<": "<< a[i]<<" ";
}
}
void printHighest(int *a, int s)
{
int i,j;
int h=0,k=0;
for(i=0;i<s;i++)
{
k=a[i];
for(j=i+1;j<s;j++)
{
if(a[j]>k)
k=a[j];
}
if(k>h) h=k;
}
cout << " Highest Score: "<< h<<" ";
}
void printLowest(int *a, int s)
{
int i,j;
int l=0,k=0;
l=a[0];
for(i=0;i<s;i++)
{
k=a[i];
for(j=i+1;j<s;j++)
{
if(a[j]<k)
k=a[j];
}
if(k<l) l=k;
}
cout << " Lowest Score: "<< l<<" ";
}
void average(int *a, int s)
{
int i,j;
float l=0;
int k=0;
for(i=0;i<s;i++)
{
l=l+a[i];
}
cout << " Average Score: "<< (float)l/s<<" ";
}
void printOneScore(int *a, int s)
{
int i,r;
int k=0;
cout<<" Enter row#: ";
cin >> r;
if(r<1 || r>s)
cout << " Invalid Row. ";
else
{
k=a[r-1];
cout <<"Value at row#"<<r<<": "<< k<<" Values Geater than "<<k<<" ";
for(i=0;i<s;i++)
{
if(a[i]>k)
cout << a[i]<<" ";
}
}
}
int readMenu()
{
int option;
cout<< " ------------------------------------- 1-D Array Processing Menu Options ------------------------------------- ";
cout << " 1. Read in 10 scores from user. 2. Read in 10 scores from the file, scores.txt. 3. Print all scores. 4. Print the highest score.";
cout << " 5. Print the lowest score. 6. Print the average score. 7. Print one score (give its entry number) 8. Quit program. Enter OPtion: ";
cin >> option;
return option;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.