Write a function named countUnique that returns the number of unique values in a sorted array. Since the array is in sorted order, duplicates will be grouped together. For example, consider the array: | int list[] = {5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41}; The call countUnique(list, 15) should return 9 because this list has 9 unique values (5, 7, 8, 22, 23, 31, 35, 40 and 41). It is possible that the list might not have any duplicates. If passed an empty array, your function should return 0. arrays.cpp 1 #include // size_t 2 int countUnique (const int a[], size_t len) { int count{0}; 3 4 7 8. 9. return count; } 10 Tester.cpp #include using namespace std; 4 int countUnique (const int a[], size_t len); 5 int main() { int a[] = {5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41}; cout « countUnique(a, 15) <« endl; cout « "Expected: 9" « endl « endl; 7 9. 10 11 int b[] = {1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41}; cout « countUnique(b, 15) <« endl; cout « "Expected: 15" « endl « endl; 12 13 14 15 int c[] = {2, 4, 6}; cout « countUnique(c, 3) « endl; cout « "Expected: 3" « endl « endl; 16 17 18 19 int d[] = {}; cout « countUnique(d, 0) << endl; cout « "Expected: 0" << endl « endl; 20 21 22 23 int e[] = {-4, -2, 2, 4, 6, 8, 10, 12, 12}; cout « countUnique(e, 9) « endl; cout « "Expected: 8" « endl « endl; 24 25 26 27 int f[] = {0, 0, 0, 0, 0, 0, 0, 0, 1}; cout « countUnique(f, 9) <« endl; cout « "Expected: 2" « endl « endl; } 28 29 30 31 CodeCheck Reset 123
Write a function named countUnique that returns the number of unique values in a sorted array. Since the array is in sorted order, duplicates will be grouped together. For example, consider the array: | int list[] = {5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41}; The call countUnique(list, 15) should return 9 because this list has 9 unique values (5, 7, 8, 22, 23, 31, 35, 40 and 41). It is possible that the list might not have any duplicates. If passed an empty array, your function should return 0. arrays.cpp 1 #include // size_t 2 int countUnique (const int a[], size_t len) { int count{0}; 3 4 7 8. 9. return count; } 10 Tester.cpp #include using namespace std; 4 int countUnique (const int a[], size_t len); 5 int main() { int a[] = {5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41}; cout « countUnique(a, 15) <« endl; cout « "Expected: 9" « endl « endl; 7 9. 10 11 int b[] = {1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41}; cout « countUnique(b, 15) <« endl; cout « "Expected: 15" « endl « endl; 12 13 14 15 int c[] = {2, 4, 6}; cout « countUnique(c, 3) « endl; cout « "Expected: 3" « endl « endl; 16 17 18 19 int d[] = {}; cout « countUnique(d, 0) << endl; cout « "Expected: 0" << endl « endl; 20 21 22 23 int e[] = {-4, -2, 2, 4, 6, 8, 10, 12, 12}; cout « countUnique(e, 9) « endl; cout « "Expected: 8" « endl « endl; 24 25 26 27 int f[] = {0, 0, 0, 0, 0, 0, 0, 0, 1}; cout « countUnique(f, 9) <« endl; cout « "Expected: 2" « endl « endl; } 28 29 30 31 CodeCheck Reset 123
C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 29SA
Related questions
Question
C++
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning