You are on page 1of 7

UNIVERSITY OF TECHNOLOGY, JAMAICA

School of Computing & Information Technology CMP1025: Programming II Lab Work

Functions

1. Write a function Addition () that when called from main will allow the user to add as many numbers that the user enters until the user enters a 0 to quit. The function should display the final sum. The output should be as (close as possible) follows: 12 + 28 40 + 123 163 + 0 163 2. Write a function factor() that will accept a number as a parameter and will display all the factors of that number. Note. A factor is any number that can divide a number without leaving a remainder. 3. Write a program that inputs a series of integers and passes them one at a time to function even which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the number is even and 0 otherwise. 4. Write a function sumRange to calculate the sum of the integers between m and n, inclusive, where m and n are passed to it as parameters. With an m of 1 and an n of 100, the function should print 5050. Supply a simple main program that tests it. Include any necessary error-checking. 5. Write a function printStars to print n stars where n is passed as a parameter. For example, the call printStars(10) should produce ********** as output. Use this function in a new function, printBar, which should be passed the width and height of the bar to print. So the call printBar (5, 3) should produce this output: ***** ***** ***** 6. At the end of the two week practical classes students should have built a functional scientific calculator using a menu option demonstrating the different kinds of functions (user-defined/built-in) showing parameter passing and incorporating recursive functions. a) Write a menu function that does not accept a parameter and returns a parameter (of your type) that represents an option from a list of calculator functions. E.g. ***M E N U *** +..Addition -...Subtraction *..Multiply /...Divide x..Exit Enter choice:

b) Write a function add that accepts a parameter and allows the user to add as many numbers as they wish until the user enters a 0 to end the addition. The function will then display the final total. c) Implement your main program that will utilize the menu function above that will allow the execution of any of the calculator functions until the user decides to exit. d) Define the remaining functions one at a time, Execute your program and evaluate. e) Add some additional built-in functions to your menu such as: finding x to the power of a number, square root of a number and the trigonometric functions. (If implemented wisely your program should make use of all the control structures: Repetition, Selection (Case/if) and Sequence)). 1. (i) Write a function that determines if a number is prime (ii) Use this function in a program that determines and prints all the prime numbers between 1 and 10,000.Count and print the amount. 2. Trying to find a students grade point average. If students average is 90 -100, then gpa is 4, 80 89, then gpa is 3, 70-79 gpa is 2, 60-69, then gpa is 1, under 60 is 0.

Random Numbers
7. a) Define a function PlaceWager() that will prompt the user for a wager and return a valid wager. All wagers should be greater than $10 for they to be considered valid. If the wager is valid then the function DiceRoll() should be invoked to roll the dice and check a persons winnings. b) Define a function DiceRoll() that will simulate rolling a pair of dice at the users request and calculate and display their winnings. A person wins 4 * their wager if the dice shows snake eyes or 2 * their wager if the dice shows a total of 7 otherwise they lose their wager. c) Write the main function that will use the functions in a) and b) above to get a persons wager and check and display how much the person has won/loss through the dice. Make the main function repetitive and the users win accumulative until the user decides to quit.

1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the coin 100 times and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads. 8. LuckyPot is an electronic lottery game played in the Lucky Villa community where one lucky citizen receives $1500 for correctly determining the ball number drawn by the computer. Ball numbers range from 1 to 500. Write a program that will randomly generate the winning ball number for each execution of the program, and inform the citizen whether he/she has won the $1500.

9. Write a program that will help a primary school student learn multiplication. The program should generate two random numbers, ranging from 1 to 10. It should pass these numbers to a function multiply_numbers which will present a question such as: How much is 5 * 8? The student will then type in an answer that will be matched with the computercalculated answer to see if the student is correct. The student should receive a message Very good, if the answer is correct or Sorrywrong answer if the answer is incorrect. The function returns nothing. 10. Modify the program above to allow the student to answer 10 questions (using random number generation). Count the number of incorrect and incorrect responses typed by the student. If the percentage is lower than 75%, your program should print Please ask your teacher for help. 1. Generate a game option that generates a chosen number between 33 and 50. If the person guesses the correct number, the winning message Congratulations the number is?, otherwise Please Try again. The winning number is generated randomly. 1. Simulate the throwing of die by two players, the first player to accumulate 100, is the winner. .

Recursion
1. Write a recursive function that will multiply two numbers without using the multiplication sign (*) 2. Modify your multiplication function in your calculator program to use the recursive multiplication function you completed in your tutorial. 3. Modify your calculator program to include a recursive factorial function that displays the factorial of a number. The factorial of a number can be found recursively as follows: n! = n * (n-1)! Eg 4! = 4 * 3! Where 3! = 3 * 2! etc 4! = 4 * 3 * 2 * 1 4. Modify your coin tossing game to allow for wagering. Package as a function the portion of the program that runs one game of the coin toss. Initialize a variable balance to $1000. Prompt the user to enter a wager. Use a while loop to check that wager is valid and if not prompt the user to reenter wager until a valid wager is entered. After a correct wager is entered, run one game of coin toss, where the user will guess heads or tails. If the player wins, increase balance by wager, print the new balance, check if balance has become zero and display the message Sorry! Youre busted! As the game progresses, print various messages to create some chatter such as Oh, youre going for broke, huh?, or Aw cmon, take a chance! or Youre up big. Hows the time to cash is your chips! etc. The program should continue as long as the user decides to make a wager.

5. Write a recursive function that will return the remainder from a division operation without using the division symbol (Assume all numbers to be of type integer). 6. Use recursion to write the sequence 12,19,26,33. 7. The factorial of a number is the product of that number with a number 1 less than that number, this multiplication process continues until the number which is being multiplied, eventually becomes equal to 1. E.g 6! = 6*5*4*3*2*1. Write the function recursively. a) Write the function iteratively. b) Write the recursive function 9. Write a program that returns the greatest common divisor of two integers. (recursion)

2. Write a recursive function that will return the power of a number that is. Xy, when the user enters two values for x and for y. Eg. 24 = 16 3. Write a recursive function that will return the remainder from a division operation without using the division symbol (Assume all numbers to be of type integer). Write a main program that will generate randomly a non-zero divisor and a dividend and pass it to the recursive function and will display the remainder.

4. An integer is said to be a perfect number if the sum of its factors (except the number itself) will sum to the number. E.g. 28 is a perfect number because the sum of its factors (14 + 7 + 4 + 2 + 1 = 28). i) Define a recursive function sumFactors that will accept a num and the starting factor (1/2 the number) and will return the sum of its factors. ii) Write the main function that will prompt the user for a number and test to see if that number is a perfect number by calling the function sumFactors and check if the result is equivalent to the number. iii) Modify your main program to find all the perfect numbers between 1 and 10000.

Additional questions
a) Build an ideal calculator program using the concepts of modularity. The program should give the users a menu to select their option from a list of arithmetic and trigonometric operations. The user should be able to: i.) ii.) iii.) Add, multiply, subtract or divide (as many numbers as they wish) Find the tangent, cosine, sine, logarithm of a number Find the power, square root, cube, factorial of a number

Be creative!!!! Remember your hand-held calculator where the values calculated can always be used in another calculation or you can just return the value to 0. AND You are required to implement a simple ATM system with the following menu:
Welcome to BANK-AT-HOMEs ATM System Please select from the menu below: A. Cash Withdrawals B. Lodgments C. Balance Check D. Credit Purchase E. Exit Enter option:

The following functions/procedures should be included: 1. menu this displays the options onto the screen, accepts a character from the user and returns to the main function the option selected. 2. Cash Withdrawals this uses the current balance as a parameter to the function, prompts the user for the amount to be deposited and returns the updated balance to the main function 3. Lodgement - this uses the current balance as a parameter to the function, prompts the user for the amount to be paid and the institution it is to be paid to and returns the updated balance to the main function 4. balance_check this uses the current balance as a parameter to the function and displays it onto the screen 5. Credit Purchase This uses the current balance as a parameter to the function. Persons can purchase credit for their Digicel, Claro or Lime phones with valid purchases of 200, 300, 500 and 1000 only. The amount along with the required 25% tax should be deducted from their balance and returns the updated balance to the main function After selecting and performing a transaction, the user should be prompted as to whether he/she would like to perform another transaction. If yes, the menu should be re-displayed onto the screen otherwise the program should display a message: Thank you for using BANK-AT-HOME, then exit. Validation for the program is as follows: No matter the transactions users should always have a minimum balance of $100 in their account E-Cash Lodgment Amount cannot be less than 1000 Cannot lodge amounts not divisible by 100 6

Bill_Payment Amount cannot be less than or equal to 0 Amount cannot be greater than or equal to the current balance Updated balances are printed on the screen after each transaction has occurred. Assume the following: the starting balance for the program is $2000 no password/pin is required for accessing the program only one account is being used

You might also like