You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...
You will need another for loop nested prior to the for loop printing the letters, and a little bit of math to figure how many spaces determined by which letter you are printing...
Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2
REALLY simple math at that. Every row is the same width x, of which y spaces are covered with letters. How many spaces?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
dynamethod :: WARNING: do not read! may be more than a hint!
I don't want to ruin your post or education, but I am more curious than allowing this to slip by...
I didn't consider that possibility, but i know how... Now I want to know which way is the better way to go?
A:
B:Code:for() // cycle lines { for() {} // print leading spaces for() {} // print letters }
Is running a loop not better than comparing each cycle?Code:for() // cycle lines { for() // cycle prints per line { if() {} // print leading spaces else {} // print letters } }
Is there another way better than both of these?
Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2
In the first line, you printed out 3 spaces, and then 1 letter. Second line, 2 spaces, and two letters. Third line, 1 space, and 3 letters. Last line, 0 spaces and 4 letters.
Every line adds up to 4 char's, and the spaces are always printed (in this configuration), FIRST. Could you use a loop for the spaces, and then a nested loop inside that, for the printing of the letter?
This can also be done in one loop, but the nesting of the loops is more educational.
Last edited by Adak; 11-10-2007 at 01:54 AM.
Adak: what? There's no third nesting of loops. That would be incredibly hard to get right.
SRS: Method A is better. So yes, there's a third loop. There's a way of getting rid of both inner loops if you know the interface of std::string well enough, though.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
i feel rotten about this though because ive borrowed the code from another post, which i found here:Code:#include<iostream> #include<string> using namespace std; int main() { int x; int y; char cLetter; for(y = 1; y <= 4; y++) { cout << string(abs(y-5),' '); for(x=1; x<=y; x++) { cLetter=64+y; cout << cLetter; } cout << "\n"; } cin.get(); return 0; }after i done a search, i dont quite understand the string function fully, particularly the 'abs', but it works, still probably shouldnt have done this lol. im now doing a bit of research into the string funcion now thoughCode:http://cboard.cprogramming.com/showpost.php?p=673514&postcount=3
The abs(y - 5) is just stupid. Write 5 - y and it works the same, not to mention that it's easier to understand.
Also, the loops are weird (0 to n-1 is more idiomatic in C++ than 1 to n), and the code makes a greater assumption about character values than usual. (Most code assumes that characters are sequential. This code assumes that they have ASCII codes.)
Then there's the redundant computation of cLetter in the inner loop instead of outside the loop, where it belongs, and the string trick could be used there, too, so why isn't it?
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
if i try the same code without the 'abs':
the code compiles, but when i try execute the app i get:Code:#include<iostream> #include<string> using namespace std; int main() { int x; int y; char cLetter; for(y = 1; y <= 4; y++) { cout << string(y-5,' '); for(x=1; x<=y; x++) { cLetter=64+y; cout << cLetter; } cout << "\n"; } cin.get(); return 0; }
Code:terminate called after throwing an instance of 'std::length_error' what(): basic_string::_S_create Aborted (core dumped)
Look closely.Write 5 - y
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
y-5 is going to end up being a negative value.Code:for(y = 1; y <= 4; y++) { cout << string(y-5,' ');
And it would be a good thing to get into the habit of representing ranges as [0, n) - from 0 inclusive to n exclusive. This is the way the standard libraries do it and it can reduce adding and substracting a magic 1 in many places. The main reason is probably that it matches how arrays are indexed in C++.
So a loop that would loop 4 times would be:
Code:for (int i = 0; i < 4; ++i)
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Sorry :S, i should pay more attention
heres what i have now:
i still think im not using the string function properly, string function is new territory for me, ive still alot to learn :SCode:#include<iostream> #include<string> using namespace std; int main() { int y; char cLetter; cLetter = 64; for(y = 1; y <= 4; y++) { cout << string((5-y),' '); cout << string((0+y), cLetter+y); cout << "\n"; } cin.get(); return 0; }
It's not a function, it's a class. You're constructing a temporary object.
Last edited by CornedBee; 11-10-2007 at 07:33 AM.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
ok, just edited my code a bit more, i feel even more stupid for that last piece of code i posted now lol
ive done without the char cLetter -
Code:#include<iostream> #include<string> using namespace std; int main() { for(int y = 0; y <= 4; y++) { cout << string((4-y),' '); cout << string((0+y), 64+y); cout << "\n"; } cin.get(); return 0; }