# Thread: Student Mark Problem....(Array)

1. ## Student Mark Problem....(Array)

Hi, i have a little problem with my program that calculate the highest mark and lowest mark of a number of student.(Later i will calculate the average of all student).

My program have to read from a txt file that include 10 marks of student.
My program work, but i have to specify "array[10]", therefore it won't work if the txt file is more than or less than 10 marks.

Here is my program in .cpp

2. ## here is my txt file.

here is my txt file that my program read into it.

3. Thanks for your help.

4. Research "vectors" in the STL (Standard Template Library).

If this doesn't work for you - a homework assignment where 'arrays' must be used (?) - you've just discovered the classic drawback to standard arrays.

The only way to get around your problem is to create an artificially large array and read data until '\0' is encountered. Note that '\0' is not zero here, but, rather, the terminating character of the array.

(As you are, or should be, aware, the array size must be a constant value, so there's no way to size your array according to the text file. Vectors circumvent this shortcoming by expanding and collapsing automatically.)

You may also want to throw a descriptor, or two, into your output so that the user will know what '11' and '99' really are.

-Skipper

5. Originally posted by skipper
Research "vectors" in the STL (Standard Template Library).

If this doesn't work for you - a homework assignment where 'arrays' must be used (?) - you've just discovered the classic drawback to standard arrays.

The only way to get around your problem is to create an artificially large array and read data until '\0' is encountered. Note that '\0' is not zero here, but, rather, the terminating character of the array.
Not really. Just because the vector does the work for you doesn't make it the better choice. Forcing it to expand isn't really a great idea.

Reading until the end of the file and using the null-terminating character to find the end isn't "the only way" as you describe it. Just do what most graphics file formats do -- you include the amount in the first line of the file. Then you dynamically allocate that amount when the file is read in. Then, you never have to deal with reallocation (whether explcitly or implicitly).

Just because it goes on behind the scenes with a vector doesn't mean it doesn't happen at all. Using a regular array is more efficient and almost as simple in this case.

6. Perhaps I should have suggested that the only way to get around the problem davidvoyage200 was having, based on the level that the program is written at, is to use a artificially large array with a sentry terminator. (Forget that null character terminator garbage. Don't know where my head was on that one. )

The program is written at an elementary level...no harm there, of course...but, supposing that this is, indeed, a homework assignment, suggesting dynamic allocation is an alternative, but beyond the scope of where davidvoyage200 is at right now.

Just do what most graphics file formats do -- you include the amount in the first line of the file. Then you dynamically allocate that amount when the file is read in. Then, you never have to deal with reallocation (whether explcitly or implicitly).
Agreed...until the program is modified, as many teachers and text books will do, and could make hardcoding the size of the data file, at the beginning of the file, cumbersome, at best.

Frankly, I like your thinking, but, playing the high-percentage shot here, the chances of having to reallocate memory for a vector of int's, in this particular case, is unlikely. The fact that it could happen without the programmer having to concern himself/herself with the issue of deallocation, in my opinion, offsets a great deal.

Dull and inefficient though it may be, an over-sized standard array with a sentry terminator, i.e. a negative value, is the way I would go in this specific context. (This presumes that both of our suggestions, vectors and dynamic allocation, are not options that are available to davidvoyage200. )

-Skipper

Popular pages Recent additions