CSCI 152 Introduction to Computer Science IIAssignment 2
The due date is Friday, February 29, 2008 by 3:00 pm
Assignment 2 Instructions
- Create a subdirectory Assign2 in your CSCI152 directory: while you are in your home directory, do the following:
cd CSC152
mkdir Assign2
- Create a separate file (source file) for each program that you have in Assignment 2 - you will have 4 programs, whichyou can call your files prog1.c, prog2.c, ...
or you can choose use other names of your choice.I will assume that the programs are called prog1.c, prog2.c, ... - Before you submit your assignment, compile and run each program and
make sure that the program doesn't have any compilation or logical errors,
and the program produces the correct result.
Submission of the Assignmet 2:
1.Send the source file for each program by e-mail to
2.Print and submit printout of the source file for each of the programs (you will print prog1.c, prog2.c, ...prog5.c)
3.e-mail and printouts should be submitted before 3:00 pm on Friday, February 29, 2008
*************************************************************Do your own work. Programs which are written in groups are easily identified and will earn grades of 0 for all participants.
************************************************************************
Skill set for this assignment:
- functions
- one-dimensional arrays
- if, if-else, if - else - if, while and for statements
- Use an appropriate variable types and casting
- Understand and implement the Program Development Cycle
****************************************************************
Problem 1 (25 points):
a. Write a function void printChar(char letter, int k) which prints the value of parameter letter on one line without spaces ktimes. You can assume, that this function works only for positive value of k. For example, if the value of parameters are letter=’A’ and k = 3, the function outputs: AAA, if the value of parameters letter = ‘b’ and k = 5, the function outputs: bbbbb.
b. Write a function void printSquare (int n, int k) that prints square of krows and k columns of number n. You can assume, that this function works only for positive values of n and k.For example, if the value of parameters are n = 2 and k = 4, the function outputs:
2222
2222
2222
2222
And if the value of parameters n = 5 and k = 3, the function outputs:
555
555
555
c. Write a C program that uses functionsprintSquare and printNum to perform the following. The program reads a sequence of positive numbers, the first non-positive number indicates the end of the input. If the input number is even the program reads a character and prints the input character the amount of times that is equal to the input integer using function printChar and if the input number is odd the program reads an additional integer k, and prints the square of k rows and k columns of the first input integer uses the function prinSquare
Example of input/output session:
Please enter the positive integer: 3
The first input is odd, please enter the next integer: 2
33
33
Please enter the positive integer: 2
The first input is even, please enter the character: B
BB
Please enter the positive integer: 5
The first input is odd, please enter the next integer: 3
555
555
555
Please enter the positive integer: 4
The first input is even, please enter the character: c
cccc
Please enter the positive integer: -1
Problem 2 (25 points + 10 points bonus):
Let n and k be non-negative integers with n k. The binomial coefficient C(n, k) is defined by:
- Write a function int factorial(int n) that calculates and returns n!
- Write a function int binomial(int n, int k) that calculates binomial coefficient C(n,k) using the definition that is given above. The function should use function factorial to perform the calculations.
- Write a C program that reads 5 pairs of integers and for each valid pair (both numbers are non-negative and the first number is greater or equal than the second number) calculates the binomial coefficient using function binomial. If the pair is not valid the program prints the error message.
- (10 points bonus) Write a C program that prints the first 10 rows of Pascal Triangle. Find the connection between Pascal Triangle and Fibonacci numbers. Your program should print also first 10 Fibonacci numbers using this connection. See the links on the course website for more information.
Problem 3 (25 points)
- Write two functions: function int factorial (int n) that returns the factorial of the parameter n (n!=1*2*....*n, and 0!=1) (you already wrote this function in Problem 2) and function double valueExp (int n) that calculates the approximate value of e. The function should use the following formula: e = 1 + 1/(1!) + 1/(2!) + 1/(3!) + 1/(4!)+....
the value of parameter n denotes the amount of terms in the sum. Use function factorial to calculate the denominators in the formula. - Write a C program that reads a sequence of positive integers, the first non-positive integer terminates the input. The program calculates the approximated value of e, for each input value, consider the input value as a amount of terms in the approximated formula. Use function valueExp to calculate the approximate value.Don’t run your program with large values of n to avoid integer overflow.
For example, if the input is
1 3 5 6 -2
The output should be:
n = 1 e = 1
n = 3 e = 2.5 (1+1/1! +1/2!= 1+1+0.5 = 2.5)
n = 5 e = 2.7083 (1+1/1!+1/2!+1/3!+1/4! = 2.7083)
n = 6 e = 2.7166 (1+1/1!+1/2!+1/3!+1/4! +1/5! =2.7166)
Problem 4 (25 points):
Write a C program that reads an array of 10 integers. The program finds the sum of all non-negative elements of the array and the product of all negative elements. The program prints an output with an appropriate message. For example, if the input is 2 4 -4 5 -1 2 -3 -4 0 1 the output should be: the sum of the non- negative elements is 14 and the product of the negative elements is 48.