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

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Unhappy 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. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    here is my txt file.

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

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    Thanks for your help.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    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. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  2. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM