Hi,
I'm a beginner of C++ programming. I am trying to write a void funtion for the following question but in vain. Please help me.
Here's the question:
Suppose we want to display the following figure containing the alphabetic character V on yhe screen:
#^^^^^^^^^#
^#^^^^^^^#^
^^#^^^^^#^^
^^^#^^^#^^^
^^^^#^#^^^^
^^^^^#^^^^^
We want to be able to change the number of rows. In the figure above the number of rows is 6. If the number of rows is 4. the figure will look as follows:
#^^^^^#
^#^^^#^
^^#^#^^
^^^#^^^
Write a void function drawShape that displays such a figure on the screen. There should be one parameter (of type int), namely the number of rows as indicated above.
The hint i have here is:
Use for loops. Suppose nrP contains the number of rows. Then the ith row should consist of (i - 1)^ characters, followed by one #character, followed by (2 * nrP - 2 * i - 1)^ characters, followed by another # character, followed by another (i - 1)^ characters. The last row however, should contain only one # character.

Here's what i have written till now:
void drawShape (int nrP)
{
for (int i = 1); i <= n; i++)
cout << '#' << '^' << endl;
}
But the above does not work of course.

Recommended Answers

All 20 Replies

You'll need 2 for loops (nested). 1 will be for the width and one for the height.

for (int column = 1; column < n; column++) {
    for (int row = 1; row < n; row++){}
}

Inside the second for loop (rows) you'll have to implement the formulas they gave you in the book/assignment. Use something like if statements to calculate whether it should print a ^ or a #.

It would probably be easier to generate a small 'V' by hand and figure out what coordinates that the '#' lands on. Then you can decipher the pattern and come up with what you would need to be put inside the for loops. Sometimes it's easier to do things by hand just so you can visualize it.

void drawshape(int row)
{
   int i;

   for (i=1; i < row; i++)
       cout<<setw(i+1)<<'#'<<setw(row*2-i*2)<<' '<<'#'<<endl;
       cout<<setw(i+1)<<'#'<<endl;
}

the answer is in the hint. I'll add that you need to use the hint for five loops, and for the last line use a separate set of three loops. The answer is right there. look at it again. It is not as hard as you think, I promise.
Each character in the line should have its own loop.

my earlier post was able to print the required shape but '^' was replaced by space.
(row provided should fit to screen, otherwise side effects).

The code below again shows that same thing can be done in one for loop, giving an idea how c++ is rich in features.

void drawshape(int row)
{
   int i;

   for (i=1; i < row; i++)
       cout<<setw(i)<<'#'<<setw(row*2-i*2)<<cout.fill('^')<<'#'<<setw((i -1))<<cout.fill('^')<<endl;
   cout<<setw(i)<<'#'<<setw(i-1)<<cout.fill('^')<<endl;
}

If last line setw(i-1) is repalced by setw(i), the output will be in more or less square shape, otherwise as ask in post.

the answer is in the hint. I'll add that you need to use the hint for five loops, and for the last line use a separate set of three loops. The answer is right there. look at it again. It is not as hard as you think, I promise.
Each character in the line should have its own loop.

yeah, you could do it that way but it is highly inefficient. there is one way that you can do this using 2 for loops and a simple if/else statement. however codeguru_2009 has an even more efficient solution than mine.

I've been able to write the following program but there is still some problem. Can you help please?

#include <iostream>
using namespace std;

void drawShape(int nrP)
{
            for (int i = 1; i <= nrP; i++)
               {
               s1 = i - 1;
               s2 = (2 * nrP - 2 * i - 1);    
               for (int j = 1; j <= s1; j++)
                   cout << '^';
                   cout << '#';
               for (int j = 1; j <= s2; j++)
                   cout << '^';
                   cout << '#';
               for (int j = 1; j <= s1; j++)
                   cout << '^';
                   cout << endl;
               }    
}

int main()
{
    int nr;

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

    return 0;
}

Here is your code indented with code tags.

One problem with your function is that you never declared s1 and s2.

Below code fixes that :

#include <iostream>
using namespace std;

void drawShape(int nrP)
{
	int s1 = 0,s2 = 0;  //(NEW)

	for (int i = 1; i <= nrP; i++)
	{
		s1 = i - 1;
		s2 = (2 * nrP - 2 * i - 1); 
	
		for (int j = 1; j <= s1; j++)
			cout << '^';
		
		cout << '#';
	
		for (int j = 1; j <= s2; j++)
			cout << '^';
	
		cout << '#';

		for (int j = 1; j <= s1; j++)
			cout << '^';
	
		cout << endl;
	} 
}

int main()
{
       int nr = 0;

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

return 0;
}

Thank you very much firstPerson but there's still a little problem. In the last row two '#' is being displayed instead of one. And i am not being able to figure out the problem.

I used the nested for() loops for it and I got it to work fine for any input.

This is just the function:

void drawShape(int nrP)
{
	for(int r = 0; r < nrP; r++)
	{
		for(int c = 0; c < (nrP*2-1); c++)
		{
			if( r == nrP-1 && c == ((nrP*2-1)-1)/2) //last r and middle c
			{
				cout << '#';
			}
			else if( c == r || c == ((nrP*2-1)-1) - r ) //1st and last point on the V shape on the row
			{
				cout << '#';				
			}
			else //not a point on the V shape
			{
				cout << '^'; 
			}
			if( c == (nrP*2-1)-1) //end of the r
			{
				cout << endl;
			}
		}
	}
}

This would also work with recursion :

void Pattern(int Row, int Left = 0)
{
	int Right = Row - Left - 1;

	const static int Mid = (Right + Left) / 2;

	if(Left > Right ) return;

	for(int i = 0; i < Row; i++)
	{
		if(i == Left) 
			cout<<"^"<<" ";
		else if(i == Mid)
			cout<<"^"<<" ";
		else if(i == Right)
			cout<<"^"<<" ";
		else cout<<"*"<<" ";
	}

	cout<<endl;;

	Pattern(Row,Left+1);
}

But i would like to know what is the problem with what firstPerson gave in the first reply. Y it doesnt work for the last row only?

It is because all of the upper rows output 2 '#'s and he didn't put in if s1 == s2 then do not output a 2nd '#'.

in firstPerson code there is cout<<'#' at line 16 and 21 which will always get printed.
so a if else check will be required on last row.

I've been trying to put in the if else statement but it is changing the shape. I think i made a mistake in putting in the statement and i need help.

Post your code and I'll have a look.

If you are using firstPerson's Pattern() function I'm not sure if it was intentional but it draws like so.
Input 5 for rows:
^***^***^
*^**^**^*
**^*^*^**
***^^^***
****^****

To get rid of the line down the middle I just put

else if(i == Mid && i == Row-1)

since the only place that one '^' will occur is on the last row.

Here is the code:

//Assignment 2 Question 4a
#include <iostream>
using namespace std;

void drawShape(int nrP)
{
    int s1 = 0, s2 = 0;

    for (int i = 1; i <= nrP; i++)
    {
        s1 = i - 1;
        s2 = (2 * nrP - 2 * i - 1);

        for (int j = 1; j <= s1; j++)
                cout << '^';

        cout << '#';

        for (int j = 1; j <= s2; j++)
                cout << '^';

        cout << '#';

        for (int j = 1; j <= s1; j++)
                cout << '^';

        cout << endl;
    }
}

int main()
{
    int nr = 0;

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

    return 0;
}                    

Only the last row does not match. Two '#' appear instead of only one in the last row. I need to adjust this code.

First of all, you should learn how to click the code button to put code tags around your code.

Second two of us posted fixes for this already but if you really want to use this very first post then read the comments below for the change in code.

#include <iostream>
using namespace std;

void drawShape(int nrP)
{
int s1 = 0,s2 = 0;  //(NEW)

for (int i = 1; i <= nrP; i++)
{
    s1 = i - 1;
    s2 = (2 * nrP - 2 * i - 1); 

    for (int j = 1; j <= s1; j++)
        cout << '^';

    cout << '#';

    for (int j = 1; j <= s2; j++)
        cout << '^';
    if( i != nrP ) //this says if it is on the last line then do not output
        cout << '#';

    for (int j = 1; j <= s1; j++)
        cout << '^';

    cout << endl;
} 
}

int main()
{
   int nr = 0;

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

    return 0;
}
commented: Starting to help out, eh? +5

Hey. Seems like you've made some progress. Thanks great! Below is my solution but as noted previously, there are many. Hope it helps!

/**
 * @file printV.c
 * @description Prints a V shape.
 **/

#include <stdio.h>

void printV(int);

int main() {
	printV(4);  // Change # to change the number of rows.
}

void printV(int numRows) {
	int i,j;
	int cols = (numRows * 2) - 1;
	
	char vChar = '#';
	char emptyChar = ' ';

	for(i=1; i <= numRows; i++) {
		for(j=1; j <= cols; j++) {
			if(i==j || i+j == numRows*2)
				printf("%c", vChar);
			else
				printf("%c", emptyChar);
		}
		printf("\n");
	}
}
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.