C++
3. Random modification of all elements in an integerarray.
a) Initialize an integer array of capacity 5.
b) Implement an array output function.
c) Output the array to console.
d) Implement an offset function that accepts an integer array asinput and randomly
modifies each element by +/- 5.
e) Use the offset function to update the array.
f) Output the modified array.
Example Output (input in bold)
Original: 5 1 4 9 2
Offset: 7 -2 6 11 4
Answer
#include <iostream>
using namespace std;
void printArray(int arr[]){
for(int i=0;i<5;i++)
cout<<arr[i]<<” “;
cout<<endl;
}
int main()
{
int arr[]={5 ,1 ,4 ,9 ,2};
printArray(arr);
for(int i=0;i<5;i++){
if(rand()%2==0)
arr[i]+=5;
else
arr[i]-=5;
}
printArray(arr);
return 0;
}
5
OR
OR