QUESTION Please i need help in documenting each line of this
QUESTION:
Please i need help in documenting each line of this program...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
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
int main()
{
char names[5][20], temp[20];
int t; //Declaring variable t
int index[5]; //Declaring an array with index 5
for (int i=0;i<5;i++)
index[i]=i;
for (int i= 0; i< 5; i++) // run the loop to store values
{
cout<<\"\ enter name \"<< i+1<<\": \";
cin>>names[i];
}; // stroing the values in to the array using the above for loop.
for (int k=0; k< 5-1; k++)
for (int i=0; i< 5-1; i++)
{
if (strcmp(names[index[i]], names[index[i+1]]) >0) // checking the condition.
{
t = index[i];
index[i] = index[i+1];
index[i+1] = t; // Swapping the values.
}
}
//print out sorted list using index
cout<<\"\ print sorted list using index. \";
for (int i=0; i< 5; i++)
cout<<\"\ \"<< names[index[i]];
//sort
for (int k=0; k< 5-1; k++)
for (int i=0; i< 5-1; i++)
{
if (strcmp(names[i], names[i+1]) >0)
{
//cout<<\"\ need to swap. \"<< names[i] <<\" and \" <<names[i+1];
strcpy(temp, names[i]);
//cout<<\"\ name in temp \"<<temp;
strcpy(names[i],names[i+1]);
//cout<<\"\ name in subsript \"<<i<< \" is \" <<names[i];
strcpy(names[i+1], temp);
//cout<<\"\ the name in names \"<<i<<\" is \"<<names[i];
}
}
cout<<\"\ \ print out sorted list after sorting.\";
for (int i=0; i< 5; i++)
cout<<\"\ \"<< names[i];
cout<<endl;
char c;
cin>> c;
return 0;
}


