Patterns in C++

Oct 5, 2008 at 7:11am
I have just started a short C++ course and gotten stuck in the creation of the pattern shown below which is a right angle triangle of dots within a rectangle of stars using nested looping techniques:

* * * * * * * *
* . * * * * * *
* . . * * * * *
* . . . * * * *
* . . . . * * *
* . . . . . * *
* * * * * * * *

So far all I have managed to come up with is the pattern below and this is with the use of an id statement and I am not sure if this is allowed:

* * * * * * * *
* . * * * * * *
* * . * * * * *
* * * . * * * *
* * * * . * * *
* * * * * . * *
* * * * * * * *

The code I have to do this is as follows:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
int i,j;

for(i=0;i<7;i++)
{
for(j=0;j<8;j++)
if(j==i && j*2 > 1 && j*2 < 11)
printf(".");
else
printf("*");
printf("\n");
}
getch();
}

I would be very grateful for someones help in this matter as it is driving me absolutely bonkers lol, I have been looking at the screen since 12 o clock last night it is now 8.11 am please help me


Oct 5, 2008 at 9:34am
I wouldnt say I got it but:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using std::cout;    using std::endl;

int main()
{
    for(int i=0;i<8;i++)
    {
        cout<<'*';
        for(int k=0;k<i;k++)
        cout<<'.';
        for(int q=0;q<(8-i);q++)
        cout<<'*';
        cout<<endl;
    }
    cout<<"*********";

    return 0;
}


Its not *exactly* as you wanted it, but its very similar.
Last edited on Oct 5, 2008 at 9:34am
Oct 5, 2008 at 5:42pm
The display is now coming up as correct all except for the last line where I now wish to have a line of 8 stars. Would it be considered bad coding to simply put the 8 stars on a new line via the cout << function ?

* * * * * * * *
* . * * * * * *
* . . * * * * *
* . . . * * * *
* . . . . * * *
* . . . . . * *

My code is now as follows:

#include <iostream.h>
#include <conio.h>

main()
{
int i,k,q;

for(i=0;i<6;i++)
{
cout<<'*';
for(k=0;k<i;k++)
cout<<'.';
for(q=0;q<(7-i);q++)
cout<<'*';
cout<<"\n";
}
//cout << "********";
getch();
}

I would appreciate it if someone would be able to help me in understanding this problem. Also I would appreciate it if you would tell me if it would be considered bad coding to simply place in the last line of stars to complete the display.
Oct 5, 2008 at 11:02pm
Well...if you don't need to use loops, I would just cout the whole thing, but I assume you are required/want to do it that way.

What I would do is just always print out one * in the beginning, then find out how many spaces (if any) I need to print, then make the rest *s, using a const int to check how many spaces/*s should be on one line.
Oct 6, 2008 at 10:51am
I doubt it would be considered bad coding, since there seems to be no other way to print 8 *s on the screen. You can use another for loop though, but that would be kind of useless.

1
2
for(int j=0;j<8;j++)
cout<<'*';


PS: Why exactly are you using C-style programming in C++?
int i,k,q;
its better to declare the variables when you need them, it makes the code more understandable.
Last edited on Oct 6, 2008 at 10:54am
Topic archived. No new replies allowed.