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
1. You are given a database to create, however, you realize that in the instructions you are told to create relationships betwee
Alik [6]

Answer:

In your table create statement, you can reference a primary key of another table. This is called a foreign key.

The syntax varies per database type.

6 0
3 years ago
Jim wants to buy a car, he’ll probably only need it for a couple of years he has a short commute to work so he won’t put many mi
Papessa [141]
Is there more information? or options?
4 0
3 years ago
What is Computer system software <br>​
myrzilka [38]

Answer:

<em><u>System</u></em><em><u> </u></em><em><u>soft</u></em><em><u>ware</u></em> is software designed to provide a platform for other software...

6 0
4 years ago
You are to write a program that performs basic statistical analysis on a 500 element array of integers. Your program should incl
mrs_skeptik [129]

Answer:

#include <iostream>  

#include <cstdlib>  

#include <ctime>  

using namespace std;

int* generateArray(int size)  

{

 int *numbers = new int[size];

 for(int i=0;i<size;i++){

   numbers[i] = (rand()%100) + 10;

 }

 return numbers;  

}

int findMode(int *arr, int size)  

{

 int countMode = 0;

  int mode ;

 int count;

 for(int i=0;i<size;i++) {

    count = 1;

   for(int j=i+1;j<size;j++) {

     if(arr[j] == arr[i] ){

       count++;

      }

     if(count > countMode) {

        countMode = count;

       mode = arr[i];

     }

   }

}

 return mode;

}  

int findMedian(int *arr, int size)  

{

  sort(arr,size);

  int median;

   if(size%2 == 0){

   median = (arr[int((size-1)/2)]+arr[size/2])/2;

 } else{

     median = arr[(size-1)/2];

 }

 return median;

}

void sort(int *arr, int size)

{

 int min;

  for(int i=0;i<size-1;i++) {

    min = i;

    for(int j=i+1;j<size;j++){

     if(arr[j] < arr[min]){

       min = j;

     }

     if(min != i) {

       int temp = arr[i];

       arr[i] = arr[min];

       arr[min] = temp;

     }

   }

 }

}

int main()  

{

 srand(time(0));  

 int size = 501;    

 int *numbers = generateArray(size);

 cout<<"Mode : "<<findMode(numbers,size)<<endl;

 cout<<"Median : "<<findMedian(numbers,size)<<endl;  

 return 0;  

}

 

Explanation:

The C++ program is able to generate 500 integer items into the defined array and statistically analyze and provide the average and modal number in the array of integer dataset.

3 0
3 years ago
...........is a systems development technique that tests system concepts and provides an opportunity to examine input, output, a
EleoNora [17]

Answer:Prototyping

Explanation:Prototyping is the software activity in which there is the production of the prototypes which has the incomplete form of application .It is used for the development of the real-world operating system and used in the field of the software development. It provides the benefit to the user by examining about the product's reliability , output, input etc .

3 0
3 years ago
Other questions:
  • A common chart type used to show the contributions of items to a whole is a _____ chart.
    6·1 answer
  • Which process best describes how you might prepare to apply for a job?
    6·2 answers
  • Your database was damaged due to hardware failure. What can you use to restore it?
    5·1 answer
  • Define what is meant by an entity in a data model. How should an entity be named? What information about an entity should be sto
    5·1 answer
  • What is computer ?how does it work​
    6·2 answers
  • Which of the following demonstrates the proper way to specify a variable-length argument list?
    7·1 answer
  • Which of the following is true of a procedure? Check all that apply.
    10·2 answers
  • Write the simulate method, which simulates the frog attempting to hop in a straight line to a goal from the frog's starting posi
    10·1 answer
  • Place the steps in order for adding an additional email account in outlook
    12·1 answer
  • Pieter is a network administrator for a growing company and has decided to implement Kerberos. He knows that Kerberos uses a tic
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!