943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2088
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 20th, 2009
0

End of file function + Array

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. ((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]);

error C2106: '=' : left operand must be l-value
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 20th, 2009
0

Re: End of file function + Array

You cannot asign an unknown value to the size of an array at compile time. You need to use dynamic memory.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Mar 20th, 2009
0

Re: End of file function + Array

So I have defined a variable:

C++ Syntax (Toggle Plain Text)
  1. double x=0.549;
And editted the equation:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 20th, 2009
0

Re: End of file function + Array

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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 20th, 2009
0

Re: End of file function + Array

[QUOTE]
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
C++ Syntax (Toggle Plain Text)
  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.

Quote ...
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
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 20th, 2009
1

Re: End of file function + Array

>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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 20th, 2009
0

Re: End of file function + Array

Click to Expand / Collapse  Quote originally posted by Narue ...
>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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 20th, 2009
1

Re: End of file function + Array

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 20th, 2009
0

Re: End of file function + Array

How do we define the initial array for the second choice?

int P[?], V[?], T[?] ???
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 20th, 2009
1

Re: End of file function + Array

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Questions about memmove, malloc and realloc - C/C++ - Urgent
Next Thread in C++ Forum Timeline: opengl won't map textures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC