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);



}

Anyone? Please give some input if you have any knowledge on this subject. We are pressed for time. Thanks

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...

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... :icon_wink:

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.

#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

PS: Hope he'll be better soon. And I hope he's doing most of these projects... :icon_wink:

Thanks for the help, I am trying to put something together right now.

I know this a long dead subject but... if anyone else has this problem like I did!
here is the solution

#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
}

Helps this help others out

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.