Program to be revised down below Revise Project #5 by replacing your array with
ID: 3689687 • Letter: P
Question
Program to be revised down below
Revise Project #5 by replacing your array with a vector. The entire project will remain exactly the same except it will use a vector instead of an array Please note the following 1. #include 2. Change declaration Array: double scores[MAX_NUM_SCORES]; Vector: vector scores, I declare an empty vector 3. Although arrays are automatically reference parameters, vectors are not. So in your functions you need to decide if the vector needs to be a value, reference or constant reference parameter 4. When reading from the file Arrays: inFile> scores]: Vectors inFile >> oneScore: II this is a double variable that holds one score from the file scores.push back(oneScore 5. Since you are using push. .back to store the scores in the vector, you will always be able to determine how many elements are in the vector by using the size function. So you will not have to pass the numberfScores as a parameter to your functions. Anays: for (int index = 0; indexExplanation / Answer
Hi, I have coded using vector. Since i do not have scors.txt, so i did not test. Please test it.
Code is compling fine;
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
void openFile(ifstream &inFile);
void readArray(ifstream &inFile, vector<double>);
void determineHighLow(vector<double>);
void calculateScore(vector<double>,double high, double low, double total);
void displayScore(double total);
double high, low, total;
int main(){
ifstream inFile;
inFile.open("scores.txt");
vector<double> scores;
openFile(inFile);
readArray(inFile, scores);
determineHighLow(scores);
calculateScore(scores, high, low, total);
displayScore(total);
return 0;
}
void openFile(ifstream &inFile){
inFile.open("scores.txt");
if(!inFile){
cout<<"Can't find the input file"<<endl;
system("pause");
exit(-1);
}
}
void readArray(ifstream &inFile, vector<double> numbers){
double oneScore;
while(!inFile.eof()){
inFile>>oneScore;
numbers.push_back(oneScore);
}
}
void determineHighLow(vector<double> numbers){
high = numbers[0];
low = numbers[0];
for(unsigned int i=0; i<numbers.size(); i++){
if(numbers[i] > high){
high = numbers[i];
}else if(numbers[i] < low){
low = numbers[i];
}
}
cout<<"Dropped Scores "<<high<<" "<<low<<endl;
}
void calculateScore(vector<double> numbers, double high, double low, double total){
total = 0;
for(unsigned int i=0; i<numbers.size(); i++){
total = total + numbers[i];
}
total = total - high - low;
}
void displayScore(double total){
cout<<"Received score is "<<total<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.