|
Answer» Ummm im trying to LEARN c++ but im stuck on a problem. how can i make a triangle look like this
* * ** ** *** *** **** **** ***** ***** ************ ***** ***** **** **** *** *** ** ** * *
using loops in c++? I've been trying to do it but i cant SEEM to get it. So help would be greatly appreciated. Like this? (This is what you typed, shown in monospaced font) There are actually 4 triangles in that figure.
Code: [Select]* * ** ** *** *** **** **** ***** ***** ************ ***** ***** **** **** *** *** ** ** * * I am guessing not. You have not described the problem very precisely. Is it homework?
The continuous line in the middle has 12 asterisks so I will assume your pattern is meant to be 12 characters wide, and that you need to successively show 11 lines of text that consist of:
a number A of asterisks - a number S of spaces - the same number A of asterisks
If the display is 12 chars wide and you assume 1 character step in size
Then A - S - A will be:
1 asterisk 10 spaces 1 asterisk 2 asterisks 8 spaces 2 asterisks 3 asterisks 6 spaces 3 asterisks 4 asterisks 8 spaces 4 asterisks 5 asterisks 2 spaces 5 asterisks 6 asterisks 0 spaces 6 asterisks 5 asterisks 2 spaces 5 asterisks 4 asterisks 8 spaces 4 asterisks 3 asterisks 6 spaces 3 asterisks 2 asterisks 8 spaces 2 asterisks 1 asterisk 10 spaces 1 asterisk
Now all you have to do is think of an algorithm to generate that sequence of numbers or rather such a sequence for any given width of pattern. Study the figures and you will see a pattern. Then use writeln to do the lines.
the figure basically looks like the head of an axe. Two triangles point to point. it looks weird and i apologize for that. i know the code for it is suppose to be relativly short, so my 2 page long code DOESNT seem right.Pseudocode:
produce square figure (equal height & width)
H=height W=width W=H for n=1 to (H/2) step 1 ( print n stars, print W - (2*n) spaces, print n stars )
for n=( (H/2)-1) to 1 step -1 ( print n stars, print W - (2*n) spaces, print n stars ) thank you, ill try to figure it out from here. My math cant be THAT bad.this is what i have
Code: [Select]#include <stdio.h> #include <iostream> #include <math.h> #include <stdlib.h> #include <windows.h> #include <assert.h> #include <ctype.h> #include <float.h> #include <limits.h> #include <string.h> #include <iostream.h>
void main(void) { int i; int asterick1 = 0; int asterick2 = 0; int acounter1 = 1; int acounter2 = 1; int iscounter = i - 2; int scounter = 0; int spaces = i - 2; int middlecounter = 0; int middlecounter2 = 0; int x = acounter1 + acounter2;
cout << "Please enter an INTEGER" << endl; cin >> i; cout << "*";
while ( x < i) { while (asterick1 <= acounter1) { cout << "*"; asterick1 = asterick1 + 1; } while (scounter <= spaces) { cout << " "; scounter = scounter + 1; } while (asterick2 <= acounter2) { cout << "*"; asterick2 = asterick2 + 1; }
acounter1 = acounter1 + 1; acounter2 = acounter2 + 1; spaces = spaces - 2; cout<< endl; }
while (middlecounter2 <= 2) { while (middlecounter <= i) { cout << "*"; } cout << endl; } system ("PAUSE"); return 0; }[/glow]
and it doesnt work.I am stuck on a similar problem. The outcome has to look like this. Can someone please help! * ********** ********** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* ********** * * **********use the methods described above, extending your solution to four triangles instead of 2.
Seriously- we aren't here to DO the homework for you, we're here to help- and I think all the information you need was provided in previously posted answers.
WE? I appreciate the help that I got from the previous posts. I was just looking for somewhere to start from.
|