The assignment name for turnin is cs352_hw3
1- gcd calculation
The greatest common divisor of two positive integers is the largest integer that is a divisor of both. The gcd function should have the following prototype:
int recgcd(int, int); AND int itergcd(int, int);
Both take the numbers to find the gcd of, and return the gcd of the two.
Hint: Use the fact that gcd(x,y)=gcd(y, x mod y)
2- Monte Carlo Simulations
In a roomful of people, at least two of them can be expected to have birthdays on the same day of the year. Write a function "birthday", that will simulate this probability. More specifically, the prototype of the function is as:
double birthday(int n);
where n is the number of people in the room, and the function returns the probability that two people have the same day of birth.
Create a (global!) array of 365 items to count the number of people in the room that were born in each of the 365 days. In order to find the day of birth for a person you can use:
rand() % 365 + 1
Do 1000 independent trials to get a good approximation. In one trial when an array element gets to two, then that trial is true. The simulated probability is the sum of true trials divided by 1000.
Write a program that calls this function, and prints out the value of the smallest n that makes the probability ½ or more. Put your program into a file called montecarlo.c
3- Declarations
The following declarations have some errors. Identify each of them, and put into a file called errors:
#define N 4
int a[N] = {0, 2, 2, 3, 4};
int b[N-5];
int c[3.0];
4- Polynomials
a) A real polynomial of degree n or less is given by:
P(x) = a_0 + a_1*x + a_2*x^2+ …. a_n*x^n
With the coefficients a_0, a_1, …a_n representing real numbers, and x^i represents x to the power of i. If a_n != 0, then the degree of P(x) is n.
a) Write a function that adds two polynomials of degree at most n. Put your function into a file called addpoly.c
/* f = g + h; n is the degree of g, and m is of h*/
void add(double f[], double g[], double h[], n, m)
{
….
b) Write a function to multiply two polynomials of degree at most n. Put your function into a file called multpoly.c Use the function add from part a, to sum intermediate results.
c) Put the main function that calls the two functions into a file called mainpoly.c.
5- Palindromes
A palindrome is a string that reads the same both forward and backward. Write a function that takes a string argument and returns 1 if the string is a palindrome, 0 otherwise. Put your function into a file called palindrome.c (Ignore the blanks and capitals in the matching process. For example,
"ott o" is a palindrome, so is "Ab C Ba".)