Thread: Loop

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Loop


    i am to print this:

    ae
    bd
    cc
    db
    ea
    but i cant get it to work, can you check my code to see what is wrong?
    Code:
    #include <stdio.h>
    
    main()
    {
    	int x;
    	
    	for(x=97; x<=101; x++)
    	{
    		printf("%c\n", x);
    	}
    	printf("\b");
    	for(x=101; x>=97; x--)
    	{
    		printf("%c\n", x);
    	}
    	return 0;
    	getch();
    		
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    but i cant get it to work
    Why can't you get it to work? You've told us what you wish your program would produce but you haven't told us what you're program in producing now.

    But one thing I notice is that you're printing a new line character after each character, so every character will be on it's own line, is that the problem?


    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by jimblumberg
    Why can't you get it to work? You've told us what you wish your program would produce but you haven't told us what you're program in producing now.

    But one thing I notice is that you're printing a new line character after each character, so every character will be on it's own line, is that the problem?


    Jim
    this is what my code does:

    a
    b
    c
    d
    e
    e
    d
    c
    b
    a
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Part of your problem is that you're printing the newline character after each character in the first loop but you want two characters on the same line. But just removing the newline character will not solve all your problems.

    You really only want one loop. You want to print two characters. The first character you want to start at 'a' and then increase with each iteration. The second character starts at the last character and decrements with each iteration.

    Do you know that each section of a for loop can contain multiple statements? This fact can be used to print the two characters in the same loop.

    Code:
    	int x, y;
    
    	for(x = 'a', y = 'e' ; x <= 'e'; x++, y--)
    	{
    Jim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by jimblumberg
    Part of your problem is that you're printing the newline character after each character in the first loop but you want two characters on the same line. But just removing the newline character will not solve all your problems.

    You really only want one loop. You want to print two characters. The first character you want to start at 'a' and then increase with each iteration. The second character starts at the last character and decrements with each iteration.

    Do you know that each section of a for loop can contain multiple statements? This fact can be used to print the two characters in the same loop.

    Code:
    	int x, y;
    
    	for(x = 'a', y = 'e' ; x <= 'e'; x++, y--)
    	{
    Jim

    awesomeness level over 9000 XD so thats the trick there. didnt know that that is possible, thanks a million. :) any, other approach other than that? using more than one loop?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You really don't want more than one loop for this problem. But yes there are many different ways to do this, for example:

    [CODE] char x, y = 'e';

    for(x = 'a'; x <= 'e'; x++)
    {
    printf("%c %c\n", x, y--);

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo