| | |
Recursion help....PLEASE!!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 67
Reputation:
Solved Threads: 0
Hi there,
My son has been recently hospitalized after getting into an accident. Now I am trying to help him with some of his projects. Right now I am stuck on this recursion problem. We need to figure out how to form a diamond shape out of *'s using a recursive function. In my code here, I am using two recursive functions, because I cannot figure out how to use just one. Also, if you run this program, you will realize the top half of the diamond works, and the bottom half doesn't seem to work correctly. Please take a look at my code and REMEMBER, I am completely new to this. I don't even work with computers and I am trying to catch on quick to help my son keep up with his school work while he is hospitalized for the next couple of weeks. Your help is much appreciated, and please try not to laugh at my effort : ) Below is the diamond shape that I am trying to accomplish, and the code that I am using.
....*....
...* *...
..* * *..
.* * * *.
..* * *..
...* *...
....*....
The periods are just spaces.
#include <iostream>
using namespace std;
int printup(int m);
int print(int n);
int main(void)
{
printup(4);
print(3);
return 0;
}
int print(int n)
{
if(n == 0)
return 0;
for (int i = 0; i < n; i++)
if(i<=n)
cout << "* ";
else
cout << " ";
cout <<endl;
n = print(--n);
}
int printup(int m)
{
if (m == 0)
return 0;
for (int i = 0; i < 5; i++)
if(i>=m)
cout << "* ";
else
cout << " ";
cout << endl;
m = printup(--m);
}
My son has been recently hospitalized after getting into an accident. Now I am trying to help him with some of his projects. Right now I am stuck on this recursion problem. We need to figure out how to form a diamond shape out of *'s using a recursive function. In my code here, I am using two recursive functions, because I cannot figure out how to use just one. Also, if you run this program, you will realize the top half of the diamond works, and the bottom half doesn't seem to work correctly. Please take a look at my code and REMEMBER, I am completely new to this. I don't even work with computers and I am trying to catch on quick to help my son keep up with his school work while he is hospitalized for the next couple of weeks. Your help is much appreciated, and please try not to laugh at my effort : ) Below is the diamond shape that I am trying to accomplish, and the code that I am using.
....*....
...* *...
..* * *..
.* * * *.
..* * *..
...* *...
....*....
The periods are just spaces.
#include <iostream>
using namespace std;
int printup(int m);
int print(int n);
int main(void)
{
printup(4);
print(3);
return 0;
}
int print(int n)
{
if(n == 0)
return 0;
for (int i = 0; i < n; i++)
if(i<=n)
cout << "* ";
else
cout << " ";
cout <<endl;
n = print(--n);
}
int printup(int m)
{
if (m == 0)
return 0;
for (int i = 0; i < 5; i++)
if(i>=m)
cout << "* ";
else
cout << " ";
cout << endl;
m = printup(--m);
}
For starters, you typed right over the instructions on using code tags to make the code readable, and obviously didn't read the post titled "Read Me: Read This Before Posting"...
The recursive function must:
when called,
1) output a specified number of *s and SPACEs.
2) test if the value is at a defined end value
2a) if not, call again with the next value
2b) if so, return
when returned
1) output a specified number of *s and SPACEs exactly as above
2) return
That should do it...
The recursive function must:
when called,
1) output a specified number of *s and SPACEs.
2) test if the value is at a defined end value
2a) if not, call again with the next value
2b) if so, return
when returned
1) output a specified number of *s and SPACEs exactly as above
2) return
That should do it...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 67
Reputation:
Solved Threads: 0
Thanks for the reply. I am all new to this stuff as I stated. Being a 49 year old guy who has never touched a computer really puts me at a disadvantage. I understand kind of what you are trying to tell me what to do in your instructions. But I have no idea how to actually go about doing that. If you could show me some example I would be so grateful. And sorry for not making the code readable, I had no idea that I had to do anything to make it readable.
Yep. You're at a disadvantage alright... 
What I can do for you is give you a sample of a recursive function you can look at. It's up to you to think through the changes for your program.
Output:
PS: Hope he'll be better soon. And I hope he's doing most of these projects...
What I can do for you is give you a sample of a recursive function you can look at. It's up to you to think through the changes for your program.
c Syntax (Toggle Plain Text)
#include <stdio.h> void recursiveFunction(int val) { // The working area of the function printf("called value is %d \n", val); val--; // decrement val // The test of the end-value // In this simple example it's simply waiting until val becomes 0 if (val > 0) { recursiveFunction(val); // >0 call again } else { return; // <= 0, done } // Returns will come here and val will be restored to // it's previous value // Do the next 'work' printf("return value is %d \n", val); return; } int main() { recursiveFunction(5); // start the function off return 0; }
Output:
•
•
•
•
called value is 5
called value is 4
called value is 3
called value is 2
called value is 1
return value is 1
return value is 2
return value is 3
return value is 4
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
I know this a long dead subject but... if anyone else has this problem like I did!
here is the solution
Helps this help others out
here is the solution
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void spaces(int x) //I'm thinking, everybody else is going to do a diamond full of stars. So why not //put spaces in mine and show the outline of the shape { for (int c = 1; c <= x; c++) cout << " ";//the spaces } void stars(int x)//the stars { for (int c = 1; c <= x; c++) { cout << "*"; } } void row(int r, int rows) //print out each row { spaces (rows - r); stars (1);//outline of diamond stars (2 *(r - 1));//fills up inside of daimond. stars(1); cout << endl; } void top_row(int rows) { spaces (rows); stars (1); cout << endl; } int getDiff(int X, int Y) { // Tail Implementation X++; // Inc X if (X == Y) return 1; return (1+getDiff(X, Y)); } void getDiff2(int X, int Y, int Diff = 0) //subtracting x from y { X++; Diff++; if (X != Y) getDiff2(X, Y, Diff); else cout << "Diff Was: " << Diff << endl; return; } int main(void) { int r, rows; cout << "Size of diamond? ";//input the number cin >> rows; cout << "Difference Between 1 & 7 Is: " << getDiff(1, 7) << endl;//getting the difference between 1 and 7 top_row(rows); for (r = 1; r <= rows; r ++) row(r, rows); for (r = rows - 1; r >= 1; r --) row (r, rows);//This is where the equation for recursions takes place top_row(rows); return 0; //end program }
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C++ Forum
- Previous Thread: starter in visual c++ need help........
- Next Thread: how to fix compile error
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






