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
|
/* This Program will read in a file of sales dollar amounts(doubles) ant store them
in an array. The Maximum number of amounts that the file could contain is 20,
but it could have less, and zero (0.00) marks the end of the valid data. */
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void fill_array(double amount_values[], int array_size, int& number_slots);
void sort_array(double amount_values[], int number_slots);
int index_smallest(const double a[], int start_index, int number_slots);
void swap_values(double &v1, double &v2);
void program_output(double amount_values[], int number_slots);
const int MAX_NUMBER_AMOUNT = 20;
ifstream in_stream;
ofstream out_stream;
int main()
{
in_stream.open("sales_amounts.txt");
out_stream.open("program_output.txt");
double amount_values[MAX_NUMBER_AMOUNT];
int number_slots;
fill_array(amount_values, MAX_NUMBER_AMOUNT, number_slots);
sort_array(amount_values, number_slots);
program_output(amount_values, number_slots);
return 0;
}
void fill_array(double amount_values[], int array_size, int& number_slots)
{
int index = 0;
double next;
in_stream >> next;
while((next > 0) && (index < array_size))
{
amount_values[index] = next;
index ++;
in_stream >> next;
}
amount_values[index] = 0;
number_slots = index;
}
void sort_array(double amount_values[], int number_slots)
{
double index_next_smallest;
for (int index = 0; index < number_slots -1; index ++)
{
index_next_smallest = index_smallest(amount_values, index, number_slots);
swap_values(amount_values[index], amount_values[index_next_smallest]);
}
}
int index_smallest(const double a[], int start_index, int number_slots)
{
int index_of_min;
int min = a[start_index];
index_of_min = start_index;
for (int index = start_index + 1; index < number_slots; index ++)
{
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
}
return index_of_min;
}
void swap_values (double& v1, double &v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
void program_output(double amount_values[], int number_slots, int index, int slot)
{
out_stream.setf(ios::fixed);
out_stream.setf(ios::showpoint);
out_stream.precision(2);
out_stream << "The number of sales amounts in the file is : $ " << number_slots << endl;
out_stream << "The lowest sales amount is : $ " << amount_values[0] << endl;
out_stream << "The highest sales amount is : $ " << amount_values[number_slots -1] << endl;
out_stream << "Here is the list of amounts sorted lowset to highest : $ " << endl;
out_stream << amount_values[index] << endl;
while (slot < (number_slots))
{
out_stream << "$" << amount_values[slot] << endl;
slot++;
}
}
|