Arrays in C++
Apr 9, 2014 at 6:28am UTC
Hi everyone,
I am trying to write a program that inputs 7 scores into an array and then drops the lowest score and averages the remaining scores. However, I am having trouble getting the program to run, I thought I had all the code I needed but visual studio won't run my program. Any help is greatly appreciated, thank you. Below is my code that I have written so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
double GetLowest(const double [], int );
void GetScores(double [], int );
double GetTotal(const double [], int );
int main()
{
const int SIZE = 7;
double TestScores[SIZE],
total,
Lowestscore,
average;
cout << fixed << showpoint << setprecision(1);
GetScores(TestScores, SIZE);
total = GetTotal(TestScores, SIZE);
Lowestscore = GetLowest(TestScores, SIZE);
total -= Lowestscore;
average = total / (SIZE - 1);
cout << "The average of the test scores with the lowest score excluded is " << average << ".\n" ;
return 0;
}
void GradeArray(double score[], int size)
{
int count;
//get the 7 test scores
for (count=0; count <= size; count++)
{
cout << "Enter test score number " << (count + 1) << ": " ;
cin >> score[count];
}
}
double GetTotal(const double array[], int size)
{
double total = 0;
for (int count = 0; count < size; count++)
total += array[count];
return total;
}
double GetLowest(const double array[], int size)
{
double lowest;
lowest = array[0];
for (int count = 1; count < size; count++)
lowest = array[count];
return lowest;
}
Apr 9, 2014 at 7:09am UTC
void GetScores(double[], int);
Function definition is unavailable.
void GradeArray(double score[], int size)
Looks like this is GetScores function.
Last edited on Apr 9, 2014 at 7:10am UTC
Apr 9, 2014 at 7:14am UTC
1 2 3 4 5
@12 const int SIZE = 7;
@13 double TestScores[SIZE],
@34 void GetScores(double score[], int size)
@38 for (count=0; count <= size; count++)
@49 total += array[count];
The loop index range is 0-7 which means memory access of array[0]....array[7]. But array is of size 6 elements array[0]...array[6]. The for loop should be
for (count=0; count < size; count++)
Apr 9, 2014 at 2:51pm UTC
Thanks for the help!
And the program is giving me an average of 80.7 instead of 82.66667. Which is what I need, I believe it may be dropping my last entered score instead of my lowest score. But I'm really not sure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes
double GetLowest(const double [], int );
void GetScores(double [], int );
double GetTotal(const double [], int );
int main()
{
const int SIZE = 7;
double TestScores[SIZE],
total,
Lowestscore,
average;
cout << fixed << showpoint << setprecision(1);
GetScores(TestScores, SIZE);
total = GetTotal(TestScores, SIZE);
Lowestscore = GetLowest(TestScores, SIZE);
total -= Lowestscore;
average = total / (SIZE - 1);
cout << "The average of the test scores with the lowest score excluded is " << average << ".\n" ;
return 0;
}
void GetScores(double score[], int size)
{
int count;
//get the 7 test scores
for (count=0; count < size; count++)
{
cout << "Enter test score number " << (count + 1) << ": " ;
cin >> score[count];
}
}
double GetTotal(const double array[], int size)
{
double total = 0;
for (int count = 0; count < size; count++)
total += array[count];
return total;
}
double GetLowest(const double array[], int size)
{
double lowest;
lowest = array[0];
for (int count = 1; count < size; count++)
lowest = array[count];
return lowest;
}
Apr 11, 2014 at 6:29am UTC
@28 average = total / (SIZE - 1);
This should be just SIZE. Like:
Apr 11, 2014 at 9:04am UTC
Also, the function "GetLowest" is wrong. it doesn't find the lowest,but just assign the last iteration within the loop.
Topic archived. No new replies allowed.