QUESTION Please i need help in documenting each line and als
QUESTION:
Please i need help in documenting each line and also a summary of what this program does...Thanks
1 #include <iostream>
2 #include <stdlib.h>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 int main()
 8 {
 9 char names[5][20], temp[20];
 10 int t;
 11 int index[5];
 12 for (int i=0;i<5;i++)
 13 index[i]=i;
 14 for (int i= 0; i< 5; i++)
 15 {cout<<\"\  enter name \"<< i+1<<\": \";
 16 cin>>names[i];
 17 };
 18 //index sort
 19 for (int k=0; k< 5-1; k++)
 20 for (int i=0; i< 5-1; i++)
 21 {if (strcmp(names[index[i]], names[index[i+1]]) >0)
 22 { t = index[i];
 23 index[i] = index[i+1];
 24 index[i+1] = t;
 25 }}
 26
 27 //print out sorted list using index
 28 cout<<\"\  print sorted list using index. \";
 29 for (int i=0; i< 5; i++)
 30 cout<<\"\  \"<< names[index[i]];
 31 //sort
 32 for (int k=0; k< 5-1; k++)
 33 for (int i=0; i< 5-1; i++)
 34 {if (strcmp(names[i], names[i+1]) >0)
 35 {//cout<<\"\  need to swap. \"<< names[i] <<\" and \" <<names[i+1];
 36 strcpy(temp, names[i]);
 37 //cout<<\"\  name in temp \"<<temp;
 38 strcpy(names[i],names[i+1]);
 39 //cout<<\"\  name in subsript \"<<i<< \" is \" <<names[i];
 40 strcpy(names[i+1], temp);
 41 //cout<<\"\  the name in names \"<<i<<\" is \"<<names[i];
 42 }}
 43 cout<<\"\ \  print out sorted list after sorting.\";
 44 for (int i=0; i< 5; i++)
 45 cout<<\"\  \"<< names[i];
 46 cout<<endl;
 47 char c;
 48 cin>> c;
 49 return 0;
 50 }
 51
 52 /*
 53 enter name 1: wilson
 54 enter name 2: jones
 55 enter name 3: galit
 56 enter name 4: an
 57 enter name 5: williams
 58
 59 print sorted list using index.
 60 an
 61 galit
 62 jones
 63 williams
 64 wilson
 65
 66 print out sorted list after sorting.
67 an
 68 galit
 69 jones
 70 williams
 71 wilson
 72
 73
 74 */
Solution
This program takes set of names as string input from the user ,sorts them in alphabetical order and print the names again in sorted order.
we include string.h and stdlib.h to use predefined String functions
Use the for loop to take multiple name input.
Using a double for loop compare each string with every other string using strcmp method and swap strings indexes in index array using strcpy method
This process is applied until all the strings indexes arr sorted alphabetically
Then the strings are arranged in the names array using another double for loop and index array values
Finally using another double for loop we print the names in sorted order in the names array
Declare one 2D array to store multiple names,declare one 1D char array to store temporary names.


