Two errors
1 : int foundIndex = linearSearch(arr, Size, SearchNum);
change arr[] to arr
2 : int linearSearch(int arr[], int Size, int SearchNum)
change linearsearch to linearSearch
try to understand the error messages of the compiler give you.
Bonus :
1 : c++ is not c89, you don't need to declare every parameters before the function, please study what is "lazy initialization".
2 : please study stl, it is an essential part of c++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
int main()
{
std::array<int, 5> arr{{1,2,3,4,5}};
//or you could use int arr[] = {1,2,3,4,5};
auto it = std::find(std::begin(arr), std::end(arr), 3);
if(it != std::end(arr)){
//you could use std::distance to find the distance too, it is more generic
std::cout<<*it<<" found at index "<<(std::end(arr) - it)<<std::endl;
}
}
| |
3 : to write robust, efficient c++ codes, we only need to know the contents of ch1~ch16 of c++ primer 5(I am still studying edition 5)