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]
2 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:
eduard2 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
Your friend really likes talking about owls. Write a function owl_count that takes a block of text and counts how many words the
andriy [413]
Text = “ I really like owls. Did you know that an owls eyes are more than twice as big as the eyes of other birds of comparable weight? And that when an owl partially closes its eyes during the day, it is just blocking out light? Sometimes I wish I could be an owl.

word = ‘owl’
texts = text.lower()
owlist = list(texts.split())
count = text.count(word)
num = [owlist, count] #num has no meaning just random var
print(num)


Alter in anyway you want so that you can succeed. ✌
4 0
3 years ago
Why might a variable used for output have to be of a different type then a variable used for input?​
telo118 [61]

Answer:

You may use a different variable type for input in order to process the data appropriately and may use a different variable type to accommodate your program.

Explanation:

Your input may have to be different then output varying on what data you are processing.

For example, just like the last question you asked about calculating the area of the rectangle, your input MUST be converted to a different a numerical data type (i.e int or float) in order to do the math.

Based on your last question, if we didn't convert your input into a string your results wouldn't add the numbers together but only concatenate them. If I typed 3 & 5 as length & width, I would get 35 instead of 15.

Now another example is using functions (or methods whatever you want to call them), your input can be one thing and outputs another.

Let's say I want a function to tell me if I am smart. The way I want it to determine if its smart is by inputting my GPA into it, which is a numerical value. So the function would look something like this:

<u>Code (Python)</u>

def IsSmart(gpa):

  if (gpa >= 4):

       return True

  else

       return False

As you can see I am inputting a numerical value and it is outputting a boolean value. I can use this in a if else chain to spit out an output to the user.

7 0
3 years ago
HAS ANYONE HEARD OF THE GAME SCHOOL DAY
Musya8 [376]

Answer:

NO BUT IT SOUNDS FUN

Explanation:

I PROBABLY HAVE NOT HEARD OF IF BC IM HOMESCHOOLED :D

3 0
2 years ago
Write some positive and negative impacts of computer in our daily life​
8090 [49]
Positive impacts are
1. Communicating with people
2. Daily update of what going on in the world
3. learn new things
Negative impacts are
1. Viruses
2. Private information shared
3. Less face to face conversations
3 0
2 years ago
What is the name of this tool and what can it be used for?<br> Thanks!!
Iteru [2.4K]
That is a corqet tool it is used for fixing tires
6 0
3 years ago
Other questions:
  • Arrange these stages of website design in the order that they follow. developing planning learning designing testing and deliver
    12·1 answer
  • If there are no differences between the amino acid sequences in the cytochrome c protein of humans and chimpanzees why aren't we
    15·1 answer
  • RAID level ________ refers to disk arrays with striping at the level of blocks, but without any redundancy. A. 0 B. 1 C. 2 D. 3
    7·1 answer
  • Write a program that asks the user for two file names. The first file will be opened for input and the second file will be opene
    8·1 answer
  • Gina is driving her car down the street. She has a teddy bear sitting on the back seat. A dog runs in front of Gina's car, so sh
    15·2 answers
  • Question #4
    10·1 answer
  • True or False? Using your traffic analytics report, you can see the source of traffic to your website
    13·1 answer
  • How would I collect a number from the user to use for the radius of a circle?
    13·1 answer
  • What features should you present when demonstrating 2023 murano’s confident cornering?.
    11·1 answer
  • You have been working as a junior data analyst at Bowling Green Business Intelligence for nearly a year. Your supervisor, Kate,
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!