Thread: New at C++ Need help

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Cool New at C++ Need help

    I am trying to create a program based on the input of a number to generate a pattern: if the user enters the number 3 the pattern should be -
    Code:
     *** ***
     **   **
     *     *
    but if I enter the number 4 the pattern would be -
    Code:
    ********
    ***  ***
    **    **
    *      *
    Can anyone help out?

    Thank-you


    [code][/code]tagged by Salem

  2. #2
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    two words : For Loop
    Last edited by abrege; 12-06-2002 at 09:23 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    while

    I can get a pattern to cascade - but I do not understand the while loop

    //displays a pattern of asterisks
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	//variables
    	int outer = 0;
    	int inner = 0;
    
    	//perform loops
    	outer = 6;
    	while (outer > 0)
    	{
    		inner = 1;
    		while (inner <= outer)
    		{
    			cout << "*";
    			inner = inner + 1;
    		}//end while
    		outer = outer - 1;
    		cout << endl;
    	}//end while
    
    	return 0;
    } //end of main function

    &#91;code]&#91;/code]tagged by Salem

  4. #4
    check this out... its the inverse of what you are looking for but with a little thinking you can switch it around

    it uses nested for loops

    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
     int rows;
     cout<<"How many rows: ";
     cin>>rows;
     cout<<"\n\n\n";
     if(rows == 0)
      {
       cout<<"Thats to little";
       return 0;
      }
      else
      {
     for(int x=0;x<rows;x++)
     {
      
      cout<<"\n";
       for(int y=rows;y>x;y--)
       {
        cout<<" ";
       }
       cout<<"*";
       for(int z=0;z<x;z++)
       {
        cout<<"**";
       }
      cout<<"*";
     }
     }
     getchar();
     return 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    Still won't work - please help

    I am trying to create a program based on the input of a number to generate a pattern: if the user enters the number 3 the pattern should be -


    code:-------------------------------------------------------------------------------- *** ***
    ** **
    * *--------------------------------------------------------------------------------


    but if I enter the number 4 the pattern would be -

    code:--------------------------------------------------------------------------------********
    *** ***
    ** **
    * *--------------------------------------------------------------------------------


    Can anyone help out?

    Thank-you

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Heres an obfuscated solution for you:

    Code:
    #include<iostream>
    #define o cout<<
    #define x(X,Y,Z) for(int Z=X;Z-->0;o Y)
    #define q x(i,"*",j);
    using namespace std;
    int main(){
            int s;
            o"Enter a size:";
            cin>>s;
            x(s+1,"\n",i){
            q x(2*(s-i)," ",j);q
            }
    }
    Think your teacher will accept it?

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    4

    More help

    Nope I don't think he will like it. Here is where I am at now - somewhat closer

    It is the while loop I am having problems with.

    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    int main()

    {
    //variables
    string asterik ="";
    asterik.assign(3, '*');
    int count =1;
    int inum = 0;

    while (asterik !=0)
    {

    cout << asterik.assign(inum,'*');
    asterik = asterik + "*";
    count = count + 1;


    cout << asterik +" "+" "+ asterik << endl;

    return 0;
    } //end of main

Popular pages Recent additions subscribe to a feed