Laboratory 5 Solutions
Solution to pascal.cc code:
/*******************************************************************
* Name: Brenda Student
* Date: 3/12/14
* Course: CSCI 132 Section 01
* Assignment: Lab 5
* Instructor: Royden
* Program: pascal.cc
* Purpose: A program that calls a recursive function to compute the coffecient
* of pascal's triangle for position (n, k)
***************************************************************************/
#include <iostream>
using namespace std;
int coeff(int, int);
int main(void) {
int num; //number of rows in pascal triangle
cout << "Enter an integer greater than zero: ";
cin >> num;
for ( int n = 0; n < num; n++) {
for (int k = 0; k <= n; k++) {
cout << coeff(n, k) << " ";
}
cout << endl;
}
}
/*Recursively compute pascal coefficient*/
int coeff(int n, int k) {
if ((k == 0) || (k == n)) {
return 1;
} else {
return coeff(n-1, k) + coeff(n-1, k-1);
}
}
Home | | Syllabus | | Assignments | | Lecture Notes