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 mobs[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 mob\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 mobs, so element swap in BOTH array when mobs[tail]<mobs[head]
- if ((menu == 'b' ? tels[tail] < tels[head] : mobs[tail] < mobs[head]))
- {
- tmp = tels[head];
- tels[head] = tels[tail];
- tels[tail] = tmp;
- tmp = mobs[head];
- mobs[head] = mobs[tail];
- mobs[tail] = tmp;
- }
- }
- }
- }
- // display users after sorting or by 'a' menu request, if menu == 'x' do nothing
- if (menu != 'x')
- {
- std::cout << "Tel#\tMob#\n";
- for (int i{ 0 }; i < arrSize; ++i)
- {
- std::cout << tels[i] << '\t' << mobs[i] << '\n';
- }
- }
- } while (menu != 'x');
- return 0;
- }
Add Comment
Please, Sign In to add comment