Download Assignment 1 - Widener University | Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

History of the function concept wikipedia , lookup

Function (mathematics) wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Halting problem wikipedia , lookup

Collatz conjecture wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
CSCI 152 Introduction to Computer Science II
Assignment 1
The due date is Friday, February 9
Assignment 1 Instructions

Create a subdirectory CSCI152 in your home directory (if you didn't do it yet in
the Lab). In order to create a directory type the following
mkdir CSCI152

Create a subdirectory Assign1 in your CSCI152 directory: while you are in your
home directory, do the following:
cd CSC152
mkdir Assign1



Create a separate file (source file) for each program that you have in Assignment
1 - you will have 5 programs, which
you 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.
In order to compile the first program do the following:
gcc -o prog1.c prog1


This command will create an executable file prog1 for your first program
You can run the executable file as follows:
prog1

Compile and Run all 5 programs before you go to the next step - Submission.
Submission of the Assignmet 1:
1. Send the source file for each program by e-mail to
[email protected]
2. Print and submit printout of the source file for each of the programs
(you will print prog1.c, prog2.c, ...prog5.c)
*************************************************************
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:





recursive functions
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 (20 points):
a. Write a recursive function int sumCube (int n) which accepts one integer parameter
3
3
3
and returns the sum of the cubes of the numbers from 1 to n: 1  2  ...  n
For
example, if n = 3, the function returns 36 (1 + 8 + 27 = 36), and if, for example, n = 5 the
function returns 225 ( 1+ 8 + 27 + 64 + 125 ).
b. Write a recursive function void printNum (int n, int k) which prints the value of
parameter n on one line without spaces k times. Assume, that this function works only for
positive numbers. For example, if the value of parameters are n = 2 and k = 3, the
function outputs: 222, if the value of parameters n = 3 and k = 3, the function outputs:
333.
c. Write a C program that uses functions sumCube and printNum to perform the
following. The program reads a sequence of positive numbers, the first negative number
indicates the end of the input and performs the following: if the input number is even the
program calculates the sum of the cubes from 1 to the input number using function
sumCube and if the input number is odd the program uses the function printNum to print
this number on one line amount of time that equals to the number itself.
For example, if the input is: 3, 2, 5, -1, the output should be:
n = 3 and k = 3, the printout is: 333
1+8 = 9
n= 5 and k = 5, the printout is: 55555
Problem 2 (20 points + 10 points bonus):
Let n and k be non-negative integers with n  k. The binomial coefficient C(n, k) is
n!
defined by: C ( n, k ) 
k ! ( n  k )!
a. Write a recursive function int factorial (int n) that calculates n!
b. Write a non-recursive 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.
c. Write a recursive function int binomialRec (int n, int k) that calculates binomial
coefficient C(n,k), using the following recursive definition of the binomial
coefficients:
k0
 1,

C ( n, k )   1,
nk
C ( n  1, k )  C ( n  1, k  1), n  k  0

d. 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 of equal than the second
number) calculates the binomial coefficient using both functions. If the pair is not
valid the program prints the error message.
e. (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 (20 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.
Problem 4 (20 points)
Write a C program that reads two character arrays. Each array consists of 10 characters.
The program will calculate the similarity score of two arrays – the amount of the same
characters on the same position in the array. The program should distinct between the
lower and upper case letters.
For example, if the input is
aAbcdefGdA
AabcKabcds
The output should be: The similarity score is 3 (only b – position 3, c – position 4 and d position 9 are taken in account)
For example if the input is
abcdefg123
ABCDEFG*/3
The output should be: The similarity score is 1 (only last character is the same)
Problem 5 (20 points)
In this program you need to implement the simplified version of the new score system in
the Figure Skating competition.
Description of the simplified version: There are 12 judges that are present.
First step: all 12 judges submit their scores.
Second step: 9 scores will be randomly selected.
Third step: the highest and lowest score are thrown out from the nine counting judges.
Last step: the remaining seven scores are averaged.
Write a program that reads 12 scores submitted by 12 judges for the specific event and
calculates the final score according to the rules written above. Your program is supposed
to print the results after each step of calculations (see example below for the output
format). Also, the program is supposed to run in different ways on the same input in
different runs, since you are using random selection at step 2. Each input score is an
integer number between 0 and 100. The input numbers are separated by space. You can
assume that the input is valid.
Example 1:
Input:
100 100 100 100 100 100 100 100 100 100 90 100
Output:
Step 1: input scores are:
100 100 100 100 100 100 100 100 100 100 90 100
Step 2: 9 remaining sores are:
100 100 100 100 100 100 100 100 100
Step 3: after the lowest and highest score is thrown away the scores are:
100 100 100 100 100 100 100
Step 4: the final average is:
100.0
Example 2:
Input: 12 49 100 98 99 16 13 100 99 12 99 100
Output:
Step 1: Input Scores: 12 49 100 98 99 16 13 100 99 12 99 100
Step 2: Nine random selected scores: 12 100 98 99 16 13 12 99 100
Step 3: The remaining seven scores: 100 98 99 16 13 12 99
Step 4: The final score is: 62.428571
Bonus Problem (10 points)
Collatz's Conjecture says that every positive integer finally ends up at 1 when the
following function is recursively applied:

 x / 2,
if x is even

Collatz ( x )  
 3 x  1, if x is odd


In this function, x is the positive integer that denotes the starting point of the sequence.
For example, if x = 5 we will get the following sequence: 5, 16, 8, 4, 2, 1. We consider 1
as a terminator of the sequence. It is an open question in mathematics whether this
sequence will always goes to 1 for every possible starting value.
a. Write a recursive function int collatzRec (int x) that accepts one integer
parameter x and prints the Collatz sequence starting from x and returns the
amount of steps that it took to go to 1. For example, if x = 5, the function prints:
5, 16, 8, 4, 2, 1 and returns 5 (5 steps to go from 5 to 1), if x = 10, the function
prints: 10, 5, 16, 8, 4, 2, 1 and returns 6, if the x = 3, the function prints: 3, 10, 5,
16, 8, 4, 2, 1 and returns 7.
Here is the recursive definition of the Collatz sequence C:
C (0) = x (where x is the function parameter, the first element of the sequence)
if C( i  1) is even
 C( i  1) / 2,

C( i )  
3 * C( i  1)  1, if C( i  1) is odd


1 is a terminator of the sequence.
For example, if x = 5, the Collatz sequence will be:
C(0) = 5, C(1) = 16, C(2) = 8, C(3) = 4, C(4) = 2, C(5) = 1
See also:
http://www.holtorama.com/russell/writing/1996/Collatz/collatz.html
http://mathworld.wolfram.com/CollatzProblem.html
b. Write a C program that reads a sequence of positive integers, the first negative or
zero value terminates the input. For each input number the program uses
recursive function collatsRec to print the collatz sequence and the amount of steps
that takes to go to 1.