Advertisement
enkacang

Array Example

May 18th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void exampleOne();
  6. void exampleTwo();
  7. void exampleThree();
  8.  
  9. int main()
  10. {
  11.     //exampleOne(); // One-D Array
  12.     //exampleTwo(); // Two-D Array
  13.     exampleThree(); // Example-Usage
  14.  
  15.     return 0;
  16. }
  17.  
  18. void exampleOne()
  19. {
  20.            //index -->       0                 1                2
  21.     string user_name[]{"@hmad Mispun", "Zakiman Maznun", "Luqman Hakim"};
  22.  
  23.     //1 - Example printing array
  24.     cout << "array at index 0 - " << user_name[0] << endl;
  25.  
  26.     cout << "array at index 1 - " << user_name[1] << endl;
  27.  
  28.     cout << "array at index 2 - " << user_name[2] << endl << endl;
  29.  
  30.     //2 - Example Access and change value inside an array
  31.     user_name[0] = "Ahmad Mispun";
  32.  
  33.     //3 - Output after change
  34.     cout << "array at index 0 - " << user_name[0] << endl;
  35.  
  36.     cout << "array at index 1 - " << user_name[1] << endl;
  37.  
  38.     cout << "array at index 2 - " << user_name[2] << endl << endl;
  39. }
  40.  
  41. void exampleTwo()
  42. {
  43.     int user_rating[3][4]
  44.     {
  45.         0 , 4 , 2 , 3 ,
  46.         3 , 1 , 2 , 3 ,
  47.         0 , 1 , 5 , 3
  48.     };
  49.  
  50.     //1 - Example printing array
  51.     cout << "array at index [0][0] - " << user_rating[0][0] << endl;
  52.  
  53.     cout << "array at index [1][0] - " << user_rating[1][0] << endl;
  54.  
  55.     cout << "array at index [2][0] - " << user_rating[2][0] << endl << endl;
  56.  
  57.     //2 - Example Access and change value inside an array
  58.     user_rating[0][0] = 1;
  59.  
  60.     //3 - Output after change
  61.     cout << "array at index [0][0] - " << user_rating[0][0] << endl;
  62.  
  63.     cout << "array at index [1][0] - " << user_rating[1][0] << endl;
  64.  
  65.     cout << "array at index [2][0] - " << user_rating[2][0] << endl << endl;
  66. }
  67.  
  68. void exampleThree()
  69. {
  70.     string user_name[]{ "Ahmad Mispun", "Zakiman Maznun", "Luqman Hakim" };
  71.     string movie_name[]{ "Big Daddy", "Green Mile", "Lake Placid", "Blue Streak" };
  72.  
  73.     int user_rating[3][4]
  74.     {
  75.         0 , 4 , 3 , 5 ,
  76.         2 , 3 , 3 , 3 ,
  77.         1 , 4 , 5 , 3
  78.     };
  79.  
  80.     while (true)
  81.     {
  82.  
  83.         for (size_t i = 0; i < 3; i++)
  84.         {
  85.             cout << "|-" << i << "-|" << user_name[i];
  86.         }
  87.         cout << endl;
  88.  
  89.         for (size_t i = 0; i < 4; i++)
  90.         {
  91.             cout << "|-" << i << "-|" << movie_name[i];
  92.         }
  93.         cout << endl << endl;
  94.  
  95.         int user_input{ 0 };
  96.         cout << "Enter user name :";
  97.         cin >> user_input;
  98.  
  99.         int movie_input{ 0 };
  100.         cout << "Enter movie name :";
  101.         cin >> movie_input;
  102.  
  103.         cout << "User -> " << user_name[user_input];
  104.         cout << "| Rate Movie --> " << movie_name[movie_input];
  105.         cout << "| " << user_rating[user_input][movie_input] << "-STAR"<< endl << endl;
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement