Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

problem in template programing

2 posters

Go down

problem in template programing Empty problem in template programing

Post by Navitha Fri Apr 09, 2010 1:18 pm

#include<iostream>

using namespace std;

template <class t>
t min1(t *x)
{
int i=0;
t m;
m=x[0];
while(x[i]!='\0')
{
if(m > x[i])
{
m = x[i];
i++;
}
}
return m;
}

int main()
{
int i;
int a[10];
//float b[10];
for(i=0;i<3;i++)
{
cout<<"Enter the values of a"<<endl;
//for(i=0;i<10;i++)
cin>>a[i];
}
cout<<"enter complete"<<endl;
//for(i=0;i<10;i++)
/*cout<<"Enter the values of b"<<endl;
for(i=0;i<10;i++)
cin>>b[i];*/
cout<<"minimum value is a"<<min1 <int> (a)<<endl;
//cout<<"minimum value is b"<<min1(b)<<endl;
return 0;
}

[335937@oracleclient FIN]$ ./a.out
Enter the values of a
1
Enter the values of a
2
Enter the values of a
3
enter complete



Nothing is displayed after that.

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

Back to top Go down

problem in template programing Empty Re: problem in template programing

Post by Christopher Fri Apr 09, 2010 1:29 pm

Code:
#include<iostream>

using namespace std;

template <class t>
t min1(t *x)
{
int i=0;
t m;
m=x[0];
for(int i=0;i<3;i++)
{
if(m > x[i])
{
m = x[i];
i++;
}
}
return m;
}

int main()
{
int i;
int a[10];
for(i=0;i<3;i++)
{
cout<<"Enter the values of a"<<endl;
cin>>a[i];
}
cout<<"enter complete"<<endl;
cout<<"minimum value is a"<<min1 <int> (a)<<endl;
return 0;
}

Dont use NULL to check the end of an array. All Arrays doesnot ends with NULL. Only character array has NULL value at the end.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

problem in template programing Empty Re: problem in template programing

Post by Navitha Fri Apr 09, 2010 1:36 pm

thank you. BUt how to check the end of the integer array..

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

Back to top Go down

problem in template programing Empty Re: problem in template programing

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum