Thread: Some homework help?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    Some homework help?

    My desired output is the following
    Code:
    __*
    _***
    *****
    The underscores represent space. This is what I've done so far... please help because I'm really confused.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    
    {
    
    	int value1, space, star;	
    	printf("\n\n");	
    	for(value1=5; value1>=1; value1=value1-2)	
    
    		
    
    	   {
    
    		
    
    
    	   	for(space=1; space==value1/2; space++)	
    		
    		
    
    	   	   {
    	
    			printf(" ");	
    		   
    	   	   }
    
    			
    		
    		
    
    		   for(star=0; star < 6-value1; star++)		
    
    		      {
    
    			printf("*");	
    			
    		      }
    			
    	printf("\n");
    
    
    	   }
    		
    	printf("%d  %d  %d", value1, space, star);
    
       getch();	
    
    }
    With the above this is my output::

    *
    _***_
    *****

    Except for the first star it's all good. And can someone please quickly explain how for loops execute all of their statements/conditions if another for loop is enveloped? I'm really confused and can't concentrate on anything... I'm just typing something, compiling and doing it over and over. For example... I have no idea why I put "star < 6-value1;" in the third for loop. I know that it means that star is less than 6 subtracted by value 1... but if value1 starts out as 5 then should star always be a negative? Please help!! TIA.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's one way of doing it.

    Declare a 2X2 int array:
    Code:
    int array[5][5];
    int i,j;
    //Initialize all the value to zero
    for(i=0;i<5;i++)
        for(j=0;j<5;j++)
             array[i][j] = 0;
    Now you can assign '*' to the elements that you want.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Thank you for your timely reply but I can't use Array and stuff-I haven't learned it yet. Sorry. Is there any other way I can do it using what I did?

    ANd for the FOR loop, I know how it works... I just get confused when there's enveloped for loops, one for loop in another... anyone mind explaining how those execute too? I read all the tuts on this site to no avail.

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    My desired output is the following

    Does it have to look like the way you have it so far or what becuase here is a simple draw pattern program. It may also get you kick started into the same direction your traveling.
    -c-c-peace-out


    #include<stdio.h>

    #define SYMBOL '*' /*basicaly replaces all words that have SYMBOL with * marks*/
    #define OFFSET 0 /*basicaly replaces all words that have OFFSET with 0*/
    #define LENGTH 19

    void display(char, int, int); /*this is a function prototype*/
    void draw(char , int);

    int main(void)
    {
    display(SYMBOL, OFFSET, LENGTH); /*his is a display function to display these 3 variables*/
    return 0;
    }

    void display(char c, int m, int n)
    {
    if (n> 0) {
    draw(' ', m);
    draw(c, n);
    putchar('\n');
    display(c, m + 2, n - 4); /*will display these integers + 2 and - 4 onto your display*/
    }
    }

    void draw(char c, int k)
    {
    if (k > 0) { /*if int k is less than 0 pint character*/
    putchar(c);
    draw(c, k - 1); /*draw function will draw the pattern * minus 1*/
    }
    }

    Good Luck!
    Last edited by correlcj; 07-18-2002 at 07:09 PM.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Uh, that's great, but I don't understand ... .. any of it.

    Sorry to keep bothering all of you... is there anyway I can modify my current code to do it?

  6. #6
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    ANd for the FOR loop, I know how it works... I just get confused when there's envelop

    Sure!
    an example: for (a =1; a<i; ++a)
    for (a =2; a<i; ++a)
    This is only an example though it doesnt work, kay. Anyways, the for loops back to back only say that iif the for satement is true it will execute the next stement and return.
    same as
    a=1; /*this is a initialization*/
    while (a<i); /*while true execute ++a until false but if false to begin with then skip that staement and continue on*/
    ++a
    Hope this helps! let me knopw if thats what you meant, kay?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    So If I had something like:

    Code:
    for(blah; blah; blah)
      {
         for(blah; blah; blah)
          {
    
               for(blah; blah; blah)
               {
               }
    
           }
    
       }
    It executes the first for, and if the condition is TRUE, then it executes the modification of the var. So if it's a-- and a is = to 5, then a becomes 4? Then it goes to the second for loop, checks blah, blah is TRUE, so it mods blah then it goes to third loop, checks blah, blah is TRUE, so it executes whatever is in body , then what? back to outer? Outermost? TIA...

    And what of my homework!?

  8. #8
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    then what?

    Eventually...
    YES!
    It keeps going and going like the energizer bunny!
    you would have to end your loop by using either a return or exit or clear screen, etc.., etc...
    I have edited my program so you can better understand and run it to see:
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here's a working version of you wanted to do:

    Code:
    #include <stdio.h>
    
    #define MAX 10
    
    int main()
    {
    	int value1, space;	
    	int i = 0;
    	printf("\n\n");	
    	for(value1 = MAX; value1 >= 1; value1 -= 2)	
    	{
    		space = 0;
    		i = value1 - 1;
    		while(space != value1/2)
    		{
    			printf(" ");
    			space++;
    		}
    		while(i < MAX)
    	    {
    			printf("*");
    			i++;
    		}
    		printf("\n");
    	}
    	printf("%d  %d", value1, space);
    	
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    I kind of get it... but I'm still really confused!! And ........ed! ........ed to the mAx! Does a for loop in a for loop run until its condition is false or just run it's body once and then go back and give control back to the outer for loop!?

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    A nested for loop runs it's entire length then goes back to the original. If the original condition is still met, it runs the entire nested loop again.

    for example
    Code:
    int i,j;
    
    for( i=0; i<3; i++)
    {
         for( j=0; j<5; j++ )
            printf("%d", j);
         printf("\n");
    }
    would produce:

    01234
    01234
    01234


  12. #12
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    for( i = 0; i < 3; i++)
    {
          //do what's in here
    
          for( j = 0; j < 10; j++)
          {
                  //do what's in here
          }
    }
    Let's analyze :

    First, i is set to zero in the main/outer loop.
    1. Then the program jumps into the loop and sees another for
    loop.

    2. Now the inner loop must be completed before the program
    can jump out of it.

    3. j is set to zero and program loops until j equals 10.

    4. Now it jump's out of inner loop and sees the end of the
    main/outer loop, so it increments i.

    5. Now go back to step 1. if i is not equal 3.

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    It's beginning to sink in! (Thanks!!!!!!) Now, if I have something like this:

    Code:
    for(x=5; x>=1; x--)
    
       {
            for(y=1; y<=x; y++)
    
           {
    
              printf("printmenow")
    
           }
    
       }
    So now will printmenow loop itself until y=x? And for the first for, does x-- happen right away? Meaning, will the second loop regard x's value as 4? TIA!

  14. #14
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    It's beginning to sink in! (Thanks!!!!!!) Now, if I have something like this:

    You got it now, my young padywan!
    May the force be with you!

    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Yes, print me now will loop until y=x, then it will start all over again with x=4. That probably answers your second question, the block of code is executed, then the increment/decrement takes place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  3. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM