4 Replies - 6364 Views - Last Post: 16 July 2013 - 08:52 AM Rate Topic: -----

#1 kmk13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-July 13

Pascal triangle c++ recursively problem

Posted 15 July 2013 - 10:03 PM

Hi there,

I wish you can help me with this coding issue. I'm trying to implement a code for a c++ pascal traingle using recursion technique with this main function :
#include <iostream>
#include <cstdlib>

using std::cin;
using std::cout;
using std::endl;

int *pascal(int);

int main(int argc, char *argv[]) {

    int num = argc == 2 ? atoi(argv[1]) : 1;

    for (int i = 1; i <= num; ++i) {
        int *a = pascal(i);

        cout << (i < 10 ? " " : "") << i << ": ";

        for (int j = 0; j < i; ++j) {
            cout << a[j] << ' ';
        }
        cout << endl;
    }

    return 0;
}


I need to implement a pointer to int function that takes an argument 'n' and
returns the n-th line of Pascal's Triangle, something like this :
int *pascal(int n) {

    return new int[n];
}


so I trying to implement the funtion using a recursive call, i came up with this:
int *pascal(int n)   {
 	
	if ( n == 0)
		return (1);
	else 
		return n * (pascal(n-1));
	
}


I keep getting 2 errors : cannot convert ‘int*’ to ‘int*(int)’ in assignment...and....invalid operands of types ‘int’ and ‘int*’ to binary ‘operator*’

I have to use this pointer function, pointers do confuse me a lot...not sure what is the problem, what should this function return (my thinking is a reference may be !) in order to be used with my main function to print out pascal triangle ? looking forward to your response....Thank you.

This post has been edited by macosxnerd101: 15 July 2013 - 10:08 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Pascal triangle c++ recursively problem

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Pascal triangle c++ recursively problem

Posted 15 July 2013 - 10:09 PM

I'm not a C++ guy, but the fact that you're doing this recursively doesn't make sense.

The nth row of Pascal's triangle is generated by the individual terms in the sequence for the binomial theorem. So the ith element in the nth row is C(n, i), where C(n, i) represents the choose function.
Was This Post Helpful? 1
  • +
  • -

#3 kmk13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-July 13

Re: Pascal triangle c++ recursively problem

Posted 15 July 2013 - 10:18 PM

I know there is other ways around it, but my assignment requires me to do it recursively using this stupid pointer function that takes only one int argument. I think the code is right but i can't figure out this invalid operands of types ‘int’ and ‘int*’ to binary ‘operator*’ error, thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#4 kmk13   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-July 13

Re: Pascal triangle c++ recursively problem

Posted 15 July 2013 - 11:34 PM

My problem is in the pascal class :
int *pascal(int n) {

if ( n == 1 || n == 0)
return new int[1];
else
return n * (pascal(n-1));

}
 


I keep getting the following error:
invalid operands of types ‘int’ and ‘int*’ to binary ‘operator*, what's wrong here?
Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 409
  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Pascal triangle c++ recursively problem

Posted 16 July 2013 - 08:52 AM

You are mixing up types. Your function needs to return an int pointer (or an array of integers). Your last line says to return an integer. That being said, you are pretty far off from getting the correct results. Here's an outline of what you should be shooting for.

int * pascal(int n)
{
	int *p;  //represents the current line.
	if(n<2)  //base case.  may want to add other validation
	{
		p = new int[1];
		p[0] = 1;
	}
	else
	{
		p=new int[n]; //the nth line has n items;
		int * prevP = pascal(n-1); //gets the previous line.  If you have the previous line, you can fill out the current line

		/*Add logic to populate p based on the values in prevP*/
	}
	return p; //returns the current line.
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1