combinatorial function c++ using Recursion - c++

Now this is the combinatorial function if you don't know it:
C(n,k)= { 1 if k=0 or k = n
C(n−1,k−1)+C(n−1,k) otherwise
Now, What I really need is to use recursion to print a Pascal's triangle.
Ok,so what I've done so far is this simple recursion function:
#include <iostream>
using namespace std;
int Pas(int r, int c) {
if (c == 0 || c == r) {
return 1;
} else {
return Pas(r - 1, c - 1) + Pas(r - 1, c);
}
}
int main(){
cout << Pas(4,2) << endl;
return 0;
}
Now this function computes perfectly for example:
Pas(4,2) = 6
But I'm having problem using it to print the whole Pascal's triangle, because I'm new into C++ and especially recursion ..
I'd appreciate any feedback, and I hope that someone would help figure out this problem. But I'd appreciate it more if you guys don't just give me the whole answer (code) just like that; I want to learn.
Thanks!

Something similar to this might to the job
void printTriangle(int printRows, int a = 1, int b = 0)
{
if (a > printRows) return;
int val = Pas(a, b);
cout << val << " ";
if (a == b) {
cout << endl;
printTriangle(printRows, a + 1, 0);
} else {
printTriangle(printRows, a, b + 1);
}
}
Running printTriangle(7) should print the first 7 rows.

Tail recursion is the recursive equivalent to iterative loops. The following function when called with sum(0, 5)
int sum(int start, int end, int resultSoFar = 0) {
if (start == end) return resultSoFar;
return sum(start + 1, end, resultSoFar + start);
}
is equivalent to the iterative function called with sum(0, 5).
int sum(int start, int end) {
int resultSoFar = 0;
for (int i = start; i < end; i++) {
resultSoFar += i;
}
return resultSoFar;
}

If you are allowed to use your recursive function in a loop, the easiest way would be something like:
int n=4;
for (int i=0; i<=n; i++) {
for (int j=0; j<=i; j++) {
cout << Pas(i,j)<<" ";
}
cout <<endl;
}
If you want to reinforce the layout, you coud also #include <iomanip> and <limits> to use a fixed size number output, using the number of digits required to display an integer: just replace the output statement with:
cout << setw(numeric_limits<int>::digits10+1)<<Pas(i,j);
Edit:
You could easily build a recursive function to print lines of the triangle:
void PrintPas(int r) {
if (r==1)
cout << Pas(1,0);
else {
PrintPas(r-1);
for (int c=0; c<=r; c++)
cout << Pas(r,c)<< " ";
}
cout <<endl;
}
Edit 2
If you want a fully recursive version:
void PrintPas(int r, int c) {
if (r==1)
cout << Pas(1,0)<<" ";
else if (c==-1) {
PrintPas(r-1,r-1);
}
else {
PrintPas(r,c-1);
cout << Pas(r,c)<< " ";
}
if (r==c)
cout <<endl;
}
Online demo

Related

I want to sum all my vector indexes with a recursive function but starting backwards

I am stuck with this program so far, and I don't know what else to do.
I just need help with the recursive function so I can do the other modes too, which are multiplication and subtraction.
#include "std_lib_facilities.h"
//this contains all the libraries my university offers.
int sum(vector <int> test, int i){
if (i < 0){
return 0;
}
else {
return test[i] + sum(test, i - 1);
}
}
int main(){
int n;
int x;
int mode;
int result = 0;
cout << "give the size of the vector:\n";
cin >> n;
while (n < 0){
cout << "you can't give negative number!give again:\n";
cin >> n;
}
vector <int> myvector;
for (int i = 0; i <= n-1; i++){
cout << "give numbers to fill the vector:\n";
cin >> x;
myvector.push_back(x);
}
cout << "give 1 for sum 2 for multiplication 3 for substraction\n";
cin >> mode;
while (mode < 1 && mode > 3){
cout << "this mode doesn't exist.give again:\n";
cin >> mode;
}
if (mode == 1){
result = sum(myvector, n);
cout << "result is:" << result;
}
}
I want to get all the indexes starting from backwards to sum them all.
You are accessing outside the bounds of your array.
The simple fix to your issue is to pass n-1 to your sum function.
result=sum(myvector,n - 1);
However, there is no need for you to pass n at all, rather i would suggest you add a function that starts the recursion like this. (this is a rather common pattern for recursion)
int sum(const vector<int>& test, int i);
int sum(const vector<int>& test)
{
if(test.empty())
{
return 0;
}
if(test.size() == 1)
{
return test[0];
}
return sum(test, test.size() - 1);
};
int sum(const vector<int>& test, int i)
{
if(i < 0)
{
return 0;
}
return test[i] + sum(test, i - 1);
};
then call it like sum(myvector);

Unable to find the error in the code I wrote for a question on loops in C++. Could anyone point it out?

a beginner at coding here.
I was practising loops(c++) when I stumbled upon this problem:-
Write a program in C++ to find the perfect numbers between 1 and 500. (6,28 and 496)
Perfect number: It is a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3.
I wrote the following code:-
#include <iostream>
using namespace std;
int main() {
int n=2; //test numbers from 2 to 500.
int div=1; //divisor for n.
int sum=0; //sum of divisors which divide n.
while (n<=500) {
while (div<n){ //if div divides n, then it will added to sum and incremented, else only incremented.
if (n%div==0){
sum=sum+div;
div++;
} else{
div++;
}
}
if (sum==n){
cout<<n<<" is a perfect number."<<endl;
n++;
} else{
n++;
}
}
return 0;
}
The code is supposed to print that 6, 28 and 496 are perfect numbers.
But instead, it's not printing anything. Haven't been able to find the error yet after checking for 30+ minutes.
Could anyone point out the error?
You forget to re-initialize some variables in your loop.
for seems more appropriate than while here.
Create sub function also help to "identify" scope.
#include <iostream>
bool isPerfectNumber(int n)
{
int sum = 0;
for (int div = 1; div != n; ++div) {
if (n % div == 0) {
sum += div;
}
}
return sum == n && n > 0;
}
int main()
{
for (int i = 2; i != 501; ++i) {
if (isPerfectNumber(i)) {
std::cout << n << " is a perfect number." << std::endl;
}
}
return 0;
}
#include<iostream>
using namespace std;
bool perfect_num(int x);
int main() {
int m, n, x;
cout << "input the range m, n: " << "\n";
cin >> m >> n;
for (x = m; x <= n; ++x) {
if (perfect_num(x)) {
cout << x << " ";
}
}
return 0;
}
bool perfect_num(int x) {
bool flag = false;
//initialize
int sum = 0, i;
//loop 1 to x
for (i = 1; i < x; ++i) {
//judge whether is the factor
if (x % i == 0) {
sum += i;
}
}
//update flag
flag = (sum == x);
return flag;
}
#include<iostream>
using namespace std;
//judge function
bool isPerfectNum(int num){
int tmp = 0;
for (int i = 1; i < num; ++i) {
if (num % i == 0) {
tmp += i;
}
}
return tmp == num;
}
int main(){
cout << "Perfect Number contains: ";
for (int i = 1; i <= 500; ++i){
if (isPerfectNum(i)) {
cout << i << " ";
}
}
cout << "\n";
return 0;
}
at the end of your first loop, you should bring back div and sum to their default value.
int main() {
int n=2; //test numbers from 2 to 500.
int div=1; //divisor for n.
int sum=0; //sum of divisors which divide n.
while (n<=500) {
while (div<n){ //if div divides n, then it will added to sum and incremented, else only incremented.
if (n%div==0){
sum=sum+div;
div++;
} else{
div++;
}
}
if (sum==n){
cout<<n<<" is a perfect number."<<endl;
n++;
} else{
n++;
}
div = 1; // you should bring them back here.
sum = 0;
}
return 0;
}

Tournament Structure Sorting C++

Here I have this code that sorts a tournament structure like array in descending order. It sorts all but one number and always returns a -1 as the lowest integer it sorts, I have read through this code multiple times and I can't seem to figure out why it's not sorting properly, I'm not sure if it's just missing my eyes or if there is a small typo somewhere.
#include <iostream>
#include <cmath>
using namespace std;
int maxi(int i, int j)
{
if (i > j) return(i);
else return(j);
}
int mini(int i, int j)
{
if (i < j) return(i);
else return (j);
}
int buildtourn(int tourn[], int n)
{
int min1=0, a;
//Compute tournament structure
for (int i=2*n-2; i>1; i=i-2)
{
tourn[i/2] = maxi(tourn[i], tourn[i+1]);
a=mini(tourn[i], tourn[i+1]);
if (min1>a) min1=a;
}
return min1;
}
int getnext(int tourn[], int n, int low)
{
int i = 2;
//Part 1 - downward traversal
while (i <= 2*n-1)
{
if (tourn[i]>tourn[i+1])
{
tourn[i]=low;
i=2*i;
}
else
{
tourn[i+1]=low;
i=2*(i+1);
}
}
//Part 2 - upward traversal
for (i = i/2; i>1; i=i/2)
{
if (i%2==0) tourn[i/2]=maxi(tourn[i],tourn[i+1]); // go to the right of i
else tourn[i/2]=maxi(tourn[i], tourn[i-1]); // to the left of i
}
return 0;
}
int main()
{
int tourn[100], n, i, low;
//Read
cout << "Give n :" ;
cin >> n;
cout<< "Enter the integers to be sorted : " << endl;
for (i=n; i<=2*n-1; i++)
cin >> tourn[i];
//build tournament
low=buildtourn(tourn,n)-1;
//Sorting
cout << " Sorted items are : " << endl;
for(i=1; i<=n; i++)
{
cout << tourn[i] << '\t';
getnext(tourn,n,low);
}
cout << '\n';
return 0;
}
I believe the error lies solely in my function that builds the tournament structure but, i'm not quite sure if i'm looking in the wrong place.
int buildtourn(int tourn[], int n)
{
int min1=0, a;
//Compute tournament structure
for (int i=2*n-2; i>1; i=i-2)
{
tourn[i/2] = maxi(tourn[i], tourn[i+1]);
a=mini(tourn[i], tourn[i+1]);
if (min1>a) min1=a;
}
return min1;
}
Thank you in advance for any help and If I need to add anymore details to this problem please let me know in the comments.
EDIT: This is a link to view the output i am receiving.
http://imgur.com/a/KNDO8
EDIT 2: If i were to use the numbers 20 14 1 3 8 to be sorted, it would sort them as 20 8 1 3 -1
In mini maxi functions, replace < and > by <= and >=

C++ Vector Subscript out of Range Error 1221

I am trying to make a program that recieves numbers from the user, and then rearranges the from least to greatest. I am using vectors (which I just learned about), and it gives me a subscript out of range error. I am not able to find what part of the code gives me this error, so hopefully someone more knowledgeable on vector and c++ can find it:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void order(int a, int b);
void orderRev(int a, int b);
int main() {
vector<int> num;
bool going = true;
do {
cout << "\nEnter a number or type 'x' to order:" << endl;
string reply;
getline(cin, reply);
if (reply != "x") {
int a = atoi(reply.c_str());
num.push_back(a);
cout << "\nYou currently have " << num.size() << " numbers added." << endl;
}
else {
going = false;
}
} while (going);
for (int i = 0; i < num.size(); i++) {
order(num[i], num[i + 1]);
}
for (int i = num.size() - 1; i >= 0; i--) {
orderRev(num[i + 1], num[i]);
}
cout << "\nThe number you entered in order from least to greatest are: " << endl;
for (int i = 0; i < num.size(); i++) {
cout << num[i] << " ";
}
void order(int a, int b) {
if (a > b) {
int c = b;
b = a;
a = c;
}
}
void orderRev(int a, int b) {
if (a < b) {
int c = b;
b = a;
a = c;
}
}
Fix these lines to this:
// added the -1 as this will now go up to the 2nd to last element
// for `n`, and the last element for `n+1`
for (int i = 0; i < num.size() - 1; i++) {
order(num[i], num[i + 1]);
}
// changed the starting number to size -2 (for the same reasoning)
for (int i = num.size() - 2; i >= 0; i--) {
orderRev(num[i + 1], num[i]);
}
Why does this need to be this way? Think about how indices in C++ work. They are zero-indexed! That means that if you want both the element and the one in front of it, you must go up to the size of the vector minus 1. Hence, for a vector of 10 items (size 10), at i == 9 your code will work like this:
for (int i = 0; i < num.size(); i++) {
// i = 9
order(num[9], num[9+1]);// index 10 does not exist! Hence, you really need to go up to num.size() - 1!
}
Vectors index start with 0. index will be 0 to n-1 , if you use num[i + 1] it will exceed the vector size, if you don't check in loop condition.
Your code has more than one flaw. The output will be same as the input , hint: know the difference between pass by reference and pass by value and after that check some sorting algorithms.

Fibonacci series

I am trying to do an exercise with the Fibonacci series.
I have to implement with a recursive function, a succession of the prime n number of Fibonacci and print them
in the same function. The problem is that my function print also the intermediate number.
The results, for example, for n = 6, should be : 1 1 2 3 5 8.
Any solutions?
Thanks
#include<iostream>
using namespace std;
int rec(int n)
{
int a, b;
if (n == 0 || n == 1)
{
return n;
}
else
{
a = rec(n - 1);
b = rec(n - 2);
cout << a + b << endl;
return a + b;
}
}
int main()
{
int n = 6;
rec(n);
return 0;
}
I have taken help of static int. That worked the way you wanted.
void rec(int n)
{
static int a=0,b=1,sum;
if(n>0)
{
sum = a+b;
a=b;
b= sum;
cout<<sum<<" ";
rec(n-1);
}
}
Though you have to print the first Fibonacci number yourself in main().
cout<<"0 ";
rec(n);
You can use this:
#include<iostream>
using namespace std;
#define MAXN 100
int visited[MAXN];
int rec(int n)
{
if(visited[n])
{
return visited[n];
}
int a, b;
if (n == 0|| n==1)
{
return n;
}
else
{
a = rec(n - 1);
b = rec(n - 2);
cout << " " <<a + b;
return visited[n] = a + b;
}
}
int main()
{
int n = 6;
cout<< "1";
rec(n);
cout<<endl;
return 0;
}
This implementation uses dynamic programming. So it reduces the computation time :)
Because you are printing in rec, its printing multiple times because of recursion. No need to print in the recursive function. Instead print the result in main
#include<iostream>
using namespace std;
int rec(int n)
{
int a, b;
if (n == 0 || n == 1)
{
return n;
}
else
{
a = rec(n - 1);
b = rec(n - 2);
//cout << a + b << endl;
return a + b;
}
}
int main()
{
int n = 6;
for (int i = 1; i <= n; i++)
{
cout << rec(i) << endl;
}
system("pause");
return 0;
}
I'm pretty sure you have gotten working solutions but I have a slightly different approach that doesn't require you to use data structures:
/* Author: Eric Gitangu
Date: 07/29/2015
This program spits out the fibionacci sequence for the range of 32-bit numbers
Assumption: all values are +ve ; thus unsigned int works here
*/
#include <iostream>
#include <math.h>
#define N pow(2.0,31.0)
using namespace std;
void fibionacci(unsigned int &fib, unsigned int &prevfib){
unsigned int temp = prevfib;
prevfib = fib;
fib += temp;
}
void main(){
int count = 0;
unsigned int fib = 0u, prev = 1u;
while(fib < N){
if( fib ==0 ){
fib = 0;
cout<<" "<< fib++ <<" \n ";
continue;
}
if( fib == 1 && count++ < 2 ){
fib = 1;
cout<< fib <<" \n ";
continue;
}
fibionacci(fib, prev);
cout<< fib <<" \n ";
}
}
Try this recursive function.
int fib(int n)
return n<=2 ? n : fib(n-1) + fib(n-2);
It is the most elegant solution I know.