1 Replies - 1397 Views - Last Post: 01 March 2011 - 03:37 AM Rate Topic: -----

#1 ScttN485  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-March 11

3 Side-by-Side Triangles Using Asterisks

Posted 01 March 2011 - 03:08 AM

I saw a similar post in a different thread, but was still unsure of how to go about doing it. I'm having an issue on a lab problem I'm working on and could use a few pointers. I'm just starting out in C++ and am stuck on this one. I got one triangle to appear but realized I probably needed to do it a different way. The directions say to use nested for loops and we haven't even gotten into arrays yet, so that's out of the question. I tried all I could and my book doesn't help with shapes that are next to each other. I saw a similar post in a different thread, but was still unsure of how to go about doing it. I'll be happy to elaborate if you need more info.

They're supposed to look something like this (the center row goes up to 10):

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



#include <iostream>
using namespace std;
int main()
{
    int i = 1;
    for (int x = 0; x < 10; x++)
    {
        for (int y = 0; y < i; y++)
        {
            cout << "*";
        }
        cout << endl;
        i++;
    }
    i -= 2;
        for (int x = 0; x < 10; x++)
    {
        for (int y = 0; y < i ; y++)
        {
            cout << "*";
        }
        cout << endl;
        i--;
    }
    return 0;
}


This post has been edited by Dogstopper: 01 March 2011 - 03:10 AM
Reason for edit:: Added code tags to the shapes.


Is This A Good Question/Topic? 0
  • +

Replies To: 3 Side-by-Side Triangles Using Asterisks

#2 akhena  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 69
  • Joined: 20-January 11

Re: 3 Side-by-Side Triangles Using Asterisks

Posted 01 March 2011 - 03:37 AM

You have to first find out the "logic" behind how the computer will display those triangles. I would suggest you first "re-draw" what the output must look like, but replacing the stars and white spaces by the number of times each one has to be displayed.

So just for the first line, assuming you're displaying triangles with 10 stars at most, and assuming you only want one white space between each triangles, this would be :
1 (star) - 9 (whitespaces) - 1 (whitespace to seperate the second triangle) - 1 (star) - 9 (whitespaces) - 1 (whitespace to seperate the third triangle) - 1 (star) - 9 (whitespaces) - end of line

Write this now for the second line, etc, and try to find some patterns.

Also I would suggest you start first with displaying only one triangle. You can add the other 2 once you one how to display one.

This post has been edited by akhena: 01 March 2011 - 03:39 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1