# Thread: Need some help for a beginner

1. ## 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. Originally Posted by wasabiman123
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.

Originally Posted by wasabiman123
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.

3. 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
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. 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.

5. And so it begins......

6. Originally Posted by quzah
It must compile without warnings or errors using -Wall -pedantic,
Ahem... on my compiler Wall and pedantic ARE errors...

7. Originally Posted by CommonTater
Ahem... on my compiler Wall and pedantic ARE errors...
Do you compile with -ILOVEGETS?

Quzah.

8. 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. 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. Originally Posted by wasabiman123
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.

Originally Posted by wasabiman123
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,

Originally Posted by wasabiman123
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.

Originally Posted by wasabiman123
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.

Originally Posted by wasabiman123
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);
}```

11. 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.

12. 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.

13. 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.

14. Originally Posted by quzah
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.

15. Originally Posted by AndrewHunter
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.