Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions exercises/verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,27 @@ The number 3 is [not] present in the array.
#include <iostream>
using namespace std;

template<typename T> bool contains(const T* Arr, const size_t& dim, const T& toFind){
for(size_t i=0; i<dim; i++)
if(Arr[i]==toFind)
return true;

return false;
}

int main()
{
// placeholder
int N[10] = {3, 4, 5, 1, 2, 3, 4, 9, 13, 0};
int toFind;

cout << "Insert number to find: " << endl;
cin >> toFind;

if(contains(N, 10, toFind))
cout << "The number " << toFind << " is present in the array" << endl;
else
cout << "The number " << toFind << "is NOT present in the array" << endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

le due righe dentro l'if e l'else sono praticamente identiche, potrsti ridurre la ridondanza di codice nel seguente modo:

cout << "The number " << toFind << "is " << contains(N, 10, toFind) ? "" : "NOT " << "present in the array" << endl;


return 0;
}