-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemG1.cpp
More file actions
68 lines (58 loc) · 1.48 KB
/
Copy pathProblemG1.cpp
File metadata and controls
68 lines (58 loc) · 1.48 KB
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
/*
Jingi Min
CIS 22A 2023 Fall
Laboratory Assignment G
ProblemG1
open the file, get a integers and find sum, smallest, largest and average of integers
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream integerFile;
int num, intCount = 0, sumInt = 0, smallest, biggest, avg = 0;
intCount = 0, sumInt = 0, avg = 0;
integerFile.open("file1.txt");
if (!integerFile.is_open())
{
cout << "Could not open the file file1.txt" << endl;
return 1;
}
integerFile >> num;
biggest = num, smallest = num;
while (!integerFile.fail())
{
intCount++;
sumInt += num;
if (num < smallest)
{
smallest = num;
}
else if (num > biggest)
{
biggest = num;
}
integerFile >> num;
}
cout << "The integer count: " << intCount << endl;
cout << "The sum of the integers: " << sumInt << endl;
cout << "The smallest integer: " << smallest << endl;
cout << "The largest integer: " << biggest << endl;
cout << "The average of the integers: " << static_cast<double>(sumInt) / intCount << endl;
integerFile.close();
return 0;
}
/*
Execution results:
The integer count: 7
The sum of the integers: 141
The smallest integer: 9
The largest integer: 33
The average of the integers: 20.1429
The integer count: 8
The sum of the integers: 181
The smallest integer: 9
The largest integer: 40
The average of the integers: 22.625
*/