-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbestgenericfunction.cpp
86 lines (86 loc) · 1.33 KB
/
bestgenericfunction.cpp
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
80
81
82
83
84
85
86
#include<cstdlib>
#include<iostream>
using namespace std;
template<typename X>
void sort(X *arr,int size) //generic sort
{
int i,j;
X temp;
for(i=0;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if(arr[j+1]<arr[j])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
template<typename Y> //generic insertions
void insert(Y *arr,int size)
{
int i;
cout<<"Lets make the insertions now"<<endl;
for(i=0;i<size;i++)
{
cout<<"Enter the "<<i+1<<" element now"<<endl;
cin>>arr[i];
}
}
template<typename Z> //generic display
void display(Z *arr,int size)
{
int i;
cout<<"Sorted array is now being displayed"<<endl;
for(i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
}
int main()
{
int quit=0,ch,n;
cout<<"Sorting program in Templates"<<endl;
cout<<"Enter the size of the array you want to sort"<<endl;
cin>>n;
int A[n];
float B[n];
char C[n];
do
{
cout<<"Option Menu"<<endl;
cout<<"1: Int array"<<endl;
cout<<"2: Float array"<<endl;
cout<<"3: Character array"<<endl;
cout<<"4: Quit"<<endl;
cout<<"Enter you choice"<<endl;
cin>>ch;
switch(ch)
/*Note that you cannot declare arrays inside switch*/
{
case 1:
insert(A,n);
sort(A,n);
display(A,n);
break;
case 2:
insert(B,n);
sort(B,n);
display(B,n);
break;
case 3:
insert(C,n);
sort(C,n);
display(C,n);
break;
case 4: quit=1;
break;
default: cout<<"Wrong Choice"<<endl;
}
}while(quit!=1);
return 0;
system("clear");
}