const int MIN_LENGTH = 4; const int MAX_LENGTH = 100;
int input_array_length() { int result; cout << "Enter the number of elements in the array: "; cin >> result; cout << endl;
while (result < MIN_LENGTH || result > MAX_LENGTH) { cout << "The number of elements must be between 10 and 100." << endl; cout << "Please enter again: "; cin >> result; cout << endl; } return result; }
void input_array(int* dest, int length) { for (int z = 0; z < length; z++) cin >> dest[z]; }
void mix_array(int* array, int length) { int tmp; int index_1; int index_2;
srand((unsigned long)time(NULL) / 32000); for (int k = 0; k < length / 2; k++) { /* Get 2 random array indices */ index_1 = rand() % length; index_2 = rand() % length;
/* Swap array elements at those indices */ tmp = array[index_1]; array[index_1] = array[index_2]; array[index_2] = tmp; } }
void merge_arrays(int* first, int* second, int length, int* dest) { for (int o = 0; o < length; o++) { dest[o] = first[o]; dest[length + o] = second[o]; } }
void display_array(int* array, int length) { for (int c = 0; c < length; c++) { cout << array[c] << ' '; if (c + 1 % 16 == 0) //put the line break after every 16 elements cout << endl; } if (length % 16 != 0) cout << endl; }
void main() { int arrLen = input_array_length(); int* first = new int[arrLen]; int* second = new int[arrLen]; int* last = new int[arrLen * 2];
/* Input and interchange first array elements */ cout << "Enter " << arrLen << " elements of the first array." << endl; input_array(first, arrLen); mix_array(first, arrLen); cout << endl;
/* Input and interchange second array elements */ cout << "Enter " << arrLen << " elements of the second array." << endl; input_array(second, arrLen); mix_array(second, arrLen); cout << endl;
/* Merge these two arrays and display the resulting array */ merge_arrays(first, second, arrLen, last); cout << "The final array:" << endl; display_array(last, arrLen * 2); cout << endl;
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments
Leave a comment