Need help on what to do next in a program

I need help on what to do next in a program. Here are the directions, and what I have so far. I just need help on coming up with how to write the function to calculate the "low,' and I can do the rest. Thanks is advance.

You are to write a program to read an unknown number of real (float) values from a file and output their distribution. The distribution classes are as follows.

Low: Count of values < average –5
Mid: Count ofvalues in range [average-5,average+5]
High: of values > average +

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
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
        using namespace std;

void CalcAverage(string, float&);
void PromptForFile(string&);

int main()
{
string dataFileName;
float average;

PromptForFile(dataFileName);
CalcAverage(dataFileName, average);
}

void PromptForFile(string& fileName)
{
cout << endl << "What's the data filename? ";
cin >> fileName;
return;
}

void CalcAverage(string fileName, float& ave)
{
  ifstream inData;

 float total, value;
  int num;

  inData.open(fileName.c_str());

  total = 0;
  num = 0;
  inData >> value;
  while(inData)
    {
    num = num + 1;
    total = total + value;
    inData >> value;
    }
  ave = total / num;
  return;
}
closed account (D80DSL3A)
Now that the average is found you could read through the file a 2nd time and find the other 3 numbers.
Prefer this method of reading from file:
1
2
3
4
5
6
while( inData >> value )
{
    if( value < ave - 5.0f ) ++lowCount;// increment counts
    else if( value > ave + 5.0f ) ++highCount;
    else ++midCount;
}
I suggest not reading from the file every time you want to get a number to compute some value. Read the entire file once, and then do whatever you want with the read data. This solution assumes the floats are separated by whitespace characters in the file.

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
#include <iostream>
#include <fstream>
#include <string>

bool readFileToArray(std::string filename, float array[], const int size) {

	std::ifstream file(filename.c_str(), std::ios::binary);
	if (!file) {
		return false;
	}

	for (int i = 0; i < size && file >> array[i]; ++i);

	file.close();
	return true;
}

float computeAverage(float array[], const int size) {
	float average = 0.0;
	for (int i = 0; i < size; ++i) {
		average += array[i];
	}
	average /= (float)size;
	return average;
}

int main() {

	constexpr int size = 4;
	float array[size] = {};

	std::string filename = "floats.txt";

	if (!readFileToArray(filename, array, size)) {
		std::cout << "There was an error!" << std::endl;
		return 1;
	}

	float average = computeAverage(array, size);

	std::cout << average << std::endl;

	return 0;
}


In the case that the file contains less floats than the size of the array - let's say the array has four floats, but the file only has three - the average is still computed, and the fourth, missing number is treated as a zero. This is because our array has been default initialized, so any element that hasn't been overwritten by a read float will retain it's initial value of zero.
Last edited on
Topic archived. No new replies allowed.