0

It is required to read from a text file without knowing the size of array once and then calculate the mean and variance. Now I cant create the specific arrays. As a result, I cant calculate the mean and variance. I really hope that you guys can help.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include "fraction.h"

using namespace std;

void ascending(fraction fractionarr[]);
fraction mean(fraction fractionarr[]);
fraction variance(fraction fractionarr[]);
static const int countlinenumber(ifstream& data);


void ascending(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction temp;
fraction test(0, 1);
int n = 0;
int linenumber = countlinenumber(data);

for (int i = 0; i < 100; i++)
{
    if(fractionarr[i] != test)
    {
        n++;
    }
}
for (int i = 0; i < linenumber; i++)
{
    for(int j = 0; j < linenumber-1; j++)
    {
        if(fractionarr[i+1] != test)
        {
            if(fractionarr[j] > fractionarr[j+1])
            {
                temp = fractionarr[j];
                fractionarr[j] = fractionarr[j+1];
                fractionarr[j+1] = temp;
            }
        }
    }
}
}

fraction mean(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction sum = fractionarr[0];
fraction average;
fraction n(1,1);
fraction test(0,1);
int linenumber = countlinenumber(data);

for (int i = 1; i < linenumber; i++)
{
    if(fractionarr[i] != test)
    {
        sum = sum + fractionarr[i];
        n = n + fraction(1,1);
    }
}

average = sum / n;
return average;
}  

fraction variance(fraction fractionarr[])
{
ifstream data("data1.txt");
fraction average = mean(fractionarr);
fraction* semi = new fraction[100];
fraction total;
fraction t(1, 1);
fraction variance;
fraction test(0,1);
int linenumber = countlinenumber(data);

for (int i = 0; i < linenumber; i++)
{
    if(fractionarr[i] != test)
    {
        semi[i] = (fractionarr[i]-average) * (fractionarr[i]-average);
        t = t + fraction(1, 1); 
    }
}
total = semi[0];
for (int i = 1; i < 100; i++)
{
    if(semi[i] != test)
    {
        total = total + semi[i];
    }
}
variance = total / t;
return variance;
}

static const int countlinenumber(ifstream& data)
{
string temp;
int linenumber = 0;

while(getline(data, temp) && !data.eof())
{
    linenumber++;
}
return linenumber;
}

int main()
{
ifstream data("data1.txt");
ofstream data_out("data1_out.txt");
stringstream ss;

string temp;
char op;
fraction testing(0,1);
fraction average;
static const int linenumber = countlinenumber(data);

int* numerator = new int[linenumber];
int* denominator = new int[linenumber];
fraction* fractionarr = new fraction[linenumber];

for(int i = 0; i < linenumber; i++)
{
    numerator[i] = 0;
    denominator[i] = 0;
}

for(int i = 0; i < linenumber; i++)
{
    if(!data.eof())
    {
        getline(data, temp, '\n');
        ss << temp;
        ss >> numerator[i] >> op >> denominator[i];
        ss.clear();
    }
}
delete [] numerator;
delete [] denominator;

ascending(fractionarr);
for (int i = 0; i < linenumber; i++)
{
    if(fractionarr[i] != testing)
        data_out << fractionarr[i] << endl;
}
data_out.close();

cout << mean(fractionarr) << endl;
average = mean(fractionarr);
cout << variance(fractionarr) << endl;

system("pause");
return 0;


}
  • What does 'I cant create the specific arrays' mean? Looks to me like you do create the arrays. The main problem with your code seems to be the file handling, which is all over the place. – john Oct 04 '13 at 17:15
  • 1
    `std::vector` to the rescue. – Jerry Coffin Oct 04 '13 at 17:23
  • Also you're reading the file several times, but you've been asked to read a text file once. I guess what this question is really about is, how can I read the data into an array when I don't know the size of the array beforehand and I'm not allowed to reread the data? One answer is to use std::vector as Jerry says, but I guess you have to use arrays. If that's the case then you have to *resize* the array when you know you need a bigger array. So the array starts small but you increase it's size as you read more and more data. – john Oct 04 '13 at 17:25
  • @john i think i do...but when i compile the whole thing, the numerator array and denominator array are absent... – user2847449 Oct 04 '13 at 17:33
  • @JerryCoffin I havent learnt std::vector before...and i have googled this topic for a few days but still cant figure it out – user2847449 Oct 04 '13 at 17:35

1 Answers1

1

I'm afraid this looks enough like homework that I feel compelled to provide only an idea of a direction instead of a complete solution to do everything you've asked for. I hope this is enough to get you started though:

#include <sstream>
#include <vector>
#include <iterator>
#include <numeric>

int main(){
    std::istringstream input("1 200 50 9.5 300.1");

    std::vector<double> numbers{std::istream_iterator<double>(input), 
                    std::istream_iterator<double>()};

    double sum = std::accumulate(numbers.begin(), numbers.end(), 0.0);

    std::cout << "Sum: " << sum << "\n";
    std::cout << "Count: " << numbers.size() << "\n";
}

This doesn't compute the mean or variance, but based on what's here, I believe adding computation of the mean should be truly trivial, and the variance only a little more difficult (note: for the variance you'll want the version of std::accumulate that uses a lambda to compute each squared difference from the mean).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111