C++ Using output file help

Okay, so I am asked to output something like this:

1
2
3
The 10 random numbers are: 44 61 98 45 45 17 63 24 9 95 
The average is 50.10.
There are 4 numbers above the average. 


I must use an output file, where the randomly generated numbers are first summed up and saved into an output file. After the average is computed, the numbers are read back one by one and compared with the average.

Here's my code thus far:

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>   
#include <time.h>    
using namespace std;

int main()
{
   double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, average;
   srand (time(NULL));
   
   num1 = rand() % 100 + 1;
   num2 = rand() % 100 + 1;
   num3 = rand() % 100 + 1;
   num4 = rand() % 100 + 1;
   num5 = rand() % 100 + 1;
   num6 = rand() % 100 + 1;
   num7 = rand() % 100 + 1;
   num8 = rand() % 100 + 1;
   num9 = rand() % 100 + 1;
   num10 = rand() % 100 + 1;
   
   cout << "The 10 random numbers are: " << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10 << endl; 
   
   average = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10)/10;
   
   cout << fixed << setprecision(2) << "The average is " << average << ".";
   
   ofstream outputFile;
   
   outputFile.open("Numbers.txt");
   
   outputFile << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10;
}


I am lost on how to further continue with the output file to come up with the total numbers above the average.

Thank you!!
Are you familiar with arrays? If not, that's fine, as a student there are lots of subjects awaitng you. Arrays can wait, but would be helpful here.

It will help you slightly if you append "std::endl" to the end of your output (as you did when you used std::cout to list the 10 random numbers).

Let me describe rather that write code. This way you exercise what you already know to write code yourself.

Close outputFile.

Open an inputFile (ifstream) of "Numbers.txt";

At that point you can get one number at a time. Do you know loops? While? For?

You can read one entry with "inputFile >> num;". If you did that inside a loop, you'd end up processing one number at a time, one after the other.

Since you have the average already, all you need to do is check the "num" to see if it is greater than the average you calculated, and "cout" it as you loop.

I suspect that this should be solved without arrays and using only one `num' variable, not ten.
otherwise, ¿what would be the point of «reading back» the contents of the file?
you need to learn to be lazier, think how would you make your program to work with 1000 numbers instead (and no arrays/vector/list)
I'm not familiar with loops, I've opened an inputFile but confused on what to do next.
@ne555 I don't think you can correctly calculate the average without storing multiple numbers.
Edit:
What I wrote was misleading. To clarify, I mean I don't think you can keep track of which numbers are above the average until all the numbers have been read in, in order to calculate the average. Calculating the average itself is possible without an array.

I've opened an inputFile but confused on what to do next.
Without arrays/loops?
Try:
inputFile >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8 >> num9 >> num10;

This is ridiculous to do without arrays. I suggest learning them. Otherwise, it's going to be 10x repeated if-statements checking of numX is greater than the average.
http://www.cplusplus.com/doc/tutorial/arrays/

Edit 2: Actually, to ne555's credit, if you open the inputFile twice, then it avoids having to use arrays, so you are still correct :)
Last edited on
inputFile >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8 >> num9 >> num10;

How would I use these inputs toward calculating how many numbers are above the average?

This is ridiculous to do without arrays.
For this task I must use output file.
The following program opens inputfile the first time to calculate the average, and then opens it a second time to compare each number against the pre-computed average. I left out the "outputfile" part of the program since you've already accomplished that in your own way.

Example inputfile:
46 45 67 4 99   55 86 59 61 48


Hope this helps.
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>   
#include <ctime>    
#include <sstream>

int main()
{
    using namespace std;

    //
    // This program intentionally avoids using arrays.
    //

    double average;
    {
        ifstream inputfile("file.txt");

        double total = 0;
        int count = 0;
        double num;
        while (inputfile >> num)
        {
            std::cout << num << " ";
            total += num;
            count++;
        }
        
        average = total / count;
    }
    
    std::cout << '\n';
    
    int count_above_average = 0;
    {
        ifstream inputfile("file.txt");
        double num;
        while (inputfile >> num)
        {
            if (num > average)
            {
                count_above_average++;
            }
        }
    }
    
    std::cout << count_above_average << " numbers above average.\n"; 
}
Last edited on
Thank you, it helped. :)
Can someone help me to do this with an array?

Here is what I had with output file method:

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


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    
   int count = 0;
   double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, average;
   srand (time(NULL));

   num1 = rand() % 100 + 1;
   num2 = rand() % 100 + 1;
   num3 = rand() % 100 + 1;
   num4 = rand() % 100 + 1;
   num5 = rand() % 100 + 1;
   num6 = rand() % 100 + 1;
   num7 = rand() % 100 + 1;
   num8 = rand() % 100 + 1;
   num9 = rand() % 100 + 1;
   num10 = rand() % 100 + 1;
   
   ofstream outputFile;
   
   outputFile.open("Numbers.txt");
   
   outputFile << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10;
   
   outputFile.close();
   
   cout << "The 10 random numbers are: "; 
   
   {
        ifstream inputfile("Numbers.txt");
  
        double total = 0;
        double num;
        while (inputfile >> num)
        {
            std::cout << num << " ";
            total += num;
            count++;
        }
        
        average = total / count;
        cout << fixed << setprecision(2) << "\nThe average is " << average << ".";
   }
    
    std::cout << '\n';
    
    int count_above_average = 0;
    {
        ifstream inputfile("Numbers.txt");
        double num = 0;
        while (inputfile >> num)
        {
            if (num > average)
            {
                count_above_average++;
            }
        }
    }
    
    std::cout << "There are " << count_above_average << " numbers above the average.\n"; 
}


Thanks.
Change double num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, average;
to
1
2
double nums[10];
double average; 


Change
1
2
3
4
5
6
7
8
9
10
   num1 = rand() % 100 + 1;
   num2 = rand() % 100 + 1;
   num3 = rand() % 100 + 1;
   num4 = rand() % 100 + 1;
   num5 = rand() % 100 + 1;
   num6 = rand() % 100 + 1;
   num7 = rand() % 100 + 1;
   num8 = rand() % 100 + 1;
   num9 = rand() % 100 + 1;
   num10 = rand() % 100 + 1;

to
1
2
3
4
for (int i = 0; i < 10; i++)
{
    nums[i] = rand() % 100 + 1;
}


I'll leave it as an exercise to figure out how you should change line 30. Hint: use a for loop.
I'm completely lost.

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

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    
   int count = 0;
   double nums[10];
   double average;
   srand (time(NULL));

   for (int i = 0; i < 10; i++)
   {
   
   nums[i] = rand() % 100 + 1;
       
   }
   
   cout << "The 10 random numbers are: "; 
   
   for (int i = 0; i < 10; i++)
   {
   
   cout << nums[i];
   
   }
   
   {
        ifstream inputfile("Numbers.txt");
  
        double total = 0;
        double num;
        while (inputfile >> num)
        {
            std::cout << num << " ";
            total += num;
            count++;
        }
        
        average = total / count;
        cout << fixed << setprecision(2) << "\nThe average is " << average << ".";
   }
    
    std::cout << '\n';
    
    int count_above_average = 0;
    {
        ifstream inputfile("Numbers.txt");
        double num = 0;
        while (inputfile >> num)
        {
            if (num > average)
            {
                count_above_average++;
            }
        }
    }
    
    std::cout << "There are " << count_above_average << " numbers above the average.\n"; 
}
Okay, well, some things to clear up:

(1) The weird solution I had in my posts a few replies back was a workaround to open and process the input file twice to avoid using arrays. If we're going to use arrays anyway, my previous method just becomes bloat here... (it's not wrong, it's just unnecessary).

(2) You are never writing your randomly generated numbers to a file, unlike your original post. You are opening a file for reading called "Numbers.txt", but nowhere in this code are you writing to that file. I was just assuming you were leaving this part out for brevity.

Look back at your first post, you wrote to the file.
1
2
3
4
5
   ofstream outputFile;
   
   outputFile.open("Numbers.txt");
   
   outputFile << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << " " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10;


You can do this in a loop instead now...
1
2
3
4
   for (int i = 0; i < 10; i++)
   {
       outputFile << nums[i] << ' ';
   }


You need to combine these things.

Generate the numbers [possibly into an array], write the numbers to a file, close the file, then open that file for reading, read the numbers, find the average.
Last edited on
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


#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    
   int count = 0;
   double nums[10];
   double average; 
   srand (time(NULL));

   for (int i = 0; i < 10; i++)
   {
       
   nums[i] = rand() % 100 + 1;
   
   }
   
   ofstream outputFile;
   
   for (int i = 0; i < 10; i++)
   {
       outputFile << nums[i] << ' ';
   }
   
   outputFile.close();
   
   cout << "The 10 random numbers are: "; 
   
   {
       
        ifstream inputfile("Numbers.txt");
  
        double total = 0;
        double num;
        while (inputfile >> num)
        {
            std::cout << num << " ";
            total += num;
            count++;
        }
        
        average = total / count;
        cout << fixed << setprecision(2) << "\nThe average is " << average << ".";
   }
    
    std::cout << '\n';
    
    int count_above_average = 0;
    {
        ifstream inputfile("Numbers.txt");
        double num = 0;
        while (inputfile >> num)
        {
            if (num > average)
            {
                count_above_average++;
            }
        }
    }
    
    std::cout << "There are " << count_above_average << " numbers above the average.\n"; 
}


So how do I now read the array with the inputFile?
You never opened your outputFile stream with a valid file to write to...

You need to do:
ofstream outputFile("Numbers.txt");
So it runs now, is this the right way to do this with an 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
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    
   int count = 0;
   double nums[10];
   double average; 
   srand (time(NULL));

   for (int i = 0; i < 10; i++)
   {
       
   nums[i] = rand() % 100 + 1;
   
   }
   
   ofstream outputFile("Numbers.txt");
   
   for (int i = 0; i < 10; i++)
   {
       outputFile << nums[i] << ' ';
   }
   
   outputFile.close();
   
   cout << "The 10 random numbers are: "; 
   
   {
       
        ifstream inputfile("Numbers.txt");
  
        double total = 0;
        double num;
        while (inputfile >> num)
        {
            std::cout << num << " ";
            total += num;
            count++;
        }
        
        average = total / count;
        cout << fixed << setprecision(2) << "\nThe average is " << average << ".";
   }
    
    std::cout << '\n';
    
    int count_above_average = 0;
    {
        ifstream inputfile("Numbers.txt");
        double num = 0;
        while (inputfile >> num)
        {
            if (num > average)
            {
                count_above_average++;
            }
        }
    }
    
    std::cout << "There are " << count_above_average << " numbers above the average.\n"; 
}
Topic archived. No new replies allowed.