Skip to content

Commit 130b8c5

Browse files
authored
sortcom.cpp
Input: //sorts these string values string names1[] = {"George","Penny","Estelle","Don","Mike","Bob"}; //converts these inches to centimetres double inches[] = {3.5,6.4,6,7,8}; Output: 8.89 16.256 15.24 17.78 20.32 5.34025e-317 22.5806 Don is element 3 on the list Bob Don Estelle George Mike Penny
1 parent aa95f46 commit 130b8c5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

sorting/sort_strings/sortcom.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//sorts array of strings with user written comparison function
2+
#include<iostream>
3+
#include<string.h>
4+
#include<functional>
5+
#include<algorithm>
6+
using namespace std;
7+
char *names[] = {"George","Penny","Estelle","Don","Mike","Bob"};
8+
bool alpha_comp(char *,char*);
9+
string names1[] = {"George","Penny","Estelle","Don","Mike","Bob"};//Input Strings Array For Sorting
10+
bool isDon(string name)
11+
{
12+
return name=="Don";
13+
}
14+
double in_to_cm(double s)
15+
{
16+
return (s * 2.54);
17+
}
18+
int main()
19+
{
20+
double centi[7];
21+
double inches[] = {3.5,6.4,6,7,8};//Input Values In Inches For Converting To Centimetres
22+
transform(inches,inches+7,centi,in_to_cm);
23+
for(int i=0;i<7;i++)
24+
cout << centi[i] << "\t";
25+
cout << endl;
26+
string *ptr;
27+
ptr=find_if(names1,names1+5,isDon);
28+
if(ptr==names1+5)
29+
cout << "Don is not on the list \n";
30+
else
31+
{
32+
cout << "Don is element " << (ptr-names1) << " on the list\n";
33+
}
34+
sort(names,names+6,alpha_comp);
35+
for(int i=0;i<6;i++)
36+
cout << names[i] << endl;
37+
return 0;
38+
}
39+
bool alpha_comp(char *s1,char *s2)
40+
{
41+
return (strcmp(s1,s2)<0)?true:false;
42+
}

0 commit comments

Comments
 (0)