• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Asterik Triangles

NomisST

Member
Hello Programmers! I'm currently a student at Rutgers Engineering School and I'm an EE Major. I kinda suck at programming and I'm stuck on this problem. I know how to make 4 triangles go from up to down, but I can't do it making them align next to eachother. This is my program on how it should look:
* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********

I am using nested for loops and my program looks like this:
*
**
***
****
*****
******
*******
********
*********
**********
**********
*********
********
*******
******
*****
****
***
**
*
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********

This part of the lab assignemt was worth 8/10 points, which I have.
The second part is the followup assignment is where you change the program and make the triangles align side by side and it's worth only 2/10 points, the thing is i'm already handing it in late and I kinda don't have to do it because I'll only get 1 point since I'm handing it in late, but I want to know how to do it cause well it's driving me crazy! I've spent already like 2 hours trying to put them side by side when it only took me 5 min to do the other program. I don't get why it's not working, I tried getting rid of the cout << endl; but that obviously didn't work, I also tried looping the row with a for (row=1;row<=40;row++) and then nesting more for loops under it, but I get infinite loops. I'll post my program along with this.

#include <iostream>
using std::cout;
using std::endl;

int main()
{

int row;
int column;
int space;

for (row = 1; row <= 10; row++)
{
for (column = 1; column <= row; column++)
cout << "*";
cout << endl;
}
cout << endl;

for (row = 10; row >=1; row--)
{
for (column = 1; column <= row; column++)
cout << "*";
cout << endl;
}
cout << endl;

for (row = 10; row >= 1; row--)
{
for (space = 10; space >= row+1; space--)
cout << " ";
for (column = 1; column <= row; column++)
cout << "*";
cout << endl;
}
cout << endl;

for (row = 10; row >= 1; row--)
{
for (space = 1; space <= row-1; space++)
cout << " ";
for (column = 10; column >= row; column--)
cout << "*";
cout << endl;
}
cout << endl;

return 0;
}

I'm not looking for someone to rewrite my whole program, although that would be kinda nice, but I wouldn't learn anything I think I rather have a programmer give me good hints, like REALLY REALLY good hints where I can figure out what to do. I'm kind of stumped and It's like 1:40 in the mourning and I have a early class. This isn't due until the 5th, I'll be trying on my own to fix this, but I don't think I can figure this out. Anyone out there! Please help me!?!?! I'm using bloodshed Dev-C++ for my compiler. Thanks to anyone that helps out! MUCH APPRECIATED!
 
Ugh the picture didn't come out right at the top, it should be the triangles next to eachother not making a rectangle.
 
You can see the picture correctly when you quote your post.

The trick for the last part is that you want to print each row one after another. Instead of looping from 0 to 40, you just want to use the inner loop for each of your 4 triangles one after another within a single for loop. Because of this you want to get the outter for loop to be the same for each triangle. You'll notice that on yours you have:

for (row = 1; row <= 10; row++)
for (row = 10; row >= 1; row--)
for (row = 10; row >= 1; row--)
for (row = 10; row >= 1; row--)

Refactor them to all be the same and then you can just put all of your inner loops inside a single outter loop to get your output.

One consequence of this is that you need to print the spaces on the end of the triangles too. For instance, your first triangle only prints the stars, not the spaces.
for (row = 1; row <= 10; row++)
{
for (column = 1; column <= row; column++)
cout << "*";
cout << endl;
}
cout << endl;
This seems unimportant until you try to put another triangle to the right of it.

I redid your assignment from scratch and this is what I came up with. Hope the example helps.
 
Man...someone is a 1337 Hax0rz! Haha j/k, I'm suprised you acutally did it from scratch just for me. You are really helping me learn this cause like I really hate it when people just go here is the answer, I learn nothing! But thanks for breaking it down for me. Hehe in the future I might have to PM you haha I got like 9 more labs to go in this semester. Next up is arrays. Anyway thanks for everything! Take care.
 
Originally posted by: Kyteland
You can see the picture correctly when you quote your post.

The trick for the last part is that you want to print each row one after another. Instead of looping from 0 to 40, you just want to use the inner loop for each of your 4 triangles one after another within a single for loop. Because of this you want to get the outter for loop to be the same for each triangle. You'll notice that on yours you have:

for (row = 1; row <= 10; row++)
for (row = 10; row >= 1; row--)
for (row = 10; row >= 1; row--)
for (row = 10; row >= 1; row--)

Refactor them to all be the same and then you can just put all of your inner loops inside a single outter loop to get your output.

One consequence of this is that you need to print the spaces on the end of the triangles too. For instance, your first triangle only prints the stars, not the spaces.
for (row = 1; row <= 10; row++)
{
for (column = 1; column <= row; column++)
cout << "*";
cout << endl;
}
cout << endl;
This seems unimportant until you try to put another triangle to the right of it.

I redid your assignment from scratch and this is what I came up with. Hope the example helps.

thats a lot of uneeded code all you need is 2 for loops to do it

Here is the code that will make that it is really short and sweet.
 
OP, am confused, is this the effect u want?

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

if so, don't think of solving the problem as joinning a bunch of triangles.
think of it as drawing a big rectangle with holes punched out.
so, maybe something like this:

 
Originally posted by: Cooler
thats a lot of uneeded code all you need is 2 for loops to do it

Here is the code that will make that it is really short and sweet.
I hate to break it to you, but you did the problem wrong. 0/10 for you. 😉 Try running my program to see the difference in output.

Edit: See attachment for what it should look like.
 
Originally posted by: Kyteland
Originally posted by: Cooler
thats a lot of uneeded code all you need is 2 for loops to do it

Here is the code that will make that it is really short and sweet.
I hate to break it to you, but you did the problem wrong. 0/10 for you. 😉 Try running my program to see the difference in output.

Edit: See attachment for what it should look like.



it can still be done with 2 for loops.
 
Originally posted by: Cooler
No you have more locals so it would not be the same.
:roll: Fine I fixed it in the code.

Honestly, your implementation gets you nothing. Modern compilers will take care of the multiple locals. The dude is trying to learn the basic concepts here, not get confused.
 
Originally posted by: Kyteland
Originally posted by: Cooler
No you have more locals so it would not be the same.
:roll: Fine I fixed it in the code.

Honestly, your implementation gets you nothing. Modern compilers will take care of the multiple locals. The dude is trying to learn the basic concepts here, not get confused.

thats true .
 
Originally posted by: Cooler
Originally posted by: Kyteland
Originally posted by: Cooler
thats a lot of uneeded code all you need is 2 for loops to do it

Here is the code that will make that it is really short and sweet.
I hate to break it to you, but you did the problem wrong. 0/10 for you. 😉 Try running my program to see the difference in output.

Edit: See attachment for what it should look like.



it can still be done with 2 for loops.


yes... I'm in for Programmers Unite
 
My programming prof. emphasized the importance of making efficient code... the less typing the better. (Be thankfull you dont have to use Unix programs to code.)

By now I have forgotten it all... so a lot of good her teaching did. 😕

Oh yeah... she wrote the tests for all of the sections too. Out of 300+ CSE students the avg. grades for the tests that semester never reached 60.

I hope your teacher is more forgiving. 🙂
 
Back
Top