| | |
void function
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
You'll need 2 for loops (nested). 1 will be for the width and one for the height.
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 #.
C++ Syntax (Toggle Plain Text)
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 #.
•
•
Join Date: Aug 2009
Posts: 31
Reputation:
Solved Threads: 6
c++ Syntax (Toggle Plain Text)
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; }
Last edited by codeguru_2009; Sep 18th, 2009 at 12:27 pm. Reason: syntax highlighting
•
•
Join Date: Aug 2009
Posts: 31
Reputation:
Solved Threads: 6
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.
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.
(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)
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; }
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
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.
Last edited by Barefootsanders; Sep 19th, 2009 at 11:43 am.
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
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;
}
#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.
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 :
One problem with your function is that you never declared s1 and s2.
Below code fixes that :
C++ Syntax (Toggle Plain Text)
#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; }
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
![]() |
Similar Threads
- Conversion error passing Vector as a pointer to void function... (C++)
- Weird error by a void function (C++)
- Void function does not return the value (C++)
- Question on non-void function (C++)
- void function (C++)
Other Threads in the C++ Forum
- Previous Thread: [HELP]Adding string between text.
- Next Thread: glut still not working... much
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






