Thread: i didn't understand part of this algorithm to calculate highest value

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    i didn't understand part of this algorithm to calculate highest value

    now i know there's a simple way to calculate higest value.

    but here i have this algorithm which i understood part of it.
    the parts that i didn't understand i marked which comments ( //) .
    if someone could show me the first iteration of it. it would help understand it better. i know that *largest is point to the first element of begin which is 5. so in the first if statement its 5<5 at the first iteration ?

    Code:
    // i marked the parts i didnt understand with " //" 
    
    #include <stdio.h>
    
    int *print(int *begin ,int *end ){
        if(begin==end)
            return 0;
        int *largest = begin;
    
        for (; begin!=end; begin++){
            printf("%d\n", *begin);    // until here i understood. 
            if(*largest < *begin){    //from here i didnt understand.
               largest = begin;          // also...
            }
        }
        return largest;
    }
    
    
    int main(){
        int numbers[]={5,1,22,26,2};
        int size= sizeof(numbers) / sizeof(numbers[0]);
    
        int *largest = print(numbers , numbers+size); // from here also didnt understand
        if (largest) {      //also ....
            printf("largest %d\n", *largest);
        }
    }
    
    //thanks
    



  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    If you are unsure, it always helps to add some printfs, and then run the code.

    i.e.
    Code:
    for (; begin!=end; begin++)
    {
    
      //printf("%d\n", *begin);    // until here i understood. 
    
      printf("*largest=%d, *begin=%d\n\r", *begin, *largest);
    
      if(*largest < *begin)    //from here i didnt understand.
      {
        printf("*largest was smaller than *begin - *largest now has the value of *begin");
        largest = begin;          // also...
      }
    }
    If it is the pointer notation that you are unformiliar with, Pointers in C - Tutorial - Cprogramming.com

    If it is what the program is doing that is confusing you, the clue for what it is doing is in the naming of the variable "largest"
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Quote Originally Posted by Idan Damri View Post

    so in the first if statement its 5<5 at the first iteration ?
    Yes , yes it is..the first iteration of you loop compares checks if(5 < 5), your second if(5<1) and so on.. i think might be missing some knowledge on pointer arithmetic .try here too

  4. #4
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Code:
    return 0; // Bad style code, because we must return pointer, not number.
    return NULL; // Good code style.
    
    
    // By the way, I would totally rewrite so:
    
    
    int* find_max(int* begin, int* end)
    {
        int* max = NULL;
        while( begin < end ) {
           if( !max || *begin > *max ) {
               max = begin;
           }
           ++begin;
        }
        return max;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zub
    Code:
    // By the way, I would totally rewrite so:
     
     
    int* find_max(int* begin, int* end)
    {
        int* max = NULL;
        while( begin < end ) {
           if( !max || *begin > *max ) {
               max = begin;
           }
           ++begin;
        }
        return max;
    }
    Why test for max on each iteration? It is not wrong, but it does not seem to be an improvement over checking the range before the loop.
    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

  6. #6
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by laserlight View Post
    Why test for max on each iteration? It is not wrong, but it does not seem to be an improvement over checking the range before the loop.
    In many other cases, I would agree with you. However, it seemed to me simpler and more logical to do so. Think of it as an example of a different approach for solving the problem.

    By the way, the original version can also be a bit optimized. On the first iteration occurs completely meaningless comparison of two identical numbers.

    Code:
    int* find_max(const int* begin, const int* const end)
    {
        if( begin >= end ) { return NULL; } // attention to comparison!
        int* max = begin++; // attention to increment!
        for( ++begin; begin != end; ++begin ) { // other place for same increment.
            if( *max > *begin ) {
                max = begin;
            }
        }
        return max;
    }
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zub
    By the way, the original version can also be a bit optimized. On the first iteration occurs completely meaningless comparison of two identical numbers.
    Yeah. However, you incremented begin twice in your revised example, which is wrong. I would suggest:
    Code:
    int *find_max(int *begin, int *end)
    {
        int *max = NULL;
        if (begin < end)
        {
            max = begin;
            for (++begin; begin < end; ++begin)
            {
               if (*begin > *max)
               {
                   max = begin;
               }
            }
        }
        return max;
    }
    Comparing for begin != end rather than begin < end should be fine too, upon which the implied pre-condition is that begin <= end.
    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

  8. #8
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by laserlight View Post
    Yeah. However, you incremented begin twice in your revised example, which is wrong.
    No, I have two options of the same increment. Read the comments carefully.

    I really made ​​a mistake in the code, but in a different line (*max > *begin). It's just a typo. I was distracted and did not have time to fix it in 60 minutes.
    Last edited by zub; 08-04-2014 at 11:55 AM.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zub
    No, I have two options of the same increment. Read the comments carefully.
    I read your code, not just your comments. Compile and run this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    const int* find_max(const int* begin, const int* const end)
    {
        if( begin >= end ) { return NULL; } // attention to comparison!
        const int* max = begin++; // attention to increment!
        for( ++begin; begin != end; ++begin ) { // other place for same increment.
            if( *begin > *max ) {
                max = begin;
            }
        }
        return max;
    }
    
    int main(void)
    {
        int numbers[] = {0, 2, 1};
        const int *max = find_max(numbers, numbers + sizeof(numbers) / sizeof(numbers[0]));
        if (max)
        {
            printf("%d\n", *max);
        }
        return 0;
    }
    You will find that the output is 1 even though the maximum is obviously 2. The reason is that you increment begin once, to point to the element at index 1, then you increment begin again, to point to the element at index 2. Since the element at index 2 is greater than the element at index 0, it is set to be the maximum, skipping the element at index 1, which is the true maximum.

    Quote Originally Posted by zub
    I really made ​​a mistake in the code, but in a different line (*max > *begin). It's just a typo. I was distracted and did not have time to fix it in 60 minutes.
    Heh, I did not spot that either, though my example code fixed the bug.
    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

  10. #10
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Apparently, I can not articulate my thoughts in English. I did increment in two lines, just to show that it can be made in any of them. I showed one possible place to increment and another possible place for the same increment. That is what is written in the comments.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, now I understand. It might have been clearer to explain outside of the code since "other place for same increment" does not convey the intended meaning of "the increment from the previous line could have been placed on this line instead", especially since the increment does appear in the for loop's third expression.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I didn't understand how each word newline program
    By amadeus111 in forum C Programming
    Replies: 7
    Last Post: 02-26-2013, 02:02 PM
  2. Replies: 0
    Last Post: 07-31-2009, 09:46 AM
  3. Don't understand part of the source... Help!
    By tggqqa in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2007, 10:33 PM
  4. Replies: 4
    Last Post: 08-09-2006, 01:06 PM
  5. Karaoke algorithm didn't work...
    By loobian in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2003, 01:09 PM