Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Need help entering Values into an array from a file (http://www.go4expert.com/forums/help-entering-values-array-file-t20348/)

Lightning 12Dec2009 01:19

Need help entering Values into an array from a file
 
I need to be able to enter these values
3
74
5
-14
62
71
-100
999
from a statdat.txt file into an array, int values[NUMELS] = {#, #, #, #, #, #, #, #)
Im suppose to write a function that reads the values in one at a time and counts them as they are entered.

This is what I have thus far.

Code: CPP

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;
double findAvg(int[], int);
double stdDev(int[], int, double);

int main()
{
    double average, sDev;
    const int NUMELS = 50;
    ifstream infile;
    infile.open("a:statdat.txt");
    int values[NUMELS]; // This is where I need my values to go
    int temp[NUMELS];
    for(int i = 0; i < NUMELS; i++) //This is my code to extract the values from the file
    {
        double count = 0;
        infile >> temp[i];
        if(temp[i] == 999)
            break;
        else
            count = count + 1;
    }
    average = findAvg(values, NUMELS);
    cout << "The average of the numbers is "
         << setw(5)
         << setprecision(2) << average << endl;
    sDev = stdDev(values, NUMELS, average);
    cout << "The standard deviation of the numbers is "
         << setw(5)
         << setprecision(2) << sDev << endl;
    return 0;
}
double findAvg(int nums[], int numel)
{
    int i;
    double sumnums = 0.0;
    for(i = 0; i < numel; i++)
        sumnums = sumnums + nums[i];
    double average = (sumnums / numel);
    return average;
}
double stdDev(int nums[], int numel, double av)
{
    int i;
    double sumdevs = 0.0;
    for (i = 0; i < numel; i++)
        sumdevs = sumdevs + pow((nums[i] - av),2);
    double sDev = (sqrt(sumdevs/numel));
    return[size=2] sDev;
}


any help would be greatly appreciated.. thanks

SaswatPadhi 27Dec2009 19:00

Re: Need help entering Values into an array from a file
 
I think using vectors instead of arrays will be better in this situation.

Because you don't know the size of the list and count it while reading it, vectors will be much simpler to deal with than arrays.

And, from your code .. I think you assume 999 to be the last of the numbers, you break there.

So .. I have modified your code to this ::
Code: CPP

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <fstream>

using namespace std;

int sizeofVector;
double findAvg(vector<int>);
double stdDev(vector<int>);

int main()
{
    double average, sDev;
    vector<int> values;
    int temp;

    ifstream infile;
    infile.open("c:\\test.txt");
    infile >> temp;

    while(temp != 999)
    {
        values.push_back(temp);
        infile >> temp;
    }
    sizeofVector=values.size();

    cout << "The average of the numbers is " << setw(5) << setprecision(3) << findAvg(values) << endl;
    cout << "The standard deviation of the numbers is " << setw(5) << setprecision(3) << stdDev(values) << endl;

    return 0;
}
double findAvg(vector<int> K)
{
    double sumnums = 0.0;
    for(int i = 0; i < sizeofVector; ++i)
        sumnums +=K[i];
    return sumnums/sizeofVector;
}
double stdDev(vector<int> K)
{
    double sumdevs = 0.0;
    double av=findAvg(K);
    for (int i = 0; i < sizeofVector; i++)
        sumdevs +=  pow((K[i] - av),2);
    return sqrt(sumdevs/sizeofVector);
}


I hope that will be useful.

Offtopic comment:
(*) Please post your code within [code]...[/code]
(*) Please *always* post properly indented code, makes things easier for us.


All times are GMT +5.5. The time now is 05:46.