CSCI 152

CSCI 152: LAB 4

Skill set for this LAB:

Problem 1 Write a function int sumSqr (int n) that accepts one integer parameter and returns the sum of the squares of the numbers from 1 to n: 1*1 + 2*2 + 3*3 + ... +n*n. For example, if n = 1, the function returns 1, if n = 2, the function returns 5, if n = 3, the function returns 14.

  • Write a C program that reads a sequence of positive numbers, the first non-positive integer terminates the input. The program prints for each input number the corresponded sum of squares, using function sumSqr.

    Problem 2 Write the following functions:
    (1)Function sum_divisor that accepts one integer parameter num and returns the sum of the PROPER divisors of the parameter num.

    (2)Function isabundant that accepts one integer parameter and returns 1 if the number is abundant and 0 otherwise. Definition: an abundant number or excessive number is a number for which the sum of PROPER divisors is larger than the number itself. Use functin sum_divisors that you wrote, to find the sum of proper divisors.

    Examples:
    12 is abundant, since the sum of the proper divisors is 1+2+3+4+6=16 > 12
    20 is abundant, since the sum of the proper divisors is 1+2+4+5+10=22 > 20
    6 is NOT abundant, since the sum of proper divisors is 1+2+3 = 6 and it is NOT larger than 6
    10 is not abundant, since the sum of proper divisors is 1 + 2 + 5 = 8 < 10

    Write main that reads a sequence of positive integers and for each input checks and prints if the input number is abundant or not.

    Problem 3 Write the following functions:

    1. Function sumEven that accepts two parameters - integer array and its size and returns the sum of the even elements of the array. If there are no even elements, the function returns -1.

    2. Function countEventhat accepts two parameters - integer array and its size and returns the number of the even elements of the array.

    3. Function averageEven that accepts two parameters - integer array and its size and returns the average of the even elements. If there are no even elements the function returns -1. This function MUST use functions sumEven and countEven to perform the task

    Write a program that randomly generates the size of the array (integer between 5 and 20) and an array of integers between 0 and 10. Use function make_array we wrote in class. Print randomly generated an array using function print_array we wrote in class. The program will use function averageEven to find the average of the even elements

    Problem 4

    1. Function count_even_divisor that accepts one integer parameter num and returns the number of even divisors of parameter num

    2. Function array_even_divisor that accepts two parameters - integer array and its size and for each element of the array prints the number of the even divisors. Use function count_even_divisor.

    Write a program that randomly generates the size of the array (integer between 5 and 20) and an array of integers between 0 and 100. Use function make_array we wrote in class. Print randomly generated an array using function print_array we wrote in class. The program will use function array_even_divisor to print even the number of even divisors of each element of the array. Make sure that you specify the case with no even divisors.