-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedfileIO.cpp
62 lines (52 loc) · 1.15 KB
/
AdvancedfileIO.cpp
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
// This program will read a file and compare the numbers
// inside it
#include <iostream>
#include <fstream>
using namespace std;
int const SIZE = 12;
int main()
{
// Variables
fstream iFile;
int numbers;
int array[SIZE];
int highest;
int lowest;
int count = 0;
float sum = 0;
float ave;
iFile.open("C:\\temp\\numbers.txt");
// Verify if the file is open
if (!iFile)
cout << "Error\n";
else
{
// Read the numbers
while (iFile >> numbers)
{
array[count] = numbers;
sum += numbers;
count++;
}
// Find biggest number
highest = array[0];
for (int count = 0; count < SIZE; count++)
if (array[count] > highest)
highest = array[count];
cout << "The highest value is " << highest << endl;
// Find smallest number
lowest = array[0];
for (int count = 0; count < SIZE; count++)
if (array[count] < lowest)
lowest = array[count];
cout << "The lowest value is " << lowest << endl;
// Display the sum
cout << "The sum of the numbers is " << sum << endl;
// Find and display the average
ave = sum / SIZE;
cout << "The average of the numbers is " << ave << endl;
// Close the file
iFile.close();
}
return 0;
}