CPP 7
Loop Interview Guest on 19th January 2024 05:04:52 AM
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. // recursively display and remove the first integers of a vector
  7. void recursive(vector<int> &arr) {
  8.         //note: the pointer *arr.begin() == arr[0]
  9.         //base case with a vector of 1 int
  10.         if (arr.size() == 1) {
  11.                 cout << *arr.begin() << endl;
  12.                 //vector of 2 or more int
  13.         } else {
  14.                 cout << *arr.begin() << endl;
  15.                 //remove first element and recurse
  16.                 arr.erase(arr.begin());
  17.                 recursive(arr);
  18.         }
  19. }
  20.  
  21. int main() {
  22.         //While guessing game
  23.         srand(time(nullptr));
  24.         const int randomNumber = rand() % 10 + 1;
  25.         int guess = 0;
  26.         int guesses = 0;
  27.         cout << "Guess a number between 1 and 10: " << endl;
  28.         while (guess != randomNumber && guesses < 4) {
  29.                 cout << "You have " << 4 - guesses << " guesses left!" << endl;
  30.                 cin >> guess;
  31.                 guesses++;
  32.                 if (guess < randomNumber) {
  33.                         cout << "Too low!" << endl;
  34.                 } else if (guess > randomNumber) {
  35.                         cout << "Too high!" << endl;
  36.                 } else {
  37.                         cout << "Congratulations! You guessed the number." << endl;
  38.                 }
  39.         }
  40.  
  41.         //wait for input
  42.         system("pause");
  43.  
  44.         //Do-While-Free-Trial
  45.         char card = '-';
  46.         do {
  47.                 if (card == '-') {
  48.                         cout << "Thank you for activating your free Netflix trial." << endl;
  49.                 }
  50.                 cout << "One month of Netflix service used." << endl;
  51.                 cout << "Want to pay for another month? y or n" << endl;
  52.                 cin >> card;
  53.         } while (card == 'y');
  54.         cout << "No more Netflix for you!" << endl << endl;
  55.  
  56.         //windows-specific pause
  57.         system("pause");
  58.         cout << "Here's a demonstration of the same counting loop using 7 different loop styles:" << endl;
  59.         system("pause");
  60.  
  61.         cout << "While loop with manual iteration" << endl;
  62.         //WHILE loop
  63.         int i = 0;
  64.         while (i < 5) {
  65.                 cout << i << endl;
  66.                 i++;
  67.         }
  68.  
  69.         system("pause");
  70.  
  71.         //DO-WHILE loop
  72.         cout << endl << "Using Do-While Loop and manual iteration" << endl;
  73.         int j = 0;
  74.         do {
  75.                 cout << j << endl;
  76.                 j++;
  77.         } while (j < 5);
  78.  
  79.         system("pause");
  80.  
  81.         //FOR loop
  82.         cout << endl << "Using For Loop" << endl;
  83.         for (int k = 0; k < 5; k++) {
  84.                 cout << k << endl;
  85.         }
  86.  
  87.         //for use in recursion and for-each loop
  88.         vector<int> numbers = {0, 1, 2, 3, 4};
  89.  
  90.         system("pause");
  91.  
  92.         //FOREACH loop
  93.         cout << endl << "Using For-Each Loop and a vector" << endl;
  94.         //int numbers[] = {0, 1, 2, 3, 4};
  95.         for (const int number: numbers) {
  96.                 cout << number << endl;
  97.         }
  98.  
  99.         system("pause");
  100.  
  101.         //RECURSIVE function call
  102.         cout << endl << "Using Recursion through a vector" << endl;
  103.         recursive(numbers);
  104.  
  105.         system("pause");
  106.  
  107.         //GOTO loop in Do-While style
  108.         cout << endl << "Using post-test Goto Loop with manual iteration" << endl;
  109.         int l = 0;
  110. doloop:
  111.         cout << l << endl;
  112.         l++;
  113.         if (l < 5) {
  114.                 goto doloop;
  115.         }
  116.  
  117.         system("pause");
  118.  
  119.         //GOTO loop in While style
  120.         cout << endl << "Using post-test Goto Loop with manual iteration" << endl;
  121.         int m = 0;
  122. whileloop:
  123.         if (m < 5) {
  124.                 cout << m << endl;
  125.                 m++;
  126.                 goto whileloop;
  127.         }
  128.  
  129.         system("pause");
  130.  
  131. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.