Answer to Question #301176 in C++ for Norri

Question #301176

How to execute this using vector







#include <iostream>





bool doesexists(int array[], int toseaarch, int size);







const short int SIZE = 10;







int main() {





int myarray[SIZE], distinct_ctr = 0, index = 0;







std::cout << "Enter 10 numbers: ";







for (index = 0; index < SIZE; index++) {





int tmp;





std::cin >> tmp;







if (!doesexists(myarray, tmp, SIZE)) {





myarray[distinct_ctr] = tmp;





distinct_ctr++;





}





}







std::cout << "distinct numbers are: ";





for (index = 0; index < distinct_ctr; index++)





std::cout << myarray[index] << " ";







std::cout << "\n";







return 0;





}







bool doesexists(int array[], int toseaarch, int size) {





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





if (toseaarch == array[index])





return true;





}





return false;





}

1
Expert's answer
2022-02-23T00:10:59-0500
#include <iostream>
#include <vector>

using namespace std;

bool doesexists(vector<int> vec, int toseaarch, int size);

const short int SIZE = 10;

int main() {
  vecotr<int> myvec(SIZE), distinct_ctr = 0, index = 0;
  cout << "Enter 10 numbers: ";
  for (index = 0; index < SIZE; index++) {
    int tmp;
    cin >> tmp;
    if (!doesexists(myvec, tmp, SIZE)) {
      myvec[distinct_ctr] = tmp;
      distinct_ctr++;
    }
  }
  cout << "distinct numbers are: ";
  for (index = 0; index < distinct_ctr; index++) {
    cout << myvec[index] << " ";
  }
  cout << "\n";
  return 0;
}

bool doesexists(vector<int> vec, int toseaarch, int size) {
  for (int index = 0; index < size; index++) {
    if (toseaarch == vec[index])
      return true;
  }
  return false;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog