Question

In: Computer Science

Write a code to initialize a 2D array with random integers (not has to be different)....

Write a code to initialize a 2D array with random integers (not has to be different). Write a function to count the number of cells that contain an even number within the 2D array.

C++

Solutions

Expert Solution

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

using namespace std;

#define ROWS 3
#define COLUMNS 4

int count_evens(int arr[ROWS][COLUMNS]) {
    int count = 0;
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLUMNS; ++j) {
            if (arr[i][j] % 2 == 0) {
                ++count;
            }
        }
    }
    return count;
}

int main() {
    srand(time(NULL));
    // create and populate a 2d array with random data
    int arr[ROWS][COLUMNS];
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLUMNS; ++j) {
            arr[i][j] = rand() % 100;
        }
    }
    // print array
    cout << "2D array is" << endl;
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLUMNS; ++j) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
    // print number of evens
    cout << "Number of even numbers in array is " << count_evens(arr) << endl;
    return 0;
}


Related Solutions

write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
a) Write C code initialize an array of ints to the last four digits of your...
a) Write C code initialize an array of ints to the last four digits of your phone number. Use a loop to add the digits. Calculate and display the average of the digits. b) Write C code using a struct to hold student information: integer id integer number of hours taken integer number of hours passed double gpa
For c++ Write the code to initialize an array such that each element gets two times...
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
Declare and initialize an array to store the course name or code.
Declare and initialize an array to store the course name or code.
Write code that would allocate the space for an array of 20 integers. It will be...
Write code that would allocate the space for an array of 20 integers. It will be pointed to by a variable named "junk" Write code that puts “file did not open” into an error stream.
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
Java- Create a new class named Forest that has a 2D array of integers to store...
Java- Create a new class named Forest that has a 2D array of integers to store a forest. Create a private inner class Position to store the row and column of the current cell. Create a Depth First Search Method that uses a stack to remove the current tree and search its neighbors based on the following pseudocode: // given a forest "f" // store positions on fire Stack<Position> cellsToExplore // the forest is aflame! for each tree in the...
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of...
Submit a well-commented C program.   Create code. That will build a random 8x8 2D array of small-case characters. Demonstrate how it functions by printing out the result on your screen. Add a function will print out a single line of the array. If the user enters a value between 0 and 7 to select to print out a single row of the original 2D array.                void printLine(char *,int);    // This is the prototype declaration             void printLine(char myArr[][])     //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT