Advertisement
Guest User

lab08a.cpp

a guest
Mar 31st, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. struct Person
  8. {
  9.     string name;
  10.     int age;
  11. };
  12.    
  13.  
  14. int main()
  15. {    
  16.    
  17.     Person people[20];
  18.     int age,
  19.         arrayCounter = 0,
  20.         minIndex;
  21.    
  22.     string name;
  23.     fstream inputStream;
  24.    
  25.     inputStream.open ("person-data.txt");
  26.     while (inputStream >> name >> age)
  27.     {
  28.         people[arrayCounter].name = name;
  29.         people[arrayCounter].age = age;
  30.         arrayCounter = arrayCounter + 1;
  31.     }
  32.    
  33.    
  34.    
  35.     for(int i = 0; i < arrayCounter; i++)
  36.     {
  37.         int minAge = people[i].age;  
  38.         string minName = people[i].name;
  39.         minIndex = i;
  40.         for(int j = i+1; j< arrayCounter; j++)
  41.         {
  42.             if(people[j].age < minAge)
  43.             {
  44.                 minAge = people[j].age;
  45.                 minName = people[j].name;
  46.                 minIndex = j;
  47.             }
  48.         }
  49.         people[minIndex].age = people[i].age;
  50.         people[minIndex].name = people[i].name;
  51.         people[i].age = minAge;
  52.         people[i].name = minName;
  53.     }
  54.    
  55.     for(int i = 0;i < arrayCounter; i++)
  56.     {
  57.         cout << people[i].name << " : "<< people[i].age << endl;
  58.     }
  59.    
  60.    
  61.     inputStream.close();
  62.     return 0;
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement