Problem in Nested Loops

This is a discussion on Problem in Nested Loops within the C++ Programming forums, part of the General Programming Boards category; I know this kind of seems easy at first, but the answer is simply avoiding me. I ave to make ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    5

    Exclamation Problem in Nested Loops

    I know this kind of seems easy at first, but the answer is simply avoiding me.
    I ave to make a DOS based console application that gives the following output, centre of screen.
    PLEASE HELP!!
    OUTPUT:
    *
    ***
    *****
    *******
    *********
    ***********
    *************

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    A nested loop as specified.
    the outer manages the space between column one and the asterixs and the inner manages the stars.

    I get

    *
    ***
    *****
    *******
    *********
    ***********

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Provide some code

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    Here is my version.
    Code:
    main(){		
    int stars, rows, space;
    for(rows=1; rows<=10; rows++){
    	for(space=40; space>0; space-=2)
    		printf("\xFF");
    	for(stars=1; stars<=rows; stars+=2)
    		printf("*");
    printf("\n");}
    system("pause");
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    1. Indent your code properly.
    2. Include the appropriate header files in your example program.
    3. main() returns an int, so state as such.
    4. Why do you set space=40 and then write space-=2?
    5. Why do you printf("\xFF") when you want to print a space?
    6. Why do you write stars+=2?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    1. It is indented except for main()
    2. include the stdio.h and stdlib.h
    3.HUH??
    4.we learnt that the centre of the screen is at x=40, and 1 position is removed for every star. That is 2 for every row.
    5.was trying different stuff. \xFF stuck for some reason.
    6. to add 2 more stars in each line.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by umair_crash View Post
    1. It is indented except for main()
    Which is exactly the point. Everything inside a curly brace should go in "one step".
    2. include the stdio.h and stdlib.h
    So post that.
    3.HUH??
    Declare "int main()" instead of "main()". Yes, it means the same thing in an old compiler. Modern compilers (that support the now about 9 year old the C99 standard) will not compile the code you have posted.
    4.we learnt that the centre of the screen is at x=40, and 1 position is removed for every star. That is 2 for every row.
    5.was trying different stuff. \xFF stuck for some reason.
    That is not very reliable, as 0xff is certainly not guaranteed to be a space, so use " " instead.
    6. to add 2 more stars in each line.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    1. It is indented except for main()
    It can be improved, and frankly, just indent for the body of main() as well so that function scope can be clearly separated from global scope.

    2. include the stdio.h and stdlib.h
    Yes, though in C++ it should be <cstdio> and <cstdlib>, respectively.

    3.HUH??
    As in write:
    Code:
    int main() {
    Return 0 from main() at the end, or omit the return statement (the global main() function is special in that it will return 0 if no return statement is encountered).

    4.we learnt that the centre of the screen is at x=40, and 1 position is removed for every star. That is 2 for every row.
    Then instead of using magic numbers, use named constants, or at the very least comment your code. That said, your reasoning sounds wrong: the first row only has one star, so if you want column 40 to be at the centre, then you need to print 39 spaces followed by a star. On the next iteration you would print 38 spaces followed by 3 stars, etc.

    5.was trying different stuff. \xFF stuck for some reason.
    However, it is not correct (and is another magic number that can be avoided by using a literal space character instead of \x20).

    6. to add 2 more stars in each line.
    You are actually skipping a star.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    5
    you are really confusing me

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    you are really confusing me
    What are you confused about?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    OK, you really need to fix all the above. But in any case your logic in incorrect.
    Look carefully at the ouput. Think that the computer starts from the upper left and prints one by one character.

    At the first line it will print 6 spaces, 1 star and 6 spaces. At the second line 5 spaces, 3 stars, 5 spaces. Etc etc.

    You are printing space 20 spaces (or 19) at each loop, then you print rows+2 stars. Then you change the line. The stars are fine, the changing of line is correct, the spaces are NOT.
    First of all the loop that prints spaces is fixed. Meaning it will always print the same amount of spaces for each row. That is wrong. The variable rows should be inside the loop for the spaces (somehow). And, you print spaces, stars and space again. That is the only way.

    And at least the } for the outer for should really be in a separate line than printf()

  13. #13
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    See also cross-forum post here where he states problem is solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  2. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 07:43 PM
  3. Problem with either rand() or loops....not sure
    By Welshy in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 02:08 PM
  4. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 09:25 AM

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