void function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 6
Reputation: shahanakazi is an unknown quantity at this point 
Solved Threads: 0
shahanakazi shahanakazi is offline Offline
Newbie Poster

void function

 
0
  #1
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 147
Reputation: necrolin is on a distinguished road 
Solved Threads: 14
necrolin's Avatar
necrolin necrolin is offline Offline
Junior Poster

Re: void function

 
0
  #2
Sep 18th, 2009
You'll need 2 for loops (nested). 1 will be for the width and one for the height.

  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 #.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: void function

 
0
  #3
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 31
Reputation: codeguru_2009 is on a distinguished road 
Solved Threads: 6
codeguru_2009 codeguru_2009 is offline Offline
Light Poster

Re: void function

 
0
  #4
Sep 18th, 2009
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: willywonka is an unknown quantity at this point 
Solved Threads: 1
willywonka's Avatar
willywonka willywonka is offline Offline
Newbie Poster

Re: void function

 
0
  #5
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 31
Reputation: codeguru_2009 is on a distinguished road 
Solved Threads: 6
codeguru_2009 codeguru_2009 is offline Offline
Light Poster

Re: void function

 
0
  #6
Sep 19th, 2009
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

Re: void function

 
0
  #7
Sep 19th, 2009
Originally Posted by willywonka View 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.
Last edited by Barefootsanders; Sep 19th, 2009 at 11:43 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: shahanakazi is an unknown quantity at this point 
Solved Threads: 0
shahanakazi shahanakazi is offline Offline
Newbie Poster

Re: void function

 
0
  #8
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,188
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 148
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: void function

 
0
  #9
Sep 19th, 2009
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 :

  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.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 6
Reputation: shahanakazi is an unknown quantity at this point 
Solved Threads: 0
shahanakazi shahanakazi is offline Offline
Newbie Poster

Re: void function

 
0
  #10
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC