c++ double larger (double x, double y)

Discussion in 'Mac Programming' started by rockclimber9up, Aug 28, 2012.

  1. rockclimber9up, Aug 28, 2012
    Last edited: Sep 4, 2012

    rockclimber9up macrumors newbie

    Joined:
    Aug 28, 2012
    #1
    Hi, I have a C++ problem (just started learning the language) which requires me to pick the largest of 15 numbers. I have written the code already, but the problem also states that "This should be done using the prototype:
    double larger (double x, double y).

    The problem goes on to say to use a for loop (which I have done). I just have no idea what the double larger (double x, double y) statement means, and therefore whether I followed the requirements. My code btw is:

    *Code deleted to prevent plagiarism

    Any input would be greatly appreciated! (And the simpler the explanation the better, just starting to lean C++ like I said!) Thanks
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Let me add a word:
    "This should be done using the function prototype:
    double larger (double x, double y)."​

    So it's saying you should define a function with the given prototype, and have it return the larger of the two numbers it's given.

    If you don't know what a function or its prototype is, it should have been covered in whatever you're learning from (book, online tutorial, or a class).
     
  3. rockclimber9up thread starter macrumors newbie

    Joined:
    Aug 28, 2012
    #3
    Yeah, that was just a missed word typo. It wasn't really that first line that was confusing me (I know those definitions) it was the second line that was giving me trouble. But it seems as if I wrote the program correctly regardless.
     
  4. drsoong macrumors member

    Joined:
    Mar 24, 2008
    Location:
    Munich
    #4
    correct doesn't necessarily mean the way as requested

    Even though your program might work correct, it most likely will not get you full credit on the assignment!

    Chown already highlighted the important part. College problems are posed to let you practice particular aspects of the language you are learning, and this asks you to use a function in the solution to the problem, giving you even the prototype double (double x, double).

    You should go back to your notes, read about functions in C++, and use that concept in your program to provide a solution to the assignment.

    While in this small example, your and the solution asked for seem equivalent, it will teach you an important aspect about modular programming. ;)

    -drsoong
     
  5. theyoda3, Aug 29, 2012
    Last edited: Aug 30, 2012

    theyoda3 macrumors member

    Joined:
    Sep 27, 2006
    #5
    Aside from the implementation not being what was asked for, your program does not provide the correct solution. The problem asks for you to use double, but you chose to use int instead. What do you think will happen when someone provides decimal numbers as input? What happens if all the numbers given are the same integer but different values in the decimal place?
     
  6. rockclimber9up, Aug 29, 2012
    Last edited: Sep 4, 2012

    rockclimber9up thread starter macrumors newbie

    Joined:
    Aug 28, 2012
    #6
    So I rewrote my program, once again not sure if this is correct since I just started (that said, thanks for all the responses except for that last one which was just rude, I have been learning this language for a total of three days gosh).

    Anyways this is my new program:

    *Code deleted to prevent plagiarism



    Once again any input would be appreciated, oh and Xcode says one of my } which i colored blue has a semantic issue (control reaches end of non-void function). While the program still runs despite this I was wondering how to fix it (just take out the brackets?)

    Thanks to those previous responses!
     
  7. phantax, Aug 29, 2012
    Last edited: Aug 29, 2012

    phantax macrumors member

    Joined:
    Feb 2, 2009
    #7
    You are receiving that error because you aren't returning anything in your function even though you specified it should return a double.

    Also, you aren't using the function correctly. All you are doing is calling it and then you have user interface / input inside of the function, not a very good design decision.

    Without doing your assignment for you, what you should do is try moving your input stuff to main, then call your function supplying your two values taken from user input. Your function will then return a double and you use that for your answer.

    Another note: Your naming conventions are already getting confusing and your code is barely a few lines long, readability goes a long way.
     
  8. theyoda3 macrumors member

    Joined:
    Sep 27, 2006
    #8
    I am sorry you interpreted my message as rude. I was not asking those questions as a way to criticize you, but so you could think about the problems that would arise by yourself. I hope you recognize why the difference matters besides just what was asked for in the problem. It is not just a difference in words between double and int, truncation will occur if a decimal number is stored as an int.
     
  9. theyoda3, Aug 29, 2012
    Last edited: Aug 29, 2012

    theyoda3 macrumors member

    Joined:
    Sep 27, 2006
    #9
    Based on your last post I think it is clear that you do not completely understand the function prototype and what is asked for in the question. So let's look at the function prototype given in the problem and break it down.

    double larger (double x, double y)
    1) double is a signed double precision floating point number. It can have values that are positive or negative and they can be decimal values. This is similar to a float except that a float has less precision. A float is 32bits and double is 64 bits.
    2) double larger - this indicates that the function is called larger and returns a double. This is the output of the function.
    3) double x, double y - these are the variables that you are passing into the function larger. x represents one of the numbers you want to compare and y represents the other number you want to compare. Both of these numbers are doubles and are the inputs to the function.

    Looking at the two snippets of code you posted, the first one is a better starting point. First you need to replace all the ints with doubles because int cannot represent decimal numbers, it will just cut off the decimal. Next, you need to look at you code and ask yourself if there is a section where you compare two numbers and return the larger of the two. If so, replace that section with a call to larger. Pass in the two numbers you want to compare and store the return value. For example if your two numbers are A and B and Result is the larger of the two numbers the call might look like this, Result = larger(A, B). Lastly, you need to define what the function larger does. You do not need a class for this, just put the function definition before main, similar to your second code snippet, but without the class definition.
    (Basically, this is what phantax said)
     
  10. rockclimber9up, Sep 1, 2012
    Last edited: Sep 4, 2012

    rockclimber9up thread starter macrumors newbie

    Joined:
    Aug 28, 2012
    #10
    Think i got it

    *Code deleted to prevent plagiarism

    So I rewrote it based on what you guys said, this is what I got. I think it's correct except I am getting a warning "Variable 'num' is uninitialized when used here" based on the "num" i colored blue. Xcode tries to fix this by changing where I define num to a line saying double num = 0.0 . Is that correct/something I should do? Or should I just ignore that (and if I am supposed to do that why exactly is it needed?) Note the program works correctly without this change.
     
  11. theyoda3, Sep 1, 2012
    Last edited: Sep 2, 2012

    theyoda3 macrumors member

    Joined:
    Sep 27, 2006
    #11
    It is not quite right yet, but you are very close.

    1) The issue with your variable num is that it is not initialized before you use it when you pass it to the function larger. In simpler terms, you did not explicitly specify its value after declaring the variable. Sometimes when you do this the variable is given a default value, but sometimes it can be a random number, it depends on the object you are declaring. It is good practice to always explicitly assign the value of the variable at declaration when possible. When not possible, you need to make sure you explicitly give it a value before you use it. This makes it clear what the initial value is and can avoid debugging headaches if your compiler is not as forgiving as the one you are using in Xcode. You did not see this problem with max because you assigned it a value by reading from input using the line cin >> max;

    2) Your function larger should not be reading from input to assign the value of num, that defeats the purpose of passing the value of num as one of the function parameters. Take out the cin and cout lines from your larger function and move them inside your for loop in main, just before your call to larger. This also helps to keep your function larger more general and modular.

    3) Don't forget to return 0 at the end of main to indicate the program exited successfully. I over looked this too until lloyddean's post. His method for return is nice and is very clear to the reader, but both methods are common.

    4) You should consider moving your comments to the line above the code you are commenting instead of the same line. Professionally, it is still common for software teams to have a policy of limited line width and inline comments like that just waste useful space. Placing it above maintains the useable width. Also, it is a little bit easier to read the comments above the code.

    5) You should also consider using brackets with your if statements even if they are one line. Many times you will find that when you write an if statement you will come back to it later and add more code. Also, it is easier to read and visually recognize which code is included in the if statement when you use brackets.

    If you are interested in good guidelines for styling, then you could take a look at the Google C++ style guide, http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. This is not the final word on styling, but having some consistent method that is commonly used can be very helpful. Professionally, you will come across people and teams that use different styling methods. The most important thing is that you are consistent and try to make your code as readable as possible.

    You almost have everything right, so here is your code with my suggestions:
    Code:
    #include <iostream>
    
    using namespace std;
    
    // Use function larger
    double larger(double max, double num)
    {
      // Compare new numbers to saved max
      if ( num > max )
      {
        // If new number is larger than previous max, it will become the new max
        max = num;
      }
    
      return max;
    }
    
    int main()
    {
      // Define max used to describe max integer
      double max;
      // Define number to describe each inputted number being compared
      double num;
    
      // Call for first number
      cout << "Enter a number: ";
      // Consider this number max as it is the only number at this point
      cin >> max;
    
      // Use for loop expression to repeat following step 14 times
      for (int counter = 2; counter <= 15; counter++)
      {
        // Call for new numbers
        cout << "Enter another number: ";
        // Consider these new numbers num
        cin >> num;
        // Call function larger()
        max = larger(max , num);
      }
    
      // Report max number from series
      cout << "The max number is " << max << endl;
    
      return 0;
    }
    
    Note that I did not initialize max and num at declaration, but they are both initialized using cin >> before they are used.
     
  12. rockclimber9up thread starter macrumors newbie

    Joined:
    Aug 28, 2012
  13. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #13
    Code:
    #include <cstdlib>
    //#include <algorithm>
    #include <iostream>
    
    
    double larger(double x, double y)
    {
        // You might wish to use 'max' see -
        //      <http://en.cppreference.com/w/cpp/algorithm/max>
    
        // we must return a numerical result so if both values are equal it matters
        // not which value is returned
    
        return ((x > y) ? x : y);
    }
    
    
    int main()
    {
        double      valueMax;
    
        std::cout   << "\nPlease provide a (decimal) number: ";
        std::cin    >> valueMax;
        
        for ( int counter = 15; --counter; )
        {
            double      valueUser;
            std::cout   << "\nPlease provide a (decimal) number: ";
            std::cin    >> valueUser;
            
            valueMax    = larger(valueMax, valueUser);
        }
    
        std::cout   << "\nThe max number is " << valueMax << std::endl;
        
        return EXIT_SUCCESS;        // <http://en.cppreference.com/w/cpp/utility/program/EXIT_status>
    }
    
     

Share This Page