r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int size, int value) P6.1 Write a program that initializes an array with ten random integers and then prints four lines of output, containing Every element at an even index. Every even element. All elements in reverse order. Only the first and last element. Sol #include <iostream> #include <ctime> using namespace std; int main() int randoms[10]; srand(time(0)); for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); cout << "Elements at even indexes: "; for (int j = 0; j < 10; j = j + 2) cout << randoms[j] << " "; cout << endl << "Even elements: "; for (int k = 0; k < 10; k++) if (randoms[k] % 2 == 0) cout << randoms[k] << " "; cout << endl << "All elements in reverse: "; for (int m = 9; m >= 0; m--) 1
cout << randoms[m] << " "; cout << endl << "First and last elements: "; cout << randoms[0] << " " << randoms[9] << endl; return 0; P6.2 Write array functions that carry out the following tasks for an array of integers: For each function, provide a test program. a. Swap the first and last element in an array. Sol a. Swap first and last elements in an array. #include <iostream> #include <ctime> using namespace std; void swap_first_last(int arr[], int size) int last = size - 1; int temp = arr[0]; arr[0] = arr[last]; arr[last] = temp; int main() int randoms[10]; srand(time(0)); 2
// Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Perform the swap. swap_first_last(randoms, 10); // Print again to see new order. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; b. Shift all elements by one to the right and move the last element into the first position. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16. b. Shift all elements by one to the right. 3
#include <iostream> #include <ctime> using namespace std; void rotate_right(int arr[], int size) int last = size - 1; int temp = arr[last]; for (int i = last; i > 0; i--) arr[i] = arr[i - 1]; arr[0] = temp; int main() int randoms[10]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); 4
// Print the values as they are assigned. cout << randoms[i] << " "; // Rotate the array once to the right. rotate_right(randoms, 10); // Print again to see new order. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; c. Replace all even elements with 0. c. Replace all even elements with 0. #include <iostream> #include <ctime> using namespace std; void replace_even(int arr[], int size) 5
for (int i = 0; i < size; i++) if (arr[i] % 2 == 0) // Number is even arr[i] = 0; int main() int randoms[10]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < 10; i++) randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Replace the even elements. replace_even(randoms, 10); 6
// Print again to see new elements. for (int i = 0; i < 10; i++) cout << randoms[i] << " "; return 0; e. Remove the middle element if the array length is odd, or the middle two elements if the length is even. e. Remove the middle element if the array length is odd, or middle two if even. #include <iostream> #include <ctime> using namespace std; void remove_middle(int arr[], int &size) if (size % 2 == 0) // Size is even // Figure out starting point for removal int first_to_remove = size / 2-1; // Remove middle two elements for (int i = first_to_remove; i < size - 2; i++) 7
arr[i] = arr[i + 2]; size = size - 2; else // Size is odd // Figure out starting point for removal int first_to_remove = size / 2; // Remove middle element for (int i = first_to_remove; i < size - 1; i++) arr[i] = arr[i + 1]; size = size - 1; int main() int a_size = 11; int randoms[11]; srand(time(0)); // Create a test array containing random numbers. for (int i = 0; i < a_size; i++) 8
randoms[i] = (rand() % 100 + 1); // Print the values as they are assigned. cout << randoms[i] << " "; // Remove the middle element(s) remove_middle(randoms, a_size); // Print again to see new order. for (int i = 0; i < a_size; i++) cout << randoms[i] << " "; return 0; j. Return true if the array contains duplicate values (which need not be adjacent). j. Returns true if array contains duplicate values (not necessarily adjacent). #include <iostream> using namespace std; 9
bool contains_duplicates(int arr[], int size) // Assume no dupes. bool dupes = false; // Loop through array, checking for duplicates for (int i = 0; i < size; i++) for (int j = i + 1; j < size; j++) if (arr[i] == arr[j]) dupes = true; return dupes; int main() int arr_1[10] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 42; int arr_2[10] = 2, 1, 3, 4, 5, 4, 7, 4, 9, 4; // Check if array 1 has dupes. 10
if (contains_duplicates(arr_1, 10)) cout << "Array contains duplicates." << endl; else cout << "Array does NOT contain duplicates." << endl; // Check if array 2 dupes. if (contains_duplicates(arr_2, 10)) cout << "Array contains duplicates." << endl; else cout << "Array does NOT contain duplicates." << endl; return 0; P6.5 Write a function void remove_min that removes the minimum value from a partially filled array without calling other functions. #include <iostream> using namespace std; void remove_min(double input[], int &size) 11
// Find the minimum value first double min = input[0]; int min_pos = 0; for (int i = 1; i < size; i++) if (input[i] < min) min = input[i]; min_pos = i; // Now "eliminate" the minimum value for (int j = min_pos; j < size - 1; j++) input[j] = input[j + 1]; // Decrement the size size--; int main() const int SIZE = 9; double my_array[size] = 22.3, 22.1, 20.3, 17.7, 45.2, 12
11.9, 11.8, 21.6, 22.1; int my_size = SIZE; remove_min(my_array, my_size); for (int i = 0; i < my_size; i++) cout << my_array[i] << " "; return 0; P6.9 Write a function bool equals(int a[], int a_size, int b[], int b_size) that checks whether two arrays have the same elements in the same order. #include <iostream> using namespace std; bool equals(int a[], int a_size, int b[], int b_size) if (a_size!= b_size) 13
return false; for (int i = 0; i < a_size; i++) if (a[i]!= b[i]) return false; return true; int main() const int SIZE1 = 8; const int SIZE2 = 9; int arr1[size1] = 1, 2, 3, 4, 5, 6, 7, 8; int arr2[size1] = 1, 2, 3, 4, 5, 6, 7, 8; int arr3[size2] = 1, 2, 3, 4, 5, 6, 7, 8, 9; int arr4[size1] = 1, 2, 3, 4, 5, 4, 7, 8; cout << "Arrays 1 and 2 are "; if (!equals(arr1, SIZE1, arr2, SIZE1)) cout << "not "; 14
cout << "equal." << endl; cout << "Arrays 1 and 3 are "; if (!equals(arr1, SIZE1, arr3, SIZE2)) cout << "not "; cout << "equal." << endl; cout << "Arrays 1 and 4 are "; if (!equals(arr1, SIZE1, arr4, SIZE1)) cout << "not "; cout << "equal." << endl; return 0; Extra --- 2 Dimensional Arrays 1. This program calculates and prints a multiplication table for all values between 1 and 9 (inclusive). Note that when printing the table, the for loops start from 1 instead of 0. This is to omit printing the 0 column and 0 row. 15
2. 16
17