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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*******************************************************************************
Purpose: To write a program that will process a data set of numeric information,
storing the information within arrays to be displayed, sorted, and displayed again.
The program will also take data from an external data file and plug that data into the program.
*******************************************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//Prototypes
int buildArray( double array[] );
void printArray( string reportTitle, double array[], int numberOfValues );
void sortArray( double array[], int numberOfValues );
int main()
{
double array [50];
int size;
size = buildArray(array);
printArray("Unsorted Numbers", array, size);
sortArray(array, size);
printArray("Sorted Numbers", array, size);
return 0;
}
/***************************************************************
Function: int buildArray( double array[] )
Use:
Arguments:
Returns:
***************************************************************/
int buildArray( double array[] )
{
int i = 0;
double userVal;
cout<< "Enter a number (-99.99 to quit): ";
cin>> userVal;
while( userVal != -99.99)
{
array[i] = userVal;
i++;
cout<< "Enter another number (-99.99 to quit): ";
cin>> userVal;
}
return i;
}
/***************************************************************
Function: void printArray( string reportTitle, double array[], int numberOfValues )
Use:
Arguments:
Returns: Nothing
***************************************************************/
void printArray( string reportTitle, double array[], int numberOfValues )
{
for( int i=0; i< numberOfValues; i++)
{
cout<< array[i] << endl;
}
}
/***************************************************************
Function: void sortArray( double array[], int numberOfValues )
Use:
Arguments:
Returns: Nothing
***************************************************************/
void sortArray( double array[], int numberOfValues )
{
int minIndex, minValue;
//outer loop responsible for moving us along the entire array
for (int startScan=0; startScan < numberOfValues-1; startScan++)
{
//begins each iteration, assuming current element is the smallest remaining
minIndex=startScan;
minValue=array [startScan];
//inner loop responsible for finding lowest remaining value in the array
for (int index = startScan + 1; index < numberOfValues; index++)
{
//performs comparisons, looking for smallest remaining value in the array
if(array [index] > minValue)
{
minValue=array[index];
minIndex=index;
}
}
//performs the swap
array[minIndex]= array [startScan];
array[startScan]=minValue;
}
}
|