Thread: Finding the lowest number of a certain input

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Finding the smallest integer

    Hi,

    I've just started programming a few weeks ago and i have a problem which involves me finding the lowest number. That is, if the input is 123 12 1342 1 the program should print out the lowest number, ie. 1 in this example.


    Could anyone please lend me some advice on how to go about

    solving the problem. I have been able to find the min of set

    amount of numbers (like the minimum of 2/3/4 numbers) , but i have no idea how to go about solving

    the problem where the size of the numbers inputted varies.

    sorry in advance if some of it doesnt make sense....its a bit hard to explain.

    Thanks in advance,
    Theorbb
    Last edited by theorbb; 04-15-2006 at 12:49 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Keep a variable called 'currentSmallestNumber'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    /*assume that an array with some ten elements in different order.
    finding the smallest among them*/
    
    small <- first element of array
    for i=1 till array size - 1 
        if small > ith element of array then
            small <- ith element of array
        end if
    loop
    
    print small
    ssharish2005

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    could you please expand?....i dont really know what you mean, sorry.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    my previous post was all about the pesudo code. u can actually convert that into the code. i think u dint get any of those words. let me explain u

    Code:
     assume array of 6  elements [ 10 3 5 1 29 20]
    
     assume first elemenst of the array is the smalest of all elements. 
     small <- 10;
    
     compare the small element with other elements of the array.
     
     for(i=1//because first is already small assumption;i<6;i++)
        compare the small with all other elements
        if(small > ith element)
        store the ith element in the small overwritting the older value
    
        next time the loop goes back it check with the updated small value
    hope this helps u

    ssharish2005

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    Hi!

    I'm new to this too but it seemed like a good practice exercise, so I toyed around with it a bit and this is what I came up with, just using the basics.

    Code:
    #include <stdio.h>
    
    int n;//how many numbers
    int s=999999;//smallest number (This number must be bigger than the biggest number to be entered.
    
    int main()
    
    {
    
    int x;//inputted number
    int y;//for loop counter
    int z[n];
    
    n=1;
    
          for (y=0;y<n;y++) {
              
                            scanf("%d", &x);//Input x here with scanf if entering numbers one by one.
                                            //Input 0 to break the loop
              
                                           if (x != 0) {  //If loop is not broken...
                                                 
                                                       z[n]=x;//Set number entered to z[n]...
              
                                                       if  (z[n]<s) {//If new number is smaller than smallest...
                  
                                                                     s=z[n];//New number becomes smallest...
                  
                                                                     }
                                                                     
                                                       n++;//n is incremented by one allowing the loop to continue.              
                                        
                                                       }
                                                       
                            }
                            
           printf("%d is the smallest number.", s);//Once a zero is entered, smallest number is displayed.
           getchar();
           getchar();                          
                      
    }
    It's not perfect, but provided the user is entering the numbers it works... More or less.

    Anyway it was fun bending my mind around all the variables.

    And now it's bedtime.



    -James-

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > could you please expand?
    Well no, not until you provide some code to show what you can actually achieve yourself.

    I mean, just reading input and printing it out would be a start.
    Then move onto comparing the current number with the previous number (say).

    Kazmeister's answer shows the basic idea, but has too many bugs to be actually run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Quote Originally Posted by Salem
    > could you please expand?
    Well no, not until you provide some code to show what you can actually achieve yourself.
    well sorry for asking Mr.Poseidon - "God of the C".....because i can do SOOO much with the knowledge of keeping a variable called 'currentSmallestNumber'.


    But, thanks ssharish2005 and Kazmeister, i think im getting the logic behind how to go about the problem.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're an idiot if you can't figure out from "Keep a variable called 'currentSmallestNumber'" that you need to oh, I don't know... STORE THE SMALLEST NUMBER.

    You fail at basic problem solving. You also fail common sense.


    Quzah.
    Last edited by quzah; 04-16-2006 at 09:01 AM. Reason: Notification of failure.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you should post next time what you've actually managed to do yourself if you want some meaningful help.

    Sure, anyone can post lots of waffle about "I'm a noob" and "I'm stuck", and sure someone will happen along a spoon-feed you the answer, and you'll go away happy and think you've learnt something.

    But you won't have really....

    Well that depends, are you actually going to post your answer for all to see, or are you just going to take the answer and hand it in - bugs and all?

    Nah, you won't do that, you don't have the stones to post any of your code in a forum - hell, I doubt you've ever written any yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    4
    is this the answer?

    Code:
    #inlude <stdio.h>
    int main ()
    {
           int counter;
           int number[5] = {10, 21, 29,78,3};//your input can be different, then try to convert codes
           int min;
    
           min = number[0];//or initialize to a very big number
           for ( counter = 0; counter < 5; counter++ ){
                  if ( number[i] < min ){
                         min = number[i];
                 }
          }
          printf ("%d\n", min);
                
    }

  12. #12
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Close, but a few errors. Actually, one compile error, and one optimization tip. For the sake of the original poster, I won't point them out. If he can't identify the problem, then he should take up something else. The logic you have is correct, though.
    Last edited by SlyMaelstrom; 04-16-2006 at 09:37 AM.
    Sent from my iPadŽ

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by quzah
    You're an idiot if you can't figure out from "Keep a variable called 'currentSmallestNumber'" that you need to oh, I don't know... STORE THE SMALLEST NUMBER.

    You fail at basic problem solving. You also fail common sense.


    Quzah.
    You seem bitter. Is there anything you wish to share with the group?

  14. #14
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by SlyMaelstrom
    Close, but a few errors. Actually, one compile error, and one optimization tip. For the sake of the original poster, I won't point them out. If he can't identify the problem, then he should take up something else. The logic you have is correct, though.
    I counted 5 errors. Even after I corrected them, the code doesn't work.

  15. #15
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Okay, make that 6 errors. After I corrected them all, I got the correct output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. help with finding lowest number entered
    By volk in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2003, 01:21 PM