Write a C++ program that will create 3 collections (arrays). The size of each ar
ID: 2247838 • Letter: W
Question
Write a C++ program that will create 3 collections (arrays). The size of each array should be 10 elements. Each array will store a random number in the range of 50 - 100. Use a ranged for loop to load each array with random numbers.
After you have created and loaded the 3 arrays you will need to make copies of the three arrays. Give the copies the names array1_copy, array2_copy, and array3_copy.
Create 3 functions that will take a single integer and double it. The doubled value will be stored in the array being processed. Use loops to call the functions to double the values in the array elements.
1. The first function will have a return type of int and use call-by-value.
use on array1 function double1
2. The second function will have a return type of void and use call-be-reference using a reference.
use on array2 function double2
3. The third function will have a return type of void and use call-by-reference using a pointer.
use on array3 function double3
Create 3 functions that will take an integer array and double every element in the array. Use loops in the functions that are called to triple the values in the array elements.
1. The first function will have a return type of void and use call-by-reference using a c-style array.
Use [ ] in the function header: void triple1( int array1[ ], int size )
use on array1_copy function triple1
2. The second function will have a return type of void and use call-be-reference using a reference
use on array2_copy function triple2
3. The third function will have a return type of void and use call-by-reference using a pointer.
use on array3_copy function triple3
Here is the sequence you should follow:
1. Create the 3 arrays. Print out each array on a single line nicely formatted [hint - use the setw() stream manipulator]
2. Double the elements in the original 3 arrays using the first set of 3 functions. Print out each array on a single line nicely formatted
3. Triple the elements in the copied 3 arrays using the second set of 3 functions. Print out each array on a single line nicely formatted
Note: You may use traditional random number generation functions or use the new C++2011 more finely tuned random number generation functions.
Note: Do NOT use a loop to make the copies of the array and vector objects. Copies can be made using the assignment operator or using the copy constructor!
Use the new C++2011 ranged for loop instead of the traditional for loop with 3 expressions for ALL loops except for loops working with a c-style (aka built-in) array. A ranged for loop won't work with a c-style array passed to a function.
Here are the function prototypes for the double and triple functions you may use in your program:
int double1 (int number);
void double2 (int &number);
void double3 (int *number);
void triple1 (int array1[], int size);
void triple2 (array<int, 10> &array2);
void triple3 (vector<int> *array3);
Explanation / Answer
//Please see the below code:
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <array>
#include <vector>
#include <cmath>
#include <ctime>
#include <random>
#include <cctype>
using namespace std;
int double1(int element)
{
return element *= 2;
}
void double2(int &element)
{
element *=2;
}
void double3(int *element)
{
*element *= 2;
}
void triple1( int array1_copy[ ], int size )
{
for(int j=0; j<size; j++)
{
array1_copy[j] *=3;
}
}
void triple2(array<int,10> &a2)
{
for(auto &element: a2)
{
element*=3;
}
}
void triple3(vector<int> *a3)
{
for(auto &element : *a3)
{
element*=3;
}
}
int main()
{
//a c-style array
int array1[10];
//An array class object
array<int,10>array2;
//An array class object
vector<int>array3(10);
//Array Copies
int array1_copy[10];
array<int,10>array2_copy;
vector<int>array3_copy(10);
//Double Functions Prototype
int double1(int);
int double2( int &element);
int double3( int *element);
//Triple Functions Prototype
void triple1(int[] , int);
void triple2(array<int,10> &a2 );
void triple3(vector<int> *a3);
default_random_engine engine( static_cast<unsigned>( time(0) ) ); // seed engine
uniform_int_distribution<> distributionInt(50, 100);
cout<< "Each array after store a random number in the range of 50 - 100"<<endl;
//After you have created, loading the 3 arrays you will need
for(auto &element:array1)
{
element = distributionInt(engine);
for(int j=0; j<10; j++)
{
array1_copy[j] = array1[j];
}
cout<< setw(5) <<element;
}
cout<< endl;
for(auto &element:array2)
{
element = distributionInt(engine);
cout<< setw(5) <<element;
}
array2_copy = array2;
cout<< endl;
for(auto &element:array3)
{
element = distributionInt(engine);
cout<< setw(5) <<element;
}
array3_copy = array3;
cout<< endl << endl;
cout<< "Each array after calling a functions to double each element in the array"<<endl;
//calling functions to double the value in the arrays
for(auto &element:array1)
{
element = double1(element);
cout<< setw(5) <<element;
}
cout<< endl;
for(auto &element:array2)
{
double2(element);
cout<< setw(5) <<element;
}
cout<< endl;
for(auto &element:array3)
{
double3(&element);
cout<< setw(5) <<element;
}
cout<< endl<<endl;
// functions to triple array values
triple1(array1_copy,10);
cout<<endl;
triple2(array2_copy);
cout<< endl;
triple3(&array3_copy);
for(auto element:array1_copy)
{
cout<< setw(5) <<element;
}
cout<< endl;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.