Computer >> 컴퓨터 >  >> 프로그램 작성 >> C++

STL에서 배열을 구현하는 C++ 프로그램

<시간/>

배열 및 의사 코드에 대한 다른 작업:

Begin
In main(),
   While TRUE do
      Prints some choices.
      Take input of choice.
      Start the switch case
         When case is 1
            Print the size of the array.
            Break
         When case is 2
            Insert the values in array.
            Break
         When case is 3
            Print the front element array.
            Break
         When case is 4
            Print the back element of array.
            Break
         When case is 5
            Print all the elements of the array.
            Break
         When case is 6
            Exit.
         When case is default
            Print wrong input.
End.

예시

#include <iostream>
#include <array>
#include <cstdlib>
using namespace std;
int main() {
   array<int, 7> a; //declaring array
   array<int, 7>::iterator it; //declaring an array as iterator
   int c, i; //declaring integer variables
   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: "; // print size of the array.
            cout<<a.size()<<endl;
            break;
         case 2:
            cout<<"Enter value to be inserted: "; // enter values in the array.
            cin>>i;
            a.at(cnt) = i;
            cnt++;
            break;
         case 3:
            cout<<"Front Element of the Array: "; //print front element of the array.
            cout<<a.front()<<endl;
            break;
         case 4:
            cout<<"Back Element of the Arrary: "; //print back element of the array.
            cout<<a.back()<<endl;
            break;
         case 5:
            for (it = a.begin(); it != a.end(); ++it ) //print all elements of the array.
            cout <<" "<< *it;
            cout<<endl;
            break;
         case 6:
            exit(1); //exit
            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
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: 2
Enter value to be inserted: 7
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: 2
Enter value to be inserted: 6
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: 2
Enter value to be inserted: 5
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: 2
Enter value to be inserted: 4
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: 2
Enter value to be inserted: 3
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: 2
Enter value to be inserted: 2
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: 2
Enter value to be inserted: 1
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: 3
Front Element of the Array: 7
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: 4
Back Element of the Stack: 1
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: 5
7 6 5 4 3 2 1
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: 6
exit status 1