CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2012
    Posts
    9

    Nested looping to create a pattern

    Hi, I am working on some coursework for university at the moment, and one of the questions asks me to 'write a function that will generate the following pattern using nested looping techniques.'

    Pattern:

    - . . . . . . .
    . - . . . . . .
    . . - . . . . .
    . . . - . . . .
    . . . . - . . .
    . . . . . - . .
    . . . . . . - .
    . . . . . . . -

    So far I have the following code, but when I compile it I get a host of different errors.
    #include <iostream>

    int main()
    {
    int A[8][8],i,j;
    for(i=0;i<8;i++)
    {
    for(j=0;j<8;j++)
    {
    if(i==j)
    A[i][j]=-;
    Else A[i][j]=.
    }
    cout << ā€\nā€;
    }
    }
    I'm sorry I don't know how to add code tags on this,

    Thanks in advance for any pointers.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Nested looping to create a pattern

    Quote Originally Posted by hcoyle545
    I'm sorry I don't know how to add code tags on this
    Post your code in [code][/code] bbcode tags, e.g.,
    Code:
    #include <iostream>
    
    int main()
    {
        int A[8][8],i,j;
        for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                if(i==j)
                    A[i][j]=-;
                Else A[i][j]=.
            }
            cout << ”\n”;
        }
    }
    Quote Originally Posted by hcoyle545
    So far I have the following code, but when I compile it I get a host of different errors.
    At a glance, you need to:
    • Quote your characters, i.e., "'-'" instead of "-" and "'.'" instead of ".".
    • Use the correct keywords, i.e., "else" instead of "Else".
    • Quote your string literals with the correct quotes, i.e., '"' instead of '”'. This mistake may have been the result of writing code in a text processor like Microsoft Word; use a programming text editor or a programming IDE instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2012
    Posts
    9

    Re: Nested looping to create a pattern

    Thanks for your reply laserlight, I was infact using Microsoft word. Now i'm using Codeblocks for editing code.

    I have edited the code now so that it no longer gives me any errors, however when I run it it does not make the pattern, but displays an empty console box.

    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
        int A[8][8],i,j;
        for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                if(i==j)
                    A[i][j]='-';
                else A[i][j]='.';
            }
            cout << "\n";
        }
    }

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Nested looping to create a pattern

    That is because you did not actually print anything except newlines. You should be printing each element of the array (and in fact you don't even need an array here since you can print without one).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2012
    Posts
    9

    Re: Nested looping to create a pattern

    So should I have everything like cout << " " ...

    If so, i'm not sure how I would structure this.

    Maybe cout << i, j << endl; ???

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Nested looping to create a pattern

    Quote Originally Posted by hcoyle545
    So should I have everything like cout << " " ...
    Yes, where the "..." is:
    Code:
    cout << A[i][j];
    If you ditch the array, it is as simple as replacing:
    Code:
    A[i][j]='-';
    with:
    Code:
    cout << '-';
    (and similiarly for '.')
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jul 2012
    Posts
    9

    Re: Nested looping to create a pattern

    Okay, i've done this and my code now looks like this:

    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
        int A[8][8],i,j;
        for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                if(i==j)
                    A[i][j]='-';
                else A[i][j]='.';
            }
            cout << "\n";
            cout << A[i][j];
        }
    }
    But when I run I get this series of numbers



    Thanks for your help by the way.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Nested looping to create a pattern

    A should be an array of arrays of char instead. Furthermore, you need to print in the right place.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jul 2012
    Posts
    9

    Re: Nested looping to create a pattern

    Ah, thank you.

    Finally


  10. #10
    Join Date
    Dec 2013
    Posts
    2

    Re: Nested looping to create a pattern

    please give me that code....

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Nested looping to create a pattern

    Code:
    #include <iostream>
    
    int main()
    {
    	for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++)
    			std::cout << (char)('-' + (i != j));
    
    		std::cout << std::endl;
        }
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Dec 2013
    Posts
    2

    Re: Nested looping to create a pattern

    thank a lot.. somebody help me to find out the code printed like this.. when the user enter number 5...
    Pattern 2 Pattern2
    $$$$5 $$$$$
    $$$5$ $$$5$
    $$5$$ $$$55
    $5$$$ $$555
    5$$$$ $5555

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Nested looping to create a pattern

    Quote Originally Posted by gussey9873 View Post
    thank a lot.. somebody help me to find out the code printed like this.. when the user enter number 5...
    Pattern 2 Pattern2
    $$$$5 $$$$$
    $$$5$ $$$5$
    $$5$$ $$$55
    $5$$$ $$555
    5$$$$ $5555
    How about you do your own homework, or at least make an attempt.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Nested looping to create a pattern

    From the code already provided in this thread and your own knowledge, you should have no difficulty at least attempting this. The only hint I'm going to give is to consider the conditions for when to display the 5.

    PS If you still need some advice after first attempting this yourself, I would suggust starting a new thread.
    Last edited by 2kaud; December 11th, 2013 at 10:12 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured