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
|
// This program reads data from a file into an array.
#include <iostream>
#include <fstream> //notice this needed Simkin
using namespace std;
// Function prototypes
int getLowest(int [], int );
int getHighest(int [], int );
int getSum(int [], int );
double getAverage(int [], int);
const int ARRAY_SIZE = 12; // Array size
int numbers[ARRAY_SIZE];
int total;
int main()
{
const int ARRAY_SIZE = 12; // Array size
int numbers[ARRAY_SIZE]; // Array with 100 elements
int count = 0; // Loop counter variable
ifstream inputFile; // Input file stream object
int lowest, highest, total, average;
inputFile.open("numbers.txt"); // Open the file.
// Read the numbers from the file into the array.
// After this loop executes, the count variable will hold
// the number of values that were stored in the array.
while ( (count < ARRAY_SIZE) && (inputFile >> numbers[count]) )
{
count++;
}
//Simkin the statement (inputFile >> numbers[count]) will be false when there is no more
//data to be read from the file. istream help is very difficult to find and understand.
//Bottom line this works...
// Close the file.
inputFile.close();
// Display the numbers read.
cout << "The numbers are: ";
for (int index = 0; index < count; index++)
cout << numbers[index] << " ";
cout << endl;
lowest = getLowest(numbers, ARRAY_SIZE);
cout << "The lowest number is: "<< lowest <<endl;
highest = getHighest(numbers, ARRAY_SIZE);
cout << "The highest number is: "<< highest <<endl;
total = getSum(numbers, ARRAY_SIZE);
cout << "The sum of the numbers is: "<< total <<endl;
average = getAverage(numbers, ARRAY_SIZE);
cout << "The average of the numbers is: "<< average <<endl;
system("pause");
return 0;
}
int getLowest(int anArray [], int mySize)
{
int count;
int lowest;
lowest = numbers [0];
for (count = 1; count < ARRAY_SIZE; count++)
if (numbers [count] < lowest)
lowest = numbers [count];
return lowest;
}
int getHighest(int anArray [], int mySize)
{
int count;
int highest;
highest = numbers [0];
for (count =1; count < ARRAY_SIZE; count++)
if (numbers [count] < highest)
highest = numbers [count];
return highest;
}
int getSum(int anArray [], int mySize)
{
const int NUMBERS = 12;
int numbers[NUMBERS];
int sum = 0;
for (int count = 1; count < NUMBERS; count++)
total+= numbers[count];
return total;
}
double getAverage(int anArray[], int mySize)
{
const int NUMBERS = 12;
int numbers[NUMBERS];
double average;
total = getSum(numbers, ARRAY_SIZE);
average = total / ARRAY_SIZE;
return average;
}
/*
Proof
The numbers are: 47 89 65 36 12 25 17 8 62 10 87 62
The lowest number is: 0
The highest number is: 0
The sum of the numbers is: -858993468
The average of the numbers is: -143165578
Press any key to continue . . .
/* */
|