Starting from:

$30

Lab 3: Functions, Logic, and Debugging

APS105 Page 1 of 4
APS 105 — Computer Fundamentals
Lab 3: Functions, Logic, and Debugging
The goal of this laboratory is to practice the material on functions. You are to write one C program that
consists of a main() and several functions. The program displays the Pascal’s triangle as explained in the
following section.
Instructions:
• Similar to previous labs, your solution will be marked by your TA during your scheduled lab period
for programming style and your understanding.
Preparation
Read through this entire document carefully and do the work to create the programs that are described
below. You are encouraged to ask for assistance from your lab/tutorial TAs.
Notes:
• In the sample output examples that follow:
o The text <enter> stands for the user pressing the enter key on the keyboard. On some
keyboards, the enter key may be labeled return.
• Throughout this lab, there is a single space after the colon (:) in each output line.
Pascal’s Triangle
In a file called Lab3.c, write a C function named “triangle” that outputs Pascal’s triangle (for example) as
follows:
APS105 Page 2 of 4
In general, the Pascal’s triangle can be represented as:
0C0
 1C1 1C0
 2C2 2C1 2C0
 3C3 3C2 3C1 3C0
 4C4 4C3 4C2 4C1 4C0
...
where nCr represents how many ways there are to choose r from n, not counting duplicates.
In mathematics, it is usually presented as . The formula used to calculate nCr can be written as:
��� = �!
�! (� − �)!
where �! is the factorial of �.
The function triangle:
• Is called by value where exactly one parameter (the number of rows of the Pascal’s triangle) is
passed to it.
• Returns void (i.e. no value).
• Prints out the Pascal’s triangle to the standard output.
• Employs two other functions namely:
int choose(int n, int r);
§ That chooses r from n
int factorial(int n);
§ That calculates factorial of �.
Note: You are allowed to use any other function as need be.
You are required to provide a complete C program by writing the code for the main() function
• Prompts user to supply the number of rows in the Pascal’s triangle.
• Calls the function triangle such to displays the Pascal’s triangle based on the number of rows
provided by the user.
• Terminates the run of the program is the user supplies a negative value.
An example of the required output from your program is as follows:
APS105 Page 3 of 4
APS105 Page 4 of 4
Notes:
• The number of the rows is limited to integers between 0 and 13 inclusive.
• Automarker tests inclusively between 0 and 13.
• If 0 then there are no triangles displayed.
• If negative integer, then program terminates.
• The number displayed in the last column MUST be always displayed at the start of the line. In other
words, and as an example:
§ If the number of rows entered is 3 then the first row displays its number at column 7.
§ Carefully calculate the spaces used between the numbers so that the numbers are properly
aligned across rows.
§ Use of a function spaces() that deals with spacing is recommended.
Good Luck!

More products