Write a function to take an integer and two character. Your function should draw a shape similar to the one below. For example:

DrawShape( 5, '#', '*')

Outputs the following

*####

*

*

*

*

p.s: (without the(. at the begining) i added cous the shape wont set right if it starts with a #)

Edited 1 Year Ago by lamlomeh: the shape wont go right

  1. We don't do your homework for you.
  2. See #1
  3. See #2
    .
    .
    .
    Post your work, errors, problems, etc and we may be able to help.

You know Public Domain Curses, libpdcurses.so will enable you to output interesting ASCKey characters besides '#' and '*'. Google for pdcurses.

hello there so i just need to ask well i solved this assignment with this

#include<iostream>
using namespace std;
int main()



    {




    int n,j,i;
        char ch1,ch2;
        cout<<"enter n , ch1 and ch2 :";
        cin>>n>>ch1>>ch2;
        for(i=1;i<=n;i++)


         {




        for (j=1;j<=n;j++)

            if(j==i) cout<<ch2;
            else cout<<ch1;
            cout<<endl;
        }
        return 0;
    }


**when compiling it gave me the right shape 
and there is another sol whith a diff way but im not getting it and the other sol goes like 
**



/*

           i
    ####*  1
    ###*#  2
    #*###  4
    *####  5        


    */



 .# include <iostream>
using namespace std;

void DrawShape(int n, char ch1, char ch2)
{
    /*
    for(int i = 1; i <= n; i++) 
    {
        for(int j = 1; j <= n-i; j++)
            cout << ch1;
        cout << ch2;
        for(int k = 1; k <= i-1; k++)
            cout << ch1;
        cout << endl;     
    }
    */

    for(int i = 1; i<=n; i++)
    {
        for(int j = 1; j <=n; j++)
            if (i+j == n+1)
                cout << ch2;
            else 
                cout << ch1;
        cout << endl;
    }
}

int main()
{
    int n; 
    char c1, c2;

    cout << "Enter n: ";
    cin >> n;
    cout << "Enter c1: ";
    cin >> c1;
    cout << "Enter c2: ";
    cin >> c2;

    DrawShape(n, c1, c2);

    return 0;
}

(i know that there is comments)
but why using two programs
the function and the main program im confused
btw the question goes like that

Write a function to take an integer and two character. Your function should draw a shape similar to the one below. For example:

DrawShape( 5, '#', '*')

void draw_shape(n,A,B)
{
int A;
int B;

for(int j=0;j<n;j++)
{
if(j==1)
{
cout<<"*";
for(int k=0;k<A; k++)
{
cout<<" #";
} // 2nd loop end

} // if end

else 
for(int g=0;g<n;g++)
{
cout<<"*"<<endl;
} // else end

}// 1st for loop end
} // function end

Edited 1 Year Ago by basit_3: forget a brace

This example also ensures the prgram will NOT crash if bad data was entered:

// drawShape.cpp //

#include <iostream>

/*
**when compiling it gave me the right shape
and there is another sol whith a diff way but im not getting it
and the other sol goes like ...
**
    ####*  1
    ###*#  2
    ##*##  3 // Did you ??? forget this line ??? //
    #*###  4
    *####  5
*/

const int MAX_LEN = 8;


void drawShape( int n, char ch1, char ch2 )
{
    using std::cout; using std::endl;

    for( int i = 0; i < n; ++ i )
    {
        for( int j = n-1; j >= 0; -- j )
            if( j == i ) cout << ch2;
            else cout << ch1;
        cout << endl;
    }
}



int main()
{
    using namespace std;

    int n;
    char ch1, ch2;

    // get valid data ...
    bool done = false;
    while( !done )
    {
        cout << "Enter n, ch1, ch2: ";
        if( cin >> n >> ch1 >> ch2 && n <= MAX_LEN && n > 1 )
        {
            cin.sync(); // 'flush' cin stream
            done = true;
        }
        else
        {
            cin.clear(); // clear all cin error flags
            cin.sync(); // 'flush' cin stream
            cout << "\nIllegal entry! Number entered must be in range 2.."
                 << MAX_LEN << "\n\n";
        }
    }

    cout << "\nOk ... now drawing shape ... \n";
    for( int i = 0; i < n; ++ i )
    {
        for( int j = 0; j < n; ++ j )
            if( j == i ) cout << ch2;
            else cout << ch1;
        cout << endl;
    }

    cout << "\nThis time using a 'reverse shape function' call ...\n";
    drawShape( n, ch1, ch2 );

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get();
}
This article has been dead for over six months. Start a new discussion instead.