Question

In: Computer Science

Write a statement to call prepare Arr to set values for the array (Using C++) #include...

Write a statement to call prepare Arr to set values for the array (Using C++)

#include
using namespace std;
const int NUM = 10;
void prepareArr(int a[]);
int countEven (int b[]);
int main() {
int arr[NUM];
// write a statement to call prepareArr to set values for the array


// write a statement to call countEven and print the data returned

for(int i = 0; i cout << arr[i] <<" ";
cout <

return 0;

}
void prepareArr(int a[])
{
//randomly generate NUM integers in the range [0,99] and save them in the array parameter.

}
int countEven (int b[])
{
//count the number of even integers in the array parameter and return the number, note that the size of the array is specified with NUM.

}

Answer in C++

Solutions

Expert Solution

Since here we're supposed to pass the array as an argument to the functions. I would like to tell you something about how arrays are passed as an argument in cpp. Unlike any other datatype variables, the entire array is not passed to the function, instead only the first element's address is passed.

Now, coming to your question. Here's the complete, commented code:

#include<iostream>
using namespace std;
const int NUM = 10;
void prepareArr(int a[]);
int countEven (int b[]);

int main() 
{
    int arr[NUM];
    // write a statement to call prepareArr to set values for the array
    prepareArr(arr);  // as the return type of prepareArr() is void, nothing is returned to be stored into another variable.

    // write a statement to call countEven and print the data returned
    int evenCnt = countEven(arr);  // as the return type is int, the value returned by the function has to be caught by some other int type variable.
    cout << "Value returned by countEven() : " << evenCnt << endl;   // printing the returned value
    for(int i = 0; i<NUM; i++)
        cout << arr[i] <<" ";
    cout <<endl;
    return 0;
}
void prepareArr(int a[])
{
//randomly generate NUM integers in the range [0,99] and save them in the array parameter.
    int lower = 0, upper = 99;
    for(int i = 0; i < NUM; i++)
        a[i] = (rand() % (upper - lower + 1)) + lower;  // this formula is used to set the range to generate random numbers between a given range.
}
int countEven (int b[])
{
//count the number of even integers in the array parameter and return the number, note that the size of the array is specified with NUM.
    int cnt = 0;   // variable to store the final answer
    for(int i = 0; i < NUM; i++)   // for traversing through the array
        if(b[i]%2 == 0)    // check for even number
            cnt++;   // if yes, then increment the variable.
    
    return cnt;  // returning the value.
}

Here's the screenshot of the code in my IDE for a better understanding of indentation:

And here's the screenshot of the output generated from this:

Although the code is heavily commented and everything is explained there itself, but in case you have a query do feel free to ask it out in the comments down below :)


Related Solutions

Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2,...
Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2, 7, 7, 5}, N = 5 Output: Duplicate Element: 5 Duplicate Element: 7 Example 2 Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8 Output: Duplicate Element: 2 Duplicate Element: 5 Duplicate Element: 6 CODE:: class Main {    public static void FindDuplicate(int[] arr){ // write your code to find duplicates here and print those values    }   ...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values...
MUST BE DONE IN C (NOT C++) Using an array and a function, print the values of an array backwards. Please follow these guidelines: - Setup your array manually (whichever values you want, as many as you want and whichever datatype you prefer). - Call your function. You should send two parameters to such function: the array’s length and the array. - Inside the function, go ahead and print the array backwards. - Your function shouldn’t return anything
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values in arr store in the memory of 0(x21), 8(x21), .... , 32(x21). Transform the following RISC-V code into a C program. ld x5, 16(x21) addi x6, x0, 3 sll x5, x5, x6 sd x5, 8(x21)
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT