Threshold and Blank Algorithm

This is a discussion on Threshold and Blank Algorithm within the C Programming forums, part of the General Programming Boards category; Hmmmm. Still get the ol' core dump. Troubling. Also, my class hasn't covered dynamic array structure with malloc, so I'm ...

  1. #16
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    Hmmmm. Still get the ol' core dump. Troubling.
    Also, my class hasn't covered dynamic array structure with malloc, so I'm betting it isn't expected.

    That makes me think something is wrong in the loop structure.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you fix the problem with the blank variable being uninitialized?

    Since you've changed the program, we'd need to see the new version to make good suggestions.

    No, I wouldn't recommend making your arrays dynamic. Adds complexity you don't need, right now.

  3. #18
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    while(fscanf(fp,"%f %f", &t[i], &b[i])!=EOF) {
    			while (b[i] < threshold) {
    				i++;
    			}
    This is wrong, I think. What if the value you read in is less than threshold? Then you are going to increment i, and try to do operations on the array value at index i before you actually read data into it.

    Read your data into your array once, then process it.

    Also, nothing you posted in that code is finding local maxima.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Illiad, if you post up a small (say 100 rows) data file, I'll be glad to run it through your program. I see problem with it though. Like here:


    Code:
    while(b[i] < threshold) {
      i++;
    }
    Will surely cause i to go out of range, many times. Add && i < 3601 or whatever the number of rows in the file is, or explicitly check for the end of the file.

    Seems like the inner loop should know what the outer loop was already given in logic - but no, it doesn't do that.

  5. #20
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Will surely cause i to go out of range, many times. Add && i < 3601 or whatever the number of rows in the file is, or explicitly check for the end of the file.
    still won't be enough. Consider the very first iteration of the outer loop - the very first element of the arrays will have information in it, all the others will have whatever was in memory before (or does declaring arrays on the stack cause them to get zeroed out? Either way, it doesn;t matter).

    If that one information piece is less than threshold, i gets incremented and the code then attempts to do operations on later array elements. Which don't have useful information in them. So even if i stays in bounds, the results will be garbage.

    Whih is why I suggest that he read in all of his information once, using arrays declared with 3601 elements instead of 300000.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by CommonTater View Post
    I also seriously doubt you need 100000 characters (roughly the size of a small novel) for a filename... 256 would probably do it.
    As the code is written, there's no need to allocate any space at all - just pass argv[1] into fopen. Not that I think that is the main problem with the code but it can't hurt to fix it.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21