Hello this is for an assignment. I'm not looking for the answer but rather a point in the right direction. The areas where I have comments are places I still need to fill in. And I need help knowing if what little I have right now is right or very wrong. I haven't done programming in a while and have suffered for it.
#include<iostream>
#include<iomanip>
#include<string>
usingnamespace std;
// Function prototypes
double* getScores(int &); // calls getScores function
void sort(double*, int); // calls sort function
double calcAvg(double*, int); // calls calcAvg function
void displayRpt(double*, int, double); // calls displayRpt function
int main()
{
//Declares a pointer to an array of doubles.
// Welcome message
cout << "Welcome to my Quiz Score Calculator." << endl;
cout << endl;
// Free array
system("pause");
// Exit
return 0;
}
double* getScores(int &)
{
// Promp for number of scores
int *arrayPtr = NULL;
cout << "How many test scores will you enter? " << endl;
int scores;
cin >> scores;
// Dyanamically allocate array
arrayPtr = newint[scores];
int temp;
// Loop and prompt for scores
for (int i = 0; i < scores; i++)
{
cout << "Enter test score " << i + 1 << ":" << endl;
cin >> temp;
*(arrayPtr + i) = temp;
}
// Do not accept negative scores
while(scores < 0)
{
cout << "Negative scores not accepted." << endl;
cin >> scores[arrayPtr];
}
//Returns arrayPtr.
return arrayPtr;
}
void sort(double*, int)
{
// Loop through array for all elements
int i, temp, swapped;
// Loop through array + 1 finding lowest element
for (i = 0; i < numOfScores; i++)
{
scores[i] = (scores + 1);
}
// Swap with outer loop element
while (1)
{
swapped = 0;
for (i = 0; i < numOfScores - 1; i++)
{
if (scores[i]>scores[i + 1])
{
int temp = scores[i];
scores[i] = scores[i + 1];
scores[i + 1] = temp;
swapped = 1;
}
}
if (swapped == 0)
{
break;
}
}
}
double calcAvg(double*, int)
{
// Loop through array
// Sum each average
// Calculate average
}
void displayRpt(double*, int, double)
{
// Print headings
cout << "The test scores in ascending order, and their average, are: " << endl;
cout << endl;
// Loop through array and print scores
cout << "Score" << endl;
cout << "-----" << endl;
cout << endl;
// Print average score
cout << "Average score: " << endl;
}
The sample run is supposed to look like this.
Welcome to Pat Programmer's Quiz Score Calculator
How many test scores will you enter? 5
Enter test score 1: 60.00
Enter test score 2: 70.00
Enter test score 3: 80.00
Enter test score 4: 90.00
Enter test score 5: 100.00
The test scores in ascending order, and their averages, are:
Not sure what you are asking. You have an int pointer and allocate it into a dynamic array correctly.
I personally avoid pointer syntax in favor of array syntax
*(arrayPtr + i) = temp;
looks like
arrayPtr[i] = temp;
it looks good, just incomplete.
the sort seems odd, with the while(1). The standard sorts would just loop using the array's size. I am not sure the sort is correct or not, but if it is working, leave it be and focus on the other stuff.
Given what is there I can't see anything you should not be able to do. If the sort works, I would say its on the right track.
I'm pretty new to pointers and this assignment I'm supposed to use them. I've also never done bubblesort before and I'm supposed to use that in order to get the user input sorted in ascending order. I'm not sure I'm doing it right personally.
The comment
// declares a pointer to an array of doubles
In getScores you know how to create an pointer to an int array. Just change int to double.
BTW. getScores is supposed to return a double* but you return an int*
int *arrayPtr = NULL;
cout << "How many test scores will you enter? " << endl;
int scores;
cin >> scores;
// Dyanamically allocate array
arrayPtr = new int[scores];
for a double, change int to double: //losing the pointless null assignment and rolling into one statement
double * d = new double[some_size];
bubble sort is a pair of for loops over the size of the array. the inner loop moves the smallest value to the top, and the outer loop cuts the array down by 1 each time and does the inner loop on the new, reduced array (because you skip the sorted values that you already moved).
so 4321
inner loop 1 moves 1: 1432
inner loop 2 moves 2: 1243
inner loop 3 moves 3: 1234
inner loop 4 moves 4 which happens to be correct (you can skip this last iteration if you like).