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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*************************************************************************************************
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>
#include <cstdlib>
#include <string>
using namespace std;
//Prototypes
int buildArray( double array[] );
void printArray( string reportTitle, double array[], int numberOfValues );
void sortArray( double array[], int numberOfValues );
int main()
{
//declare the array
double array [50];
int size;
size = buildArray(array);
//call functions
printArray("Unsorted Numbers", array, size);
sortArray(array, size);
printArray("Sorted Numbers", array, size);
return 0;
}
/**********************************************************************
Function: int buildArray( double array[] )
Use: to fill an array of doubles
Arguments: an array of doubles: the array that will hold the numeric
information.
Returns: an integer that is equal to the number of values
that were placed in the array.
**********************************************************************/
int buildArray( double array[] )
{
ifstream inFile;
inFile.open ( "nums.txt" );
if (inFile.fail() )
{
cout<< "The nums.txt input file failed to open";
exit(-1);
}
int i = 0;
double userVal;
inFile>>userVal;
while( inFile)
{
array[i] = userVal;
i++;
inFile>>userVal;
}
inFile.close();
return i;
}
/************************************************************************************
Function: void printArray( string reportTitle, double array[], int numberOfValues )
Use: to display the double numbers in the array five values at a time.
Arguments: a string that holds the title for the report that is being displayed,
an array of doubles that holds the numbers to be displayed,
and an integer that holds the number of values to be displayed.
Returns: Nothing
************************************************************************************/
void printArray( string reportTitle, double array[], int numberOfValues )
{
cout << endl << reportTitle << endl<<endl;
for( int i=0; i< numberOfValues; i++)
{
cout << setiosflags( ios:: fixed ) << setprecision(2) <<" "<<array[i] << " ";
if ((i + 1) % 5 == 0)
{
cout << endl;
}
}
cout<<endl<<endl;
}
/********************************************************************
Function: void sortArray( double array[], int numberOfValues )
Use: Sorts the numbers in the array into descending order.
Arguments: an array of doubles that holds the numbers to be sorted,
and an integer that holds the number of values to be sorted.
Returns: Nothing
********************************************************************/
void sortArray( double array[], int numberOfValues )
{
int minIndex;
double 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;
}
}
|