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;
}
#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;
}
Comments
Leave a comment