Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(7) Consider two arrays of size n of type int. Each array is randomly initialized to a collection of n integer values less than or

(7) Consider two arrays of size n of type int. Each array is randomly initialized to a collection of n integer values less than or equal to 3. We want to write an algorithm that determines how many times both arrays, at certain index i, are set to the same integer value 1, 2, or 3. Let's say we have array A and array B as below.

A = [a0][a1]...[an1]

B = [b0][b1]...[bn1]

We want to check if ai and bi are both set to the same integer value where ai, bi {1, 2, 3}.

Which of the below for loops most effectively counts the number of indexes at which A and B are both equal.

(a)

int a[n]; // Assume A Initialized

int b[n]; // Assume B Initialized

int set = 0;

for(int i = 0; i < n; i++) {

if(a[i]) {

if(a[i] == b[i]) {

set++;

}

}

}

std::cout << set;

(b)

int a[n]; // Assume A Initialized

int b[n]; // Assume B Initialized

int set = 0;

for(int i = 0; i < n; i++) {

if(a[i]) {

if(a[i] != b[i]) {

set++;

}

}

}

std::cout << set;

(c)

int a[n]; // Assume A Initialized int

b[n]; // Assume B Initialized

int set = 0;

for(int i = 0; i < n; i++) {

if(a[i]) {

if(a[i] = b[i]) {

set++;

}

}

}

std::cout << set

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions