473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

For loops to create four right triangle histograms...HELP!!

Hey all,
This is my first post. I am really hoping that someone out there has a solution to my C++ coding problem...

This is my code and what I have to do...
I think it is rows of 1 - 10 and then back

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

//These histograms are supposed to print beside eachother like this:

//* ******************** *
//** ********* ********* **
//*** ******** ******** ***
//**** ******* ******* ****
//***** ****** ****** *****
//****** ***** ***** ******
//******* **** **** *******
//******** *** *** ********
//********* ** ** *********
//*********** ***********
//but I can't make them go beside eachother. My code below makes two of them,
//but they are one under the other!! Not what I need....

//HELP??????



#include <iostream>
using namespace std;

int count;


main()
{



for(int count_1=1;count_1<=10;count_1++)
{
for(int count_2=1;count_2<=count_1;count_2++)
cout<<"*";

cout<<endl;
}

cout << "\n";

for(int count_3=1;count_3<=10;count_3++)
{
for(int count_4=10;count_4>=count_3;count_4--)
cout<<"*";

cout<<endl;

}


cout << "\nPress q and then Enter to quit-->"; //
char dummy;
cin >> dummy; //Wait for input
return 0;

} // main
************************************************** *****************************************

Help anyone, please!!!!!!!

Thanks,
Shelbee
Nov 25 '06 #1
16 5000
Banfa
9,065 Expert Mod 8TB
This line

cout<<endl;

and this line

cout << "\n";

are equivilent and moves the output onto a new line.

If you don't want to start on a new line then don't run this line of code.
Nov 26 '06 #2
Hey,
Thanks for responding.

I tried that, just now again, and then it runs one of the triangles and the other is all on the first line in one long line of asterisks...! LOL It's making me crazy!! I thought about pulling my hair out, but I would look funny!! LOL HELP...?

Stuck again...?

Shelbee
Nov 26 '06 #3
If you think about it, you can't add lines to what you already outputted. What I believe you're trying to do is add the triangles to what you've already outputted.

I believe what you are doing is equivalent to trying to do this:

cout << "super";
cout << endl;
cout << "man";

and you're expecting superman, yet you get super/man.

I think your whole algorithm needs refining. Let me see what I can do, I'll get back to you.
Nov 27 '06 #4
I have refined the source code for you, its probably not exact, but the output is this:

-bash-3.00$ a.out
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********


Press q and then Enter to quit-->q
-bash-3.00$

Here's the refined source code:

//These histograms are supposed to print beside eachother like this:
//* ********** ********** *
//** ********* ********* **
//*** ******** ******** ***
//**** ******* ******* ****
//***** ****** ****** *****
//****** ***** ***** ******
//******* **** **** *******
//******** *** *** ********
//********* ** ** *********
//*********** ***********
//but I can't make them go beside eachother. My code below makes two of them,
//but they are one under the other!! Not what I need....
//HELP??????



#include <iostream>
using namespace std;


int count;

int main() {

for(int stars = 1; stars <= 10; stars++) {
for(int count = 1; count <= stars; count++) {
//It'll print the amount of stars
cout << "*";
if(count == stars)
cout << " ";
}

for(int count_2 = 10; count_2 >= stars; count_2--) {
cout << "*";
if(count_2 == stars)
cout << " ";
}

for(int count_2 = 10; count_2 >= stars; count_2--) {
cout << "*";
if(count_2 == stars)
cout << " ";
}

for(int count = 1; count <= stars; count++) {
cout << "*";
if(count == stars)
cout << " ";
}


cout << endl;
}

cout << "\n";

cout << "\nPress q and then Enter to quit-->"; //
char dummy;
cin >> dummy; //Wait for input

return 0;

}


Hope ya like it ;).
Nov 27 '06 #5
Hey!!

Thanks so much! The code was great...except that I need the two middle triangles turned the other way. I will try and look at your code to see what is needed to turn them around, but your code has come closer than anything I could figure out!!

In the diagram that I have here, the two middle right triangles are supposed to have their straight sides facing the other way. So, the straight side of triangle two should face triangle one, and the straight side of triangle three should face triangle four. Make sense? I hope you can fix it because I don't know if I can...LOL.

Thanks so much and will be checking back often to see your response!! :)
Shelbee
Nov 28 '06 #6
More Help !!!!????

PLEASE??

Shelbee
Nov 28 '06 #7
Can you draw what you mean? I don't understand what you mean by having the two middle triangles facing the other way...I'll see what I can do once you aid me visually :).
Nov 28 '06 #8
Hey there!

Sure I will try to draw it here...(the last time that I drew it, it didn't leave enough space between the four triangles, LOL)

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


Hope it stays the same when I post this...

Thanks so much!
Shelbee
Nov 29 '06 #9
It didn't...
I am going to put it on here with letters instead, for display purposes, but I still need the results in asterisks and spaces.
So, the "a" will stand for the asterisks and the "p" will be spaces. The asterisks are in sets of 10.

apppppppppaaaaaaaaaaaaaaaaaaaapppppppppa
aappppppppaaaaaaaaappaaaaaaaaappppppppaa
aaapppppppaaaaaaaappppaaaaaaaapppppppaaa
aaaappppppaaaaaaappppppaaaaaaappppppaaaa
aaaaapppppaaaaaappppppppaaaaaapppppaaaaa
aaaaaappppaaaaappppppppppaaaaappppaaaaaa
aaaaaaapppaaaappppppppppppaaaapppaaaaaaa
aaaaaaaappaaappppppppppppppaaappaaaaaaaa
aaaaaaaaapaappppppppppppppppaapaaaaaaaaa
aaaaaaaaaaappppppppppppppppppaaaaaaaaaaa


Now, I will go line for line in words to explain what happened up there...LOL.
1st line - 1 'a', 9 'p', 10 'a', 10 'a', 9 'p', 1 'a'
2nd line- 2 'a', 8 'p', 9 'a', 1 'p', 1 'p', 9 'a', 8 'p', 2 'a'
3rd line - 3 'a', 7 'p', 8 'a', 2 'p', 2 'p', 8 'a', 7 'p', 3 'a'
etc....


Does that help? It messes up the view each time I try to post it with spaces instead of letters.
Thanks so much!!
Shelbee
Nov 29 '06 #10
Was wondering if that last post helped any with visualizing what I need?

Thanks,
Shelbee
Nov 29 '06 #11
Hey there! Just wanted to see if I was still remembered? Still need that code...? I was getting too far back on the board and thought maybe "outta sight, outta mind", LOL. Waiting to hear back from ya!

Thanks,
Shelbee
Nov 30 '06 #12
Feel like I have been forgotten. Can anyone help me with this code? I really NEED it...like rightaway...

Thanks,
Shelbee
Dec 1 '06 #13
Hey guys,
Can anyone turn those inside two triangles around? The best code I have, I got from here, but it has the inside two triangles' straight sides facing eachother when they should be facing the other way..?

Anyone? Please help...

Thanks,
Shelbee
Dec 2 '06 #14
Still waiting to hear back about the loops...? Can anyone help me? I really need to figure out how to switch those triangles around that are in the middle...? Anyone??

Thanks,
Shelbee
Dec 4 '06 #15
I know this thread is outdated, but you know, 2 cents.

I am not sure on the code right now, but what you need to do is realize the difference between printing just one of those triangles out, stacked on top of each other, and having them all lined up. You need a starting loop that is going to define how many total LINES there are, and then a loop that is going to define how many characters, including the spaces, that are going on each line.

next:
You have up facing triangles @ 1 char each. You have 2 triangles that have their bases up @ 10 chars each. Then you have 3 spaces. So the first line is going to look like this:

* ********** ********** * = 25 chars
You need to find a way to adjust where the " " is going to be for lines 2 -10. The middle space will always be the same, the first space will need to be added to each time the loop comes around so that it moves right, the third space needs to be subtracted from so that it moves left.

I am working and learning with C# and just finished a similar exercise where we just made the same shapes, just stacked on top of each other. I will see if I can come up with a solution to post here for you and others.
Feb 25 '08 #16
I'm back, and sooner than I thought :)

Here is the code in C#, but it can be converted easily to any language.

---------------------------------------------

int space1 = 2; //variable that will move the first spacer right
int space2 = 24; //variable that will move the third space left

for (int L = 1; L <= 10; L++) //determines number of lines
{ //start first loop

for (int C = 1; C <= 25; C++) // determines chars per line

{ //start second loop

if (C == space1) // if the char location = space1, print a space

{

Console.Write(" ");

}

else if (C == 13) // if the char location = 13 (the middle) print space

{

Console.Write(" ");

}

else if (C == space2) //if the char location = space2, print space

{

Console.Write(" ");

}

else

{

Console.Write("*"); //all else fails, print *

}

}//end second loop

space1++; //incr. space1 by one
space2--; // decr. space2 by one

Console.WriteLine(" "); // new line

} //end first loop

--------------------------------------------------------------
Hope this helps, but I encourage you to study it and understand exactly how it is working instead of just copying it, or else you haven't learned anything, really.
Feb 25 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: K k | last post by:
Hi, I need your help to resolve this problem. I have written a right outer join query between 2 indipendent tables as follows. select b.Account_desc, b.Account, a.CSPL_CSPL from...
0
by: Chris | last post by:
I am getting a bit confused again with theis com+ stuff. I am looking at the PetShop app and I see the layers BLL DAL. Do these layers have to be on the same web server? If they are on separate...
0
by: Dip | last post by:
Hello Experts, I was wondering whether it is possible to create "Cube Roles" automatically. I have a table name called tblUsers which has "ProjectID" and "EmployeeID". Employees are allocated...
0
by: simpleeshelbee | last post by:
When I went back and looked at what came out to see for my message, I saw that it came out a little different than I need it. I need it like this.... * = asterisks 1st row = 1 *, 9 spaces, 10...
10
by: lkr0210 | last post by:
Hello, I am having a huge problem with one of our PC's on the network. When this PC is connected to the network, it is very slow to open or create a new file or folder. It doesn't matter if you...
11
by: inferi9 | last post by:
hi everyone I am new here and I have this C++ program that I have to write but it keep given me nothing useful. here is the question: A right triangle can have sides that are all integers. A...
1
by: shapper | last post by:
Hello, On my CMS I am signed as administrator and I am creating a Membership user and its profile: MembershipCreateStatus status; MembershipUser user = Membership.CreateUser("Joe", "Pass",...
1
by: John Jones | last post by:
I need to calculate c, which is a quarter of the hypotenuse of a right triangle with side lengths of a and b. How can i express the formula for c using math.h?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.