int lastof(const int a[], int aLen, const int b[], int blen) { int res = 0; for (int i = 0; i < alen; i++) { for (int j = blen; j >=0 ; j--) if (blj] == a[i]) { if(res

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Write the function lastOf which searches the array a for the last occurance of any value contained
in the array b. Returns the index if found. If not found, return -1.
arrays.cpp
1
#include <cstddef>
2
3
int lastof(const int a[), int alen, const int b[], int blen)
4
{
int res = 0;
for (int i = 0; i < alen; i++) {
for (int j = bLen; j >=0 ; j--)
if (b[j] == a[i])
{
if(res<j)
{res=j;}
}
else
14
res = -1;
}
return res;
15
16
17
18 }
CodeCheck
Reset
Testers
Running Tester.cpp
fail fail pass pass fail
al->[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8]
b1->[7, 5, 9]
last0f(al, 14, b1, 3): -1
Expected: 12
al->[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8]
b2->[7, 7]
lastof (al, 14, b2, 2): -1
Expected: 12
a2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14]
b2->[7, 7]
lastof (a2, 11, b2, 2): -1
Expected: -1
а2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14]
b1->[7, 5, 9]
last0f (a2, 11, b1, 3): -1
Expected: -1
а2->[3, 2, 1, 14, 10, 6, 13, 10, 11, 13, 14]
b3->[11, 2, 1, 15]
lastof (a2, 11, b3, 4): -1
Expected: 8
Score
2/5
Transcribed Image Text:Write the function lastOf which searches the array a for the last occurance of any value contained in the array b. Returns the index if found. If not found, return -1. arrays.cpp 1 #include <cstddef> 2 3 int lastof(const int a[), int alen, const int b[], int blen) 4 { int res = 0; for (int i = 0; i < alen; i++) { for (int j = bLen; j >=0 ; j--) if (b[j] == a[i]) { if(res<j) {res=j;} } else 14 res = -1; } return res; 15 16 17 18 } CodeCheck Reset Testers Running Tester.cpp fail fail pass pass fail al->[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8] b1->[7, 5, 9] last0f(al, 14, b1, 3): -1 Expected: 12 al->[1, 9, 4, 5, 1, 5, 1, 12, 8, 11, 2, 11, 7, 8] b2->[7, 7] lastof (al, 14, b2, 2): -1 Expected: 12 a2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14] b2->[7, 7] lastof (a2, 11, b2, 2): -1 Expected: -1 а2->[3, 2, 1, 14, 10, 6, 13, 1ө, 11, 13, 14] b1->[7, 5, 9] last0f (a2, 11, b1, 3): -1 Expected: -1 а2->[3, 2, 1, 14, 10, 6, 13, 10, 11, 13, 14] b3->[11, 2, 1, 15] lastof (a2, 11, b3, 4): -1 Expected: 8 Score 2/5
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY