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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
// 1. The management wants the youngest person to occupy the first position
// in the array.
// Give yourself a function, just like the previous one, that finds
// the youngest person in the array.
// Once you know where the youngest person is, you can swap his or her data
// with the data already occupying the first position in the array.
// That way nothing is lost.
// Remember that the first position in an array is numbered zero, not one.
// 2. Now Promote the Second Youngest.
// The management has now decided not only that the youngest person must occupy
// the first position in the array, but also that the second-youngest person
// must occupy the second position in the array.
// So, after searching for the youngest and moving their data to the front
// of the array, now search the remainder of the array
// (all except the first element), and move the youngest person you find
// (which must be the second youngest of all) into the second position
// of the array.
// Make sure you swap data, so that whoever was originally
// in the second position is not lost.
// 3. More of the Same.
// The management are going to keep on adding requirements like this,
// next putting the third youngest in the third position, then the fourth,
// then the fifth.
// There is no knowing when they will grow out of this petty obsession,
// so make things easier for yourself.
// Modify your search function so that it can be told how much of the array
// to search.
// That is, give it two int parameters (let’s call them a and b);
// its job is now to search only the portion of the array between position a
// and position b, to find the youngest person therein.
// This makes it very easy to search the remainder of the array to find
// the second and third youngest.
#include <iostream>
#include <fstream>
using namespace std;
constexpr int MAXPERSONS = 1000;
struct Person
{
int ssn;
int dob;
string fname;
string lname;
int zip;
};
// Returns the index of the found person into the array 'persons'
// or -1 if the requested name can't be found.
int findPerson(const Person persons[], int upperbound);
// "...Give yourself a function [...] that finds the youngest person
// in the array..."
// Returns the index of the youngest person into the array 'persons'
// in the range lowerbound-upperbound.
// It could returns -1 if data into 'persons' are inconsistent.
int findYoungest(Person const persons[], int lowerbound, int upperbound);
void swapPersons(Person persons[], int one, int two);
void printPerson(Person persons[], int index);
int main()
{
Person persons [MAXPERSONS];
// I had to change the orginal string to test the program on my system.
// Original string was: "/home/118/people1.txt".
ifstream fin("people1.txt");
if(fin.fail())
{
cout<<"ERROR";
exit(1);
}
// Store the entire "people1.txt" file into the array.
// Count how many records are there.
int count_persons = 0;
for(int i = 0; fin; i++)
{
fin >> persons[i].ssn >> persons[i].dob >> persons[i].fname
>> persons[i].lname >> persons[i].zip;
count_persons++;
}
fin.close();
cout << "\nLet's try to find a person.\n";
int index = findPerson(persons, count_persons);
if(index > -1)
{
cout << "Found record at position " << index+1 << ": ";
printPerson(persons, index);
cout << "\n\n";
}
else
{
std::cout << "Sorry, the required name is not inside the database.\n\n";
}
// Sort persons by year of date
// Let's keep in mind:
// "i" will start at 0... that means that we will begin by searching
// from position 0
// ...but later it will grow to 2, 3, 4... it means that our lower bound
// will automatically increases
// i.e. we don't need to care about
// it.
for(int i=0; i<count_persons; i++)
{
index = findYoungest(persons, i, count_persons);
// if no errors and this is not already the youngest:
if(index>-1 && index!=i) {
swapPersons(persons, index, i);
cout << "--> Person ";
printPerson(persons, index);
cout << "\n--> swapped with person ";
printPerson(persons, i);
cout << endl;
}
}
std::cout << "\nNew data are:\n";
for(int i=0; i<count_persons; i++)
{
std::cout << '\n';
printPerson(persons, i);
std::cout << '\n';
}
return 0;
}
int findPerson(const Person persons[], int upperbound)
{
cout << "Enter a name *case sensitive*: ";
string name;
cin >> name;
int i = 0;
while(i<upperbound)
{
if(name==persons[i].fname || name==persons[i].lname)
{
return i;
}
i++;
}
/* ...or
for(int 1; i<upperbound; i++)
if(name==array[i].fname || name==array[i].lname)
return i; */
return -1;
}
void printPerson(Person persons[], int index)
{
cout << "name: " << persons[index].fname << "; surname: "
<< persons[index].lname << "; ssn: " << persons[index].ssn
<< "; birth date: " << persons[index].dob << "; zip: "
<< persons[index].zip;
}
int findYoungest(Person const persons[], int lowerbound, int upperbound)
{
int youngest = 0; // born in year 0 - two thousands years old
int ret_value = -1; // acceptable error message, if data were inconsistent
for(int i=lowerbound; i<upperbound; i++)
{
if(persons[i].dob > youngest) // found a younger person
{
youngest = persons[i].dob;
ret_value = i;
}
}
return ret_value; // if errors, it could returns -1
}
void swapPersons(Person persons[], int one, int two)
{
// Save one of the two person's data
Person tmp;
tmp.ssn = persons[one].ssn;
tmp.dob = persons[one].dob;
tmp.fname = persons[one].fname;
tmp.lname = persons[one].lname;
tmp.zip = persons[one].zip;
// Overwrite saved person's data
persons[one].ssn = persons[two].ssn;
persons[one].dob = persons[two].dob;
persons[one].fname = persons[two].fname;
persons[one].lname = persons[two].lname;
persons[one].zip = persons[two].zip;
// Overwrite other person with saved data
persons[two].ssn = tmp.ssn;
persons[two].dob = tmp.dob;
persons[two].fname = tmp.fname;
persons[two].lname = tmp.lname;
persons[two].zip = tmp.zip;
}
|