// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.
#include <iostream>
usingnamespace std;
typedefint GradeType[100]; // declares a new data type:
// an integer array of 100 elements
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades
int main()
{
GradeType grades; // the array holding the grades.
int numberOfGrades; // the number of grades read.
int pos; // index to the array.
float avgOfGrades; // contains the average of the grades.
int highestGrade; // contains the highest grade.
int lowestGrade; // contains the lowest grade.
// Read in the values into the array
pos = 0;
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
while (grades[pos] != -99)
{
pos ++;
// Fill in the code to read the grades
cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
cin >> grades[pos];
}
numberOfGrades = pos; // Fill blank with appropriate identifier
// call to the function to find average
avgOfGrades = findAverage(grades, numberOfGrades);
cout << endl << "The average of all the grades is " << avgOfGrades << endl;
// Fill in the call to the function that calculates highest grade
highestGrade = findHighest(grades, numberOfGrades);
cout << endl << "The highest grade is " << highestGrade << endl;
// Fill in the call to the function that calculates lowest grade
lowestGrade = findLowest(grades,numberOfGrades);
// Fill in code to write the lowest to the screen
cout << "The lowest grade is " << lowestGrade << endl;
return 0;
}
//****************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers
// data returned: avarage of the numbers in the array
//
//****************************************************************************
float findAverage (const GradeType array, int size)
{
float sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];
return (sum / size); //returns the average
}
//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: highest value of the numbers in the array
//
//****************************************************************************
int findHighest (const GradeType array, int size)
{
// Fill in the code for this function int highestGrade;
for (int count =0; count <size; count++)
cout << array[count] << " ";
cout <<endl;
}
//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers
// data returned: lowest value of the numbers in the array
//
//****************************************************************************
int findLowest (const GradeType array, int size)
{
// Fill in the code for this function
}
For the highest I have tried a bubble sort and the selection sort with no luck ):
int findHighest (const GradeType array, int size)
{
// Fill in the code for this function int highestGrade;
bool swap;
int highestGrade;
do
{
swap = false;
for (int pos = 0; pos < (size - 1); pos++)
{
if (array[pos] < array[pos + 1])
{
highestGrade = array[pos];
array[pos] = array[pos + 1];
array[pos +1] = highestGrade;
swap = true;
}
}
}while (swap);
int findHighest (const GradeType array, int size)
{
// Fill in the code for this function int highestGrade;
int startScan, minIndex, minValue;
for (startScan = 0;startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue =array[startScan];
for (int index= startScan +1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
I have tried switching the ints with grades pos and just about everything. I really don't understand arrays but I'm getting the hang of them kinda... Every time I have replaced the variables I get a error that says assignment of read-only location '*(array +((sizetype) (((unsigned int)minIndex or pos or GradeType. I'm at a loss, am I plugging in the wrong things? Or should not use these sorters?
# include <iostream>
usingnamespace std;
int main()
{
int n;
cout << "\nHow many random numbers(<= 0 -> stop): ";
cin >> n;
if(n <=0) return 0;
int max = 100; // 0 - max will be the number's range
int *array = newint[ n ]; // Dynamic memory allocation.
srand((unsignedint)time // Pseudo-random initialized using actual time passed as seed.
((time_t *)NULL));
for(int i = 0; i < n; ++i)
array[i] = rand() % (max + 1); // Populating array (n random integers in the range 0 - max).
cout << "\nInitial array(" << n << " random integers):\n";
for(int i = 0; i < n; ++i)
cout << array[i] << ' ';
cout << '\n';
// Here starts your "interest" ;)
int val1 = - 1; // These values have been selected because
int val2 = 101; // your numbers are in the range 0 - 100
for(int i = 0; i < n; ++i) // Finding the Highest value
if(array[i] > val1)
val1 = array[i];
for(int i = 0; i < n; ++i) // and the Lowest one
if(array[i] < val2)
val2 = array[i];
cout << "\nLowest value: " << val2 << '\n';
cout << "Highest value: " << val1 << '\n';
delete[] array; // Memory allocation release
return 0;
}
Hope this helps.
EDIT: If necessary (your compiler gives some errors) add # include <cstdlib) and # include <ctime>
Sample of output:
Thank you so much for your time. I was sticking everything in the function itself!! D: I know now! Thank you so much and ill study that code and master it!
You're welcome!
BTW, the lines (27 - 41 in my code) can be simplified as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Here starts your "interest" ;)
int highest = array[0]; // The highest and the lowest values
int lowest = array[0]; // are in the beginning the same ones ;)
for(int i = 0; i < n; ++i)
{
if(array[i] > highest) // Finding the Highest value
highest = array[i];
elseif(array[i] < lowest) // and the Lowest one
lowest = array[i];
}
cout << "\nLowest value: " << lowest << '\n';
cout << "Highest value: " << highest << '\n';