Need Help With Simple Iteration Program

This is a discussion on Need Help With Simple Iteration Program within the C++ Programming forums, part of the General Programming Boards category; I have been asked to write a program that creates the following pattern without simply printing this sequentially : 1 ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Need Help With Simple Iteration Program

    I have been asked to write a program that creates the following pattern without simply printing this sequentially :

    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


    So far this is my code but it doesn't work. I am meant to use either a for loop or a while loop.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	int max;
    
    	scanf("%d%c", &max);
    
    	while(max <= 10)
    	{
    		for(i = 1; i < max; i++)
    		{
    			printf("%d\n", i);
    			max--;
    			printf("%d\n", i);
    		}
    	}
    	
    	return(0);
    }
    Can anyone help me?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Why are you comparing max to 10, when max is decreasing?

    You probably don't want to decrease max INSIDE your inner loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	int max;
    
    	scanf("%d%*c", &max);
    
    		for(i = 1; i < max; i++)
    		{
    			printf("%d", i);
    		}
    
    		return(0);
    }
    I have refined it to this which gives me the first line of the question.

    Can anybody tell me how to repeat it so that i obtain the pattern in the question?
    Last edited by rbreakey; 03-09-2011 at 04:25 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    You modify the loop you had, not remove it altogether!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	int max;
    
    	scanf("%d%*c", &max);
    
    	while(max <= 10 && max >=2)
    	{
    		for(i = 1; i < max; i++)
    		{
    			printf("%d", i);
    		}
    		
    		printf("%d\n");
    		max--;
    	}		
    	
    	return(0);
    }
    This gives me the right answer except it produces a 0 at the end of each line.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i;
    	int max = 10;
    
    	while(max <= 10 && max >=2)
    	{
    		for(i = 1; i < max; i++)
    		{
    			printf("%d", i);
    		}
    		printf("\n");
    		max--;
    	}		
    	return(0);
    }
    This is my final code which produces the right result. Can anyone tell me if it looks ok? and if there is anything i need to modify. Cheers.
    Last edited by rbreakey; 03-09-2011 at 06:14 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    It seems OK to me.

    One small nit is that the () around return 0 are redundant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Whole code is working fine.....
    And Salem, does this () really matters in output???
    I don't think so...... And program shouldn't give 0 in the end....
    I made a dry run of this program and it's working nice....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 10:13 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

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