Newbie Problem

Discussion in 'C Programming' started by Larry Tooley, Jan 28, 2007.

  1. Larry Tooley

    Larry Tooley Guest

    I am new to C and am using "C How to Program" to get started with my C
    education. I am working an exercise where I am supposed to use only
    arithmetic, equity and relational operators, and the "if" statement to
    determine the largest and smallest of three integers. It seems like there
    is an easy answer, but I just haven't come up with a solution. It has
    been a long time since CS101 with Pascal so pardon my ignorance.

    Thanks for the help.

    Larry
     
    Larry Tooley, Jan 28, 2007
    #1
    1. Advertising

  2. Larry Tooley <> writes:

    >determine the largest and smallest of three integers.



    if A > C then swap them
    if A > B then swap them
    if B > C then swap them

    Now A is the smallest and C is the largest.


    --
    Gustavo Rondina
    http://gustgr.freeshell.org
     
    Gustavo Rondina, Jan 28, 2007
    #2
    1. Advertising

  3. Larry Tooley wrote:
    > I am new to C and am using "C How to Program" to get started with my C
    > education. I am working an exercise where I am supposed to use only
    > arithmetic, equity and relational operators, and the "if" statement to
    > determine the largest and smallest of three integers. It seems like there
    > is an easy answer, but I just haven't come up with a solution. It has
    > been a long time since CS101 with Pascal so pardon my ignorance.
    >
    > Thanks for the help.
    >
    > Larry


    /*Maximum of three integers*/
    int max(int a,int b,int c)
    {
    return ((a>b&&a>c)? a:(a>b &&a<c)? c:(b>c)?b:c);
    }
    /*Minimum of three integers*/
    int min(int a,int b,int c)
    {
    return ((a<b && a<c)?a:(a>b&&a<c)?b:(b<c)?b:c);
    }
     
    Kelly Brookes, Jan 28, 2007
    #3
  4. Larry Tooley wrote:
    > I am new to C and am using "C How to Program" to get started with my C
    > education. I am working an exercise where I am supposed to use only
    > arithmetic, equity and relational operators, and the "if" statement to
    > determine the largest and smallest of three integers. It seems like there
    > is an easy answer, but I just haven't come up with a solution. It has
    > been a long time since CS101 with Pascal so pardon my ignorance.
    >
    > Thanks for the help.
    >
    > Larry

    int max(int a,int b,int c)
    {
    return ((a>b&&a>c)? a:(a>b &&a<c)? c:(b>c)?b:c);
    }

    int min(int a,int b,int c)
    {
    return ((a<b && a<c)?a:(a>b&&a<c)?b:(b<c)?b:c);
    }
     
    Kelly Brookes, Jan 28, 2007
    #4
  5. "Larry Tooley" <> wrote in message
    >I am new to C and am using "C How to Program" to get started with my C
    > education. I am working an exercise where I am supposed to use only
    > arithmetic, equity and relational operators, and the "if" statement to
    > determine the largest and smallest of three integers. It seems like there
    > is an easy answer, but I just haven't come up with a solution. It has
    > been a long time since CS101 with Pascal so pardon my ignorance.
    >

    C does things in functions.

    So start by defining your function.

    /*
    max3 - gets the biggest of three integers.
    Params: a, b, c - three integers to test
    Returns: value of the biggest integer.
    */

    int max(int a, int b, int c)
    {
    int answer;

    /* code here */
    return answer;
    }

    Now how do we do the code?

    If we were doing two numbers we could say

    if( a > b)
    answer = a;
    else
    answer = b;

    If we are doing three numbers, the biggest of a,b,c must be either the
    biggest of a, b, or it must be c.
    So effectively we've just got two tests with two numbers.


    If you are not allowed assignment statements then you can write the whole
    lot on one line, using the ?: operator instead of if ... else. It is messy
    but it will show you how to use the operators.
     
    Malcolm McLean, Jan 28, 2007
    #5
  6. Gustavo Rondina <> writes:

    > Larry Tooley <> writes:
    >
    >>determine the largest and smallest of three integers.

    >
    >
    > if A > C then swap them
    > if A > B then swap them
    > if B > C then swap them
    >
    > Now A is the smallest and C is the largest.


    I was going to point out that assignment was not one of the allowed
    operations when I thought of:

    void order3(int a, int b, int c)
    {
    if (a > c) order3(c, b, a);
    else if (a > b) order3(b, a, c);
    else if (b > c) order3(a, c, b);
    else printf("min = %d, max = %d\n", a, c);
    }

    Function calls were not allowed either, of course, but I think they
    get a special exemption in this sort of exercise.

    --
    Ben.
     
    Ben Bacarisse, Jan 28, 2007
    #6
  7. Larry Tooley

    Leo Guest

    Larry Tooley wrote:
    > I am new to C and am using "C How to Program" to get started with my C
    > education. I am working an exercise where I am supposed to use only
    > arithmetic, equity and relational operators, and the "if" statement to
    > determine the largest and smallest of three integers. It seems like there
    > is an easy answer, but I just haven't come up with a solution. It has
    > been a long time since CS101 with Pascal so pardon my ignorance.
    >
    > Thanks for the help.
    >
    > Larry


    #include<stdio.h>

    int max(int a,int b,int c)
    {
    return ((a>b&&a>c)? a:(a>b &&a<c)? c:(b>c)?b:c);
    }

    int min(int a,int b,int c)
    {
    return ((a<b && a<c)?a:(a>b&&a<c)?b:(b<c)?b:c);
    }

    int main(void)
    {
    int a,b,c;
    printf("Enter the three integers");
    scanf("%d %d %d",&a,&b,&c);
    printf("Max:%d\nMin:%d\n",max(a,b,c),min(a,b,c));
    return 0;
    }
     
    Leo, Jan 28, 2007
    #7
  8. On Jan 28, 1:31 am, Larry Tooley <> wrote:
    > I am new to C and am using "C How to Program" to get started with my C
    > education. I am working an exercise where I am supposed to use only
    > arithmetic, equity and relational operators, and the "if" statement to
    > determine the largest and smallest of three integers.


    The following code will find the greatest of 3 integers entered into
    stdin, it is straight-forward and appears to meet your restrictions.
    If the largest value is shared by multiple variables, the first one
    entered wins. The same approach can be used to find the smallest
    integer.

    #include <stdio.h>

    int main (void) {
    int a, b, c;
    printf("Enter 3 integers\n");
    scanf("%d %d %d", &a, &b, &c);

    if (a >= b)
    if (a >= c)
    printf("a is greatest\n");
    else if (b >= c)
    printf("b is greatest\n");
    else
    printf("c is greatest\n");
    else
    if (b >= c)
    printf("b is greatest\n");
    else
    printf("c is greatest\n");

    return 0;
    }

    Robert Gamble
     
    Robert Gamble, Jan 28, 2007
    #8
  9. Larry Tooley

    Thad Smith Guest

    Malcolm McLean wrote:
    > "Larry Tooley" <> wrote in message
    >
    >>I am new to C and am using "C How to Program" to get started with my C
    >>education. I am working an exercise where I am supposed to use only
    >>arithmetic, equity and relational operators, and the "if" statement to
    >>determine the largest and smallest of three integers. It seems like there
    >>is an easy answer, but I just haven't come up with a solution. It has
    >>been a long time since CS101 with Pascal so pardon my ignorance.

    >
    > C does things in functions.
    > So start by defining your function.
    >
    > /*
    > max3 - gets the biggest of three integers.
    > Params: a, b, c - three integers to test
    > Returns: value of the biggest integer.
    > */
    >
    > int max(int a, int b, int c)
    > {
    > int answer;
    >
    > /* code here */
    > return answer;
    > }
    >
    > Now how do we do the code?
    > If we were doing two numbers we could say
    >
    > if( a > b)
    > answer = a;
    > else
    > answer = b;
    >

    ....
    > If you are not allowed assignment statements then you can write the whole
    > lot on one line, using the ?: operator instead of if ... else. It is messy
    > but it will show you how to use the operators.


    You can also use separate return statements:
    if (a > b) return a;
    else return b;

    For 3 values, nested if statements work:
    if (something) {
    if (another condition) {
    return proper_value;
    } else ...

    The result can be found by doing tests and handling each case.

    --
    Thad
     
    Thad Smith, Jan 28, 2007
    #9
  10. Larry Tooley

    CBFalconer Guest

    Robert Gamble wrote:
    > On Jan 28, 1:31 am, Larry Tooley <> wrote:
    >
    >> I am new to C and am using "C How to Program" to get started with
    >> my C education. I am working an exercise where I am supposed to
    >> use only arithmetic, equity and relational operators, and the "if"
    >> statement to determine the largest and smallest of three integers.

    >
    > The following code will find the greatest of 3 integers entered
    > into stdin, it is straight-forward and appears to meet your
    > restrictions. If the largest value is shared by multiple variables,
    > the first one entered wins. The same approach can be used to find
    > the smallest integer.
    >
    > #include <stdio.h>
    >
    > int main (void) {
    > int a, b, c;
    > printf("Enter 3 integers\n");
    > scanf("%d %d %d", &a, &b, &c);
    >
    > if (a >= b)
    > if (a >= c)
    > printf("a is greatest\n");
    > else if (b >= c)
    > printf("b is greatest\n");
    > else
    > printf("c is greatest\n");
    > else
    > if (b >= c)
    > printf("b is greatest\n");
    > else
    > printf("c is greatest\n");
    >
    > return 0;
    > }


    A sin. Using scanf without checking the return value. Suggested
    improvement:

    #include <stdio.h>

    int main (void) {
    int a, b, c;

    printf("Enter 3 integers\n");
    if (3 != scanf("%d %d %d", &a, &b, &c))
    puts("You didn't listen");
    else {
    if (a >= b)
    if (a >= c) putchar('a');
    else if (b >= c) putchar('b');
    else putchar('c');
    else if (b >= c) putchar('b');
    else putchar('c');
    puts(" is greatest");
    }
    return 0;
    }

    which will also generate considerably less object code. This also
    shows up the even shorter:

    #include <stdio.h>

    int main (void) {
    int a, b, c;

    puts("Enter 3 integers");
    if (3 != scanf("%d %d %d", &a, &b, &c))
    puts("You didn't listen");
    else {
    if ((a >= b) && (a >= c)) putchar('a');
    else if (b >= c) putchar('b');
    else putchar('c');
    puts(" is greatest");
    }
    return 0;
    }

    My point being that using the first implementation that occurs to
    you may not be optimum. Stand back and look at your code for
    insecurities and better expressions. 22 lines has become 16, even
    with input checking. It is easier to examine the above code and
    satisfy yourself that it is correct because there are no longer
    multiple paths to a single conclusion. You can easily see that
    adding 4 similar lines after "puts(" is greatest");" will also dig
    out the smallest.

    Routinely doing this will give you practice, and you will soon be
    writing the clearer and more effective code in the first place.

    I got rid of printf in favor of puts and putchar, because the added
    abilities of printf are never used. This can often significantly
    reduce the size of the end program and improve its speed. It also
    means you can't get the type signifiers wrong.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Jan 29, 2007
    #10
  11. Larry Tooley

    Richard Bos Guest

    Kelly Brookes <> wrote:

    > Larry Tooley wrote:
    > > I am new to C and am using "C How to Program" to get started with my C
    > > education. I am working an exercise where I am supposed to use only
    > > arithmetic, equity and relational operators, and the "if" statement to
    > > determine the largest and smallest of three integers. It seems like there
    > > is an easy answer, but I just haven't come up with a solution. It has
    > > been a long time since CS101 with Pascal so pardon my ignorance.


    > int max(int a,int b,int c)
    > {
    > return ((a>b&&a>c)? a:(a>b &&a<c)? c:(b>c)?b:c);
    > }
    >
    > int min(int a,int b,int c)
    > {
    > return ((a<b && a<c)?a:(a>b&&a<c)?b:(b<c)?b:c);
    > }


    Even simpler:

    int max3(int a, int b, int c)
    {
    return (a>b && a>c)? a: (b>c)? b: c;
    }

    int min3(int a, int b, int c)
    {
    return (a<b && a<c)? a: (b<c)? b: c;
    }

    If a isn't larger than both others, either b or c is. If, under those
    circumstances, b is larger than c, it must be the largest.

    (Depending on the system, changing > to >= may be more efficient. It's
    not likely to be a measurable difference unless you're doing gazillions
    of comparisons; in which case, measure, don't pre-optimise; and if it
    doesn't matter, go for legibility.)

    Richard
     
    Richard Bos, Jan 29, 2007
    #11
  12. Larry Tooley

    Leo Guest

    CBFalconer wrote:


    > A sin. Using scanf without checking the return value. Suggested
    > improvement:


    can you please explain the line that follows?how do we check for the
    return to to be say float and not a char during input?

    if (3 != scanf("%d %d %d", &a, &b, &c))
    > puts("You didn't listen");
     
    Leo, Jan 29, 2007
    #12
  13. Larry Tooley

    Leo Guest

    CBFalconer wrote:


    > A sin. Using scanf without checking the return value. Suggested
    > improvement:


    can you please explain the line that follows?how do we check for the
    return to to be say float and not a char during input?

    if (3 != scanf("%d %d %d", &a, &b, &c))
    > puts("You didn't listen");
     
    Leo, Jan 29, 2007
    #13
  14. Larry Tooley

    CBFalconer Guest

    Leo wrote:
    > CBFalconer wrote:
    >
    >> A sin. Using scanf without checking the return value. Suggested
    >> improvement:

    >
    > can you please explain the line that follows?how do we check for
    > the return to to be say float and not a char during input?
    >
    >> if (3 != scanf("%d %d %d", &a, &b, &c))
    >> puts("You didn't listen");


    Just read the documentation. For example, n869 says:

    7.19.6.4 The scanf function

    Synopsis

    [#1]
    #include <stdio.h>
    int scanf(const char * restrict format, ...);

    Description

    [#2] The scanf function is equivalent to fscanf with the
    argument stdin interposed before the arguments to scanf.

    Returns

    [#3] The scanf function returns the value of the macro EOF
    if an input failure occurs before any conversion.
    Otherwise, the scanf function returns the number of input
    items assigned, which can be fewer than provided for, or
    even zero, in the event of an early matching failure.


    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews
     
    CBFalconer, Jan 29, 2007
    #14
  15. On Jan 28, 9:14 pm, CBFalconer <> wrote:
    > Robert Gamble wrote:
    > > On Jan 28, 1:31 am, Larry Tooley <> wrote:

    >
    > >> I am new to C and am using "C How to Program" to get started with
    > >> my C education. I am working an exercise where I am supposed to
    > >> use only arithmetic, equity and relational operators, and the "if"
    > >> statement to determine the largest and smallest of three integers.

    >
    > > The following code will find the greatest of 3 integers entered
    > > into stdin, it is straight-forward and appears to meet your
    > > restrictions. If the largest value is shared by multiple variables,
    > > the first one entered wins. The same approach can be used to find
    > > the smallest integer.

    >
    > > #include <stdio.h>

    >
    > > int main (void) {
    > > int a, b, c;
    > > printf("Enter 3 integers\n");
    > > scanf("%d %d %d", &a, &b, &c);

    >
    > > if (a >= b)
    > > if (a >= c)
    > > printf("a is greatest\n");
    > > else if (b >= c)
    > > printf("b is greatest\n");
    > > else
    > > printf("c is greatest\n");
    > > else
    > > if (b >= c)
    > > printf("b is greatest\n");
    > > else
    > > printf("c is greatest\n");

    >
    > > return 0;
    > > }

    > A sin. Using scanf without checking the return value.


    The focus was on solving the OP's problem given the provided
    restrictions while trying to convey a simple concept, but I agree with
    you in principle and I could have thrown in a simple check.

    > Suggested
    > improvement:
    >
    > #include <stdio.h>
    >
    > int main (void) {
    > int a, b, c;
    >
    > printf("Enter 3 integers\n");
    > if (3 != scanf("%d %d %d", &a, &b, &c))
    > puts("You didn't listen");
    > else {
    > if (a >= b)
    > if (a >= c) putchar('a');
    > else if (b >= c) putchar('b');
    > else putchar('c');
    > else if (b >= c) putchar('b');
    > else putchar('c');
    > puts(" is greatest");
    > }
    > return 0;
    >
    > }which will also generate considerably less object code.


    Which really wasn't the goal.

    > This also
    > shows up the even shorter:
    >
    > #include <stdio.h>
    >
    > int main (void) {
    > int a, b, c;
    >
    > puts("Enter 3 integers");
    > if (3 != scanf("%d %d %d", &a, &b, &c))
    > puts("You didn't listen");
    > else {
    > if ((a >= b) && (a >= c)) putchar('a');
    > else if (b >= c) putchar('b');
    > else putchar('c');
    > puts(" is greatest");
    > }
    > return 0;
    > }


    Probably the most obvious solution but the use of && doesn't meet the
    OP's requirement of using only "arithmetic, equity and relational
    operators, and the if statement".

    > My point being that using the first implementation that occurs to
    > you may not be optimum.


    Again, optimization wasn't the goal here, providing a solution that
    clearly demonstrates the concept the given the requirements was and I
    think the solution provided did that.

    [diatribe about writing optimized code snipped]

    I would personally worry about trying to comprehend the requirements
    before worrying about unwarranted optimizations.

    Robert Gamble
     
    Robert Gamble, Jan 29, 2007
    #15
  16. Larry Tooley

    Larry Tooley Guest

    On Sun, 28 Jan 2007 12:10:08 -0700, Thad Smith wrote:

    > Malcolm McLean wrote:
    >> "Larry Tooley" <> wrote in message
    >>
    >>>I am new to C and am using "C How to Program" to get started with my C
    >>>education. I am working an exercise where I am supposed to use only
    >>>arithmetic, equity and relational operators, and the "if" statement to
    >>>determine the largest and smallest of three integers. It seems like there
    >>>is an easy answer, but I just haven't come up with a solution. It has
    >>>been a long time since CS101 with Pascal so pardon my ignorance.

    >>
    >> C does things in functions.
    >> So start by defining your function.
    >>
    >> /*
    >> max3 - gets the biggest of three integers.
    >> Params: a, b, c - three integers to test
    >> Returns: value of the biggest integer.
    >> */
    >>
    >> int max(int a, int b, int c)
    >> {
    >> int answer;
    >>
    >> /* code here */
    >> return answer;
    >> }
    >>
    >> Now how do we do the code?
    >> If we were doing two numbers we could say
    >>
    >> if( a > b)
    >> answer = a;
    >> else
    >> answer = b;
    >>

    > ...
    >> If you are not allowed assignment statements then you can write the whole
    >> lot on one line, using the ?: operator instead of if ... else. It is messy
    >> but it will show you how to use the operators.

    >
    > You can also use separate return statements:
    > if (a > b) return a;
    > else return b;
    >
    > For 3 values, nested if statements work:
    > if (something) {
    > if (another condition) {
    > return proper_value;
    > } else ...
    >
    > The result can be found by doing tests and handling each case.


    Thanks Thad. I think the nested approach is what they were looking for.
    The parameters were very restrictive.
     
    Larry Tooley, Jan 29, 2007
    #16
  17. Groovy hepcat Larry Tooley was jivin' on Sun, 28 Jan 2007 01:31:37
    -0500 in comp.lang.c.
    Newbie Problem's a cool scene! Dig it!

    >I am new to C and am using "C How to Program" to get started with my C
    >education. I am working an exercise where I am supposed to use only
    >arithmetic, equity and relational operators, and the "if" statement to
    >determine the largest and smallest of three integers. It seems like there
    >is an easy answer, but I just haven't come up with a solution. It has
    >been a long time since CS101 with Pascal so pardon my ignorance.


    Divide and conquer. That's the first principle of programming. Break
    a large problem down into a smaller one. You determine the largest of
    three numbers by first finding the largest of two of them, then
    finding the largest of the result of that operation and the last
    number. We'll express the largest of two values as MAX(x,y) where x
    and y represent the two values.
    So, say you have numbers labelled a, b and c. To find the largest of
    these three, you first find MAX(a,b). Then you find MAX(MAX(a,b),c).
    In fact, C's syntax makes it easy to do that in one go. All you need
    is a function that can return the larger of two values. This, of
    course, is trivial.

    int max(int x, int y)
    {
    if(x >= y)
    {
    return x;
    }
    else
    {
    return y;
    }
    }

    #include <stdio.h>

    int main(void)
    {
    int a = 107, b = 42, c = 23;
    int big = max(max(a, b), c);

    printf("The biggest of %d, %d and %d is %d.\n", a, b, c, big);

    return 0;
    }

    The above code is quite self explanitory. Notice the use of the max()
    function. If max(a, b) returns the largest of a and b, then max(max(a,
    b), c) returns the largest of (the largest of a and b) and c; which,
    of course, gives us the largest of the three numbers. What could be
    simpler?

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Jan 31, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Id0x
    Replies:
    4
    Views:
    1,222
    Erik Max Francis
    Jul 21, 2003
  2. JohnE

    newbie with newbie questions

    JohnE, Aug 17, 2009, in forum: ASP .Net
    Replies:
    3
    Views:
    536
    Gregory A. Beamer
    Aug 17, 2009
  3. Jerry C.
    Replies:
    8
    Views:
    279
    Uri Guttman
    Nov 23, 2003
  4. Kruno Saho
    Replies:
    0
    Views:
    170
    Kruno Saho
    Apr 7, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    147
    Dave Angel
    Apr 7, 2013
Loading...

Share This Page