I\'m getting a few errors but I don\'t know why I am getting them because I did
ID: 3691251 • Letter: I
Question
I'm getting a few errors but I don't know why I am getting them because I did declare it. I'm using Microsoft Visual Studio 2012, can someone go through it and use these numbers as the 6 scores: 89, 94, 100, 78, 84, 65 to input if output works. Program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not accept negative numbers for test scores.
Error 1 error C2062: type 'double' unexpected 85
Error 2 Error 2 error C2065: 'average' : undeclared identifier 92
code:
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void sortTestScores(int *TestScores, int size_Test);
double avgTestScore(int *TestScores, int size_Test);
void printTestScores(int *TestScores, int size_Test);
int main()
{
//Define variables
int *TestScores;
int size_Test, score;
double average;
//Get the number of test scores you want to average
cout << "How many test scores do you want to enter?";
cin >> size_Test;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new int[size_Test];
//Get test scores
cout << "Enter " << size_Test << " positive test scores below:" << endl;
for (int i=0; i<size_Test; i++)
{
//Display score
cout << "Score " << (i + 1) << ": ";
cin >> score;
// Input validation. Only numbers between 0-100
while (score<0 || score>100)
{
cout << "You must enter a score that is positive" << endl;
cout << "Please enter again: ";
cin >> score;
}
TestScores[i]=score;
}
//Dsiplay the results
cout << "Test Scores: ";
printTestScores(TestScores, size_Test);
sortTestScores(TestScores, size_Test);
cout << "The test scores, sorted in ascending order, are: ";
printTestScores(TestScores, size_Test);
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;
system ("pause");
return 0;
}
//Accepts a dynamic array of test scores and size of array, then sorts in ascending order
//Sort function implementation
void sortTestScores(int *TestScores, int size_Test)
{
int *last=TestScores+size_Test; //get last location of array
for (int *first = TestScores; first < last-1; first++)
{
for(int *next=first+1; next<first; next++)
{
if (*next<*first)
{
int temp=*first;
*first=*next;
*next=temp;
}
}
}
}
//calculates and returns average of test scores
double arrAvgScore (double *TestScores, int size_Test)
{
int total = 0,
double average;
int *arranged;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
//Prints test scores stored in array
void printTestScores(int *TestScores, int size_Test)
{
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
cout << *arranged << " " << arranged++ << endl;
}
}
Explanation / Answer
I think, within your function definition of arrAvgScore() you didnt kept semicolon so thats why it is showing errors of 'double' unexpected at line 85 and 'average' as undeclared identifier in line 92.
Below is your code:-
double arrAvgScore (double *TestScores, int size_Test)
{
int total = 0, // here use semicolon.....
double average;
int *arranged;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
Also i found few points where your sorting is getting stucked...
Point 1 -
=======
//Display score
cout << "Score " << (i + 1) << ": ";
cin >> score;
By writing (i+1) -- by giving this the i will be incremented and TestScores is taking from 1 where in printing you are looping from 0 which gives wrong output.
Instead you must keep as below ...
//Display score
cout << "Score " << i + 1 << ": ";
cin >> score;
Point 2 - In sortTestScores() the for loop must iterate till last element defined in 'last' variable and not in the 'first' variable.Hence loop is not being executed so 'first' must be replaced with 'last'.
=======
Wrong - for(int *next=first+1; next<first; next++)
Correct - for(int *next=first+1; next<last; next++)
Point 3 - In printTestScores() the 'arranged' variable must be increemented after cout statement.
=======
Try the below modified program ...I think it may help you.I had commented the errors which i noticed in the CAPITALS .just check them....
Your modified program is present below...
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void sortTestScores(int *TestScores, int size_Test);
double avgTestScore(int *TestScores, int size_Test);
void printTestScores(int *TestScores, int size_Test);
int main()
{
//Define variables
int *TestScores;
int size_Test, score;
double average;
//Get the number of test scores you want to average
cout << "How many test scores do you want to enter?";
cin >> size_Test;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new int[size_Test];
//Get test scores
cout << "Enter " << size_Test << " positive test scores below:" << endl;
for (int i=0; i<size_Test; i++)
{
//Display score
cout << "Score " << i + 1 << ": "; // HERE (i+1) IF USED THEN i WILL BE INCREMENTED SO WE MUST USE i+1
cin >> score;
// Input validation. Only numbers between 0-100
while (score<0 || score>100)
{
cout << "You must enter a score that is positive" << endl;
cout << "Please enter again: ";
cin >> score;
}
TestScores[i]=score;
}
//Dsiplay the results
cout << "Test Scores: ";
printTestScores(TestScores, size_Test);
sortTestScores(TestScores, size_Test);
cout << "The test scores, sorted in ascending order, are: ";
printTestScores(TestScores, size_Test);
average = avgTestScore(TestScores, size_Test);
cout << fixed << showpoint << setprecision(2);
cout << "The average of all the test scores is " << average << endl;
system ("pause");
return 0;
}
//Accepts a dynamic array of test scores and size of array, then sorts in ascending order
//Sort function implementation
void sortTestScores(int *TestScores, int size_Test)
{
int *last=TestScores+size_Test; //get last location of array
for (int *first = TestScores; first < last-1; first++)
{
for(int *next=first+1; next<last; next++) // LOOP MUST BE ITERATED TILL THE LAST ELEMENT
{
if (*next<*first)
{
int temp=*first;
*first=*next;
*next=temp;
}
}
}
}
//calculates and returns average of test scores
double arrAvgScore (double *TestScores, int size_Test)
{
int total = 0; // HERE SEMICOLON MUST BE USED
double average;
int *arranged;
for (int i=0; i < size_Test; i++)
{
total+= *arranged;
arranged++;
}
average= double(total) / size_Test;
return average;
}
//Prints test scores stored in array
void printTestScores(int *TestScores, int size_Test)
{
int *arranged=TestScores;
for (int i=0; i < size_Test; i++)
{
cout << *arranged << " " << arranged << endl;
arranged++; // THIS VARIABLE BUT BE INCREEMENTED AFTER PRINTING ON THE SCREEN
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.