basic c program -- 2
Budget: $10 – $30 USD
Program Design Project 3
Problem 1 (50 points)
Write a C program to determine if the parity (even or odd) of each index matches the parity
(even or odd) element at that index. The inputs are the length of an array of positive integers,
and the elements the array. For example, the parities of the elements array [3 5 1] are [odd,
odd, odd], the parities of the indices 0 1 2 are even odd even. The parities of the elements do
not match the parities of the indices.
Example input/output:
Enter the length of the array: 7
Enter the elements of the array: 2 5 4 1 6 9 12
Output: parities of elements match parities of indices
1) Name your program project3_parity.c.
2) The program reads in the number of elements of the array, for example, 4, then
read in the numbers in the array, for example, 3 6 8 9.
3) In the main function, declare the input array after reading in the number of
elements of the array, then read in the elements.
4) The program determines if the parity (even or odd) of each index matches the parity
(even or odd) element at that index and displays the result.
Problem 2 (50 points)
Write a program that removes k number of smallest element in an input array.
Example input/output:
Enter the number of elements in set A: 7
Enter the numbers in set A: 3 5 2 7 8 1 4
Enter the number of smallest elements to be removed: 4
Output:
5 7 8
The program should include the following functions:
int remove_smallest_numbers(int a[], int n, int k)
The function removes k number of smallest element in an input array a[] of length n and return
the new actual length of the array after the removal, which is n-k.
The function removes the smallest element by shifting elements to the right of the smallest
element and repeats it for k times.
For example, if the input array a contains [3, 6, 8, 2, 9, 4] of length 6, suppose k is 3.
The function will remove the smallest element, 2, at index 3, by shifting 9 and 4 to the left. The
result is [3, 6, 8, 9, 4, 4], with an actual length of 5.
The function will then remove the smallest element, 3, at index 0, by shifting 6, 8, 9, and 4 to the
left. The result is [6, 8, 9, 4, 4, 4], with an actual length of 4.
The function will then remove the smallest element, 4, at index 3, by shifting no element to the
left. The result is [6, 8, 9, 4, 4, 4], with an actual length of 3.
The program should also include the following function, which is a helper function to the
remove_smallest_numbers function.
int find_smallest_index(int a[], int n)
The function finds and returns the index of smallest element in an input array a[] of length n.
5) Name your program project3_remove_smallest.c.
6) The program will read in the number of elements of the array, for example, 4, then
read in the numbers in the array, for example, 3 6 8 9.
7) In the main function, declare the input array after reading in the number of
elements of the array, then read in the elements.
8) Your program should only use one array in the main function and the
remove_smallest_numbers function.
9) In the main function, call remove_smallest_numbers function to remove k
number of smallest element.
10) The main function should display the resulting array with length n-k.
Problem 1 (50 points)
Write a C program to determine if the parity (even or odd) of each index matches the parity
(even or odd) element at that index. The inputs are the length of an array of positive integers,
and the elements the array. For example, the parities of the elements array [3 5 1] are [odd,
odd, odd], the parities of the indices 0 1 2 are even odd even. The parities of the elements do
not match the parities of the indices.
Example input/output:
Enter the length of the array: 7
Enter the elements of the array: 2 5 4 1 6 9 12
Output: parities of elements match parities of indices
1) Name your program project3_parity.c.
2) The program reads in the number of elements of the array, for example, 4, then
read in the numbers in the array, for example, 3 6 8 9.
3) In the main function, declare the input array after reading in the number of
elements of the array, then read in the elements.
4) The program determines if the parity (even or odd) of each index matches the parity
(even or odd) element at that index and displays the result.
Problem 2 (50 points)
Write a program that removes k number of smallest element in an input array.
Example input/output:
Enter the number of elements in set A: 7
Enter the numbers in set A: 3 5 2 7 8 1 4
Enter the number of smallest elements to be removed: 4
Output:
5 7 8
The program should include the following functions:
int remove_smallest_numbers(int a[], int n, int k)
The function removes k number of smallest element in an input array a[] of length n and return
the new actual length of the array after the removal, which is n-k.
The function removes the smallest element by shifting elements to the right of the smallest
element and repeats it for k times.
For example, if the input array a contains [3, 6, 8, 2, 9, 4] of length 6, suppose k is 3.
The function will remove the smallest element, 2, at index 3, by shifting 9 and 4 to the left. The
result is [3, 6, 8, 9, 4, 4], with an actual length of 5.
The function will then remove the smallest element, 3, at index 0, by shifting 6, 8, 9, and 4 to the
left. The result is [6, 8, 9, 4, 4, 4], with an actual length of 4.
The function will then remove the smallest element, 4, at index 3, by shifting no element to the
left. The result is [6, 8, 9, 4, 4, 4], with an actual length of 3.
The program should also include the following function, which is a helper function to the
remove_smallest_numbers function.
int find_smallest_index(int a[], int n)
The function finds and returns the index of smallest element in an input array a[] of length n.
5) Name your program project3_remove_smallest.c.
6) The program will read in the number of elements of the array, for example, 4, then
read in the numbers in the array, for example, 3 6 8 9.
7) In the main function, declare the input array after reading in the number of
elements of the array, then read in the elements.
8) Your program should only use one array in the main function and the
remove_smallest_numbers function.
9) In the main function, call remove_smallest_numbers function to remove k
number of smallest element.
10) The main function should display the resulting array with length n-k.