Thread: Jumping into C++, Chapter 5 Question 1.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Jumping into C++, Chapter 5 Question 1.

    Hello. I was just doing the first question of Chapter 5 in Jumping in C++. I completed it but I was just wondering if it was the most effeicent was to do it. And if not, what is it?

    The question was: Write a program that prints out the entire lyrics to a full rendition of "99 Bottles of Bear on the Wall".

    This was my program I wrote:

    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main()
    
    
    {
        int j=98;
    
        for(int i=99;i>2;i--)
        {
    cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer.\n Take one down pass it around, "<<j<<" bottles of beer on the wall.\n";
            cout<<"\n";
            j--;
        }
    
        for(int i=2;i>1;i--)
        {
    cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer.\n Take one down pass it around, "<<j<<" bottle of beer on the wall.\n";
        cout<<"\n";
        }
    
    cout<<"1 bottle of beer on the wall, 1 bottles of beer.\n Take one down pass it around, no more bottle of beer on the wall.\n";
    
        cout<<"\n";
    
    cout<<"No more bottles of beer on the wall, No more bottles of beer.\n Go to the store but some more, 99 bottle of beer on the wall.\n";
    }
    

  2. #2
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    Dont think so much about the efficiency of such a small level program. I cant help you out if it were a high level program, but you should just focus on how to write a program doing what you want it to do and under the specifications of the task atm.
    "Derp, derp, derp" - Me

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    If you guys had to write a program for this question. What would you write to get the exact lyrics.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I won't discuss "efficiency" (in quotes, because there are various measures against which efficiency might be assessed). It is particularly pointless when most of the program is concerned with I/O, as I/O is often a bottleneck.

    However, simplicity of code is important, since code that is easier to understand is easier to get right. So ....

    1) The program does not actually provide the required output for the last few bottles. That is due to a mix of typos and some incorrect logic.
    2) There is rarely any point in having a variable j that is always equal to i-1 when it is output. Why not compute i-1? There is little benefit in removing a subtraction, just to replace it with a decrement.
    3) Some strings are duplicated. Unnecessary duplication makes code harder to understand, and increases opportunities to make mistakes (refer point 1).
    4) Why not break some of the repeated logic (including strings) into separate functions?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    So can you please write out a program that would do this with all the corrections in?

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    You could do a few things, although I don't really see the point for such a small program. You could use strings or DEFINES to hold the text, you don't need the variable j, you could create a function, you could even use a struct or vector in there, or a constant,

    Often there are several ways to get to the outcome, as you are realising a lot of programming is about writing efficient, elegant code. Maybe just keep going onto more significant examples though.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by etricity View Post
    So can you please write out a program that would do this with all the corrections in?
    No.

    I've given you pointers and, with a bit of effort, you will learn something.

    You might also want to read this site's homework policy, here. While I realise you aren't doing this as homework in a class, the purpose of the exercise is the same - doing it yourself is the way you will learn, just as it would be if you were doing a class and this was homework. If I, or anyone else, did it for you, you would not learn as much.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Hodor likes beer. I just drank a 500ml bottle of my home brewed ginger beer. Very nice indeed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping into C++ chapter 8 question 5
    By twigz in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2014, 04:43 PM
  2. [jumping into c++] Trouble on pointer chapter.
    By CrimsonMagi in forum C++ Programming
    Replies: 14
    Last Post: 11-04-2013, 07:13 PM
  3. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  4. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  5. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM

Tags for this Thread