//Hi Lads.This is my first attempt to create template functions.
//Here is the main ,which needed to create Locate.h file/header and
//in that Locate.h create templates to support the main.
//If the value does not exist in the array, -1 is returned.
// Templates.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Locate.h"
using namespace std;
typedef int ItemType;
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;
int 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;
}
//And here,my attempt to do it
//Please help me to identify the problem,i now from compiling i have 5-6 errors
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include<iostream>
template<class T >
T display_array(T ItemType[10] , T const int arraySize)
{
cout << array1 << ' ' << arraySize;
}
const int arraySize = 10;
template<class T>
streuct Item Type{
int length;
int array1[arraySize];
}
template<class T>
T arrayLocation( T ItemType[10] , T const int , T Item Type)
{
int first = 0;
int last = array1.length - 1;
int midPoint;
found = false;
while (first < last)
{
midPoint = (first + last) / 2;
if (target > array1.ItemType array1[midPoint])
first = midPoint + 1;
else
last = midPoint;
}
if (last == -1)
return -1;
else if (target == ItemType array[first])
{
found = true;
return first;
}
else
return -1;
}
aluhnev 0 Junior Poster in Training
Banfa 597 Posting Pro Featured Poster
aluhnev 0 Junior Poster in Training
Banfa 597 Posting Pro Featured Poster
aluhnev 0 Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.