-
July 15th, 2012, 10:09 AM
#1
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.
-
July 15th, 2012, 10:16 AM
#2
Re: Nested looping to create a pattern
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”;
}
}
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.
-
July 15th, 2012, 10:30 AM
#3
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";
}
}
-
July 15th, 2012, 10:46 AM
#4
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).
-
July 15th, 2012, 10:59 AM
#5
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; ???
-
July 15th, 2012, 11:03 AM
#6
Re: Nested looping to create a pattern
Originally Posted by hcoyle545
So should I have everything like cout << " " ...
Yes, where the "..." is:
If you ditch the array, it is as simple as replacing:
with:
(and similiarly for '.')
-
July 15th, 2012, 11:52 AM
#7
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.
-
July 15th, 2012, 11:54 AM
#8
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.
-
July 15th, 2012, 12:08 PM
#9
Re: Nested looping to create a pattern
Ah, thank you.
Finally
-
December 10th, 2013, 12:33 PM
#10
Re: Nested looping to create a pattern
please give me that code....
-
December 10th, 2013, 02:49 PM
#11
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)
-
December 11th, 2013, 05:51 AM
#12
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
-
December 11th, 2013, 06:37 AM
#13
Re: Nested looping to create a pattern
Originally Posted by gussey9873
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.
-
December 11th, 2013, 08:33 AM
#14
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|