Array Problem

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    #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;
    }
What is it expected to do when it's read all the data in the file?
closed account (j3Rz8vqX)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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 sum=0;
    cout<<"===Start==="<<endl;
    for(int i=0,j=0;!project.eof();i++)
    {
        cout<<endl;
        project >> myFile[i];
        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;
}


Edit:
We are assuming the file to be read will only have 15 values!
More than 15 would cause undefined behavior; if so, reallocate arrays into larger arrays and ensure to free memory - better yet, you should use vectors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <vector>
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;
    vector<int> myFile;
    vector<float> avg;
    project.open("p6.dat");
    if (!project)
    {
        cout << "error" << endl;
    }
    int sum=0;
    cout<<"===Start==="<<endl;
    for(int i=0,j=0;!project.eof();i++)
    {
        cout<<endl;
        myFile.push_back(0);
        project >> myFile[i];
        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.push_back(0);
            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;
}

If you have a strong understanding of arrays, then by all means, use them.
But if you don't, you'd best use vectors.

Quoted from a member: helios:
I won't tell you to avoid vectors, because that's just silly. If you prefer vectors and there's no technical reason to use one or the other, then go ahead and use vectors.
If you use choose to use arrays, keep in mind that you have to deal with memory management, and some people just aren't very good at it.
Last edited on
I love to vector but our teacher say we have use the array. :(
closed account (j3Rz8vqX)
Vector is an array with features.

If you know the behavior of arrays (especially dynamic) then you will appreciate vectors; since they provide methods that you will probably eventually construct, to deal with dynamic allocation and such...

As to why your professor insists on arrays instead of vectors is to improve your understanding of the processes vectors will be doing for you; and to not cripple you of primitive designs.

It is like using std::sort, and not ever learning how to make a sort.
http://www.cplusplus.com/reference/algorithm/sort/
Which there are many kinds with many exceptions.

The appreciation is that:
A vector literally manages an array for you.
A person would use a vector that uses an array (logic).
Person->vector->array.

Hopefully your problem is solved.
The first code above should provide a possible solution for array.

If you want to perform a dynamic array, you're going to have to develop copy functions and new allocation function; and process freeing memory whenever necessary - which vectors do for you.

It isn't impossible but production could go a lot faster if you hadn't need to recreate those functions.
Topic archived. No new replies allowed.