Hi all, I am a first year student at the university of South Africa. I am doing my degree via correspondence, thus no lectures. I am working through the prescribed book as best i can but they still assume a little bit of fore knowledge of programming which i don't have! Please tolerate me for asking what should probably be a very easy question for you all.

They want me to write a void function which does the following. A number is entered, eg 6 ant then the following should be the output;

#^^^^^^^^^#
^#^^^^^^^#^
^^#^^^^^#^^
^^^#^^^#^^^
^^^^#^#^^^^
^^^^^#^^^^^

I know that a 'for' loop is required to do this, but I cant figure out how many to nestle!?

The formula (2 * nrP - 2 * i - 1) is given as a hint where 'nrP' is the number of rows entered by the user for the function to use and 'i'
will be the row generated by the loop. I have no examples of this in my text book to refer to for help.

If someone could just steer me in the right direction it will already be a huge help.

PLEASE HELP!!

thanks

int nrP = 6;
for ( i = 1 ; i < nrP ; i++ ) {
  cout << i << " " << (2 * nrP - 2 * i - 1) << endl;
}

Notice anything about the sequence of numbers?

First, have you started any code? What parameters are you going to use in your code? Is it a simple iostream or an fstream that's required. Post what you have so far so we can help you out.

First, have you started any code? What parameters are you going to use in your code? Is it a simple iostream or an fstream that's required. Post what you have so far so we can help you out.

I have started with the problem. This is what i have so far :

// Draws shape V
#include<iostream>
using namespace std;

void drawShape (int nrP)
{
    for (int i = 1; i <= nrP; i++)
    {    for (int j=2; j<=i; j++)
         {cout << "#";
            for(int j=0; j < i; j++)
            cout << "^";  }
          
}

int main ()
{
    int nr;
    
    cout << "Number of rows : ";
    cin >> nr;
    drawShape(nr);
    
    return 0;
}

I just can't figure out where to put the formula provided as a hint, and the loops inside the function have me baffled. I feel a little lost, especially with the last row. how do get just one '#' sign in the last row. does it require a different loop all of its own??? Does the formula make provision for it. I am busy catching up on my calculus also, thus you can see i bit quite a big bit off for myself.

Edited 7 Years Ago by John A: added code tags

int nrP = 6;
for ( i = 1 ; i < nrP ; i++ ) {
  cout << i << " " << (2 * nrP - 2 * i - 1) << endl;
}

Notice anything about the sequence of numbers?

I have also tried the following, but it goes into an infinite loop, but I can't see why. I think i am over thinking this problem or I am trying to overcomplicate it. Here's what I tried:

void drawShape (int nrP)
{
    for (int i=1; i <= nrP; i++)
    {    
        for (int j=i-1; j<=i; j++)
         cout << "^";
        for (int k=1; k <=i; k++)
         cout << "#";
        for (int l=(2*nrP-2*i-1); l<=i; l++)
         cout << "^";
         
    cout << endl;
    }
    cout << endl;
}

Edited 7 Years Ago by John A: added code tags

So do it in stages, say the spaces up to and including the first hash.

You don't have to write the whole thing just to make progress.

So do it in stages, say the spaces up to and including the first hash.

You don't have to write the whole thing just to make progress.

Exactly the idea behind " Divide and conquer " strategy, which
in fact is very intuitive.

Thanks for all the help guys. I got it in the end. Here is my program, tell me if this is similar as to what you would have done;

// Draws shape V
#include<iostream>
using namespace std;

void drawShape (int nrP)
{
    for (int i=1; i <= nrP-1; i++)
    {   
        int x,y;
        x=i-1;
        y=2*nrP-2*i-1;

        for (int j=1; j<=x; j++)
         cout << "^";
        for (int j=1; j<=1; j++)
         cout << "#";
        for (int j=1; j<=y; j++)
         cout << "^";
        for (int j=1; j<=1; j++)
         cout << "#"; 
        for (int j=1; j<=x; j++)
         cout << "^";         

         cout << endl;
    }
    // Last row
        for (int j=1; j<=nrP-1; j++)
         cout << "^";
        for (int j=1; j<=1; j++)
         cout << "#";
        for (int j=1; j<=nrP-1; j++)
         cout << "^";
    cout << endl;
} 


int main ()
{
    int nr;

    cout << "Number of rows : ";
    cin >> nr;
    cout << endl;
    drawShape(nr);
    cout << endl;

    return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This question has already been answered. Start a new discussion instead.