File I/O

My assignment is: http://gyazo.com/789abb7199aa39c61075e222d8f2c39a

I've tried about 4 times starting from scratch and rewriting everything differently all to no avail. So far what I've been able to get written up is:

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
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{

    //Variable declarations
    int input[25];
    int count, sum=0;
    double stdev, average, numerator;

    ifstream fin;
    ofstream fout;

    //Check if files can be opened
    fin.open("Lab7_input.txt");
    if(fin.fail())
    {
        cout << "Cannot open Lab7_input.txt" << endl;
        exit (0);
    }

    fout.open("Lab7_output.txt");
    if(fin.fail())
    {
        cout << "Cannot open Lab7_output.txt" << endl;
        exit (0);
    }

    fin.get(input[]);
    while(fin.get(input[]) != -1)
    {
        sum += input[];
        count++;

        fin.get(input[]);
    }

    average = (sum/count);

    for(int i=0; i<24; i++)

    {
        numerator += pow( (input[i] - average) , 2);
    }
    stdev = sqrt(numerator/count);





    //Closing files
    fin.close();
    fout.close();

    return 0;
}


Explanation would be nice if anyone decides to reply, thanks :)
PS: I'm not even looking for anyone to do it for me, just would like some help with it, can't seem to get it to work
What specifically do you need help with?
being able to make this even run pretty much. clarification on how i should go about getting the input from the file to read into my array properly? And from there, getting the numerator variable to be calculated correctly. Basically array help in general, that's what's killing me here.
I probably should've included the text file the assignment refers to, here it is:
7111
3038
7381
4832
6959
4071
7640
5561
8591
6580
7356
662
9663
2929
4243
7267
7103
7439
2302
7373
8450
2107
2290
2465
6161
-1
oh, sorry. basically read from the file "Lab7_input.txt" (posted above) , calculate the average, sum, and standard deviation of those values and write that into the output file "lab7_output.txt" (realize I didn't write a command for that yet, not troubled by it). The value -1 should stop the program from further reading from the file and should not be included in the calculations.

Cannot figure out how to get the value that is read to be put into the array "input" so that i can use indexing later to reference certain values when calculating for numerator
Are you allowed to use std::vector? If not, are you allow to use std::string and std::istringstream?
Last edited on
i am not, we have not covered that in class just yet
Sorry, I edited my post and you were quick to respond - what about string and istringstream?
we are limited to c-strings, no strings
It's incredibly difficult to read an arbitrary number of values with those restrictions - is there an upper limit? e.g. can there be infinite numbers or only up to say 100?
infinitely many, but since there's only 25 values in the file I'm required to use I thought that'd be a logical value to use for max
Well, the issue is that your program may be tested with a shorter or longer input file, in which case problems would happen. Are you sure there's no upper limit?
i see what you mean, there is no upper limit to this
In that case, I think you have to ditch the array. I am pretty sure it is possible to make the calculations without storing any of the numbers - just process them as you read them from the file.
hhe instructions do say, though, that i must store the values in an int array
Then, you'll have to use dynamic arrays. Let me double-check - are you allowed to use dynamic arrays? (e.g. new[] and delete[])
Topic archived. No new replies allowed.