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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
using namespace std;
int* getNumber(int&); //Allows user to input, returns the pointer to data array with reference to size
string* getName(int&); //Allows user to input, returns the pointer to data array with rederence to name
void sortData(int*, int, string*); //Sorts the input data into ascending order
void displayData(int*, int, string*); //Displays sorted data and average grade to nearest hundredth
int main()
{
int size;
int* iptr = getNumber(size); //Allows user to allocate memory and interactively enter names
string* namePtr = getName(size); //Allows user to allocate memory and interactively enter names
sortData(iptr, size, namePtr);
displayData(iptr, size, namePtr);
delete[] iptr; //Deallocate dynamic memory
delete[] namePtr; //Deallocate dynamic memory
cout << endl;
cout << " Type any key to continue--> ";
_getch();
return 0;
}
//Define getNumber() function
int* getNumber(int& size)
{
do
{
cout << " How many text scores (positive whole numbers only): ";
cin >> size;
}
while (size <= 0);
int* iptr = new int[size]; //Allocate a block of memory
//Data entry loop
for (int i = 0; i < size; i++)
{
do
{
cout << " Enter a valid test score: ";
cin >> *(iptr + i);
}
while (*(iptr + i) < 0);
}
return iptr; //Return pointer to allocated memory
}
//Define getData() function
string* getName(int& size)
string* namePtr = new int[size]; //Allocate a block of memory
//Data entry loop
for (int i = 0; i < size; i++)
{
do
{
cout << " Enter a student's name: ";
cin >> *(namePtr + i);
}
while (i < size);
}
return namePtr; //Return pointer to allocated memory
//Define displayData() function
void displayData(int* iptr, int size, string* namePtr)
{
cout << " Test scores: ";
for (int i = 0; i < size; i++)
{
cout << *(iptr + i) << " ";
}
cout << " Names of Students: ";
for (int i = 0; i < size; i++)
{
cout << *(namePtr + i) << " ";
}
}
//Define sortData() function
void sortData(int* iptr, int size, string* namePtr)
{
//This function sorts the data set using the selection sort algorithm
int bottom = 0, top = size - 1, min, temp, minPosition;
while (bottom < top)
{
min = *(iptr + bottom); //The initial minimum value is at the bottom of the array
minPosition = bottom; //The initial position in array of minimum value
for (int i = bottom + 1; i <= top; i++) /*This loop determines the minimum
value in list from bottom to top and its position in the list*/
{
if (*(iptr + i) < min)
{
min = *(iptr + i);
minPosition = i;
}
}
//Swap bottom with new minimum value
string nameTemp = *(namePtr + bottom);
*(namePtr + bottom) = *(namePtr + minPosition);
*(namePtr + minPosition) = nameTemp;
bottom++; //Increment bottom to new selection of array
}
}
|