Question
(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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started