Thread: Need help with simple programs...

  1. #16
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Hopefully the first thing one learns in a class on programming is that the computer can't fix your problems. YOU have to tell the computer what to do. Therefore, being able to solve the problem (or set up the algorhythm) with pencil and paper is a prerequisite to writing the code. Think about what sort of information you need to draw an X using asterisks. If you can write out the process, you will be much more likely to be able to successfully write the code.

  2. #17
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Originally posted by Witch_King
    Code:
    #include<iostream>
    #define MAX 5
    
    int main()
    {
    	int i_loops;
    
    	for(int i=0; i< MAX;i++)
    	{
    		i_loops = 0;
    		while(i_loops++ < i) std::cout << ("%d",i_loops) << std::endl;
    
    		if(i != 0) std::cout << std::endl;
    	}
    	return 0;
    }
    Here is a way to do it without nested 'for' loops. HAHAHAHAHAHA
    Both i and i_loops are set to 0, and after i_loops is incremented the first time, it's already larger than i, so nothing will print. i is never incremented, so when is i_loops ever smaller than i, allowing printing?

  3. #18
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Both i and i_loops are set to 0, and after i_loops is incremented the first time, it's already larger than i, so nothing will print. i is never incremented, so when is i_loops ever smaller than i, allowing printing?
    No, nothing will be printed on the first loop only. If you wanted to be picky you could reduce the amount of loops required -

    Code:
    #include <iostream>
    
    const int max =5;
    
    int main()
    {
        
    
        int i_loops;
    
        for(int i=1; i< max;i++)
        {
            i_loops = 1;
            while(i_loops <= i) std::cout << i_loops++ << std::endl;
    
            if(i != 0) std::cout << std::endl;
        }
        return 0;
    }
    
    Last edited by zen; 08-30-2001 at 12:53 PM.

  4. #19
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Unfortunately for Witch-King his code does use nested loops. True they are a while loop nested in a for loop instead of two for loops, but it is a nested loop nevertheless.

    Unfortuntately for Zen, i is incremented everytime through the for loop by by the third paramenter of the for statement. The first time through the for loop i = 0, i is less than MAX so the loop proceeds. i_loop is set to 0. i_loop is the same as i so the while loop is not entered and nothing is printed. The cursor is not moved down one line because i does equal zero. Now that the for loop is done the first time i is incremented to 1. 1 is less than MAX so the body of the for loop is entered. i_loop is set to 0 again. i_loop is now < i so the while loop is entered and the value of i_loop is printed. The only question I have is whether the value of i_loop at this time is 0 or 1, depends on whether i_loop is incremented after the comparison with i in the conditional of the while loop or whether i_loop is incremented after the body of the while is completed and just before the next comparison with i. Guess I'll need to check that out on the compiler when I have it available. In any event the value of i_loop will only be printed once the second time through the for loop. When the body of the for loop is completed the second time through i is again incremented, this time to 2. 2 is less than MAX so the body of the for loop is entered. This time the while loop will be run twice, etc, etc, etc.

  5. #20
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    i_loop is the same as i so the while loop is not entered and nothing is printed
    Check my while conditional statement.

  6. #21
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    Zen, my apology. Your code looks fine to me. I should have directed my comment to Brown Drake instead of using your screen name. I haven't figured out how people are using the quote thing to be more specific in directing comments. Sorry.

  7. #22
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Whoops, my mistake, didn't catch i incremented in the for loop. Sorry. It still won't print first time around, though.

  8. #23
    BCole19
    Guest
    Thanks for all the help guys. But we can't use nested for loops for the first one, BUT I think I got it figured out on my own. It's finally come back to me, and it's really easy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  2. help with simple programs. Matrix multiplications
    By ovadoggvo in forum C Programming
    Replies: 1
    Last Post: 05-18-2005, 11:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Hints required to complete simple programs
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 09:20 PM