Thread: linking/mixing for loops?

  1. #1
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50

    linking/mixing for loops?

    Hi,
    I am reading Alan's book and i was trying to make this problem solving exercise. This might be called as nested loops, but i dont understand them. Anyway, i am trying to write program that prints out full lyrics to 99 bear song. Hoping for explanation or anything

    The output would have to look like:

    99 bottles of beer on the wall, 99 bottles of beer.
    Take one down and pass it around, 98 bottles of beer on the wall.



    My try below:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for(int i=99; i>=0 ;i--)
        cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
        for(int j=98; j>=0 ;j--){
        cout <<endl;
        cout << "Take one down and pass it around," <<j<<"  bottles of beer on the wall."<<endl;
        }
            return 0;
    }

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Well, you don't actually need to use nested loops for this problem. What you have at the moment will print all 99 versions of the first line of the song, and then afterwards it will print all versions of the second line.

    Let's try and formalize what you want to happen, forget about C++ for a moment:

    if n is the number of the current iteration, and the code runs 99 times, the output should be:
    "n bottles of beer on the wall, n bottles of beer.
    Take one down and pass it around, (n-1) bottles of beer on the wall."

    Now, this should be reasonably easy to translate into C++. Use 1 for loop. Then run your code and find out if there are any special cases you need to handle. (There might be some grammar issues for n < 2, and for n = 0 you will have -1 bottles of beer on the wall :-) ) These can be fixed with an if inside the loop that check for certain values of n, or you can make it loop from 99 to 2 and then do the last 2 iterations "by hand".

    I have no idea why the book wants you to solve this problem with nested loops, seems rather strange.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User Solarwin's Avatar
    Join Date
    Dec 2012
    Posts
    50
    Quote Originally Posted by Neo1 View Post
    Well, you don't actually need to use nested loops for this problem. What you have at the moment will print all 99 versions of the first line of the song, and then afterwards it will print all versions of the second line.

    Let's try and formalize what you want to happen, forget about C++ for a moment:

    if n is the number of the current iteration, and the code runs 99 times, the output should be:
    "n bottles of beer on the wall, n bottles of beer.
    Take one down and pass it around, (n-1) bottles of beer on the wall."

    Now, this should be reasonably easy to translate into C++. Use 1 for loop. Then run your code and find out if there are any special cases you need to handle. (There might be some grammar issues for n < 2, and for n = 0 you will have -1 bottles of beer on the wall :-) ) These can be fixed with an if inside the loop that check for certain values of n, or you can make it loop from 99 to 2 and then do the last 2 iterations "by hand".

    I have no idea why the book wants you to solve this problem with nested loops, seems rather strange.
    I made it ''by hand", but i have question: Why it starts from 98 instead of 99 if i write a line - cout<<endl; in my for loop?



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for(int i=99; i>=3 ;i--){
        cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
        cout << "Take one down and pass it around, " <<i-1<<"  bottles of beer on the wall."<<endl;
        cout <<endl;
            }
        cout <<"3"<< " bottles of beer on the wall, "<<"3"<<" bottles of beer."<<endl;
        cout << "Take one down and pass it around, " <<"2"<<"  bottles of beer on the wall."<<endl;
        cout <<endl;
        cout <<"2"<< " bottles of beer on the wall, "<<"2"<<" bottles of beer."<<endl;
        cout << "Take one down and pass it around, " <<"1"<<"  bottle of beer on the wall."<<endl;
        cout <<endl;
        cout <<"No more bottles of beer on the wall, no more bottles of beer."<<endl;
        cout <<"Go to the store and buy some more, 99 bottles of beer on the wall."<<endl;
            return 0;
    }
    I also tried to make an if statement in for loop, but it doesnt work as it should.


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for(int i=99; i>=3 ;i--){
                if(i==1){
        cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
        cout << "Take one down and pass it around, " <<i-1<<"  bottle of beer on the wall."<<endl;
        cout <<endl;}
    
                else
        cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
        cout << "Take one down and pass it around, " <<i-1<<"  bottles of beer on the wall."<<endl;
        cout <<endl;
            }
    
    
    
    
        cout <<"No more bottles of beer on the wall, no more bottles of beer."<<endl;
        cout <<"Go to the store and buy some more, 99 bottles of beer on the wall."<<endl;
            return 0;
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I run it with adding a cout<<endl; at the start of the loop, like this
    Code:
    for(int i=99; i>=93 ;i--){
            cout<<endl;
        cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
    but it started normally, from i = 99.

    The if-else statements have exactly the same body...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mixing BASH with C
    By Mikro in forum Linux Programming
    Replies: 6
    Last Post: 12-28-2005, 04:36 PM
  2. Mixing C and C++
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-10-2005, 10:30 AM
  3. What about mixing c with c++
    By gandalf_bar in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2004, 08:30 AM
  4. Mixing C and Assembly
    By Dragoon_42 in forum C Programming
    Replies: 6
    Last Post: 11-12-2003, 12:32 AM
  5. Mixing gcc 2.9* with gcc 3.*
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 07-19-2003, 12:21 AM