Thread: Very basic programming help please

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Very basic programming help please

    I am trying to make a program that will state the smallest number entered, with only 2 numbers it works.
    Code:
        if ( x > y )
        cout << "Smallest: " << x << endl;
        
        if ( y > x )
        cout << "Smallest: " << y << endl;
    With 3 or more numbers it prints all the values as the smallest as if quoting the values entered.
    Code:
        if ( x > y , z )
        cout << "Smallest: " << x << endl;
        
        if ( y > x , z )
        cout << "Smallest: " << y << endl;
    
        if ( z > y , x )
        cout << "Smallest: " << z << endl;
    I have only just started leaning c++ so if someone could please help it would be apprieciated. I have tried to work this out myself but must be missing something really simple.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The easiest way to do this is to store your numbers in an array, that way you can cycle through them with only one identifier.
    Code:
    int arry[10];
    
    // Read in 10 numbers
    
    int lowest = arry[0];  // Just assign it to whatever value starts the array
    for(int i = 1; i < 10; i++) {  // You can ignore the 0 index since it's already considered lowest
       if (arry[i] < lowest)
           arry[i] = lowest;
    }
    ...and there you go. By the way, the comma operator isn't used like that, and that's where you were probably having the most confusion. Other than that, you should consider using 'else if' and 'else'. There is no point in checking if y is greater than x if you already know x is greater than y.
    Last edited by SlyMaelstrom; 08-13-2006 at 05:29 AM.
    Sent from my iPadŽ

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This doesn't work even with 2 numbers: if x is smallest, then x > y shouldn't be true.
    Besides that, the code doesn't handle cases where x == y.

    As to three numbers, you should evaluate multiple conditions using the "and" - && operator: if (x < y && x < z). Again you should decide, how you are going to handle cases where the two (or three) smallest values are equal.

    If you want to find the smallest value in a larger collection, it's time to look for arrays.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Thanks for your help, exactly what I needed to know.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You can also use a container instead of an array. The simplest on this case would be a std::set. You could know at any time which is the lowest input entered by simply reading the first element of the set.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Mario,

    Stuff like this looks like basic homework stuff, which means use of containers that they haven't learned will get you docked points depending on the teacher. Though if this is just a person playing with the language, nevermind me...

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well, you are probably right
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    I'm trying to learn through a book so it is homework in a way but not homework that gets checked by teachers. It's good to learn different ways to achive the same result. Thanks for the help

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    There's a very simple way of finding the smallest value in a list of any size. First assume the first value is the smallest. If the next value is smaller, then use the next value, otherwise keep the current value. Try it with pencil-and-paper on small test arrays and convince yourself it works.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a basic Linux web page designer (editor)
    By BobS0327 in forum Tech Board
    Replies: 5
    Last Post: 12-30-2006, 05:30 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM