-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
74 lines (54 loc) · 1.34 KB
/
13.cpp
File metadata and controls
74 lines (54 loc) · 1.34 KB
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
/*
@Author: Amritanshu Sikdar
Session: 2018-'19
Repository: https://github.com/amritanshusikdar/CS30PracticalQuestion
*/
// To store 10 names in list, print in alphabetical order via selection sort
#include <constream.h>
#include <string.h>
void selectionNames(char[][20], int);
void main()
{
clrscr();
int i;
char name[10][20];
// Taking input
for(i=0; i<10; i++)
{
cout << "Enter name " << i+1 << ": ";
cin >> name[i];
}
// Sorting the array
selectionNames(name, 10);
// Displaying sorted array
cout << "\n\nNames in alphabetical order: " << endl;
for(i=0; i<10; i++)
{
cout << name[i] << endl;
}
getch();
}
void selectionNames(char name[][20], int size)
{
char min[20], temp[20];
int i, j, pos;
for(i=0; i<size-1; i++)
{
pos = i;
strcpy(min, name[pos]);
for(j=i+1; j<=size-1; j++)
{
if(strcmp(name[j], min) < 0)
{
strcpy(min, name[j]);
pos = j;
}
}
if(strcmp(name[i], name[pos]) > 0) // not changing or comparing with min here because changes are to be made in the array
{
strcpy(temp, name[i]);
strcpy(name[i], name[pos]);
strcpy(name[pos], temp);
}
}
}