Like Tree6Likes
  • 1 Post By AndrewHunter
  • 3 Post By quzah
  • 1 Post By AndrewHunter
  • 1 Post By quzah

Need some help for a beginner

This is a discussion on Need some help for a beginner within the C Programming forums, part of the General Programming Boards category; Hi I'm in need of some help with some programs I'm trying to write, I'm fairly new at this and ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Need some help for a beginner

    Hi I'm in need of some help with some programs I'm trying to write, I'm fairly new at this and I'm taking an online course but I need to hurry up and finish it quickly as my school starts soon and I can't have this take more of my time.

    Basically this exercise introduces sentinel values and I need to write a program that finds the min and max integers in a list, also 0 is supposed to be the sentinel value. I'm using the Dev C++ compiler.

    I would really appreciate help on this, I'm loving learning coding so far but it's confusing sometimes XD

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by wasabiman123 View Post
    Hi I'm in need of some help with some programs I'm trying to write, I'm fairly new at this and I'm taking an online course but I need to hurry up and finish it quickly as my school starts soon and I can't have this take more of my time.
    Hi, sorry to hear you are in such a rush. Maybe you should have managed your time better. Don't be to down on yourself, this is a good life lesson.

    Quote Originally Posted by wasabiman123 View Post
    Basically this exercise introduces sentinel values and I need to write a program that finds the min and max integers in a list, also 0 is supposed to be the sentinel value. I'm using the Dev C++ compiler.

    I would really appreciate help on this, I'm loving learning coding so far but it's confusing sometimes XD
    My advice, sit down and think about the problem. Decide how you would need to accomplish this if you were doing it. Then work up a solution with paper and trace through your logic to make sure it works. Once that is complete, write up a program and test it. If you have problems or errors you can't figure out, come back and post the code with the appropriate error/warning messages or the output you are receiving vice what you want. Then we can help you out.
    Salem likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,325
    In addition to what Andrew suggested, I'll break down what a sentinel value is. Basically, it's a special value that you don't want in your data, that tells you where your data ends. In your example, it's zero, so presumably, your list of number for which you find the max and min should not contain a zero. The idea is often "read numbers until you reach the sentinel value", so some pseudo code would look like:
    Code:
    do
        ask user for input
        read an integer x
        if x is not 0
            min = smallest number so far
            max = biggest number so far
    while x is not 0
    print min and max
    That loop will read integers and if it's not a zero, it will put it in your list for later. When the user types a zero, your program will ignore that (not check if 0 is the min or max) and stop asking for input, terminating the program

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use one or more of the following to solve the OP's problem for them:

    Pick one of the following methods:
    1. Use a Stack.
    2. Recursion.
    3. Use dynamic allocation.
    4. Something else interesting.

    It must compile without warnings or errors using -Wall -pedantic, and be obviously beyond normal homework solutions. The winner will be decided by random people that show up, and you will win nothing at all! Bonus points for elegance, and convolution.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    And so it begins......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    It must compile without warnings or errors using -Wall -pedantic,
    Ahem... on my compiler Wall and pedantic ARE errors...

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Ahem... on my compiler Wall and pedantic ARE errors...
    Do you compile with -ILOVEGETS?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    O.O OK first of all, I know what a sentinel value is already, it's not like I haven't worked on this at all, I'm just stuck and my brain is refusing to do jack, third, it's not like you're doing me a huge favor, this class's end grade I end up getting won't affect my scholarity at all, I just learn by example and seeing the completed thing, it seems some of you have enough time to waste trolling this topic, might was well use it to help a guy out...

    I'm not here to beg, but I'm left with few choices as no one I know in real life knows any sort of programming, I simply want to learn in a manner that actually works for me...

    Would it really be too much to ask for some help, it's obviously not a big deal since you guys are far more skilled than I am.

    Additionally I managed my time just fine, some things came up that I couldn't ignore however like my PSAT classes and homework for that....

    So... It's up to you if you'd like to acquire some karma or not >_>

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    We are acquiring a lot more karma now then we would if we were to provide an ready-made answer. Also: no one learns by watching examples (although watching examples does give people a very comforting feeling), they learn by doing examples. So do.

    (Parenthetical: PSAT classes? Really? Maybe I should be investing in Kaplan after all....)

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by wasabiman123 View Post
    O.O OK first of all, I know what a sentinel value is already,
    Oh, that's good. Be rude to the only person in this thread that helped you out. Good job.

    Quote Originally Posted by wasabiman123 View Post
    third, it's not like you're doing me a huge favor, this class's end grade I end up getting won't affect my scholarity at all,
    Oh, good. You won't mind when I don't help you then.

    Quote Originally Posted by wasabiman123 View Post
    I just learn by example and seeing the completed thing, it seems some of you have enough time to waste trolling this topic, might was well use it to help a guy out...
    As tabstop pointed out, noone learns by seeing code. Sit down and come up with something and stop being lazy.


    Quote Originally Posted by wasabiman123 View Post
    Additionally I managed my time just fine, some things came up that I couldn't ignore however like my PSAT classes and homework for that....
    Obviously not, otherwise you would have had more than enough time to complete this assigment.

    Quote Originally Posted by wasabiman123 View Post
    So... It's up to you if you'd like to acquire some karma or not >_>
    Well, if you insist; however I would like to note that you rushed me on this one so I may come up with a different solution later.
    Code:
    void y_Y(void){
    	static int m1=0, ml=0;
    	int O;
    	printf("Enter Number: ");
    	scanf("%d",&O);
    	if(O){
    		if(m1){
    			(O>0)?(O/ml)? ml=O:ml:(ml<0)?(ml/O)?ml=O:ml:ml;
    			(O<0)?(O/m1)? m1=O:m1:(m1/O)?m1=O:m1;
    		}else
    			ml=m1=O;
    		y_Y();
    	}else
    		printf("min: %d, max: %d", m1, ml);
    }
    quzah likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<limits.h>
    
    void insert( int *n )
    {
    }
    
    void lowest( int *n )
    {
        if( n )
            printf( "the lowest is: %d\n", *n );
    }
    
    void highest( int *n )
    {
        if( n )
            printf( "the highest is: %d\n", *n );
        return n == NULL;
    }
    
    void done( int *n )
    {
        printf( "done\n" );
        return n == NULL;
    }
    
    int getint( int *i )
    {
        char buf[ BUFSIZ ] = {0};
        printf( "enter a number, or just hit enter to quit: " );
        fflush( stdout );
        if( fgets( buf, BUFSIZ, stdin ) && sscanf( buf, "%d", i ) == 1 )
            return 0;
        return -1;
    }
    
    int overkill( void (*function)(int *), int *number )
    {
        static struct ll { struct ll *lower; struct ll *higher; int number; } *ln,*hn;
        if( function == insert )
        {
            while( !getint( number ) )
            {
                struct ll *nn = malloc( sizeof *nn );
                if( nn )
                {
                    nn->lower = nn->higher = NULL;
                    nn->number = *number;
                    
                    /* only node */
                    if( hn == NULL )
                    {
                        hn = ln = nn;
                    }
                    else if( hn->number < nn->number ) /* append as highest */
                    {
                        nn->lower = hn;
                        hn = nn;
                    }
                    else if( ln->number > nn->number ) /* prepent as lowest */
                    {
                        nn->higher = ln;
                        ln = nn;
                    }
                    else /* walk through it and insert */
                    {
                        struct ll *p = ln;
                        while( p->higher && p->higher->number < nn->number )
                            p = p->higher;
                        if( p )
                        {
                            nn->lower = p;
                            nn->higher = p->higher;
                            p->higher = nn;
                        }
                        else
                        {
                            printf( "you broke it\n" );
                            exit( EXIT_FAILURE );
                        }
                    }
                }
                else
                    exit( EXIT_FAILURE );
            }
        }
    
        if( function == lowest )
        {
            *number = ln ? ln->number : INT_MAX;
            function( number );
        }
    
        if( function == highest )
        {
            *number = hn ? hn->number : INT_MIN;
            function( number );
        }
    
        if( function == done )
        {
            while( ln )
            {
                struct ll *lw = ln->higher;
                free( ln );
                ln = lw;
            }
            ln = hn = NULL;
        }
    
        return -1;
    }
    
    int main( void )
    {
        int n = 0;
    
        overkill( insert, &n );
        overkill( lowest, &n );
        overkill( highest, &n );
        overkill( done, &n );
    
        return 0;
    }
    This program returns the minimum and maximum numbers entered. Or something close to it anyway.


    Quzah.
    Last edited by quzah; 08-17-2011 at 09:30 PM. Reason: void edit
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Lol....nice. I was debating on whether to go crazy long like that, however I decided to go with integer division and recursion instead. Well, the OP now has two solutions to choose from, hopefully some other people will decide to help him as well.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I originally had the functions not actually do anything, but then I figured I'd make them print out high and low so that main was nice and clean. :P

    edit - It also looks like I could make them all void. I had copied in getint from another version*, and was just passing it by itself, so I made them all mimic it. I guess I could clean that up a touch.


    *this:
    Code:
    #include<stdio.h>
    #include<limits.h>
    
    int getint( int *i )
    {
        char buf[ BUFSIZ ] = {0};
        printf( "enter a number, or just hit enter to quit: " );
        fflush( stdout );
        if( fgets( buf, BUFSIZ, stdin ) && sscanf( buf, "%d", i ) == 1 )
            return 0;
        return -1;
    }
    
    int min( int x )
    {
        int n = INT_MAX, lowest = x;
    
        if( !getint( &n ) )
        {
            lowest = min( n < x ? n : x );
            printf( "%d\n", n );
        }
        return lowest;
    }
    
    int main( void )
    {
        int low = INT_MAX;
    
        low = min( low );
        printf( "the lowest was: %d\n", low );
        
        return 0;
    }
    See? If you were as bored as I was, your homework would be done by now.


    Quzah.
    Last edited by quzah; 08-17-2011 at 09:28 PM.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    See? If you were as bored as I was, your homework would be done by now.
    Quzah.
    What are you talking about? The OP manages his time very well, and thus is never bored, nor has time to do his homework. I am going to have to take points off for posting this one though quzah, it is a little too manageable, even though not complete.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    I am going to have to take points off for posting this one though quzah, it is a little too manageable, even though not complete.
    I figured if you can do min you can do max, so I didn't bother writing it and went on to do the other version. I guess neither of them are making use of a 'sentinel value' though.
    Code:
    int getint( int *i )
    {
        char buf[ BUFSIZ ] = {0};
        printf( "enter a number, or just hit enter to quit: " );
        fflush( stdout );
        if( fgets( buf, BUFSIZ, stdin ) && sscanf( buf, "%d", i ) == 1 )
            return *i == 0;
        return -1;
    }
    But I think that would fix it.

    I was also thinking of doing one that if the latest number you entered was higher, it would call higher otherwise it would call lower if it was lower, sort of a two way recursion:
    Code:
    high( x )
        get number
        if lower than x
            low( number )
        else
            high( number )
    But I got distracted by the first posted version, and never got back to it. I was trying to decide how to return both the high and the low from both at the same time (probably a pair of pointer arguments).


    Quzah.
    Last edited by quzah; 08-17-2011 at 09:47 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Help Plz :D
    By xanimeangiex in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2010, 06:43 AM
  2. C++ Beginner Help On XP
    By rayne117 in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2009, 12:45 PM
  3. help for a beginner
    By kiz in forum C Programming
    Replies: 5
    Last Post: 09-19-2007, 08:09 AM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM
  5. C++ Beginner
    By Steevous in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2002, 03:23 PM

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