Thread: Exercise with MinMax

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    4

    Exercise with MinMax

    hello,
    to be honest i don't really know how to do that exercise and it would be great if someone could help with it.

    An integer
    N and a sequence of N integers are given. Find the order number of the last extremal (that is, minimal or maximal) element of the sequence.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know what you mean by "the order number", but the rest is quite simple. Iterate through the array and keep two indices, one for the lowest value and one for the highest. Also keep track of which index was highest or lowest last and you're done.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    by the wrong posted

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    4
    I have to do it without array that the first thing.
    Here I have some solution which should apear in the console if program works.


    Exercise with MinMax-exemple-jpg

  5. #5
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by GReaper View Post
    I don't know what you mean by "the order number", . . .
    Maybe it means:
    How many numbers are to input? 5
    Input five numbers:

  6. #6
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by Jcobbe View Post
    I have to do it without array that the first thing.
    Here I have some solution which should apear in the console if program works.

    Exercise with MinMax-exemple-jpg
    Ok, that is in this example 5 + 2.
    minimal or maximal
    And(!) not or because the result in the example is seven.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oh, so it simply wants the index. Of course, this can be done without an array too, just decide for min and max as soon as you input the number and keep track of them in three variables, one for min, one for max and one for the last extremal index.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Dec 2018
    Posts
    4
    Quote Originally Posted by GReaper View Post
    Oh, so it simply wants the index. Of course, this can be done without an array too, just decide for min and max as soon as you input the number and keep track of them in three variables, one for min, one for max and one for the last extremal index.
    Can u show me how can i do it because i still dont know

  9. #9
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    An idea, take the input numbers through two (first(x) - last(x)) and sort the last part for maximum and minimum, and then add this two numbers.

    I hope that was a right idea . . .

  10. #10
    Registered User
    Join Date
    Dec 2018
    Posts
    4
    Quote Originally Posted by Kernelpanic View Post
    An idea, take the input numbers through two (first(x) - last(x)) and sort the last part for maximum and minimum, and then add this two numbers.

    I hope that was a right idea . . .
    Yeah i get it but now i have to write it

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What's stopping you from writing it?
    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

  12. #12
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by Jcobbe View Post
    Yeah i get it but now i have to write it
    Quote Originally Posted by laserlight
    What's stopping you from writing it?
    If that not a "Last-minute-Year-Project" then make your brain clear befor:
    Reset your brain!

    Examples:
    http://edle-troepfchen.de/wp-content...rger-gross.jpg

    https://cdn1.wine-searcher.net/image...l-10632678.jpg

  13. #13
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by Kernelpanic View Post
    An idea, take the input numbers through two (first(x) - last(x)) and sort the last part for maximum and minimum, and then add this two numbers.

    Reset your brain!
    Ok, I have reset my brain and this is the idea as a program (English version, almost):

    Code:
    //Aus der 2ten Hälfe von Nummern die niedrigste und die höchste
    //herausfinden und addieren - 6./7. Dez. 2018
    //From the 2nd half of numbers the lowest and the highest
    //find out and add
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 20
    
    int maximum_aus_feld(int *eingabe, int groesse);
    int minimum_aus_feld(int *eingabe, int groesse);
    
    int main(void)
    {
        //The right field is to browsed
        int eingabe_feld[MAX];
        int rechtes_feld[MAX / 2];
        
        int *feld_eingabe_zgr = &eingabe_feld[0];
        int *feld_rechts_zgr = &rechtes_feld[0];
        
        int anzahl, eingabe;
        int rechte_haelfte;
        
        printf("\nWie viele Zahlen eingeben?: ");
        scanf("%d", &anzahl);
        
        //Eingaben 0 bis über MAX abfangen
        if (anzahl == 0)
        {
            printf("\nFalsche Eingabe!\n");
            exit(0);
        }
        else if (anzahl > MAX)
        {
            printf("\nMaximum sind 20 Zahlen!\n");
            exit(0);
        }
        else
        {
            for (int i = 0, j = 1; i < anzahl; i++)
            {
                printf("Nummer:%2d = ", j++);
                scanf("%3d", &eingabe);
                eingabe_feld[i] = eingabe;
                if (anzahl == 1)
                {
                    printf("\nOrder number = %2d", eingabe_feld[i]);
                    exit(0);
                }
                else continue;
            }
        }
        
        //And catch(?) 2 + 3
        if (anzahl == 2)
        {
            int i = 0;
            printf("\nOrder number = %2d", eingabe_feld[i + 1]);
            exit(0);
        }
        else if (anzahl == 3)
        {
            printf("\nOrder number = %2d", (eingabe_feld[1] + eingabe_feld[2]));
            exit(0);
        }    
        
        printf("\nIhre Eingabe war: ");    
        for (int i = 0; i < anzahl;)
        {        
            printf("%3d", feld_eingabe_zgr[i++]);
        }
        
        //Hälfte der Anzahl, odd or even input quantity
        if (anzahl % 2 == 0)
        {
            rechte_haelfte = anzahl / 2;
        }
        else
        {
            rechte_haelfte = ((anzahl / 2) + 1);
        }    
        
        //Without "größer gleich: >=" error with odd input numbers (quantity)
        for (int i = anzahl, j = 1, z = 0; i >= rechte_haelfte; i--, j++, z++)
        {        
            rechtes_feld[z] = eingabe_feld[anzahl - j];        
        }
        //Ordnung muß sein! ;)
        printf("\nOrder number = %3d", (maximum_aus_feld(feld_rechts_zgr, rechte_haelfte)
                                     + minimum_aus_feld(feld_rechts_zgr, rechte_haelfte)));
        
        printf("\n");
        return(0);
    }
    
    
    int maximum_aus_feld(int *eingabe, int groesse)
    {
        //Auf Anfang setzen - Put to the beginning
        int maximum = eingabe[0], i;
        for(i = 1; i < groesse; i++) 
        {
            //If "eingabe[]"(Input) greater than maximum is eingabe[]
            if(eingabe[i] > maximum)
            {
                maximum = eingabe[i];
            }
        }
        return maximum;
    }
    
    
    int minimum_aus_feld(int *eingabe, int groesse)
    {
        int minimum = eingabe[0], i;
        for(i = 1; i < groesse; i++) 
        {
            //The same like maximum only "<"
            if(eingabe[i] < minimum)
            {
                minimum = eingabe[i];
            }
        }
        return minimum;
    }
    Output examples:
    Exercise with MinMax-aus-haelfte-finden2018-12-07_191356-jpg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with exercise
    By Tasso Luz in forum C Programming
    Replies: 15
    Last Post: 06-24-2014, 06:40 AM
  2. exercise help
    By ali.franco95 in forum C Programming
    Replies: 2
    Last Post: 09-24-2011, 11:02 AM
  3. K&R Exercise 1-9
    By Anarchy in forum C Programming
    Replies: 6
    Last Post: 01-10-2010, 12:42 PM
  4. gdb exercise!
    By MK27 in forum C Programming
    Replies: 9
    Last Post: 11-24-2008, 10:17 AM
  5. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM

Tags for this Thread