Thread: Can't seem to find the problem with this one:

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    7

    Can't seem to find the problem with this one:

    Hi There,

    I'm playing around with different possibilities to test an array for the biggest and smallest numbers… I know this one is far from the best, but I’m just trying to understand why doesn't it work. It should be able to identify the highest num as long as it's positive and the lowest as long as it's smaller than 200 but still, it finds only the highest one and keeps telling me the lowest is 0.

    Any help or tips would be much appreciated!


    insert
    Code:
    #include <stdio.h>
    
    main()
    {
    int arr[100];
    int num;
    int cntr=0;
    int testH=0;
    int testL=200;
    
        printf("Enter the number of elements you would like to test \n");
        scanf(" %d", &num);
    
        for(cntr ; cntr+1 <= num ; ++cntr)
        {
            printf("Enter indigent number %d: ", cntr+1);
            scanf(" %d", &arr[cntr]);
        }
        for(cntr=0 ; cntr <= num ; ++cntr){
    
            if(arr[cntr] <= testL)
                testL=arr[cntr];
    
            if(arr[cntr] >= testH)
                testH=arr[cntr];
    
    
        }
    
        printf("Highest number is %d\n", testH);
        printf("Lowest number is %d", testL);
    
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Carefully check your 'for' loops.

    It is not often seen in C to use <= when working through the contents of an array, because the arrays are zero based.

    It is my usual for to add brackets around "&arr[cntr]" to give "&(arr[cntr])" or maybe replace it with the more usual "arr+cntr"

    Also when looking for max & min, you would usually set the testL and testH to the first value ("arr[0]" in this case), and then check the rest of the items.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Generally, if you want to loop in forward direction over the first num elements of an array, your loop will look like this:
    Code:
    for (cntr = 0; cntr < num; ++cntr)
    Your first loop is technically correct because cntr was already initialised to 0, and cntr + 1 <= num is mathematically equivalent to cntr < num as cntr and num are integers, but it is better to stick with the usual idiom as other programmers will be able to verify its correctness more quickly and surely.

    Your second loop is wrong as it has an off-by-one error: on the very last iteration you process arr[num], which remains without a proper initial value. It is quite possible that on your test runs the garbage value in arr[num] happened to be 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2020
    Posts
    7
    Thank you so much for taking the time to review and assist!
    Great feedback!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find the problem!
    By sjo in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2019, 02:38 PM
  2. Just can't find the problem.
    By Zacariaz in forum C Programming
    Replies: 0
    Last Post: 06-08-2015, 09:07 PM
  3. Array problem. Can you find the problem with this?
    By Aitra in forum C Programming
    Replies: 21
    Last Post: 01-03-2013, 10:33 AM
  4. Can't seem to find the problem, help.
    By JoeyJoe in forum C Programming
    Replies: 8
    Last Post: 03-04-2011, 02:03 PM
  5. pls help i cant find the problem :(((
    By condorx in forum C Programming
    Replies: 3
    Last Post: 11-07-2002, 09:05 AM

Tags for this Thread