#include <iostream>
#include <fstream>
usingnamespace std;
constint mySize = 15;
constint myStop = 3;
int main ()
{
int *myFile = newint[mySize];
float *avg = newfloat[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:
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
//const int mySize = 15;
constint 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.
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.