Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- const int arrSize{5};
- int tels[arrSize]{1234,9753,2468,5678,3232};
- int icqs[arrSize]{2222,1111,4444,6666,5555};
- bool correct{false};
- char menu{'a'};
- int tmp;
- do
- {
- do
- {
- std::cout << "\na - List all users\nb - Sort by tel\nc - Sort by icq\nx - Exit\n";
- std::cin >> menu;
- correct = menu == 'a' or menu == 'b' or menu == 'c' or menu == 'x';
- if (!correct)
- {
- std::cout << "\nInvalid menu entry! Try again!\n";
- }
- }while(!correct);
- // do we need to sort something??
- if (menu == 'b' or menu == 'c')
- {
- for(int head{0}; head< arrSize; ++head)
- {
- for(int tail{arrSize-1}; tail>head; --tail)
- {
- // if we get there so either 'b' or 'c' menu was chosen
- // so we choice sorting criteria
- // 'b' - sort by tel, so element swap in BOTH array when tels[tail]<tels[head]
- // not 'b' in this case - just 'c' - sort by icq, so element swap in BOTH array when icqs[tail]<icqs[head]
- if((menu == 'b' ? tels[tail]<tels[head] : icqs[tail]<icqs[head]))
- {
- tmp = tels[head];
- tels[head] = tels[tail];
- tels[tail] = tmp;
- tmp = icqs[head];
- icqs[head] = icqs[tail];
- icqs[tail] = tmp;
- }
- }
- }
- }
- // display users after sorting or by 'a' menu request, if menu == 'x' do nothing
- if (menu != 'x')
- {
- std::cout << "Tel#\tICQ#\n";
- for(int i{0}; i < arrSize; ++i)
- {
- std::cout << tels[i] << '\t' << icqs[i] << '\n';
- }
- }
- }while (menu != 'x');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement