Write a program that prints a sorted phone list from a database of names and pho
ID: 3634197 • Letter: W
Question
Write a program that prints a sorted phone list from a database of names and phonenumbers. The data is contained in two files named "phoneNames.txt" and
"phoneNums.txt". The files may contain up to 2000 names and phone numbers. The
files have one name or one phone number per line. To save paper, only print the first 50
lines of the output. Note: The first phone number in the phone number file corresponds to
the first name in the name file. The second phone number in the phone number file
corresponds to the second name in the name file. etc.
Explanation / Answer
#include<iostream>
02 using namespace std;
03
04 void getData(char names[][30], int& sizeOfArray);
05 void BubbleSort (char names[][30], int size);
06 void showArray(char names[][30], int size);
07 void strcpy(char x[], char y[]);
08
09 int main()
10 {
11 const char TOTALNAMES = 20;
12 char names[TOTALNAMES];
13 int numberOfNames = 0;
14
15 getData(names, numberOfNames);
16
17 // Display the values
18 cout << "The unsorted values are: ";
19 showArray(names, 20);
20
21 // Sort the values
22 BubbleSort(names, 20);
23
24 // Display them again
25 cout << "The sorted values are: ";
26 showArray(names, 20);
27
28 return 0;
29 }
30
31 void getData (char names[][30], int& sizeOfArray)
32 {
33 int pos = 0;
34 char name[30];
35 int value = 0;
36
37 cout << "Please enter a last name" << endl;
38 cin >> name;
39
40 while (pos < 20)
41 {
42 names[pos] = value;
43 pos++;
44
45 cout << "Please enter another last name: " << endl;
46 cin >> name;
47 }
48 sizeOfArray = pos;
49 }
50
51 void BubbleSort (char names[][30], int size)
52 { int k = 0;
53 bool done = false;
54 int limit = 0;
55 char name[30];
56 while (!done)
57 { done = true;
58 for (int n=0; n<size-1-limit; n++)
59 if (strcmp(names[k], names[k+1]) > 0)
60 { Swap (name, n, n+1);
61 done = false;
62 }
63 limit++;
64 }
65 }
66
67 void Swap (char names[][30], int n, int k)
68 { char temp[30];
69 strcpy(temp,names[n]);
70 strcpy(names[n], names[k]);
71 strcpy(names[k], temp);
72 }
73
74 void strcpy(char x[], char y[])
75 {
76 int n=0;
77 while(y[n] != '')
78 {
79 x[n] = y[n];
80 n++;
81 }
82 x[n] = '';
83 }
84
85 void showArray (char names[][30], int size)
86 {
87 int count = 0;
88
89 for (int count=0; count < size; count++)
90 cout << names[] << " " << endl;
91 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.