943,729 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1136
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 18th, 2009
0

void function

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahanakazi is offline Offline
12 posts
since Sep 2009
Sep 18th, 2009
0

Re: void function

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

C++ Syntax (Toggle Plain Text)
  1. for (int column = 1; column < n; column++) {
  2. for (int row = 1; row < n; row++){}
  3. }

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 #.
Reputation Points: 105
Solved Threads: 25
Posting Whiz in Training
necrolin is offline Offline
223 posts
since Jun 2009
Sep 18th, 2009
0

Re: void function

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Sep 18th, 2009
0

Re: void function

c++ Syntax (Toggle Plain Text)
  1. void drawshape(int row)
  2. {
  3. int i;
  4.  
  5. for (i=1; i < row; i++)
  6. cout<<setw(i+1)<<'#'<<setw(row*2-i*2)<<' '<<'#'<<endl;
  7. cout<<setw(i+1)<<'#'<<endl;
  8. }
Last edited by codeguru_2009; Sep 18th, 2009 at 12:27 pm. Reason: syntax highlighting
Reputation Points: 46
Solved Threads: 6
Light Poster
codeguru_2009 is offline Offline
31 posts
since Aug 2009
Sep 19th, 2009
0

Re: void function

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
willywonka is offline Offline
6 posts
since Sep 2009
Sep 19th, 2009
0

Re: void function

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.
c++ Syntax (Toggle Plain Text)
  1. void drawshape(int row)
  2. {
  3. int i;
  4.  
  5. for (i=1; i < row; i++)
  6. cout<<setw(i)<<'#'<<setw(row*2-i*2)<<cout.fill('^')<<'#'<<setw((i -1))<<cout.fill('^')<<endl;
  7. cout<<setw(i)<<'#'<<setw(i-1)<<cout.fill('^')<<endl;
  8. }
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.
Reputation Points: 46
Solved Threads: 6
Light Poster
codeguru_2009 is offline Offline
31 posts
since Aug 2009
Sep 19th, 2009
0

Re: void function

Click to Expand / Collapse  Quote originally posted by willywonka ...
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.
Last edited by Barefootsanders; Sep 19th, 2009 at 11:43 am.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Sep 19th, 2009
0

Re: void function

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;
}
Last edited by shahanakazi; Sep 19th, 2009 at 12:49 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahanakazi is offline Offline
12 posts
since Sep 2009
Sep 19th, 2009
0

Re: void function

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 :

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void drawShape(int nrP)
  5. {
  6. int s1 = 0,s2 = 0; //(NEW)
  7.  
  8. for (int i = 1; i <= nrP; i++)
  9. {
  10. s1 = i - 1;
  11. s2 = (2 * nrP - 2 * i - 1);
  12.  
  13. for (int j = 1; j <= s1; j++)
  14. cout << '^';
  15.  
  16. cout << '#';
  17.  
  18. for (int j = 1; j <= s2; j++)
  19. cout << '^';
  20.  
  21. cout << '#';
  22.  
  23. for (int j = 1; j <= s1; j++)
  24. cout << '^';
  25.  
  26. cout << endl;
  27. }
  28. }
  29.  
  30. int main()
  31. {
  32. int nr = 0;
  33.  
  34. cout << "Number of rows: ";
  35. cin >> nr;
  36. drawShape(nr);
  37.  
  38. return 0;
  39. }
Last edited by firstPerson; Sep 19th, 2009 at 1:12 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 19th, 2009
0

Re: void function

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shahanakazi is offline Offline
12 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: [HELP]Adding string between text.
Next Thread in C++ Forum Timeline: glut still not working... much





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC