In: Computer Science
array
• First, create a function called addNumber, which has a formal
parameter for an array of integers and increase the value of each
array element by a random integer number between 1 to 10.
o Add any other formal parameters that are needed.
• Second, create another function called printReverse that prints
this array in reverse order.
• Then, you need to write a C++ program to test the use of these
two functions.
Solution:-
C++ Code:-
// Including necessary header files
#include <iostream>
#include <time.h>
using namespace std;
// Function incrementing each array element by a random
integer
void addNumber(int A[],int k,int s)
{
// Looping for each element
for(int i=0;i<s;i++)
{
// incrementing each element by random integer
A[i] += k;
}
}
// Function printing the updated array in reverse order
void printReverse(int A[],int s)
{
cout<<"Array in reverse order: ";
// Looping form last index till first index element
for(int j=s-1;j>=0;j=j-1)
{
// Printing the array
cout<<A[j]<<" ";
}
}
// Main function
int main()
{
// Declaring an array
int num, A[] = {21,14,5,11,7,19};
// Computing size of array
int len = sizeof(A) / sizeof(A[0]);
// random seed initialization
srand(time(NULL));
// Taking random integer from 1 to 10
num = rand()%10 + 1;
// Printing the generated random integer
cout<<"Random integer = "<<num<<endl;
// Calling addNumber function
addNumber(A,num,len);
// Calling printReverse function
printReverse(A,len);
return 0;
}
Code snapshot:-
Output snapshot:-