forked from Jithendra-Bathala236/team_09_cpp_100_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
051DisplayTheArrayInAlphabeticalOrder.cpp
46 lines (37 loc) · 1.1 KB
/
051DisplayTheArrayInAlphabeticalOrder.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
//program to display array in alphabetical order
// inlcuding requried header files
#include <iostream>
#include <string>
using namespace std;
// function to sort an array using bubble sort
void sortArray(string arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n ; j++) {
if (arr[j] > arr[j + 1]) {
string temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// main function
int main() {
// input length and elements of the array from the user
int lengthOfArray;
cout << "Enter the length of the array: ";
cin >> lengthOfArray;
string array[lengthOfArray];
for (int i = 0; i < lengthOfArray; i++) {
cout << "Enter the " << i+1 << "th element of the array: ";
cin >> array[i];
}
// calling the sorting function
sortArray(array, lengthOfArray - 1);
// displaying the sorted array
cout << endl << "Names in alphabetical order:" << endl;
for (int i = 0; i < lengthOfArray; ++i) {
cout << array[i] << endl;
}
return 0;
}