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

Last edited by SaswatPadhi; 26Dec2009 at 18:21.. Reason: Code blocks