Answer to Question #22037 in C++ for rodea embradura

Question #22037
make a program that would perform the ff:

a) ask the user to enter the number of elements he wants to input into an array (the minimum number of elements is 10,while the ,maximum is 100);

b) let the user enter data elements and store it in array FIRST;

C) allow the user to enter the same number of elements , and store it in array SECOND;

D) interchange the elements and display the values, accordingly;

e) merge the elements and store it in array LAST; and

f) display the content of array LAST.
1
Expert's answer
2013-01-17T08:46:46-0500
#include <time.h>
#include <conio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

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;

/* Display arrays */
cout << "First array:" << endl;
display_array(first, arrLen);
cout << endl;

cout << "Second array:" << endl;
display_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;

delete first;
delete second;
delete last;
getch();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS