Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Finding smaller number of 2 numbers inputted by user

Options
  • 21-09-2017 3:24pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭


    Recently started a C# course, is there a more effective way of finding which int value is the smaller of the two that are inputted by a user? the program works ok but I can't but feel there's a better way of doing it

    3favVik.png


«13456

Comments

  • Registered Users Posts: 17,502 ✭✭✭✭Mr. CooL ICE


    Well for a start, you're printing out the variable name rather than the value. I'm not sure if that's intended.

    Also, is this example supposed to take into account num1 and num2 being equal?


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    I hope I won't get banned by instructing a mod how to post code :-)
    but it's easier to read it if you post the code with:
    ""
    ""
    
    You have 3 options:
    a>b
    a=b
    a<b
    You can do checks using "if" as you did - that's the obvious choice. You could also  sub a from b 
    [code]c = a - b
    
    and use case switch [1] for the check. Is it better? Probably not :D

    [1] https://en.wikipedia.org/wiki/Switch_statement


  • Registered Users Posts: 1,717 ✭✭✭Raging_Ninja


    You're learning from scratch, looks like the basics of if/else statements, assignment operations and equality tests. Handle the scenario for when the two numbers are equal and you ought to be done.

    Efficiency comes later on, once you've learned enough to be able to understand why an alternative might be more efficient.


  • Registered Users Posts: 6,013 ✭✭✭Talisman


    When starting out keep your code as simple as possible use if-else. As you become more familiar with programming you will discover the ?: ternary operator.
    class SimpleConditional
    {
        static void Main()
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
    
            // simple if-else construction
            if (a < b)
                Console.WriteLine("A is smaller.");
            else
                Console.WriteLine("B is smaller.");
    
            // ?: conditional operator
            Console.WriteLine( (a < b) ? "A is smaller." : "B is smaller." );
        }
    }
    


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Dont forget to account for when the numbers are equal.
        if(a < b)
            Console.WriteLine("a is smaller");
        else if(b < a)
            Console.WriteLine("b is smaller");
        else
            Console.WriteLine("a & b are equal");
    
    

    As a side note there is a method in the Math class which returns the higher of two numbers but you're better off at the moment writing the necessary code yourself to learn.
        int Higher = Math.Max(100, 200);
    
        // In this case the Higher variable will be set to 200. 
    
    


  • Advertisement
  • Registered Users Posts: 6,013 ✭✭✭Talisman


    Dont forget to account for when the numbers are equal.
        if(a < b)
            Console.WriteLine("a is smaller");
        else if(b < a)
            Console.WriteLine("b is smaller");
        else
            Console.WriteLine("a & b are equal");
    
    
    You don't necessarily have to deal with the case of a = b. If the question is "Is a smaller than b?" then there are only two possible answers : Yes (1) or No (0).

    If the answer is "No" then b is smaller or worst case is equal to a. The a equals b check is a special case which you only need to check in the event that you have to handle that scenario. In the scope of the question "Is a smaller than b?" the special case isn't a concern.


  • Registered Users Posts: 7,500 ✭✭✭BrokenArrows


    Talisman wrote: »
    You don't necessarily have to deal with the case of a = b. If the question is "Is a smaller than b?" then there are only two possible answers : Yes (1) or No (0).

    If the answer is "No" then b is smaller or worst case is equal to a. The a equals b check is a special case which you only need to check in the event that you have to handle that scenario. In the scope of the question "Is a smaller than b?" the special case isn't a concern.

    haha.
    The life long problem of a software developer.

    Do you write to the exact specification or do you enhance it to take into account something the spec writer missed :D


  • Registered Users Posts: 6,013 ✭✭✭Talisman


    Do you write to the exact specification or do you enhance it to take into account something the spec writer missed :D
    That's where acceptance tests save your ass.


  • Registered Users Posts: 24,281 ✭✭✭✭lawred2


    Talisman wrote: »
    When starting out keep your code as simple as possible use if-else. As you become more familiar with programming you will discover the ?: ternary operator.
    class SimpleConditional
    {
        static void Main()
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
    
            // simple if-else construction
            if (a < b)
                Console.WriteLine("A is smaller.");
            else
                Console.WriteLine("B is smaller.");
    
            // ?: conditional operator
            Console.WriteLine( (a < b) ? "A is smaller." : "B is smaller." );
        }
    }
    

    I spot a bit of a bug there horse


  • Registered Users Posts: 24,281 ✭✭✭✭lawred2


    Talisman wrote: »
    You don't necessarily have to deal with the case of a = b. If the question is "Is a smaller than b?" then there are only two possible answers : Yes (1) or No (0).

    If the answer is "No" then b is smaller or worst case is equal to a. The a equals b check is a special case which you only need to check in the event that you have to handle that scenario. In the scope of the question "Is a smaller than b?" the special case isn't a concern.

    I think the scope of the OPs question was which of the two is the smaller - ergo you do need to check for them being equal..


  • Advertisement
  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Thanks for all the replies.

    Well for a start, you're printing out the variable name rather than the value. I'm not sure if that's intended.

    Also, is this example supposed to take into account num1 and num2 being equal?


    The full exercise question was

    Prompt the user to enter two int values. your program should find the smaller of the two int values and output the number


    PrzemoF wrote: »
    I hope I won't get banned by instructing a mod how to post code :-)


    No fear of that happening, I'm not a mod of this forum :).

    Talisman wrote: »
    When starting out keep your code as simple as possible use if-else. As you become more familiar with programming you will discover the ?: ternary operator.
    class SimpleConditional
    {
        static void Main()
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
    
            // simple if-else construction
            if (a < b)
                Console.WriteLine("A is smaller.");
            else
                Console.WriteLine("B is smaller.");
    
            // ?: conditional operator
            Console.WriteLine( (a < b) ? "A is smaller." : "B is smaller." );
        }
    }
    

    When we were doing user input the other day he never showed us this int a = Convert.ToInt32(Console.ReadLine()); Is that basically converting a string or char variable too an int?


  • Registered Users Posts: 6,013 ✭✭✭Talisman


    When we were doing user input the other day he never showed us this int a = Convert.ToInt32(Console.ReadLine()); Is that basically converting a string or char variable too an int?
    Yes the Convert.ToInt32(value) method converts a string value to a 32-bit integer. There is also the Int32.Parse(value) method.


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    I'll post more code soon for y'all to dissect. :pac:


  • Registered Users Posts: 22,015 ✭✭✭✭Esel


    Any need to check that the data given are numbers?

    Not your ornery onager



  • Registered Users Posts: 6,013 ✭✭✭Talisman


    I'll post more code soon for y'all to dissect. :pac:
    You should install an offline documentation browser, it will help you discover the features of the language more easily and take a bit of frustration out of the learning process.

    Windows / Linux : Zeal (Open Source)
    Mac OS : Dash (Commercial app)


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Really struggled with the what we were doing today, we were combining if statements with loops and had too for example print out all the even number between 1 and 100 and the same for odd numbers and then print only the numbers that could be divided by 3, then we had too do the same for strings, I thought my head was gonna explode :-/.


  • Registered Users Posts: 24,281 ✭✭✭✭lawred2


    Really struggled with the what we were doing today, we were combining if statements with loops and had too for example print out all the even number between 1 and 100 and the same for odd numbers and then print only the numbers that could be divided by 3, then we had too do the same for strings, I thought my head was gonna explode :-/.

    look up the modulus operator


  • Registered Users Posts: 2,462 ✭✭✭SweetCaliber


    Really struggled with the what we were doing today, we were combining if statements with loops and had too for example print out all the even number between 1 and 100 and the same for odd numbers and then print only the numbers that could be divided by 3, then we had too do the same for strings, I thought my head was gonna explode :-/.

    As said above, modulus would be your best friend for those three tasks.
    int[] numbers = new int[101];
    
    // Populate the array (faster than using Enumerable)
    for (int i = 1; i < 101; i++) {
     numbers[i] = i;
    }
    
    
    // Get even numbers in array.
    Console.WriteLine("List of even numbers: ");
    for (int i = 1; i < numbers.Length; i++) {
    
     // Check if modulus = 0
     if (numbers[i] &#37; 2 == 0) {
      Console.WriteLine(numbers[i]);
     }
    
    }
    
    
    // Get odd numbers in array (same as even except modulus != 0)
    Console.WriteLine("List of odd numbers: ");
    for (int i = 1; i < numbers.Length; i++) {
    
     // Check if modulus = 0
     if (numbers[i] % 2 != 0) {
      Console.WriteLine(numbers[i]);
     }
    
    }
    
    // Get numbers that only divide by 3 (modulus 3 = 0)
    Console.WriteLine("List of numbers divisible by 3: ");
    for (int i = 1; i < numbers.Length; i++) {
    
     // Check if modulus = 0
     if (numbers[i] % 3 == 0) {
      Console.WriteLine(numbers[i]);
     }
    
    }
    
    


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    Thanks lads much appreciated.


  • Closed Accounts Posts: 2,910 ✭✭✭begbysback


    Really struggled with the what we were doing today, we were combining if statements with loops and had too for example print out all the even number between 1 and 100 and the same for odd numbers and then print only the numbers that could be divided by 3, then we had too do the same for strings, I thought my head was gonna explode :-/.

    I would always recommend keeping it simple - first print out all the numbers, then work out how to print even/odd - if you can attempt this then you will find there are functions which make it easier as you progress, but by then your problem solving will improve as well as your coding...


  • Advertisement
  • Registered Users Posts: 1,931 ✭✭✭PrzemoF


    Really struggled with the what we were doing today, we were combining if statements with loops and had too for example print out all the even number between 1 and 100 and the same for odd numbers and then print only the numbers that could be divided by 3, then we had too do the same for strings, I thought my head was gonna explode :-/.

    That's the feeling of new neurons being formed quickly inside your brain! :-)

    As begbysback wrote: do no try to do it all in one go. Try to split the task.

    Do you draw algorithms as part of the lesson? [1]

    [1] https://en.wikipedia.org/wiki/Algorithm


  • Registered Users Posts: 869 ✭✭✭moycullen14


    Thanks lads much appreciated.

    On the bright side, once you have this done, you'll probably be able for the FIZZ BUZZ test, that apparently most programmers can't do.

    http://wiki.c2.com/?FizzBuzzTest


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    On the bright side, once you have this done, you'll probably be able for the FIZZ BUZZ test, that apparently most programmers can't do.

    http://wiki.c2.com/?FizzBuzzTest

    Was just going to mention this. Fizz Buzz will help in understanding a lot of this


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    PrzemoF wrote: »
    That's the feeling of new neurons being formed quickly inside your brain! :-)

    As begbysback wrote: do no try to do it all in one go. Try to split the task.

    Do you draw algorithms as part of the lesson? [1]

    [1] https://en.wikipedia.org/wiki/Algorithm

    We don't no but I can how helpful that would be and will give it a go.

    We were learning about methods today, i'll post up some code tomorrow.


  • Registered Users Posts: 24,281 ✭✭✭✭lawred2


    Thanks lads much appreciated.

    On the bright side, once you have this done, you'll probably be able for the FIZZ BUZZ test, that apparently most programmers can't do.

    http://wiki.c2.com/?FizzBuzzTest

    Most new programmers


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    At the start, some flow charts may help you to visualise what you are trying to achieve before you write some code. Some people can see this immediately, some can't. Either way its a helpful tool.

    Try to avoid screen shots when posting code, use the code tags, so
    its easier for us to read / copy paste into a dev environment
    
    Makes it easier for us


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    At the start, some flow charts may help you to visualise what you are trying to achieve before you write some code. Some people can see this immediately, some can't. Either way its a helpful tool.

    Try to avoid screen shots when posting code, use the code tags, so
    its easier for us to read / copy paste into a dev environment
    
    Makes it easier for us

    Will do .


  • Closed Accounts Posts: 2,910 ✭✭✭begbysback


    Probably not ur issue - but u have = 21 twice, so both conditions are true for 21


    Also when using if, best practice is to cover all eventualities - this can be achieved by inserting one more else for greater than last number - just output "too old to drive"


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,011 Mod ✭✭✭✭Sephiroth_dude


    How do I pass values from one block of if statments too another ? I know methods would be better for this but wanna try it this way

    namespace ConsoleApplication11
    {
        class Program
        {
            static void Main(string[] args)
            {
               
               double discount1 = 0.05;
               double discount2 = 0.10;
               double discount3 = 0.15;
               double discount4 = 0.20;
                double discount5 = 0.25;
               double additionalCharge = 0.15;
               double additionalCharge2 = 0.10;
               double additionalCharge3 = 0.22;
               int carEngine1 = 2000;
               int carEngine2 = 3000;
               int premium = 800;
               
    
    
               Console.WriteLine("Please enter your Age: ");
                int driverAge = int.Parse(Console.ReadLine());
               
                
                if (driverAge >= 17 && driverAge < 21)
                {
                    Console.WriteLine("Your insurance premium is " + (additionalCharge * premium + premium));
                }
            
                else if (driverAge >=21 && driverAge < 30)
                {
                   Console.WriteLine("Your insurance premium is: " + (additionalCharge2 * premium + premium));
                 }
            
                else if(driverAge >= 30 && driverAge < 80)
                {
                Console.WriteLine("You have no additional charge and your premium is 800.");
                
                }
                else if (driverAge >= 80)
                {
                    Console.WriteLine("Your insurance premium is: " + (additionalCharge3 * premium + premium));
                
                }
    
                Console.WriteLine("Please enter the number of years you have been driving without claiming: ");
                int claim = int.Parse(Console.ReadLine());
    
                if (claim == 1)
                {
                    Console.WriteLine("You get a discount of 5%! " + (premium * discount1));
                    
                }
    
                else if (claim == 2)
                {
                 Console.WriteLine("You get a discount of 10% " + (premium*discount2));
                
                }
    
                else if (claim == 3)
                {
                    Console.WriteLine("You get a discount of 15% " + (premium*discount3));
                
                }
    
                else if (claim == 4)
                {
                    Console.WriteLine("You get a discount of 20% " + (premium*discount4));
    
                }
                else if (claim == 5)
                {
    
                    Console.WriteLine("You get a discount of 25% " + (premium * discount5));
    



    Basically I want the program too remember the choice made up in the first block of if statments and then too be applied too the next block if that makes sense :o .


  • Advertisement
  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    Not quite sure I understand the question. Since everything is in the main method, all variables that are declared are within scope, so the driverage variable that you read in at the top will still be available at the time you are asking for the claims free driving / at the end of the process.


Advertisement