954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

void functions

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

i have done the following so far & am struggling to continue:
#include
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;
}

RoselineM
Newbie Poster
2 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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:

[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.

Danny_501
Junior Poster
109 posts since Mar 2009
Reputation Points: 36
Solved Threads: 4
 

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);
}
Danny_501
Junior Poster
109 posts since Mar 2009
Reputation Points: 36
Solved Threads: 4
 

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!

GDICommander
Posting Whiz in Training
211 posts since Jun 2008
Reputation Points: 72
Solved Threads: 26
 

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);
}
Danny_501
Junior Poster
109 posts since Mar 2009
Reputation Points: 36
Solved Threads: 4
 

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<

SeeTheLite
Junior Poster
109 posts since Mar 2009
Reputation Points: 38
Solved Threads: 13
 

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);
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

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
using namespace std;
// The required function drawShape should be inserted here.
int main( )
{
int size;
cout << "Size: ";
cin >> size;
drawShape(size);
return 0;
}

DangerDuke
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You