Hello, been lurking on these forums for help these passed couple weeks. Very helpful.
I registered today as I cannot get a program to run. It is for my college assignment and due tonight at midnight.
Essentially, we are to create a program that asks the user to input how many test scores he/she will enter, takes that number and creates an array of that size, then use a pointer to reference to that array.
Once the array is created, we use 2 functions, 1 that sorts the array using a pointer reference again, and puts it in ascending order.
The other function to take the test scores inputted and output the average.
Here is my code at the moment:
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
//header files
#include <iostream>
#include <iomanip>
using namespace std;
//function prototypes declared
void sort(double*, int);
double average(double*, int);
//main
int main()
{
//used to declare array size from user input
int ttlTestScores;
cout << "How many test scores will you enter?: " << endl;
cin >> ttlTestScores;
//EDIT: made constant variable
const int SIZE = ttlTestScores;
//array created with user defined size
double array[SIZE];
//user input of actual test scores allocated into array
cout << "Please enter your " << SIZE << " test scores: " << endl;
for(int i = 0; i < SIZE; i++)
{
cout << "Enter test score " << i+1 <<": " << endl;
cin >> array[i];
}
//pointer reference declared to array previously created
double *pArray = &array;
//functions called, using pointer reference
sort(*pArray, ttlTestScores);
average(*pArray, ttlTestScores);
return 0;
}
//sorts array into ascending order
void sort(double*score, int size)
{
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;
}
cout << "In ascending order, your test scores are: " << endl;
for(int i = 0; i < size; i++)
cout << array[i];
}
//calculates array average
double average(double*score, int numScores)
{
double total = 0, average = 0;
for (int numScores = 0; numScores < size; numScores++)
{
total = total + *score[numScores];
}
average = (*score / numScores);
cout << "Average score is: " << average << endl;
}
|
The link to the assignment is :
http://web.cerritos.edu/pnguyen/SitePages/cis180/labs/cis180lab7sum17.html
I am a terrible explainer, so putting that there in case I could not make it clearer. Any feedback would be greatly appreciated. I know a main error at the moment is my pointer reference. But anything is helpful.
- Louie