Test Scores - input validation - c++

so the program needs to dynamically allocate an array large enough to hold a user=defined number of test scores. once all scores are entered, the array should be passed to a function that sorts them in ascending order. another function should be called that calculates the average score. the program should display the sorted list of scores and averages with appropriate headings. use pointer notation rather than array notation whenever possible.
the problem i am having is making it so that the program doesn't accept negative numbers for test scores.
here is the code.
source.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);
int main()
{
float *scores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr(scores, numScores);
showAverage(total, numScores);
//Free memory.
delete[] scores;
return 0;
}
void arrSelectSort(float *array, int size)
{
int startScan, minIndex;
float minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(float *array, int size)
{
for (int count = 0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
void showAverage(float total, int numScores)
{
float average;
//Calculate the average
average = total / numScores;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
system("pause");
}
modified source.cpp to use double instead of float
#include <iostream>
#include <iomanip>
using namespace std;
void arrSelectSort(double *, int);
void showArrPtr(double *, int);
double showAverage(double, int);
int main()
{
double *scores, //To dynamically allocate an array
total = 0.0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new double[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
cout << "The test scores in ascending order are: \n";
showArrPtr(scores, numScores);
showAverage(total, numScores);
delete[] scores;
return 0;
}
void arrSelectSort(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(double *array, int size)
{
for (int count = 0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
double showAverage(double total, int numScores)
{
double average;
//Calculate the average
average = total / numScores;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
system("pause");
}
errors -
Severity Code Description Project File Line Suppression State
Error C4716 'showAverage': must return a value ConsoleApplication9 c:\users\kenny\desktop\kenny_fepc1.cpp 85
Severity Code Description Project File Line Suppression State
Warning C4101 'average': unreferenced local variable ConsoleApplication9 c:\users\kenny\desktop\kenny_fepc1.cpp 12
please help fix. thank you.

You are not checking for the value that was input:
while (scores <= 0)
should be
while (scores[count] <= 0)
I guess this is homework and you are requested to use c-style arrays. However, please note that using a std::vector would make the task very much simpler as it has a dynamic size (that you dont have to handle "manually") and sorting a vector is trivial.
Btw I would suggest you to change the loop to
for (int count = 0; count < numScores; count++) {
scores[count] = -1;
while (scores[count] <= 0) {
cout << "Test Score #" << (count + 1) << "(has to be >0) : ";
cin >> scores[count];
}
}
Duplicate code is always a pain in the ass when you want to make changes to the code. Try to avoid it whenever possible (even if it is only two small lines).
Your other error appears because your showAverage is declared to return a double but it has no return. Either declare it to return nothing:
void showAverage(double total, int numScores)
or add a return statement at the end of the function:
return average;

Related

Your C++ program must use functions to input the scores, compute the average, and find the index of the highest score

I've been working on this for hours now and I'm almost done. I can't get the program to display the correct student ID.
Also the "highIndex" function is suppose to be an "int" but I started with a double. When I try to change it, everything else seems to fall apart.
How do I get the high score to be associated with the student ID so I can output it correctly? I also need to highIndex to be an int which means some other things needs to be changed.
Any help would be much appreciated :)
Here's what I have so far
void inputAnswers(int studentIds[], double scores[]);
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
double highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 1;
for (int count = 0; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return highScore;
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
double highScore[7];
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << highIndex(highScore) << " with a score of " << highIndex(scores);
return 0;
}
// Function gets student scores
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
As per my observation you haven't supplied any values to the Highscore array and it is not required as well.
If all you need is to find average score, highscore and id of student with highscore this slight change will do the trick just adjusted 3 values from your code and is documented at corresponding lines.
#include<iostream>
using namespace std;
double getAverage(double scores[])
{
double total = 0;
for (int count = 0; count <= 6; count++)
{
total += scores[count];
}
return total / 7;
}
void inputAnswers(int student[], double scores[])
{
for (int count = 0; count <= 6; count++)
{
cout << "Enter the score for student "<< student[count] << ": ";
cin >> scores[count];
}
}
int highIndex(double score[])
{
double highScore = score[0];
double indexHigh = 0; //as we are assigning from position 0
for (int count = 1; count <= 6; count++)
{
if (score[count] > highScore)
{
highScore = score[count];
indexHigh = count;
}
}
return indexHigh; //returns index of highscore
}
int main()
{
const int ids = 7;
int student[ids] = { 1234, 2333, 4432, 3323, 2143, 3425, 4123 };
double scores[7];
//double highScore[7]; no need
// Gets the test score from user
inputAnswers(student, scores);
// Calculates the average
cout << "The average score is " << getAverage(scores) << endl;
// Calculates highest score
cout << "The high score was student " << student[highIndex(scores)] << " with a score of " << scores[highIndex(scores)]; //uses returned index to find values from array
return 0;
}
Although i strongly recommend using class or structures for such data collection of any entitiy. Happy Coding :-)

How would you go about resolving this output value?

I finished this code homework assignment tonight. I thought I was done, but I just realized that my "Average" value is coming out wrong with certain values. For example: When my professor entered the values 22, 66, 45.1, and 88 he got an "Average" of 55.27. However, when I enter those values in my program, I get an "Average" of 55.25. I have no idea what I am doing wrong. I was pretty confident in my program until I noticed that flaw. My program is due at midnight, so I am clueless on how to fix it. Any tips will be greatly appreciated!
Code Prompt: "Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible."
Professor Notes: The book only states, "Input Validation: Do not accept negative numbers for test scores." We also need to have input validation for the number of scores. If it is negative, including 0, the program halts, we should consider this situation for 'counter' not to be negative while we have a loop to enter numbers. So negative numbers should be rejected for the number of scores and the values of scores.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void showArray(double* array, int size);
double averageArray(double* array, int size);
void orderArray(double* array, int size);
int main()
{
double* scores = nullptr;
int counter;
double numberOfScores;
cout << "\nHow many test scores will you enter? ";
cin >> numberOfScores;
if (numberOfScores < 0) {
cout << "The number cannot be negative.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
if (numberOfScores == 0) {
cout << "You must enter a number greater than zero.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
scores = new double[numberOfScores];
for (counter = 0; counter < numberOfScores; counter++) {
cout << "Enter test score " << (counter + 1) << ": ";
cin >> *(scores + counter);
if (*(scores + counter) < 0) {
cout << "Negative scores are not allowed. " << endl
<< "Enter another score for this test : ";
cin >> *(scores + counter);
}
}
orderArray(scores, counter);
cout << "\nThe test scores in ascending order, and their average, are: " << endl
<< endl;
cout << " Score" << endl;
cout << " -----" << endl
<< endl;
showArray(scores, counter);
cout << "\nAverage Score: "
<< " " << averageArray(scores, counter) << endl
<< endl;
cout << "Press any key to continue...";
delete[] scores;
scores = nullptr;
system("pause>0");
}
void orderArray(double* array, int size)
{
int counterx;
int minIndex;
int minValue;
for (counterx = 0; counterx < (size - 1); counterx++) {
minIndex = counterx;
minValue = *(array + counterx);
for (int index = counterx + 1; index < size; index++) {
if (*(array + index) < minValue) {
minValue = *(array + index);
minIndex = index;
}
}
*(array + minIndex) = *(array + counterx);
*(array + counterx) = minValue;
}
}
double averageArray(double* array, int size)
{
int x;
double total{};
for (x = 0; x < size; x++) {
total += *(array + x);
}
double average = total / size;
return average;
}
void showArray(double* array, int size)
{
for (int i = 0; i < size; i++) {
cout << " " << *(array + i) << endl;
}
}
I try to start my answers with a brief code review:
#include <iostream>
#include <iomanip>
using namespace std; // Bad practice; avoid
void showArray(double* array, int size);
double averageArray(double* array, int size);
void orderArray(double* array, int size);
int main()
{
double* scores = nullptr;
int counter;
double numberOfScores;
cout << "\nHow many test scores will you enter? ";
cin >> numberOfScores;
// This is not input validation, I can enter two consecutive bad values,
// and the second one will be accepted.
if (numberOfScores < 0) {
// Weird formatting, this blank line
cout << "The number cannot be negative.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
// The homework, as presented, doesn't say you have to treat 0 differently.
if (numberOfScores == 0) {
cout << "You must enter a number greater than zero.\n"
<< "Enter another number: ";
cin >> numberOfScores;
}
scores = new double[numberOfScores];
// Declare your loop counter in the loop
for (counter = 0; counter < numberOfScores; counter++) {
cout << "Enter test score " << (counter + 1) << ": ";
cin >> *(scores + counter);
if (*(scores + counter) < 0) {
cout << "Negative scores are not allowed. " << endl
<< "Enter another score for this test : ";
cin >> *(scores + counter);
}
}
orderArray(scores, counter); // Why not use numberOfScores?
cout << "\nThe test scores in ascending order, and their average, are: " << endl
<< endl;
cout << " Score" << endl;
cout << " -----" << endl
<< endl;
showArray(scores, counter); // Same as above.
cout << "\nAverage Score: "
<< " " << averageArray(scores, counter) << endl
<< endl;
cout << "Press any key to continue...";
delete[] scores;
scores = nullptr;
system("pause>0"); // Meh, I suppose if you're on VS
}
void orderArray(double* array, int size)
{
int counterx;
int minIndex;
int minValue; // Unnecessary, and also the culprit
// This looks like selection sort
for (counterx = 0; counterx < (size - 1); counterx++) {
minIndex = counterx;
minValue = *(array + counterx);
for (int index = counterx + 1; index < size; index++) {
if (*(array + index) < minValue) {
minValue = *(array + index);
minIndex = index;
}
}
*(array + minIndex) = *(array + counterx);
*(array + counterx) = minValue;
}
}
double averageArray(double* array, int size)
{
int x;
double total{};
for (x = 0; x < size; x++) {
total += *(array + x);
}
double average = total / size;
return average;
}
void showArray(double* array, int size)
{
for (int i = 0; i < size; i++) {
cout << " " << *(array + i) << endl;
}
}
When you are sorting your array, you keep track of the minValue as an int and not a double. That's why your average of the sample input is incorrect. 45.1 is truncated to 45 for your calculations. You don't need to keep track of the minValue at all. Knowing where the minimum is, and where it needs to go is sufficient.
But as I pointed out, there are some other serious problems with your code, namely, your [lack of] input validation. Currently, if I enter two consecutive bad numbers, the second one will be accepted no matter what. You need a loop that will not exit until a good value is entered. It appears that you are allowed to assume that it's always a number at least, and not frisbee or any other non-numeric value.
Below is an example of what your program could look like if your professor decides to teach you C++. It requires that you compile to the C++17 standard. I don't know what compiler you're using, but it appears to be Visual Studio Community. I'm not very familiar with that IDE, but I imagine it's easy enough to set in the project settings.
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
// Assumes a number is always entered
double positive_value_prompt(const std::string& prompt) {
double num;
std::cout << prompt;
do {
std::cin >> num;
if (num <= 0) {
std::cerr << "Value must be positive.\n";
}
} while (num <= 0);
return num;
}
int main() {
// Declare variables when you need them.
double numberOfScores =
positive_value_prompt("How many test scores will you enter? ");
std::vector<double> scores;
for (int counter = 0; counter < numberOfScores; counter++) {
scores.push_back(positive_value_prompt("Enter test score: "));
}
std::sort(scores.begin(), scores.end());
for (const auto& i : scores) {
std::cout << i << ' ';
}
std::cout << '\n';
std::cout << "\nAverage Score: "
<< std::reduce(
scores.begin(), scores.end(), 0.0,
[size = scores.size()](auto mean, const auto& val) mutable {
return mean += val / size;
})
<< '\n';
}
And here's an example of selection sort where you don't have to worry about the minimum value. It requires that you compile to C++20. You can see the code running here.
#include <iostream>
#include <random>
#include <vector>
void selection_sort(std::vector<int>& vec) {
for (int i = 0; i < std::ssize(vec); ++i) {
int minIdx = i;
for (int j = i + 1; j < std::ssize(vec); ++j) {
if (vec[j] < vec[minIdx]) {
minIdx = j;
}
}
int tmp = vec[i];
vec[i] = vec[minIdx];
vec[minIdx] = tmp;
}
}
void print(const std::vector<int>& v) {
for (const auto& i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
}
int main() {
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> dist(1, 1000);
std::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(dist(prng));
}
print(v);
selection_sort(v);
print(v);
}
I opted not to give your code the 'light touch' treatment because than I would have done your homework for you, and that's just not something I do. However, the logic shown should still be able to guide you toward a working solution.

C++ getting highest test score and using pointers

created a program to prompt the user to enter test scores and find the average, highest, and sort the test score. I am having problems I cant seem to get the higest test scores from the function I made and when I created the sorting test score function the program crashes. Can someone look at my functions and see where my problem is, I seem to get the average test score to show.
double getAverage(double*, int);
double gethighest(double*, int);
double getlowest(double*, int);
void arrayAscending(double*, int);
int main()
{
// Variable declarations
double *testScores; // Dynamically allocate an array of test scores
double average, highest, lowest;
int numTests; // Number of test scores
int count; // Counter variable
// Get the number of tests
cout << "How many tests do you wish to process? ";
cin >> numTests;
// Verify input is not a negative number
while (numTests <= 0)
{
// Get input again.
cout << "Please enter a positive number: ";
cin >> numTests;
}
// Dynamically allocate an array large enough to hold the test scores
testScores = new double[numTests];
// Get the specified number of test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> *(testScores + count);
// Verify input is not a negative number
while (*(testScores + count) < 0)
{
// Get input again.
cout << "Please enter a valid test score.\n";
cin >> *(testScores + count);
}
}
// Calculate the average test score
average = getAverage(testScores, numTests);
highest = gethighest(testScores, numTests);
lowest = getlowest(testScores, numTests);
// Display the results.
arrayAscending(testScores, numTests);
cout << endl;
cout << "The average of those scores is: " << average << endl;
cout << "The higest of those scores is: " << highest << endl;
cout << "The lowest of those scores is: " << lowest << endl;
// Free dynamically allocated memory
delete[] testScores;
testScores = 0; // Make testScores point to null
return 0;
}
//function getAverage - calculates the average of the test scores
double getAverage(double* scores, int num)
{
double avg;
double total = 0.0;
for (int count = 0; count < num; count++)
{
total += scores[count];
}
avg = total / num;
return avg;
}
double gethighest(double* scores, int num)
{
double highnum = 0.0;
for (int i = 0; i < num; i++)
{
if (scores[i] > highnum)
highnum = scores[i];
}
return highnum;
}
double getlowest(double* scores, int num)
{
double lowestnum = 100;
for (int i = 0; i < num; i++)
{
if (scores[i] < lowestnum)
lowestnum = scores[i];
}
return lowestnum;
}
void arrayAscending(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ((array[index]) < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void arrayAscending(double *[], int);
should be
void arrayAscending(double *, int);
You are sorting an array of doubles, not an array of pointers to double.
Also,
double *minElem;
is used later without any memory being allocated for it. Again, you probably need just
double minElem;
and not a pointer.
If you don't need to use pointers, then use std::vector and algorithms from the standard library like std::sort.

Trying to use pointer notation instead of array notation on this test scores program but I'm stuck

Ok so I have this assignment where I write a program that the user enters test scores and them I sort them into ascending order. But it also asks to change array notation to pointer notation. Below is my code. It runs but it has all array notation. Please help!
#include <iostream> //libraries
#include <iomanip>
using namespace std; //standard
void arrSelectSort( double*, int ); //prototypes
void showArr( double* arr, int size ) //array
{
for( int i = 0; i < size; ++i )
cout << setw(8) << arr[i];
}
int main() //main function No array boxes allowed
{
int numTests; //declare variable
cout << "How many test scores would you like to enter? " << endl; //ask user how many test scores they want to enter
cin >> numTests; //user input
cin.sync();
double* testScores = new double[numTests]; //pointer?
cout << "Enter the test scores below:" << endl; //ask user to input test scores
double sumTestScores = 0.0; //declare variable
for( int super = 0; super < numTests; ++super ) //super is inheritance
{
cout << "Test Score " << (super + 1) //inheritance
<< ": " << endl;
cin >> testScores[super]; //Need to rid of this array notation
cin.sync();
sumTestScores += testScores[super]; //need to get rid of the array notation
}
cout << "You entered testScores: " << endl; //show results
cout << fixed << showpoint << setprecision(2); //show results
showArr( testScores, numTests);
cout << "The test scores sorted in ascending order are: " << endl; //show tests in ascending order
arrSelectSort( testScores, numTests ); // show ascending
showArr( testScores, numTests );
cout << "The average score is " << sumTestScores / numTests << endl; //show average test score
delete [] testScores; // free memory
testScores = 0; // make testScores point to null
char c; //safe exit
cout << "Please hit any key and <ENTER> to continue..." << endl;
cin >> c; //user input
return 0; //exit
}
void arrSelectSort( double* arr, int size ) //ascending order sort
{
int startScan; //declare variables
double minIndex; // declare variables
double minElem; // declare variables
for (startScan = 0; startScan < (size - 1); startScan++) //loop for scores
{
minIndex = startScan; //pointer?
minElem = arr[startScan]; //no array
for(int index = startScan + 1; index < size; index++) //ascending loop sort
{
if (arr[index] < minElem) //
{
minElem = arr[index]; //
minIndex = index; //
}
}
arr[(int)minIndex] = arr[startScan]; //
arr[startScan] = minElem;//
}
}
// I have tried declaring the arrays and changing them to pointers but it won't run
//This program runs but has a line spacing problem
array[index] is equivalent to *(array + index), so replace one with the other.

Input Validation: My Program cannot accept negative numbers but it is accepting them and I'm really confused on what to do

#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
void showArray(const int arr[], int size);
void average(int testScores[], int size);
int main()
{
int *testScores;
int numGrades,count;
cout << "How many grades? " << endl;
cin >> numGrades;
testScores = new int[numGrades];
cout << "Please enter the scores below." << endl;
for (count = 0; count < numGrades; count++)
{
cin >> testScores[count];
}
sortArray(testScores, numGrades);
showArray(testScores, numGrades);
average(testScores, numGrades);
delete[] testScores;
testScores = 0;
system("pause");
return (0);
}
//function for ascending order
int * testScores[];
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count]> array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// display array function
void showArray(const int arr[], int size)
{
cout << " Scores in ascending order." << endl;
for (int count = 0; count < size; count++)
{
cout << " " << arr[count] << "";
}
cout << endl;
cout << endl;
}
// function to get average of the array
void average(int testScores[], int numGrades)
{
float total = 0.0;
for (int count = 0; count < numGrades; count++)
{
total += testScores[count];
}
float average = total / numGrades;
cout << " This is the average of the scores entered." << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << " *** " << average << " ***" << endl;
}
So this program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers. Any clues or hints would work wonders! Please and Thank You.
You are doing this:-
cin >> testScores[count];
This would accept all integers positive as well as negative. One way is to check the number, if it's positive then insert it into array.