การดำเนินการต่างๆ บน Array และ Pseudocode:
Begin In main()
While TRUE
Print choices
Take input choice
Switch case:
Case 1: Print array size; Break
Case 2: Insert values; Break
Case 3: Print front element; Break
Case 4: Print back element; Break
Case 5: Print all elements; Break
Case 6: Exit
Default: Print wrong input
End.ตัวอย่าง
#include <iostream>
#include <array>
#include <cstdlib>
using namespace std;
int main() {
array<int, 7> a;
array<int, 7>::iterator it;
int c, i;
a.fill(0);
int cnt = 0;
while (1) {
cout << "1.Size of the array" << endl;
cout << "2.Insert Element into the Array" << endl;
cout << "3.Front Element of the Array" << endl;
cout << "4.Back Element of the Array" << endl;
cout << "5.Display elements of the Array" << endl;
cout << "6.Exit" << endl;
cout << "Enter your Choice: "; cin >> c;
switch (c) {
case 1: cout << "Size of the Array: " << a.size() << endl; break;
case 2: cout << "Enter value to be inserted: "; cin >> i; a.at(cnt) = i; cnt++; break;
case 3: cout << "Front Element of the Array: " << a.front() << endl; break;
case 4: cout << "Back Element of the Array: " << a.back() << endl; break;
case 5: for (it = a.begin(); it != a.end(); ++it) cout << " " << *it; cout << endl; break;
case 6: exit(1); break;
default: cout << "Wrong Choice" << endl;
}
}
return 0;
}ผลลัพธ์
1.Size of the array 2.Insert Element into the Array 3.Front Element of the Array 4.Back Element of the Array 5.Display elements of the Array 6.Exit Enter your Choice: 1 Size of the Array: 7 ... Enter your Choice: 5 7 6 5 4 3 2 1 Enter your Choice: 6 exit status 1