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 166 167 168 169 170 171
|
// Headers and Other Technical Items
#include <iostream>
#include <fstream> // For file I/O
#include <iomanip> // For setw()
using namespace std;
// Function Prototypes
int count_file_values(char input_filename[]);
void load_array_from_file(char input_filename[], int things[], int array_size);
void display_array(int things[], int array_size);
int sum_array(int things[], int array_size);
int average_array(int things [], int array_size);
void pause(void);
// Variables
// Place the Demo_Farm_Acres_Input.txt file in a folder on your
// drive. We suggest that you create a folder named:
// Data_Files on the root level of your C: (hard disk) drive
// or flash drive as appropriate.
// File Specifications for the input file
// A file specificatoin includes a
// drive, path, filename and file extention
char data_filename[] = "C:\\Data_Files\\Demo_Farm_Acres_Input.txt";
//******************************************************
// main
//******************************************************
int main(void)
{
int record_count = count_file_values(data_filename);
int acres[record_count];
load_array_from_file(data_filename, acres, record_count);
cout << "The individual farm acerages are: \n";
display_array(acres, record_count);
cout << "\n\nThe total acerage is: " << sum_array(acres, record_count);
pause();
return 0;
}
//******************************************************
// count_file_values
//******************************************************
int count_file_values(char input_filename[])
{
fstream inData; // device token for the input stream
double next_value;
int number_of_values = 0;
inData.open(input_filename, ios::in); //Open input file
if (!inData)
{
cout << "\n\nError opening input data file: " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
// Get data values and count them
while (inData >> next_value)
{
number_of_values++;
}
// Close the files
inData.close(); //Close input file
return number_of_values;
}
//******************************************************
// load_array_from_file
//******************************************************
void load_array_from_file(char input_filename[], int things[], int array_size)
{
fstream inData; // device token for the input stream
inData.open(input_filename, ios::in); //Open input file
if (!inData)
{
cout << "\n\nError opening input data file: " << input_filename << "\n\n";
pause();
exit(EXIT_FAILURE);
}
else
{
for (int i = 0; i < array_size; i++) // Load the array
{
inData >> things[i];
}
inData.close();
}
return;
}
//******************************************************
// display_array
//******************************************************
void display_array(int things[], int array_size)
{
cout << "\n************************************";
for (int i = 0; i < array_size; i++)
{
cout << "\nOffset: " << setw(2) << i;
cout << " Memeber: " << setw(2) << i+1;
cout << " value is: " << setw(3) << things[i];
}
cout << "\n************************************";
return;
}
//******************************************************
// sum_array
//******************************************************
int sum_array(int things[], int array_size)
{
int total = 0; // Accumulator
for (int i = 0; i < array_size; i++)
{
total += things[i];
}
return total;
}
//******************************************************
// average_array
//******************************************************
int average_array(int things[], int array_size)
{
int total= 0;
for (int i = 0; i < array_size; i++)
{
total / array_size ;
}
return average;
}
//error `average' undeclared (first use this function)
// (Each undeclared identifier is reported only once for each function it appears in.)
//******************************************************
// pause
//******************************************************
void pause(void)
{
cout << "\n\n";
system("PAUSE");
cout << "\n\n";
return;
}
//******************************************************
// End of Program
//******************************************************
|