1.Input three unique numbers and print the different of the highest and lowest numbers.
2.Input three integers a determine if there are equal numbers . If There are equal numbers, print the product of the equal numbers, Otherwise print the average of the numbers.
3.input three unique numbers and average the numbers in lowest to highest order.

Recommended Answers

All 4 Replies

We don't do your homework for you. Make an honest effort, show us the code if you are having problems, and if it seems appropriate we will help you resolve your problems and point out some of your errors.

We like to help ... but if you want to learn to program then you must actually practice coding ...

Can you code an Hello World type prgram?

Can you code to ask the user to input an integer from the keboard, firstly printing out a prompt to guide the input?

Can you code for ...

if a == b then do ... else do ...

If you can do these ... you have enough coding skills to start your problem.

Show us your code for the first part ...

1.Input three unique numbers and print the different of the highest and lowest numbers

... and if you need help with that code ... we will then be able to actually see where you are at.

I will give one piece of advice, but I don't know if it will make sense to you yet. Anyway, here it goes:

When getting numbers from standard input, the natural inclination is to use scanf() directly. I don't recommend that, as it can have unforeseen issues with the newline marker, a character the keyboard issues when the Enter key is hit. When something is read in from the console to stdin, the whole line of input is temporarily saved in a memory space called a buffer. If you use scanf() to read the numbers in, the newline will still be there in the buffer, meaning that the next time you try to read something from the stdin, it will read the newline first, and cause problems as it is expecting some other kind of data.

The solution is to read the data from stdin as a line of text (that is, and array of char) using fgets(), then convert that to an int using the formatted string scanning function, sscanf() (note the extra 's'). This a well-known trick for avoiding the buffered newline problem. You would do something like this:

#define BUF_SIZE 256
/* .. later ... */

char buffer[BUF_SIZE];
int input_a;

/* even later ... */

fgets(buffer, BUF_SIZE - 1, stdin);
sscanf(buffer, "%d", &input_a);

This is just a quick sketch of how you would use it; the details will be up to you.

In light of what was said above by @Schol-R-LEA ...

I would like to suggest that each student compare ...

the below ...

fairly equivalent student type numeric input usage of ...

scanf

vs

sscanf

???

and see which you prefer?

(Note 1: both examples check for a '\n' char to exist right after the last digit that was input in the leading numeric digits ...

Note 2: both examples erroneously accept invalid input via overflow)

/* takeInNum_a.c */

/* scanf example with some validation of numeric input */


/*
    a simple student way to handle numeric input ...
    so program won't crash on bad input

    Six Fast Steps to Programming in C
    http://developers-heaven.net/forum/index.php/topic,2022.0.html
*/

#include <stdio.h>
#include <ctype.h> /* re. tolower */


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}


/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg )
{
    int done = 0, val = 0;
    while( !done )
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            done  = 1;
        else
        {
            printf( "\nInteger input only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
    return val;
}



int main ()
{
    int i;
    do
    {
        i = takeInInt( "Enter an int: " );
        printf( "You entered %d\n", i );
    }
    while( more() );

    return 0;
}

Now the sscanf example ...

/* takeInNum_b.c */

/* sscanf example with some validation of numeric input */


#include <stdio.h>
#include <ctype.h> /* re. tolower, isspace, isdigit */


/* 2 handy utilities for many C student coding problems ... */
int takeInChr( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}
int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}

int takeInInt( const char* msg )
{
    char buf[256], *p;
    int i;

    for( ; ; )
    {
        printf( msg ); fflush( stdout );
        fgets( buf, sizeof(buf), stdin );

        /* check to see if char right after last digit is '\n' */
        p = buf;
        while( *p && isspace(*p) ) ++p;
        if( *p == '-' || *p == '+' ) ++p;
        while( isdigit(*p ) ) ++p;

        if( *p == '\n' ) /* ok ... it is '\n' ... so ... */
        {
            if( sscanf( buf, "%d", &i ) == 1 )
                break;
        }
        else
            printf( "\nInvalid entry ... try again ...\n" );
    }
    return i;
}



int main ()
{
    int i;
    do
    {
        i = takeInInt( "Enter an int: " );
        printf( "You entered integer: %d\n", i );
    }
    while( more() );

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.