Question

In: Computer Science

array • First, create a function called addNumber, which has a formal parameter for an array...

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.

Solutions

Expert Solution

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:-


Related Solutions

Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
create overloaded functions called lastValue. The first function should take as a parameter a string and...
create overloaded functions called lastValue. The first function should take as a parameter a string and the second function should take as a parameter an int. each of you functions should return an int value. in the case of the function that takes the string as an argument you will return the ascii value of the last character in the string. in the case of the function that takes an int parameter your function will return the last digit in...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Define a JavaScript function named prepend that has two parameters. The first parameter is an array...
Define a JavaScript function named prepend that has two parameters. The first parameter is an array of Strings. The second parameter is a String. Your function should use the accumulator pattern to return a newly created array whose elements are the concatenation of the second parameter with the element at the same index in the first parameter.
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and...
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and an integer (the second parameter). If the second parameter is nonnegative, the function returns the value of the first parameter raised to the power of the second parameter. Otherwise, if the second parameter is negative the function returns 0. For example powerTo(2,3)=8, and powerTo(2,-3)=0
Making a blackjack game in javascript Create a function called createDeck() that will create an array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array of objects (52). Each object contains two properties: suit and value. The suit property will be either 'Hearts', 'Clubs', 'Diamonds', or 'Spades'. The value property will be either 'Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three' , or 'Two''. Note: We will probably want to store possible suits and values in separate arrays. Create a function called shuffleDeck() that will...
- Write a function with a parameter called A (which is between 1 and 10) and...
- Write a function with a parameter called A (which is between 1 and 10) and prints the multiplication table for 1 to A. • Note: Do not need to draw the horizontal and vertical lines. • Example for A = 10.
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s...
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s prototype is changed as shown below. Please explain code with comments //-------------------------------------------------- void NextPrime(int *x) //-------------------------------------------------- { bool IsPrime(const int x); }
Create a function named ‘dividing’. This function will take one parameter value, which will be an...
Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of...
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a...
(C++) In a file called pp7c.cpp, write a function called printMoney that has one parameter, a double, and it prints this parameter out formatted like a dollar amount with $ and exactly 2 digits to the right of the decimal. Write a driver that declares an array of monetary amounts like this: double amounts[MAX_AMOUNTS]; and uses a while or do while loop to ask the user for monetary amounts with -1 for the amount as a sentinel value to put...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT