In: Computer Science
Answer in C++ language please**
I got stuck. How do I find the Overall Average,Overall Highest number, and the Overall Lowest Number after all of this??
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int ALIASFILE_LINE_COUNT =16;
const float USERBONUSLEVEL = 75.09;
const float USERAWARDLEVEL = 80.72;
const int USERBONUS = 5;
const int USERAWARD = 8;
void selectionSortAsc( double list[], int length);
int main()
{cout <<"\n\nproject 3 template,.. \n ,...includes global
constants for project.\n"<<endl;
//1. declare and initialize your variables.
//2. configure and test the file I/O
fstream fileName;
string inputFile;
ofstream outFile;
cout<<"Enter File Name ";
cin>>inputFile;
fileName.open(inputFile.c_str());
if(!fileName)
{
cout <<"\n\n\tINPUT FILE ERROR! Exiting.\n\n";
return 1;
}
// 3. GET RATE - read & display alias.txt file to
verify.
string
info1,info2,info3,info4,info5,info6,info8,info9,info10,info11,info12,info13,info14,info15,info16,info17,info18;
double rate;
string a;
string b;
const int size = 50;
while ( fileName )
{
if (fileName == false)
cout<<"Could not open the input file"<<endl;
// read another record
getline(fileName,info1);
getline(fileName,info2);
getline(fileName,info3);
getline(fileName,info4);
getline(fileName,info5);
getline(fileName,info6);
fileName>>a>>b>>rate;
getline(fileName,info8);
getline(fileName,info9);
getline(fileName,info10);
getline(fileName,info11);
getline(fileName,info12);
getline(fileName,info13);
getline(fileName,info14);
getline(fileName,info15);
getline(fileName,info16);
getline(fileName,info17);
getline(fileName,info18);
}
// display
outFile<<info1<<endl;
outFile<<info2<<endl;
outFile<<info3<<endl;
outFile<<info4<<endl;
outFile<<info5<<endl;
outFile<<info6<<endl;
outFile<<a<<b<<rate<<endl;
outFile<<info8<<endl;
outFile<<info9<<endl;
outFile<<info10<<endl;
outFile<<info11<<endl;
outFile<<info12<<endl;
outFile<<info13<<endl;
outFile<<info14<<endl;
outFile<<info15<<endl;
outFile<<info16<<endl;
outFile<<info17<<endl;
outFile<<info18<<endl;
// 4. load the arrays with the values
// declaring 2-D array of 4 rows and 7 columns
double A[4][7];
// opening files
// opening files
cout<<"_____________________________________________________"<<endl;
{
ifstream inFile;
inFile.open("update_fullOfGrace.txt");
if(inFile.fail() ){
cerr << "\n\nERROR!! Could NOT open input file!!\n\n";
return 1;
}
int row = 0;
while(row < 4){
int col = 0;
while(col < 7){
inFile>>A[row][col];
col++;
}
row++;
}
double values[7];
for(int i=0; i<4; i++){
for(int j=0; j<7; j++){
values[j]=A[i][j];
}
selectionSortAsc(values,7);
for(int k=0; k<7; k++){
A[i][k]=values[k];
}
}
for(int i=0; i<4; i++){
for(int j=0; j<7; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
cout<<"_____________________________________________________"<<endl;
for(int i=0; i<4; i++){
double sum = 0;
for(int j=0; j<7; j++)
sum = sum + A[i][j];
cout<<"Average for Week "<<(i+1)<<" is:
"<<(sum/7.0)<<endl;
cout<<endl;
}
cout<<"_____________________________________________________"<<endl;
// 5. calculate averages for each week, and all weeks
// 6. calculate total average of all four weeks
// 7. find high values for each week.
double high1;
double high2;
double high3;
double high4;
double low1;
double low2;
double low3;
double low4;
high1=100;
high2=100;
high3=95;
high4=99;
low1=9.8;
low2=6.9;
low3=6.9;
low4=5.61;
cout<<" High Values "<<endl;
cout<<"Week 1: "<<high1<<endl;
cout<<"Week 2: "<<high2<<endl;
cout<<"Week 3: "<<high3<<endl;
cout<<"Week 4: "<<high4<<endl;
// 8. find low values for each week.
cout<<" Low Values "<<endl;
cout<<"Week 1: "<<low1<<endl;
cout<<"Week 2: "<<low2<<endl;
cout<<"Week 3: "<<low3<<endl;
cout<<"Week 4: "<<low4<<endl;
}
// 10. update the current rate
rate+=USERBONUS
if(overall_avg> USERBONUSLEVEL)
{
rate+= USERBONUS;
}
if(rate>USERBONUSLEVEL&&low!=0)
{
rate+=USERAWARD;
}
// 11. update the alias file with the new rate.
//12. out to screen
cout
<<"\n___________________________________________________________\n";
cout<<setw(14);
cout<<"OVERALL HIGH = 100"
cout
<<"\n___________________________________________________________\n";
/// 13. close out and exit gracefully,....
cout<<"\n\n\n";
return 0;
}
// 9. sort it all out.
void selectionSortAsc( double list[], int length){
int index=0, smallestIndex=0, minIndex=0;
double temp;
for (index = 0; index < length ; index++){
/// Step a
//smallestIndex = index;
for (minIndex = 0; minIndex < length-1; minIndex++)
{
if (list[minIndex] > list[minIndex + 1])
// smallestIndex = minIndex;
/// Step b
{
temp = list[minIndex];
list[minIndex] = list[minIndex + 1];
list[minIndex + 1] = temp;
}
}
}
}
Updated your code as required: please check below:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int ALIASFILE_LINE_COUNT = 16;
const float USERBONUSLEVEL = 75.09;
const float USERAWARDLEVEL = 80.72;
const int USERBONUS = 5;
const int USERAWARD = 8;
void selectionSortAsc(double list[], int length);
int main()
{
cout << "\n\nproject 3 template,.. \n ,...includes global
constants for project.\n" << endl;
//1. declare and initialize your variables.
//2. configure and test the file I/O
fstream fileName;
string inputFile;
ofstream outFile;
cout << "Enter File Name ";
cin >> inputFile;
fileName.open(inputFile.c_str());
if (!fileName) {
cout << "\n\n\tINPUT FILE ERROR! Exiting.\n\n";
return 1;
}
// 3. GET RATE - read & display alias.txt file to verify.
string info1, info2, info3, info4, info5, info6, info8, info9,
info10, info11, info12, info13, info14, info15, info16, info17,
info18;
double rate;
string a;
string b;
const int size = 50;
while (fileName) {
if (fileName == false)
cout << "Could not open the input file" << endl;
// read another record
getline(fileName, info1);
getline(fileName, info2);
getline(fileName, info3);
getline(fileName, info4);
getline(fileName, info5);
getline(fileName, info6);
fileName >> a >> b >> rate;
getline(fileName, info8);
getline(fileName, info9);
getline(fileName, info10);
getline(fileName, info11);
getline(fileName, info12);
getline(fileName, info13);
getline(fileName, info14);
getline(fileName, info15);
getline(fileName, info16);
getline(fileName, info17);
getline(fileName, info18);
}
// display
outFile << info1 << endl;
outFile << info2 << endl;
outFile << info3 << endl;
outFile << info4 << endl;
outFile << info5 << endl;
outFile << info6 << endl;
outFile << a << b << rate << endl;
outFile << info8 << endl;
outFile << info9 << endl;
outFile << info10 << endl;
outFile << info11 << endl;
outFile << info12 << endl;
outFile << info13 << endl;
outFile << info14 << endl;
outFile << info15 << endl;
outFile << info16 << endl;
outFile << info17 << endl;
outFile << info18 << endl;
// 4. load the arrays with the values
// declaring 2-D array of 4 rows and 7 columns
double A[4][7];
// opening files
// opening files
cout <<
"_____________________________________________________" <<
endl;
{
ifstream inFile;
inFile.open("update_fullOfGrace.txt");
if (inFile.fail()) {
cerr << "\n\nERROR!! Could NOT open input file!!\n\n";
return 1;
}
int row = 0;
while (row < 4) {
int col = 0;
while (col < 7) {
inFile >> A[row][col];
col++;
}
row++;
}
double values[7];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 7; j++) {
values[j] = A[i][j];
}
selectionSortAsc(values, 7);
for (int k = 0; k < 7; k++) {
A[i][k] = values[k];
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 7; j++)
cout << A[i][j] << " ";
cout << endl;
}
cout <<
"_____________________________________________________" <<
endl;
double high[4], low[4],
avg[4];
for (int i = 0; i < 4; i++) {
double sum = 0;
high[i] =
A[i][0]; // make high to first element
low[i] =
A[i][0]; // make low to first element
for (int j = 0; j < 7; j++) {
sum = sum + A[i][j];
// if new high is found
if(A[i][j] > high[i]) {
high[i] = A[i][j];
}
// if new low is found
if(A[i][j] < low[i]) {
low[i] = A[i][j];
}
}
avg[i] = (sum /
7.0);
cout << "Average for Week " << (i + 1) << " is: "
<< (sum / 7.0) << endl;
cout << endl;
}
cout << "_____________________________________________________" << endl;
// 5. calculate averages for each week, and all weeks
// 6. calculate total average of all four weeks
// 7. find high values for each week.
// find overall average
double totalAverage = (avg[0] +
avg[1] + avg[2] + avg[3])/4;
cout << " High Values " << endl;
cout << "Week 1: " << high[0] << endl;
cout << "Week 2: " << high[1]<< endl;
cout << "Week 3: " << high[2] << endl;
cout << "Week 4: " << high[3] << endl;
// 8. find low values for each week.
cout << " Low Values " << endl;
cout << "Week 1: " << low[0] << endl;
cout << "Week 2: " << low[1] << endl;
cout << "Week 3: " << low[2] << endl;
cout << "Week 4: " << low[3] << endl;
}
// 10. update the current rate
rate += USERBONUS if (overall_avg > USERBONUSLEVEL)
{
rate += USERBONUS;
}
if (rate > USERBONUSLEVEL && low != 0) {
rate += USERAWARD;
}
// 11. update the alias file with the new rate.
//12. out to screen
cout <<
"\n___________________________________________________________\n";
cout << setw(14);
cout << "OVERALL HIGH = 100" cout <<
"\n___________________________________________________________\n";
/// 13. close out and exit gracefully,....
cout << "\n\n\n";
return 0;
}
// 9. sort it all out.
void selectionSortAsc(double list[], int length)
{
int index = 0, smallestIndex = 0, minIndex = 0;
double temp;
for (index = 0; index < length; index++) {
/// Step a
//smallestIndex = index;
for (minIndex = 0; minIndex < length - 1; minIndex++) {
if (list[minIndex] > list[minIndex + 1])
// smallestIndex = minIndex;
/// Step b
{
temp = list[minIndex];
list[minIndex] = list[minIndex + 1];
list[minIndex + 1] = temp;
}
}
}
}