-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReading file into array
155 lines (94 loc) · 2.08 KB
/
Reading file into array
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
double minimumnumber(int[], int);
double maximumNumber(int[], int);
double gettotal(int[], int);
using namespace std;
int main()
{
ifstream inputfile;
ofstream outputfile;
string name;
string filename;
const int ARRAY_SIZE = 12;
int numbers[ARRAY_SIZE];
cout << " Enter a file name for input:";
getline(cin, filename);
//cout << ARRAY_SIZE << " " <<"read from input file" << endl;
//cout<<" Enter a file name for output:";
//getline (cin, name);
inputfile.open(filename.c_str());
// minimumnumber();
//outputfile.open (name.c_str() );
if (!inputfile.good())
{
cout << "Error opening file";
exit(0);
}
int count = 0;
while (inputfile >> numbers[count]) { //inputfile deals with the numbers of the file
count++;
}
cout <<count<<"Were read from the file";
double lowest;
//cout<<" The numbers are:";
lowest = minimumnumber(numbers, count);
//cout<<lowest;
cout << "The lowest number:" << lowest << endl;
double highest;
highest = maximumNumber(numbers, count);
cout << "The biggest number:" << highest << endl;
double totall;
totall = gettotal(numbers, count);
cout << "Total numbers:" << totall << endl;
double average;
average = totall / (count);
cout << "Average numbers is " << average << endl;
inputfile.close();
//cout<<line<<"\n";
//min = line;
/*if (line < min)
{
min = line;
cout<<min<<endl;
}
if (line > max)
{
max = line;
cout<<max<<endl;
}
}*/
system("PAUSE");
return 0;
}
double minimumnumber(int arr[], int size) {
int index = 0;
int min;
min = arr[0];
for (; index <size; index++) {
if (arr[index] < min)
min = arr[index];
//min = [index];
}
return min;
}
double maximumNumber(int arr[], int size)
{
int max = arr[0];
for (int index = 0; index < size; index++) {
if (arr[index] > max)
max = arr[index];
}
return max;
}
double gettotal(int arr[], int numbers)
{
int total =0;
for (int index = 0; index<numbers; index++)
total += arr[index];
//return total;
return total;
}