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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education