can anyone help me with this output:
^^^^^#^^^^^
^^^^###^^^^
^^^#####^^^
^^#######^^
^#########^
###########

i have done the following so far & am struggling to continue:
#include <iostream>
using namespace std;

//Function draw shape
void displayRow(int sizeP)
{
for(int i = 1; sizeP - i + 1; i++)
cout << '^';
//for (int j = 1; sizeP 2 * j - 1; j++)
//cout << '#';
}
int main()
{
displayRow(7);

return 0;
}

Recommended Answers

All 7 Replies

HI,
Just a pointer, you should put code tags around your code so its easy to read.

you should try using an overloaded displayRow() function that takes in an int for the size, an int for the hash and an int for the caret.
so like:

void displayRow(int sizeP) { int caret = sizeP; int hash = 1; //...code to print the first line displayRow(sizeP, hash +2, caret -2); } void displayRow(int sizeP, int hash, int caret) { //...code to print the next line displayRow(sizeP, hash +2, caret-2); [/CODE=CPP] I know its might not be the best way, but it works.[CODE=CPP]void displayRow(int sizeP)
{
int caret = sizeP;
int hash = 1;
//...code to print the first line
displayRow(sizeP, hash +2, caret -2);
}
void displayRow(int sizeP, int hash, int caret)
{
//...code to print the next line
displayRow(sizeP, hash +2, caret-2);
[/CODE=CPP]
I know its might not be the best way, but it works.

LOL sorry about the code tags.

void displayRow(int sizeP)
{
    int caret = sizeP;
    int hash = 1;
    //...code to print the first line
displayRow(sizeP, hash +2, caret -2);
}
    void displayRow(int sizeP, int hash, int caret)
    //...code to print the next line
    displayRow(sizeP, hash +2, caret-2);
}

Is sizeP the height of the pyramid you want?

You can print line by line the square containing the pyramid. You already know the form of the pyramid you want in that square. You surely have noticed that the number of characters on the first level of your pyramid is the sizePth odd number. It's also easy to see that the top line has the top of pyramid at the middle position, exactly at (number of characters at bottom of pyramid / 2 - 1). Let me explain further with an example:

If I want a pyramid with 3 levels, the first level will have 5 characters (2 * 3 (3rd level) - 1).

You surely have noticed that the number of ^ diminishes by 1 on each side at each level of the pyramid and the number of # is up by 2 at each level.

Let me suggest a solution: at beginning, fill a string for the ^ and another for the # (1 # only). You can know how much ^ to put, if you know how much # will be at the bottom of the pyramid (this number - 1 and you divide by 2 for each part.) Loop for each level, print the ^, the # and again the ^, remove a ^ and add two # on the strings and continue...

That's very easy to do... but if you have a better idea than me, do it!

sorry made a mistake in the code

void displayRow(int sizeP)
{
    int caret = sizeP;
    int hash = 1;
    //...code to print the first line
    displayRow(sizeP, hash +2, caret -2);
}
void displayRow(int sizeP, int hash, int caret)
{
    //...code to print the next line
    //...code to terminate the loop
    displayRow(sizeP, hash +2, caret-2);
}

Right off the bat you can see that there are two problems that will cause you a massive pain. The first problem being you dont have an cout<<endl; anywhere, how are you going to get to the next line? secondly, if you look at the pattern, you can see that the total # increases every two while the total ^ decreases by two. Now you also know that the ^ needs to come twice; before and after the #, so all you need are initial values for ^ and #. So all you need to know for any input(must be odd however) is input-1 and 1. For the first loop output ^ (input-1)/2 once, then # once, the ^ (input-1)/2 again. Go to the next line, subtract 1 from the ^ count and add two to the # count, print them in the same order, rinse, repeat.

Can you explain why it's a post about void functions?

void Shape(int r, char c1 = '^', char c2 = '#')
{
    for (int i = 0, n = r+r; i <= r; i++) {
        for (int j = 0; j <= n; j++)
            cout << (abs(j-r)>i?c1:c2);
        cout << '\n';
    }
}
...
Shape(5);

How can one solve this; the bolded expressions are confusing

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

Hint: Use for loops. Suppose sizeP contains the size. Then the i-th row should start with (sizeP - i + 1)
^ characters, followed by (2 * i - 1) # characters, followed by another (sizeP - i + 1) ^ characters.
There should be (2 * sizeP + 1) # characters in the last row.

// layout of the program
#include <iostream>
using namespace std;
// The required function drawShape should be inserted here.
int main( )
{
int size;
cout << "Size: ";
cin >> size;
drawShape(size);
return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.