1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Paladinen [302]
3 years ago
10

Write a C function that takes an STL vector of int values and determines if all the numbers are different from each other (that

is, they are distinct).

Computers and Technology
1 answer:
eduard3 years ago
7 0

Answer:

Here is the function:

#include <iostream>  //to use input output functions

#include <vector>  // to use vector (sequence containers)

#include <algorithm>   //use for sequence operations

#include<iterator>  //to move through the elements of sequence or vector

using namespace std;  //to identify objects cin cout

 void DistinctNumbers(std::vector<int> values) { /*function that takes  an STL vector of int values and determines if all the numbers are different from each other */

     sort(values.begin(), values.end());  //sorts the vector elements from start to end

     bool isDistinct = std::adjacent_find(values.begin(), values.end()) == values.end();  //checks for occurrence of two consecutive elements in vector

if(isDistinct==true)  // if all numbers are different from each other

{cout<<"Numbers are distinct";}

else  //if numbers are duplicate or same

{cout<<"Numbers are not distinct";}   }  

int main(){      //start of main function

std::vector<int> v = {1,2,3,4,5};  // vector of int values

DistinctNumbers(v); }  //function call to passing the vector v to check if its elements are distinct

Explanation:

The program that takes an STL vector of int values and the function DistinctNumbers determines if all the numbers are different from each other. It first sorts the contents of the vector in ascending order using sort() method. Then it used the method adjacent_find() to searches the range means from the start to the end of the vector elements, for first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found. The result is assigned to a bool type variable isDistinct. It then checks if all the numbers are different and no two adjacent numbers are same. If all the numbers are distinct then this bool variable evaluates to true otherwise false. If the value of isDistinct is true then the message :Numbers are distinct is displayed on screen otherwise message: Numbers are not distinct is displayed in output screen.

You might be interested in
Which of the following can be represented by a single binary digit?
Zinaida [17]

Answer:

  B.  The remainder when dividing a whole number by 2

Explanation:

A binary digit can have the values 0 or 1. The remainder from division of an integer by 2 will be 0 or 1. Hence that remainder can be represented by a single binary digit.

__

That fact can be used to do conversion of a number to binary.

8 0
3 years ago
Choose all of the items that accurately describe an operating system.
Simora [160]

Answer:

provides the platform that application software runs on, manages a computer's hardware, and implements features like file and folder management.

Explanation:

6 0
3 years ago
The ______ cloud service model provides virtual environments online that can be tailored to the needs of developers
Ratling [72]
Internet cloud service that could be accessed by other devices with a certain password
3 0
3 years ago
Which of the following agencies protects human health and the natural environment?
Alex Ar [27]
The answer to this is the EPA 
 

7 0
3 years ago
Read 2 more answers
Which steps are needed for Word to create an Index? Select two options.
marysya [2.9K]

Answer:

Insert Index

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Given the security levels TOP SECRET, SECRET, CONFIDENTIAL, and UNCLASSIFIED (ordered from highest to lowest), and the categorie
    13·1 answer
  • Recall the insertion-sort-like algorithm in Problem 4 from Homework 2, where you know that certain pairs of contiguous items in
    15·1 answer
  • Fourlotts Corp. provide integrated services that include storing manufactured goods, packaging, and delivering it to the dealers
    13·1 answer
  • Write a python function c that converts bitstring array back to an integer numpy array
    5·1 answer
  • Which statement does not describe how to save a presentation?
    11·1 answer
  • What is an assembler?
    11·1 answer
  • Henry must choose which type of smart speaker to use in his home. He asked you to help him decide which one will best suit his n
    8·1 answer
  • NumA=2<br> for count range (5,8)<br> numA=numA+count <br> print(numA)
    15·1 answer
  • What does a blinking yelow light mean on a phillips sonicare toothbrush
    9·1 answer
  • Help please! <br>Will give brainliest! ​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!