give feedback on this pointer program

Posted by JohnWong on Stack Overflow See other posts from Stack Overflow or by JohnWong
Published on 2010-05-06T03:07:17Z Indexed on 2010/05/06 3:18 UTC
Read the original article Hit count: 217

Filed under:
|
|

This is relatively simple program. But I want to get some feedback about how I can improve this program (if any), for example, unnecessary statements?

#include<iostream>
#include<fstream>
using namespace std;

double Average(double*,int);

int main()
{

    ifstream inFile("data2.txt");

    const int SIZE = 4;
    double *array = new double(SIZE);
    double *temp;
    temp = array;

    for (int i = 0; i < SIZE; i++)
    {
        inFile >> *array++;
    }
    cout << "Average is: " << Average(temp, SIZE) << endl;
}

double Average(double *pointer, int x)
{
    double sum = 0;

    for (int i = 0; i < x; i++)
    {
        sum += *pointer++;
    }
    return (sum/x);
}

The codes are valid and the program is working fine. But I just want to hear what you guys think, since most of you have more experience than I do (well I am only a freshman ... lol)

Thanks.

© Stack Overflow or respective owner

Related posts about beginner

Related posts about c++