Thread: i don't understand this whatsoever

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    i don't understand this whatsoever

    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?

  2. #2
    Uhh well ur title isn't very convincing in the first place and its homework but i'll help you anyways...

    try this:

    Code:
    #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; 
    }
    this is one half of the pyrimide and you can do the other one

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    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);
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by beege31337
    or if you want it small and compact
    I can beat that
    Code:
    #include "MyPyramid.h"
    int main(){ PrintMyPyramid(); return 0; }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    bigwullie
    Guest
    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<<"* "; //*****
    }

Popular pages Recent additions subscribe to a feed