Ask coding questions

← Back to all posts
how to resolve this code\
Deathshotgaming (0)

#include<stdio.h>
using namespace std;

template <class T>
long interpolSearch(const T& key, T* vp, int len)
{
int expect, begin =0, end = len - 1;
double temp;

if( end < 0
|| key > vp[end] || key < vp[begin])
return -1;
while( begin <= end)
{
    if(key > vp[end] || key > vp[begin])
    return -1;
    temp = (double) (key - vp[begin])/ (vp[end] -vp[begin]);
    temp = temp * (end - begin) + 0.5;
    expect = begin + (int)temp;
    if(vp[expect]== key)
        return expect;
    if(vp[expect] > key)
        end = expect - 1;
    else begin = expect+1;

}
return -1;

}

template <class T>
void insertionSort (T* vp, int len)
{
T temp;
for(int i = 0; i < len; i++ )
{
temp = vp[i];
int j;
for(j = i-1; j >= 0 && vp[j] > temp; j--)
vp[j + 1] = vp[j];
vp[j+ 1] = temp;
}
}

template <class T>
void display(T* vp, int len)
{
cout << "\n\nThe array: " << endl;
for(int i = 0; i < len; i++)
{
cout << endl;
if( (i+1)%10 == 0)
cout << endl;
}
cout << endl; cin.get();

}

short sv[5] = {7,9,2,4,1};
double dv[5] = {5,7,3.5,2.1,9.4, 4.3};

int main()
{
cout << "\n Instantiation for type shourt: " << endl;
display(sv, 5);
short key;
cout << "\nArray element? " cin >> key; cin.sync();
int pos = interpolSearch(key , sv, 5);
if (pos != -1)
cout << "\nfound!" << endl, cin.get();
else
cout << "\nnot found!" << endl, cin.get();

cout << "\nInstantation for type double: " << endl;
display(dv, 5);

insertionSort(dv, 5);
cout << "\nAfter sorting: ";
display(dv, 5);

double dkey;
cout << "\nArray element?"; cin >> dkey; cin.sync();
pos = interpolSearch(dkey, dv, 5);
if(pos != -1)
    cout << "\nfound!" << endl, cin.get();
return 0;

}

Comments
hotnewtop
19ecal (228)

This is because you are doing #include <stdio.h>, which has functions printf(), scanf(), etc. What you want to do instead is #include <iostream>, which allows you to use cout, cin, endl etc.

Deathshotgaming (0)

ooh!! stupid me bro thanks for helping me

Deathshotgaming (0)

don't know how to figure out of this code