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
icon
Related questions
Question

C++

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 <cstddef>
// size_t
int countUnique (const int a[), size_t len)
{
int count{0};
3
4
6.
7
8
9.
return count;
10
}
Tester.cpp
1
#include <iostream>
2
using namespace std;
3
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
8
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
Transcribed Image Text: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 <cstddef> // size_t int countUnique (const int a[), size_t len) { int count{0}; 3 4 6. 7 8 9. return count; 10 } Tester.cpp 1 #include <iostream> 2 using namespace std; 3 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 8 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning