Lab Assignment C programming - WED
Solve all problems. Submit one problem of your choice for grading.
- Problem 1: In this program you can assume that ALL INPUTS ARE
POSITIVE NUMBERS. Write the following functions:
- int count_digit(int num) - finds and returns the number of digits
of
parameter num
- void make_array(int array[], int size) - makes an array of size
integers. Make it USER INPUT for testing purposes.
- void print_array(int array[], int size) - prints arrayof size elements
- double ave(int A[], int size) - that has 2 parameters, array of
integers
and size of the array.
The functioin returns the average of numbers that have 3, 4 or 5 digits,
or 0 if there are no ints that have 2, 3, or 4 digits.
Use function count_digit to find the number of digits in each element.
- Problem 2: Write a function changeString that has two parameters:
two character arrays: old_array and new_array. The function
changes the original array in the following way: each lowercase letter is replaced by
the
character '*', all chars that are uppercase letters are omitted, and
the rest of the chars remain the same
Write a program that reads original string, make changes and prints string
after changes are made.
For example:
Input: Yana123*((!
Output: ***123*((!
- Problem 3: Write a function int findATG that has one parameter
array of chars. The function will look for sub-string ATG inside
the original string and returns the index of the first occurence of ATG in
the original string or -1 if ATG is not in the string
Write a program to test your function
- Problem 4: Selection Sort:
Write a program that sorts the array of integers using Selection
Sort.
Definition: Selection Sort works as follows: first find the
smallest
element in the array and exchange it with the element in the first
position, then find the second smallest element and exchange it with the
element in the second position, and continue in this way until the entire
array is sorted.
You would need to write the following functions:
- int min(int A[], int left, int right) - finds and returns
the
position of the minimal element in sub-array of A between positions left
and right
- void SelectionSort(int A [], int size) - sorts the
array
A using Selection Sort algorithm
- void ReadArray (int A [] , int size) - reads an array A
- void WriteArray (int A [] , int size) - prints an array A
- Problem 5: Write the following functions:
- Function sum_divisor that accepts one integer parameter num and
returns
the sum of the PROPER divisors of the parameter num.
- 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
- int array_abundant that has two parameters: array of integers and size of the array. The function
returns the number of
abundant numbers among array elements.
Write main to test your program. You can also write functions make_array and print_array.
- Problem 6: Write a program that calculates the disctance cars travel for each user.
Enter the number of users and the speed and total time of travel for each car.
Store speeds in one array and travel time in another array.
The program creates a new array that store distances for each user.
The formula to use: Distance = Speed * Time