Lab 8 Solution


Category: Tag:


Blaise Pascal was a French mathematician and philosopher who lived in the mid-1600’s. He accomplished much in his lifetime, including the construction of a mechanical calculator and some very early work in combinatorics and the development of differential calculus. In his studies, he devised what has become known as Pascal’s Triangle, which simplifies the calculation of the coefficients of the expression (x + y)n, where n is a positive integer. That is, we all know

(x + y)2 = x2 + 2xy + y2

(implying coefficients of 1,2,1), but what are the coefficients of (x + y)3 or (x + y)4 or (x + y)10 By using Pascal’s Triangle, these values can be quickly calculated.

The first part of Pascal’s Triangle looks like this:


  1. 1

  1. 2 1

  1. 3 3 1

  1. 4641

You can see, except for the 1’s, any coefficient is found simply by adding together the 2 numbers diagonally above it. (And, if you think about it, picture 0’s everywhere along the outside edge, and the 1’s can be calculated the same way!)

Write a lab8.c that calculates and displays the first k rows of Pascal’s Triangle. Where k is a value entered by the user. It must be displayed in the format shown above (not symmetrically)

As always I have provided: o an unchangeable main o an unchangeable lab8.h

You can’t use any statically sized arrays anywhere for this lab

All Rows must be jagged.


int menu(); — prompts the user with menu choices – must ensure the value entered is between 1 and 4 inclusive int ** createTriangle(int n); — creates and fills the appropriate pascals triangle and returns it

int retrieveTotal(char * argv); — takes the string from the command line as the first passed parameter and converts it to a number. The number must be greater than or equal to 0. If not prompt the user

void cleanUp(int ** pascal, int n); — cleans up each row first, and the the row of int * second; void printTriangle(FILE * fout, int ** pascal, int n); – prints pascals triangle for n to the screen

FILE * openOutputFileArgs(char * fn); – receives the filename from the command line and opens it for output


A zip file containing Lab8 folder which will contain

cscd240Lab8.c lab8.h

lab8.c utils

    1. fileUtils.c o fileUtils.h

cscd240Lab8out.txt – output file with all runs cscd240Lab8val.txt – valgrind run

Name your zip your last name first letter of your first name (Example:

error: Content is protected !!