1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
//header file
//header file
#pragma once
#include "stdafx.h"
#include <iomanip>
template <typename T>
void display_array(T array2, int arraysize) {
int size = arraysize;
for (int i = 0; i <size;i++){
cout << array2[i] << " ";
}
}
template <typename g>
int arrayLocation(g array2, int arraySize1, g target1) {
g input;
cout << "PLease enter value searching for " << endl;
input = target1;
for (int i = 0; i <= arraySize1; i++) {
if (input == array2[i]) {
return 1;
}
else if (i == arraySize1 && g == NULL)
return -1;
}
}
//main
#include "stdafx.h"
#include "Locate.h"
using namespace std;
typedef int ItemType;
void display_array();
int arrayLocation();
int _tmain(int argc, _TCHAR* argv[])
{
const int arraySize = 10;
ItemType array1[arraySize] = { 23,43,12,45,65,77,22,10,99,43 };
cout << "The values in the array as are follows: \n";
display_array(array1, arraySize);
ItemType target;
cout << "Enter a Value currently in the Array: ";
cin >> target;
ItemType result = arrayLocation(array1, arraySize, target);
if (result != -1)
cout << "\n\nThe Value " << target << " was found at location "
<< result << endl;
else
cout << "\n\nThe Value " << target << "was not found in the array " << endl;
return 0;
}
|