Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct Person
- {
- string name;
- int age;
- };
- int main()
- {
- Person people[20];
- int age,
- arrayCounter = 0,
- minIndex;
- string name;
- fstream inputStream;
- inputStream.open ("person-data.txt");
- while (inputStream >> name >> age)
- {
- people[arrayCounter].name = name;
- people[arrayCounter].age = age;
- arrayCounter = arrayCounter + 1;
- }
- for(int i = 0; i < arrayCounter; i++)
- {
- int minAge = people[i].age;
- string minName = people[i].name;
- minIndex = i;
- for(int j = i+1; j< arrayCounter; j++)
- {
- if(people[j].age < minAge)
- {
- minAge = people[j].age;
- minName = people[j].name;
- minIndex = j;
- }
- }
- people[minIndex].age = people[i].age;
- people[minIndex].name = people[i].name;
- people[i].age = minAge;
- people[i].name = minName;
- }
- for(int i = 0;i < arrayCounter; i++)
- {
- cout << people[i].name << " : "<< people[i].age << endl;
- }
- inputStream.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement