Thread: Need to develop a code to obtain the two smallest integers.

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Unhappy Need to develop a code to obtain the two smallest integers.

    Need to type a C program to input an unspecified number of integers and to determine and print the two smallest values. Im only allowed to use this statements: sentinel, while, if, if..else.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    So write some code and post back (and include the code) if you have problems.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To confirm: this thread is about C, not C++?
    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

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    This is about C

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Thats the problem with the code. I get really confused with the while and if..else and doesnt work at all

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by edwinhndz17
    This is about C
    Thread moved to C programming forum.

    Quote Originally Posted by edwinhndz17
    Thats the problem with the code. I get really confused with the while and if..else and doesnt work at all
    Still, show what you tried. Perhaps we can help you to fix 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

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thats the problem with the code. I get really confused with the while and if..else and doesnt work at all
    So *post* the code so that we can see where you are at on it.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Here it is. I know is wrong but i dont know how to fix it. Im studying in the university, Computer Engineering, im in second year and this is new to me. And my proffesor really stink explaining this to me.

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* header for function system */
    int main( void )
    {
    int counter, smallest, ssmallest;
    
    while( counter=!0){
    printf( "Enter integer: ");
    scanf( "%d", &smallest);
    }
    if( counter<smallest){
    scanf( "%d", &smallest);
    }
    else
    if(smallest<ssmallest){
    scanf( "%d");
    }
    else
    if(secondsmallest<smallest){
    printf( "%d are the smallest numbers",x, y);
    scanf( "%d,%d", &x, &y);
    }
    system ("pause");
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Im trying to use it with a sentinel, because i want to input an unspecific number of integers, but it doesnt work at all

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    First of all, it's imperative that you learn to indent you code properly. Here's an example:

    Code:
    #include <stdio.h>
    #include <stdlib.h> /* header for function system */
    
    int main( void )
    {
        int counter, smallest, ssmallest;
    
        while( counter =! 0 ) //  should be !=
        {
            printf( "Enter integer: " );
            scanf( "%d", &smallest );
        }
        if( counter < smallest)
        {
            scanf( "%d", &smallest );
        }
        else if( smallest < ssmallest )
        {
        /*
            careful - every format must be matched 
            with the address of some variable 
        */
            scanf( "%d" ); 
        }
        else if( secondsmallest < smallest ) // secondsmallest not declared
        {
            printf( "%d are the smallest numbers",x, y ); // x, y not declared
            scanf( "%d,%d", &x, &y );
        }
        system( "pause" );
        return 0;
    }
    So fix those errors first. Now then, the idea here is that most of the work will be done inside of the loop. The basic flow should be:

    1) declare 'smallest', set to INT_MIN
    2) declare an input variable, say 'input'
    3) loop
    3b) read 'input'
    3c) if 'input' equals some sentinal value (eg: 0, -1), break out of the loop
    3d) if 'input' is less than 'smallest' then reassign 'smallest'
    4) now outside of loop, print 'smallest'

    EDIT: So I just realized that you really do need to find the *two* smallest numbers. Still, get it working with one number first before moving forward with the advanced case
    Last edited by Sebastiani; 09-12-2009 at 11:40 AM.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Can u fix the code for me? That way i get confused. I will be very thankful if u fix the code and then reply it back. THANKS

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, we can't. This is pure simple logic, the basic stepping stone for all programmers.
    Try the logic first. How would we logically go about doing this task?
    If it were a real life situation, for example, how would you do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by edwinhndz17 View Post
    Can u fix the code for me? That way i get confused. I will be very thankful if u fix the code and then reply it back. THANKS
    This is *your* homework. You need to write the code. Now then, why don't you just start from scratch? Keep the header includes but delete everything inside of main, and just start over. If you get stuck post back here with questions and you current code.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a hint: You don't actually do anything inside your loop with your numbers. You just read one, then read another, then another... until you enter zero, which has overwritten whatever it was you did right before that anyway.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. How to obtain more key code in curses ?
    By intmail in forum Linux Programming
    Replies: 1
    Last Post: 07-08-2006, 12:21 PM
  4. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

Tags for this Thread