End of file function + Array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

End of file function + Array

 
0
  #1
Mar 20th, 2009
It's me again.

This is the question: Write a computer program that computes the temperature of a gas that is originally at P=5 atm, =V30 Liters, T=273 K. Solve the problem using one dimensional array only. Assume number of array elements is unknown (use the end of file function).

And we have 10 errors:

for
  1. int P[N], T[N], V[N];


3X error C2057: expected constant expression
3X error C2466: cannot allocate an array of constant size 0
error C2133: 'P/T/V' : unknown size


for
  1. ((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]);

error C2106: '=' : left operand must be l-value
  1.  
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. ifstream infile("TEMPER.dat");
  9. ofstream outfile("RESULT.dat");
  10.  
  11. int main()
  12.  
  13. {
  14.  
  15. int N;
  16. N=infile.eof();
  17.  
  18. cout<<"There are "<<N<<" elements in the input file"<<endl;
  19.  
  20. int i=0;
  21. double Pinit=5, Vinit=30, Tinit=273;
  22. double P[N], T[N], V[N];
  23.  
  24.  
  25. for (i=0; !infile.eof(); i++)
  26. { infile>>P[i]>>V[i];
  27. ((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]);
  28.  
  29. for (i=0; i<N; i--)
  30. {cout<<i;
  31. outfile<<i;};
  32. cout<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl
  33. <<"----"<<"------"<<"-----"<<"----"<<endl
  34. <<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;
  35. outfile<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl
  36. <<"----"<<"------"<<"-----"<<"----"<<endl
  37. <<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;
  38.  
  39. }
  40. return 0;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: End of file function + Array

 
0
  #2
Mar 20th, 2009
You cannot asign an unknown value to the size of an array at compile time. You need to use dynamic memory.

  1. double *P = new double[N];
  2. ...
  3. delete [] P;

For the second bit, you cannot do a mathematical equation on the left hand side of an assignment operator. It must be a variable etc.

Chris
Last edited by Freaky_Chris; Mar 20th, 2009 at 5:44 am.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #3
Mar 20th, 2009
So I have defined a variable:

  1. double x=0.549;
And editted the equation:

  1. x= ((P[i]*V[i])/T[i])

And the error is gone. Thanks

Btw, we cannot use dynamic memory for this one. Any alternative?

Cheers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: End of file function + Array

 
0
  #4
Mar 20th, 2009
lines 8 and 9. Files can not be opened outside a function like you have it. Move both those lines down to within main() (line 14).

line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size
  1. // set file pointer to end-of-file
  2. infile.seekp(0, ios::end);
  3. // get file location
  4. N = infile.tellg();

line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').

To get the actual number of integers in the file you will have to read each line and count them as you go along.

line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this
  1. int i = 0;
  2. while( infile>>P[i]>>V[i] )
  3. {
  4. // blabla
  5.  
  6. ++i;
  7. }
Last edited by Ancient Dragon; Mar 20th, 2009 at 7:38 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #5
Mar 20th, 2009
[QUOTE]
Originally Posted by Ancient Dragon View Post
lines 8 and 9. Files can not be opened outside a function like you have it. Move both those lines down to within main() (line 14).

line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size
  1. // set file pointer to end-of-file
  2. infile.seekp(0, ios::end);
  3. // get file location
  4. N = infile.tellg();
I won't use this code, because I'm sure, the tutor will kill me. I told you to use "End of file" function ONLY, he'd say.

line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').

To get the actual number of integers in the file you will have to read each line and count them as you go along.

line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this
  1. int i = 0;
  2. while( infile>>P[i]>>V[i] )
  3. {
  4. // blabla
  5.  
  6. ++i;
  7. }
In that way, how the initial array definition should be?

e.g. int P[n], V[n], T[n]
How do we determine the value of n?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: End of file function + Array

 
0
  #6
Mar 20th, 2009
>How do we determine the value of n?
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:
  1. int P[1000], T[1000], V[1000];
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:
  1. You potentially waste a *lot* of space.
  2. You have little choice but to ignore any records beyond N unless you're able to get clever and manage chunks of the file instead of the whole thing all at once.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #7
Mar 20th, 2009
Originally Posted by Narue View Post
>How do we determine the value of n?
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:
  1. int P[1000], T[1000], V[1000];
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:
  1. You potentially waste a *lot* of space.
  2. You have little choice but to ignore any records beyond N unless you're able to get clever and manage chunks of the file instead of the whole thing all at once.
So, I cannot use "End of File" function at all. Right?

I actually know that the input file contains 10 values only, but our tutor is such a psycho. He wants to suffer us.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: End of file function + Array

 
0
  #8
Mar 20th, 2009
>So, I cannot use "End of File" function at all. Right?
You can, but it would be more along the lines of stopping the input loop prematurely instead of determining the array size:
  1. for ( int i = 0; i < 1000; i++ ) { // Don't overrun the array
  2. infile>> P[i] >> V[i];
  3.  
  4. if ( infile.eof() )
  5. break;
  6.  
  7. // The records were successfully read
  8. }
Which is quite pointless when you could do this instead:
  1. for ( int i = 0; i < 1000 && infile>> P[i] >> V[i]; i++ ) {
  2. // The records were successfully read
  3. }
Last edited by Narue; Mar 20th, 2009 at 12:06 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #9
Mar 20th, 2009
How do we define the initial array for the second choice?

int P[?], V[?], T[?] ???
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: End of file function + Array

 
0
  #10
Mar 20th, 2009
The definition of your arrays doesn't change. But if you know that there will only be ten records, 1000 is a smidge too big.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC