I dont know what is wrong. The output didnt show anythin. FYI I have p6.dat on same dic. it just the out put didnt print the number.

#include <iostream>
#include <fstream>

using namespace std;
const int mySize = 15;
const int myStop = 3;
void impliedTask(int *&,float *&);

int main ()
{
    int *myFile = new int[mySize];
    float *avg = new float[mySize/myStop];
    ifstream project;
    project.open("p6.dat");

    if (!project)
    {
        cout << "error" << endl;
    }
    int num = 0;

    while (!project.eof()) //read file to end of file
    {
        project >> myFile[num];
        ++ num;
    }
    for (int i = 0; i < 15; i++)
    {
        cout << myFile [i] << endl;
    }

    impliedTask(myFile,avg);

    delete []avg;
    delete []myFile;

    project.close ();
    return 0;

}
void impliedTask(int *&myFile,float *&avg)
{
    int sum=0;
    cout<<"===Start==="<<endl;
    for(int i=0,j=0;i<mySize;i++)
    {
        cout<<endl;
        cout<<"Number @index["<<i<<"] is: "<<myFile[i]<<endl;
        sum+=myFile[i];//accumulate sum
        cout<<"Sum @index["<<i<<"] is: "<<sum<<endl;
        if(j>=myStop-1)//stop to make adjust if j counter >=2 (0,1,2) == 3 numbers
        {
            avg[i/myStop] = float(sum)/float(myStop);//insert avg into index i/3
            cout<<"Average index["<<i/myStop<<"] is: "<<float(int(10*avg[i/myStop]))/10<<endl;
            j=sum=0;//reset j counter and sum
        }
        else
        {
            j++;
        }
    }
}

update:

Problem is my code is run forever. Can someone show me what is make my code run forever? thank you

#include <iostream>
#include <fstream>

using namespace std;
const int mySize = 15;
const int myStop = 3;

int main ()
{
    int *myFile = new int[mySize];
    float *avg = new float[mySize/myStop];
    ifstream project;
    project.open("p6.dat");

    if (!project)
    {
        cout << "error" << endl;
    }
    int num = 0;

    while (!project.eof()) //read file to end of file
    {
        project >> myFile[num];

        ++ num;

        int sum=0;
        cout<<"===Start==="<<endl;
        for(int i=0,j=0;i<mySize;i++)
        {
            cout<<endl;
            cout<<"Number index["<<i<<"] is: "<<myFile[i]<<endl;
            sum+=myFile[i];//accumulate sum
            cout<<"Sum index["<<i<<"] is: "<<sum<<endl;
            if(j>=myStop-1)//stop to make adjust if j counter >=2 (0,1,2) == 3 numbers
            {
                avg[i/myStop] = float(sum)/float(myStop);//insert avg into index i/3
                cout<<"Average index["<<i/myStop<<"] is: "<<float(int(10*avg[i/myStop]))/10<<endl;
                j=sum=0;//reset j counter and sum
            }
            else
            {
                j++;
            }
        }

    }
    delete []avg;
    delete []myFile;

    project.close ();
    return 0;

}

All the code from lines 27 to 45 needs to be outside the loop started on line 21. You don't want to do all those calculations until the entire file has been read into the array.

while( project >> myFile[num] )
{
   num++;
}

// now do all the calculations here

Notice that project.eof() is not used because it will cause the last line of the file to be read twice.

when i the original I got a different answer : here is output

Number @index[0] is: 97
Sum @index[0] is: 97

Number @index[1] is: 0
Sum @index[1] is: 97

Number @index[2] is: 0
Sum @index[2] is: 97
Average index[0] is: 32.3

Number @index[3] is: 0
Sum @index[3] is: 0

Number @index[4] is: 0
Sum @index[4] is: 0

Number @index[5] is: 0
Sum @index[5] is: 0
Average index[1] is: 0

Number @index[6] is: 0
Sum @index[6] is: 0

Number @index[7] is: 0
Sum @index[7] is: 0

Number @index[8] is: 0
Sum @index[8] is: 0
Average index[2] is: 0

Number @index[9] is: 0
Sum @index[9] is: 0

Number @index[10] is: 0
Sum @index[10] is: 0

Number @index[11] is: 0
Sum @index[11] is: 0
Average index[3] is: 0

Number @index[12] is: 0
Sum @index[12] is: 0

Number @index[13] is: 0
Sum @index[13] is: 0

Number @index[14] is: 0
Sum @index[14] is: 0
Average index[4] is: 0

is this what is p6.dat is look like

97.99
4
61.23
49.32
30.22
80.19
21.67
86.23
30.34
62.1
38.3
93.9
42.5
13.4
68.3

Your file contains a lot of floats but your program is attempting to read them like integers. change myfile to be an array of floats, not integers.

You also have to change the signature of implied Task() like this:

void impliedTask(float *&myFile, float *&avg)

This is what I get after doing that. Don't know if the numbers are right or wrong because I don't know what the program is supposed to do.

97.99
4
61.23
49.32
30.22
80.19
21.67
86.23
30.34
62.1
38.3
93.9
42.5
13.4
68.3
===Start===

Number @index[0] is: 97.99
Sum @index[0] is: 97.99

Number @index[1] is: 4
Sum @index[1] is: 101.99

Number @index[2] is: 61.23
Sum @index[2] is: 163.22
Average index[0] is: 54.4

Number @index[3] is: 49.32
Sum @index[3] is: 49.32

Number @index[4] is: 30.22
Sum @index[4] is: 79.54

Number @index[5] is: 80.19
Sum @index[5] is: 159.73
Average index[1] is: 53.2

Number @index[6] is: 21.67
Sum @index[6] is: 21.67

Number @index[7] is: 86.23
Sum @index[7] is: 107.9

Number @index[8] is: 30.34
Sum @index[8] is: 138.24
Average index[2] is: 46

Number @index[9] is: 62.1
Sum @index[9] is: 62.1

Number @index[10] is: 38.3
Sum @index[10] is: 100.4

Number @index[11] is: 93.9
Sum @index[11] is: 194.3
Average index[3] is: 64.7

Number @index[12] is: 42.5
Sum @index[12] is: 42.5

Number @index[13] is: 13.4
Sum @index[13] is: 55.9

Number @index[14] is: 68.3
Sum @index[14] is: 124.2
Average index[4] is: 41.4
Press any key to continue . . .

Edited 2 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.