IN C++11 In the main() function, create appropriate calls for the functions from
ID: 3891061 • Letter: I
Question
IN C++11
In the main() function, create appropriate calls for the functions from (a), (b), and (c) parts to solve the following problem:The file dataForHW2.txt contains both text and numerical content. The first numerical value in the file is the length of the sequence. We will call it n in the sequel, but you may choose to peek at the file and see what it is. The next n integers form a sequence x[0]x[0] , x[1]x[1] , …… , x[n1]x[n1] . Create a new sequence yy of length nn that satisfies y[0]=x[0]y[0]=x[0] , y[1]=x[0]+x[1]y[1]=x[0]+x[1] , y[2]=x[0]+x[1]+x[2]y[2]=x[0]+x[1]+x[2] , and similarly, y[i]=x[0]+x[1]++x[i]y[i]=x[0]+x[1]++x[i] for all i{0,1,…,n1}i{0,1,…,n1} . Write the terms of the sequence y (separated by commas) to the file hw2Output.txt. IN C++11
I wrote this code but it does not work properly.
#include<iostream>
#include<fstream>
#include<cmath>
int getNextInteger(char *text, std::streampos size, std::streampos *pos);
void readSequenceFromFile(std::string filename, int** sequencePointer, int *length);
void printToFile(std::string filename, int *seq, int len);
int main()
{
int *seqP;
int length=0;
seqP=new int[length];
readSequenceFromFile("dataForHW2.txt", &seqP, &length);
int *newSeq;
newSeq=new int[length];
newSeq[0]=seqP[0];
for(int i=1; i<length; ++i)
{
newSeq[i]=newSeq[i-1]+seqP[i];
}
printToFile("hw2OutputDebug.txt", seqP, length);
printToFile("hw2Output.txt", newSeq, length);
delete[] newSeq;
delete[] seqP;
return 0;
}
int getNextInteger(char *text, std::streampos size, std::streampos *pos)
{
int result=0;
int stop=-10;
while( (*pos<size) && (text[*pos]!='-') &&
( (text[*pos]<'0') || (text[*pos]>'9') ) )
{
*pos+=(std::streampos)(1);
}
if(*pos < size)
{
int multiplyingFactor=1;
if(text[*pos]=='-')
{
multiplyingFactor=-1;
*pos+=(std::streampos)(1);
}
if(text[*pos]>='0' && text[*pos]<='9')
{
result*=10; result+= int(text[*pos]-'0');
*pos+=(std::streampos)(1);
result*=multiplyingFactor;
return result;
}
}
return stop;
}
void readSequenceFromFile(std::string fileName, int** sequencePointer, int *length)
{
std::ifstream f(fileName, std::ios::in|std::ios:: binary|std::ios::ate);
if(f.is_open())
{
int firstInt;
std::streampos size=f.tellg();
char *entireText;
entireText=new char[size];
f.seekg(0,std::ios:: beg);
f.read(entireText, size);
std::streampos position;
position=std::streampos(0);
firstInt=getNextInteger(entireText,size,&position);
*length=firstInt;
f.close();
int *y;
y = new int[firstInt];
int i=0;
int j=0;
while(i<firstInt
&& (std::abs(getNextInteger(entireText,size,&position))>=0 &&
std::abs(getNextInteger(entireText,size,&position))<=9))
{
y[i]=getNextInteger(entireText,size,&position);
if(y[i]==-9)
{
std::cout<<"One of the integers is -9"<<std::endl;
break;
}
else if(y[i]==-10)
{
std::cout<<"The # of integers in the text is less than the length of the sequence."<<std::endl;
}
else if(y[i]>=0 && y[i]<=9)
{
++i;
}
}
delete[] *sequencePointer;
*sequencePointer=y;
delete[] entireText;
}
}
void printToFile(std::string fileName, int *seq, int len)
{
std::ofstream f;
f.open(fileName);
int i=0;
while(i<len)
{
f<<(seq)[i]<<" "<<std::endl;
++i;
}
f.close();
}
Explanation / Answer
It took quite a long time investigating your code, but could not find a bug. Runs well and good in the system. Could you please provide me with the input file, so that I can test with that particular file.
Please note the posible points of error. I have added debugg statemets to help you debugging...
#include<iostream>
#include<fstream>
#include<cmath>
int getNextInteger(char *text, std::streampos size, std::streampos *pos);
void readSequenceFromFile(std::string filename, int** sequencePointer, int *length);
void printToFile(std::string filename, int *seq, int len);
int main()
{
int *seqP;
int length=0;
seqP=new int[length];
std::cout << "main pass 1 of 5 ";
readSequenceFromFile("dataForHW2.txt", &seqP, &length);
std::cout << "main pass 2 of 5 ";
int *newSeq;
newSeq=new int[length];
newSeq[0]=seqP[0];
for(int i=1; i<length; ++i)
{
newSeq[i]=newSeq[i-1]+seqP[i];
}
std::cout << "main pass 3 of 5 ";
printToFile("hw2OutputDebug.txt", seqP, length);
std::cout << "main pass 4 of 5 ";
printToFile("hw2Output.txt", newSeq, length);
std::cout << "main pass 5 of 5 ";
delete[] newSeq;
delete[] seqP;
return 0;
}
int getNextInteger(char *text, std::streampos size, std::streampos *pos)
{
int result=0;
int stop=-10;
std::cout << "getNextInteger pass 1 of 3 ";
while( (*pos<size) && (text[*pos]!='-') &&
( (text[*pos]<'0') || (text[*pos]>'9') ) )
{
*pos+=(std::streampos)(1);
}
std::cout << "getNextInteger pass 2 of 3 ";
if(*pos < size)
{
std::cout << "getNextInteger pass 2a of 3 ";
int multiplyingFactor=1;
if(text[*pos]=='-')
{
std::cout << "getNextInteger pass 2aa of 3 ";
multiplyingFactor=-1;
*pos+=(std::streampos)(1);
}
if(text[*pos]>='0' && text[*pos]<='9')
{
std::cout << "getNextInteger pass 2ab of 3 ";
result*=10; result+= int(text[*pos]-'0');
*pos+=(std::streampos)(1);
result*=multiplyingFactor;
return result;
}
}
std::cout << "getNextInteger pass 3 of 3 ";
return stop;
}
void readSequenceFromFile(std::string fileName, int** sequencePointer, int *length)
{
std::cout << "readSequenceFromFile pass 1 of 3 ";
std::ifstream f(fileName, std::ios::in|std::ios:: binary|std::ios::ate);
std::cout << "readSequenceFromFile pass 2 of 3 ";
if(f.is_open())
{
std::cout << "readSequenceFromFile pass 2a of 3 ";
int firstInt;
std::streampos size=f.tellg();
char *entireText;
entireText=new char[size];
f.seekg(0,std::ios:: beg);
f.read(entireText, size);
std::streampos position;
position=std::streampos(0);
firstInt=getNextInteger(entireText,size,&position);
*length=firstInt;
f.close();
std::cout << "readSequenceFromFile pass 2b of 3 ";
int *y;
y = new int[firstInt];
int i=0;
int j=0;
while(i<firstInt
&& (std::abs(getNextInteger(entireText,size,&position))>=0 &&
std::abs(getNextInteger(entireText,size,&position))<=9))
{
std::cout << "readSequenceFromFile pass 2ca of 3 ";
y[i]=getNextInteger(entireText,size,&position);
if(y[i]==-9)
{
std::cout << "readSequenceFromFile pass 2cba of 5 ";
std::cout<<"One of the integers is -9"<<std::endl;
break;
}
else if(y[i]==-10)
{
std::cout << "readSequenceFromFile pass 2cbb of 3 ";
std::cout<<"The # of integers in the text is less than the length of the sequence."<<std::endl;
}
else if(y[i]>=0 && y[i]<=9)
{
++i;
}
}
std::cout << "readSequenceFromFile pass 2d of 3 ";
delete[] *sequencePointer;
*sequencePointer=y;
delete[] entireText;
}
std::cout << "readSequenceFromFile pass 3 of 3 ";
}
void printToFile(std::string fileName, int *seq, int len)
{
std::cout << "printToFile pass 1 of 3 ";
std::ofstream f;
f.open(fileName);
std::cout << "printToFile pass 2 of 3 ";
int i=0;
while(i<len)
{
std::cout << "printToFile pass 2a of 3 ";
f<<(seq)[i]<<" "<<std::endl;
++i;
}
f.close();
std::cout << "printToFile pass 3 of 3 ";
}