Question

In C++ Write a function called fillArray that will fill an array of any constantly declared...

In C++ Write a function called fillArray that will fill an array of any constantly declared array size variable with random numbers in the range of 1 - 100.

Write a function called printArray that will print an array of any size.

Write a third function called countEvens which will count and return the number of even numbers in the array.

In main create an array that holds 25 ints. Use your functions to fill, print, and count the number of even numbers. You should

Fill the array

Print the array

Print the number of even numbers in the array

What not to do:

global variables

cout in any function other than main and printArray

goto statements

Please don't forget countEvens

Your output should look like the following:

xid-15584178_1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void fillArray(int arr[], int size)
{
for (int i = 0; i < size; ++i) {
arr[i] = 1 + (rand() % 100);
}
}

void printArray(int arr[], int size)
{
cout << "Array is: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}

int countEvens(int arr[], int size)
{
int count = 0;
for (int i = 0; i < size; ++i) {
if (arr[i] % 2 == 0) {
count++;
}
}
return count;
}

int main()
{
srand(time(NULL));
int arr[25];
fillArray(arr, 25);
printArray(arr, 25);
int evens = countEvens(arr, 25);
cout << "Number of even numbers in array is " << evens << endl;
return 0;
}

Array is: 9 20 32 29 ? 25 65 38 2 30 94 14 97 99 23 ? 3? 61 22 2 15 б3 Number of even numbers in array is 10 Process exited a

Add a comment
Know the answer?
Add Answer to:
In C++ Write a function called fillArray that will fill an array of any constantly declared...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Create a function called countOccurences that will count how many times a certain number occurs...

    C++ Create a function called countOccurences that will count how many times a certain number occurs in the array. The function should take an array, a size and a variable, which is what you are searching for. Heres what I got so far: #include <iostream> #include <string> #include <vector> using namespace std; void fillArray(int a[], int size); void printArray(int a[], int size); int countEvens(int ar[], int size); int main() {    int ar[10];    fillArray(ar, 10);    printArray(ar, 10);   ...

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and...

    C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and the size of the array.as parameters. The function should then create a new array that is the same size as the original array. The function should copy the elements from the first array into the second array in reverse order. So if the initial array is {'a' , 'b', 'c'} then the new array will be {'c', 'b', 'a'} Then in your main(), create...

  • 17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int...

    17.9 Worksheet 7 (C++) Follow the instructions commented into the given template. int main() { int array1[20] = {3, 18, 1, 25, 4, 7, 30, 9, 80, 16, 17}; int numElements = 11; cout << "Part 1" << endl; // Part 1 // Enter the statement to print the numbers in index 4 and index 9 // put a space in between the two numbers    cout << endl; // Enter the statement to print the numbers 3 and 80...

  • In C++ Write a function that accepts two int arrays of the same size. The first...

    In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...

  • I cannot figure out how to get the function to work in separate source files in...

    I cannot figure out how to get the function to work in separate source files in C++ Given that arrayIntValues [MAX_ROWS][MAX_COLUMNS] is a 2 dimensional array of positive integers, write a C++ function howManyEven to find the total number of even elements in the array. It should have input parameter array arrayIntValues. The function should return an integer. Also create a C++ function called printArray, with the input parameter array arrayIntValues, to print out the elements in the array (be...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • X43: countEvens Write a function in Java that takes an int array as its parameter and...

    X43: countEvens Write a function in Java that takes an int array as its parameter and returns the number of even ints it contains. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1. Complete the code bellow. Complete the code below. Use: logic conditionals if arrays loops mod public int countEvens(int[] nums) { }

  • C++ ProgrammingWrite a function that takes an integer array, the array size and a number....

    C++ ProgrammingWrite a function that takes an integer array, the array size and a number. And it determines how many times that number appears in the array. Just copy the following code and paste it to your answer. And fill the corresponding part. #include < iostream >using namespace atd; int howmany (int array lint , int number){ //Your code comes here} int main() {const int N =10; int array[N] = [11,3,2,1,3,4,6,9,1.3); int count = howlany(array,N,3);cout << 3 < "appesa" << cout << "times!"; return 0;

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT