WRITE IN C++ ***Separate part 1 and part 2 In this program you will be creating
ID: 3723685 • Letter: W
Question
WRITE IN C++ ***Separate part 1 and part 2
In this program you will be creating 2 arrays - one will have values input into it, the second will be used to store values from the first array that are divisible by X (where X will be input). Both arrays will be ints.
Part 1.
The main program should have 2 arrays both set to a maximum of 20 elements. (The number of elements should be set to a const for easy changing.)
The purpose of part 1 is to fill the first array with values. The user will enter how many elements should be entered and then call a function to fill the array. Be sure the array won't go out of bounds. After the number of elements has been input output a newline;
Once you have come back to the main program you will call a print function that will output the contents of the array - one element on each line.
The input function (do NOT call this function input). The function should take an int array and number of elements that will be input as an int, and an int indicating the maximum size of the array. It will return nothing. Also, after all values have been input output a newline.
The output function (do not call the function output) will accept an int array and the number of element in the array as an int. It returns nothing. Do not allow the function to change the values in the array passed in.
Part 2.
In part 2, in the main program asks the user to enter a value to check as a divisor. The value will then be passed to a function along with both arrays. If the value in the first array (filled in function 1 above) is exactly divisible by the divisor value passed in then the value is to be stored in the second array.
The divisor function take the following parameters: Two integer arrays followed by an int indicating how many elements are in the first array. The first array should not be changed. There will be a second int passed in which is the divisor value previously entered. The function will return the number of elements in the second array.
There are two more functions required by this part. A function that returns the maximum value of an int array, and a function that returns the minimum value of an int array. Both functions are passed the array and an integer indicating the number of elements in the array. Do not allow the array to be changed. The maximum or minimum is returned respectively.
The print function from part 1 will no longer print the original array - in this part it should now print the divisor filled array followed by the min and max.
Example Output
Please enter how many numbers
Enter 20 numbers
Please enter a divisor:
The array is:
2
4
6
8
10
-10
-8
The min = -10
The max = 10
Explanation / Answer
Part 1:
#include <iostream>
using namespace std;
void fill_array(int inp[], int n, int maximum){ // function to enter values into the array
if(n == 0) return;
if(n > maximum){
cout << "Cannot enter more than " << maximum << " elements. ";
n = min(n, maximum);
}
cout << "Enter " << n << " numbers ";
for(int i = 0; i < n; i++){
cin >> inp[i];
}
cout << endl;
}
void print_array(const int inp[], int n){
cout << "The contents of the array are ";
for(int i = 0; i < n; i++)
cout << inp[i] << " ";
}
int main()
{
const int MAX_SIZE = 5; //constant int. Its value will not change.
int inp[MAX_SIZE];
int div[MAX_SIZE];
int n;
cout << "Please enter how many numbers ";
cin >> n;
fill_array(inp, n, MAX_SIZE);
if(n > MAX_SIZE)
n = min(n, MAX_SIZE);
print_array(inp, n);
return 0;
}
Part 2:
#include <iostream>
using namespace std;
void fill_array(int inp[], int n, int maximum){
if(n > maximum){
cout << "Cannot enter more than " << maximum << " elements. ";
n = min(n, maximum);
}
cout << "Please enter " << n << " numbers : ";
for(int i = 0; i < n; i++){
cin >> inp[i];
}
cout << endl;
}
void print_array(const int inp[], int n){
cout << "The array is: ";
if(n == 0){
cout << "No elements to print ";
return;
}
for(int i = 0; i < n; i++)
cout << inp[i] << " ";
}
int divisible(const int inp[], int div[], int n, int d){
int k = 0;
for(int i = 0; i < n; i++){
if(inp[i] % d == 0)
div[k++] = inp[i];
}
return k;
}
int find_max(int arr[], int n){
int mx = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] > mx)
mx = arr[i];
}
return mx;
}
int find_min(int arr[], int n){
int mn = arr[0];
for(int i = 1; i < n; i++){
if(arr[i] < mn)
mn = arr[i];
}
return mn;
}
int main()
{
const int MAX_SIZE = 5;
int inp[MAX_SIZE];
int div[MAX_SIZE];
int n, d;
cout << "Please enter how many numbers (less than " << MAX_SIZE << ") : ";
cin >> n;
fill_array(inp, n, MAX_SIZE);
cout << "Please enter a divisor: ";
cin >> d;
if(d == 0){
cout << "Cannot divide by 0. Terminating the program ";
return 1;
}
int num = divisible(inp, div, n, d); //num will store tha number of elements in div[]
int max_el, min_el;
if(num){ //if number of elements in div[] > 0
max_el = find_max(div, num); //find max
min_el = find_min(div, num); //find min
}
if(!(min_el == 0 && max_el == 0)) // calls print_array() only when min_el and max_el are not zero.
print_array(div, num);
if(num == 0)
cout << "No min element ";
else
cout << "The min = " << min_el << " ";
if(num == 0)
cout << "No max element ";
else
cout << "The max = " << max_el << " ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.