1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help entering Values into an array from a file

Discussion in 'C++' started by Lightning, Dec 11, 2009.

  1. Lightning

    Lightning New Member

    Joined:
    Dec 11, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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:
    #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
     
    Last edited by a moderator: Dec 26, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    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:
    #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.

    [Comment](*) Please post your code within [noparse]
    Code:
    ...
    [/noparse]

    (*) Please *always* post properly indented code, makes things easier for us.
    [/comment]
     
    Last edited: Dec 27, 2009

Share This Page