a little trouble with for loops

This is a discussion on a little trouble with for loops within the C Programming forums, part of the General Programming Boards category; Hey all, I'm having a little trouble with a homework problem. Here it is: Write a problem that creates the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    a little trouble with for loops

    Hey all, I'm having a little trouble with a homework problem. Here it is:
    Write a problem that creates the following pattern:

    1 2 3 4 5 6 7 8 9
    1 2 3 4 5 6 7 8
    1 2 3 4 5 6 7
    1 2 3 4 5 6
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1


    Now I know that this will involve nested for loops, but every attempt I've made gives me results like:
    999999999
    888888888
    777777777
    666666666
    555555555
    444444444
    333333333
    222222222
    111111111

    Any ideas?

  2. #2
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    make the second loop depend on how far the first loop's gone and make it count from 1-9 (and counting down) instead of just filling the array with which value the first loop was on.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      /*Local Definitions*/
      int row;
      int col;
      int limit;
      
      /*Statements*/
        limit = 9;
        for (row = 1; row < limit; row++)
            for (col = 1; col < limit; col--)
                    printf("%d", row);
                    printf("\n");
                    
       
       
             
        
        
      system("PAUSE");	
      return 0;
    }
    this is what I have based on that...it's wrong , I know. Help.
    melee

  4. #4
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    to clerify my first post I didn't mean that the code should be counting down using -- I ment that is should shorten how high the values would be able to go, sorry if if it caused you any trouble.

    something like

    Code:
    for (i=0:i<10;i++)
    {
       for (j=0;j<10-i;j++)
       {
        /*stuff*/
       }
       /*stuff*/
    }
    Last edited by Shogun; 10-18-2004 at 04:01 AM.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Hi, try it likle this, it should produce the output as you're asked for.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int y=0, z=0;
    
    	for(y=9; y>0; y--)
    	{
    	     for(z=1; z<y+1; z++)
    	    {
    
    	          fprintf(stdout,"%d",z); /* print the numbers */
    
    	    }  /* end of nested for loop */
             
    	     printf(stdout,"\n");   /* print the new line */   
    	}   /* end of outer loop */
    	return 1;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Any occasion I can use this code makes for a good day!
    Code:
    void O_Oy( char *o_o, int o_O )
    {
            char V_V[BUFSIZ]="%s\n";
            char T_T[BUFSIZ]="%%[^%s]";
            char v_v[BUFSIZ]={0};
            char x_x[BUFSIZ]={0};
    
            while( o_O -- )
            {
                    sprintf( v_v, T_T, o_o + o_O + 1  );
                    sscanf( o_o, v_v, x_x );
                    printf( V_V, x_x );     
            }
    }
    
    int main( void )
    {
            char o_o[] = "1234567890";
    
            O_Oy( o_o, 0x9 );
    
            return 0;
    }


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Quzah, I love that code...unfortunately, it is completely over my head at the moment. However, I'm saving it...and one day...when I'm a grown up programmer, I'll understand it...hopefully. Thanks!!

    micklarko, shogun: Thank you ever so much. Shogun's code worked excellently!

    melee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 11:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2003, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21