Thread: No idea on how to do this...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    No idea on how to do this...

    "Write a program that uses nested loops to produce the following output.
    A1B1B2B3A2B1B2B3"

    Any tips on where I should start?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Sembhi View Post
    "Write a program that uses nested loops to produce the following output.
    A1B1B2B3A2B1B2B3"

    Any tips on where I should start?
    The pattern looks like "A1 x A2 x A3 x..." Where "x" is "B1 B2 B3". So you have an outer loop counting through the "A"s and an inner loop counting through the "B"s. Does that help?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <iostream>
    
    int main()
    {
        /* Insert code here */
        return 0;
    }
    Start here.

    Research loops if you need to. Look back at the output you need to spit out, and then try to figure out how you can use loops to do it. Once you figure it out, put it into code.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    Thumbs up

    Quote Originally Posted by brewbuck View Post
    The pattern looks like "A1 x A2 x A3 x..." Where "x" is "B1 B2 B3". So you have an outer loop counting through the "A"s and an inner loop counting through the "B"s. Does that help?
    Yea that does.

    Need help with another program real quick. I have to keep getting a number from the user until they enter 0. I also need to add up all the numbers they enter and display it. How do I do this?

    Heres my code:

    Code:
    #include <iostream>
    
    int main()
    {
    	int sum = 0;
    	int num;
    	int total;
    
    	do
    	{
    		cout << "Enter a number (Enter 0 to quit): ";
    		cin >> num;
    		if (num == 0)	
    
    		{total = num;
    		 total = total + num;
    	         cout << total;
    	break;}
    	 
    	}
    	while (1);
    	return 0;
    }
    When I enter 0 to end the program and display the sum...it comes up as 0. Help?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Look at your if statement and when you add to the total.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Still confused. This is what I have.

    Code:
    	do
    	{
    		cout << "Enter a number (Enter 0 to quit): ";
    		cin >> num;
    		
    		total = num;
    		total = total + num;
    	
    		if (num == 0)
    		
    		{
    	        cout << total;
    		break;
    		}
    I don't understand why total showing up as 0.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What value does total have when the program starts?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You didn't do that in the code you posted, and you're overwriting the value of total with num, and then adding num to total. On the other hand, you do have a variable called sum, which is initialized to 0.

    Step through the program logic very slowly. I think you may be trying to do a bunch of small edits, which are making the program very ridiculous. Just go over it and make sure everything is done in the proper order.
    Last edited by MacGyver; 12-05-2007 at 07:15 PM.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Yea I saw. Thanks for the help. Works fine now.

    The reason I had sum = 0 was because the problem told me. Not sure why though. Oh well, that one is done.

    I'm starting on the one I had originally posted about.
    Last edited by Sembhi; 12-05-2007 at 07:25 PM.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Quote Originally Posted by brewbuck View Post
    The pattern looks like "A1 x A2 x A3 x..." Where "x" is "B1 B2 B3". So you have an outer loop counting through the "A"s and an inner loop counting through the "B"s. Does that help?
    OK I get what you're saying but I'm not sure how to put that into code.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Think about if you just wanted the output to be A1A2A3. You would want something that would repeat three times, and each time add 1 to the number after A. Which kind of loop could do that?

    Then once you figure out the A's, you should be able to figure out the B's as well.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Got it. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 09:56 AM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  3. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 09:52 PM
  4. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 12:09 PM