C 3 Random Modification Elements Integer Array Initialize Integer Array Capacity 5 B Imple Q37216488

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 1 49 2 0 6-1 4-3 . . Program finished with exit code 0 Press ENTER to exit console

5

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.