I am still not getting this whole loop thing. :( I have to make a pyarmid using *'s with a nested loop program and I don't get it. I think I'll put my fist through this computer.
Help?
Printable View
I am still not getting this whole loop thing. :( I have to make a pyarmid using *'s with a nested loop program and I don't get it. I think I'll put my fist through this computer.
Help?
Uhh well ur title isn't very convincing in the first place and its homework but i'll help you anyways...
try this:
this is one half of the pyrimide and you can do the other oneCode:#include <iostream.h>
int main()
{
int nrows = 5; //number of rows
for(int x=0; x<nrows; x++)
{
if(x == 0)
{
cout<<"*";
}
if(x == 1)
{
cout<<"\n**";
}
if(x == 2)
{
cout<<"\n***";
}
if(x == 3)
{
cout<<"\n****";
}
if(x == 4)
{
cout<<"\n*****";
}
if(x == 5)
{
cout<<"\n******";
}
}
return 0;
}
why use all those if's?
use a nested loop
Code:#include<iostream>
using namespace std;
int main(){
int i;
for(i=0;i<6;i++){
for(int j=i;j>0;j--)
cout<<"*";
cout << endl;
}
for( ;i>0;i--){
for(int j=i;j>0;j--)
cout<<"*";
cout << endl;
}
}
or if you want it small and compact
Code:#include<iostream>
int main(){
char *x="*******";
int i=0;
for(;i++<7;)printf("%.*s\n",i,x);
for(;i-->0;)printf("%.*s\n",i,x);
}
I can beat that ;)Quote:
Originally posted by beege31337
or if you want it small and compact
Code:#include "MyPyramid.h"
int main(){ PrintMyPyramid(); return 0; }
Heres a neat solution
you need this include<iomanip.h>
for (int i =0;i<15;i++)//loop to print pyrimid of asterisks
{
if(i==0)cout<<setw(6);
if (i ==1)cout<<setw(5)<< endl;//*
if (i ==3)cout<<setw(4)<<endl; //**
if (i ==6)cout<<setw(3)<<endl;//***
if (i ==10)cout<<setw(2)<<endl;//****
cout<<"* "; //*****
}