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).
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
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
>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:
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:
You potentially waste a *lot* of space.
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.
>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:
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:
You potentially waste a *lot* of space.
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.
>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:
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.