-
stupid pyramid
can someone please help me with this stupid thing.. I was not born to be a programmer..
i have to make this
*
**
***
****
*****
using a loop thing..
for (int k=0; k<5; k++)
for (int x=-1; x<k; x++)
cout << "*";
cout << endl;
I dont get how to put the spaces in to make it look like a pyramid though.. you guys will say "im not doing your homework for you".. but I could use the help.. if you dont help me Ill fail college and work at mcdonalds for the rest of my life..
sigh
-
well dont rely on these ppl will help you. (this ppl here only will tell you do it your own) but jeez you got to give him some hint
Here some example i show you but you have to learn it.
int main()
{
int x,y;
for (x=1;x<=9;x++) {
{ for (y=1;y<=x;y++)
cout<<1;
}
cout<<endl;
}
or
{
int x,y,z;
for (x=1;x<=9;x++) {
for (z=10;z>=x;z--) {
cout<<" ";
}
{ for (y=1;y<=x;y++)
cout<<" 1";
}
cout<<endl;
}
-
>if you dont help me Ill fail college and work at mcdonalds for the rest of my life..
Hehe. :) This works just fine, though I question whether it would be accepted.
Code:
#include <iostream>
#include <iomanip>
int main()
{
int n;
std::cout<<"Enter the number of lines: ";
std::cin>> n;
std::cin.ignore();
for ( int i = 0; i < n; i++ ) {
std::cout<< std::setw ( n - i ) << std::setfill ( ' ' ) <<"";
std::cout<< std::setw ( i * 2 + 1 ) << std::setfill ( '*' ) <<""<<std::endl;
}
std::cin.get();
}
And another that would be equally unacceptable, though your instructor would marvel at your fluency with printf. :D
Code:
#include <iostream>
#include <string>
#include <cstdio>
int main()
{
int n;
std::cout<<"Number of lines: ";
std::cin>> n;
std::cin.ignore();
std::string s ( n * 2, '*' );
for ( int i = 1; n > 0; i += 2 )
std:: printf ( "%*c%.*s\n", n--, ' ', i, s.c_str() );
std::cin.get();
}
-Prelude
-
the best way is to learn how to trace the code.. and understand it well .
-
cuz then you can modify it and apply it to any other problems you have.