Lab 3 Part 2 - Practice

Objectives:

LAB PRACTICE

  1. Write a C program that reads a sequence of non-negative integers, the first negative integer terminates the input. For each input value, find and print the square root of the input number. Use function sqrt from math.h library.

    Make sure that you included math.h library at the beginning of your program.

    To compile C program that using math.h library use the following command gcc name_of_file.c -lm

  2. Write a function int power ( int base, int exponent) that returns the value of base ^ exponent. For example if the base is 2 and exponent is 3 the power function returns 8. Write a main program that reads the sequence of non-negative numbers. The first negative number indicates the end of the input sequence. Assume that the amount of input numbers is even. Starting from the first input number, consider the pair of inputs as base and exponent and find the base ^ exponent for each pair. The program prints the results on different lines printing the value of base and exponent as well as the value of base^exponent. If both numbers in the pair are 0, the program will print the error message "0^0 is not defined" For example if the input is: 2, 3, 1, 4, 2, 0, 0, 5, 0, 0, 5, 2, 3, 3, -4. The program will print:
    2^3 = 8
    1^4 = 1
    2^0 = 1
    0^5 = 0
    0^0 is not defined
    5^2 = 25
    3^3 = 9

  3. Write a function int millionaire (float deposit, float interest) that accepts two float parameters: deposit amount of money and interest. You can assume that the interest is the float number after division by 100. For example, if the interest is 2% the actual parameter will be 0.02. Function returns the number of years that will take to get 1000000 dollars starting from deposit and taking in account the interest per year
    Write a main program that reads even amount of non-negative numbers. The first negative number indicates the end of the input sequence. For each input pair of numbers, that define initial deposit and year interest, the program finds and prints the amount of years that will take to become a millionaire.

  4. Write a function float average ( int n ) that accepts one parameter that indicates amount of float numbers that your function reads from the user. The function returns the average of the input numbers. For example, if the parameter is 4 and user input 1.0, 2.0, 3.0, 4.0 , function returns 2.5 ( (1.0 + 2.0 + 3.0 + 4.0)/4).

    Write a program, that reads one integer that indicates the amount of the input numbers. The program uses function average to find an average of the input numbers.

  5. Write three functions: function int factorial (int k) that returns the factorial of the parameter k (k!=1*2*....*k and 0!=1), function int power (int x, int k) that calculates x^k (you can assume that the function works for x > 0 and k >= 0. If k = 0, function returns 1 for any positive value of x. You wrote this function in lab 2), and function double valueExp (int x, int n) that calculates the approximate value of e^x . The function should use the following formula: e^x = 1 + x/(1!) + x^2/(2!) + x^3/(3!) + x^4/(4!)+....
    the value of parameter n denotes the amount of terms in the sum. Use function power to calculate the numerator and function factorial to calculate the denominators in each term of the formula.

    Write a C program that reads a sequence of positive integers, the first non-positive integer terminates the input. Assume that the amount of input numbers is even. Starting from the first input number, consider the pair of inputs as value of x and the value of n and find the approximated value of e^x using n terms of the given formula. The program prints the results on different lines printing the value of x and the value of n as well as the value e^x. Use function valueExp to calculate the approximate value.

  6. Write a function int dieRes (void) that rolls the six-sided die one time using random number generator and returns the value between 1 and 6 that indicates the number that was rolled in one roll.

    Write a C program that reads one integer, that indicates the amount of times that the six-sided die will be rolled in the program. The program will calculate and return the frequencies of appearance of integers between 1 and 6.

  7. Write a program that simulates rolling two six-sided dice. The program reads one integer that idicate the amount of rolls. In each roll program finds the sum of the numbers that appeared on the faces of two dice. The program calculates and prints the frequencies of the sums.