Starting from:

$30

Homework 03 Solution




Part 1 50pts




In this part you will deal with prime numbers and Goldbach conjecture. Goldbach conjecture claims that every even integer that is greater than 2 can be written as a sum of two prime numbers. You will implement isPrime function that checks whether given integer is a prime number or not and goldbach function which takes an integer and returns two prime numbers whose sum is equal to given integer.

 

Please consider that each different approach may result in finding different prime numbers so that there will be multiple

correct solution for goldbach function.







Signature




int isPrime(int num);




int goldbach(int num, int *p1, int *p2);




Sample Usage




int num = 824, p1, p2;




if(goldbach(num,&p1,&p2))




printf("%d = %d + %d",num,p1,p2); /* may print 824 = 821 + 3 */




else







printf("You should provide even number.");



Return Value




isPrime - 1 if num is prime, 0 otherwise.




goldbach - 1 if num is even and two prime numbers found, 0 otherwise.




 




Part 2 30pts




Write a function that dispenses change. Function takes 8 parameters, first parameter is the paid amount, second parameter is the due amount. Rest of the parameters are the number of Turkish lira coins that should be returned from this function. Parameters are sorted in descending order. Your function should return 0 in case of error, 1 otherwise.

 




Signature




int dispenseChange(double paid, double due, int *tl1, int *krs50, int *krs25, int

*krs10, int *krs5, int *krs1);




Sample Usage




double paid = 5, due = 3.75;




int tl1,krs50,krs25,krs10,krs5,krs1;




if(dispenseChange(paid,due,tl1,&krs50, &krs25, &krs10, &krs5, &krs1)) /* returns 1 */




printf("Change: 1TL:%d, Kurus-50:%d,25:%d,10:%d,5:%d,1:%d\n",tl1,krs50,krs25,krs10,krs5,krs1);



else







printf("Unable to dispense change.");
Part 3 20pts




In the last part, you’re given a travelling card (IstanbulCard) automation. Every member has a balance(bakiye) and a remaining monthly use (Aylık kullanım hakkı). There are 3 different card types: Normal, Student, and Teacher cards. Cards are charged with different amounts: Normal cards are charged 2.30₺, student cards are charged 1.15₺, teacher cards are charged 1.65₺. If user has a monthly subscription, you should decrease it by 1. If user has no subscription, you should charge user according to his/her card type. If remaining monthly use is lower than 1, you should assume that user does not have a monthly subscription.

 

You should not use if statements or ternary operator for determining user’s card types, you can only use switch statement. Card types are represented with integers:

Normal Card : 1, Student Card : 2, Teacher Card : 3

 

Return values and error conditions:

- Insufficient balance or monthly use: return -1

- Invalid card type: return -2

- 0, if there is no error.













Signature




int charge(int cardType, int* monthlyUse, double* balance);




Sample Usage




int monthlyUse = 120; double balance = 20.0; if(!charge(1,&monthlyUse, &balance) < 0)

printf("Remaining monthly use: %d – Remaining Balance: %.2f\n",monthlyUse,balance);




else







printf("Insufficient balance.");









Good luck!

More products