Advertisement
TheLegend12

Untitled

Oct 23rd, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // Task 1: Populate board using user input
  8. void populateBoard(char board[5][5]) {
  9.     string userWord;
  10.  
  11.     for (int i = 0; i < 5; ++i) {
  12.         cout << "Enter the input of 5 length of characters: " << endl;
  13.         cin >> userWord;
  14.         for (int k = 0; k < 5; ++k) {
  15.             char letter = userWord.at(k);
  16.             board[i][k] = letter;
  17.         }
  18.     }
  19. }
  20.  
  21. // Task 2: Check if a word is in the dictionary using binary search
  22. int binarySearchString(string searchWord, vector<string> dictionary) {
  23.     int low = 0;
  24.     int high = dictionary.size() - 1;
  25.  
  26.     while (low <= high) {
  27.         int mid = (low + high) / 2;
  28.         int searchResult = searchWord.compare(dictionary[mid]);
  29.  
  30.         if (searchResult == 0) {
  31.             return mid; // Word found in the dictionary
  32.         } else if (searchResult < 0) {
  33.             high = mid - 1;
  34.         } else {
  35.             low = mid + 1;
  36.         }
  37.     }
  38.  
  39.     return -1; // Word not found in the dictionary
  40. }
  41.  
  42. void checkExists(char board[5][5], vector<string> dictionary) {
  43.     for (int i = 0; i < 5; ++i) {
  44.         string word = "";
  45.         for (int j = 0; j < 5; ++j) {
  46.             word += board[i][j];
  47.         }
  48.         int isWord = binarySearchString(word, dictionary);
  49.         if (isWord == -1) {
  50.             cout << "Row " << i << " word does not exist in dictionary" << endl;
  51.         } else {
  52.             cout << "Row " << i << " word does exist in dictionary" << endl;
  53.         }
  54.     }
  55. }
  56.  
  57. // This function displays a board that is given through parameters
  58. void displayBoard(char board[5][5]) {
  59.     for (int row = 0; row < 5; row++) {
  60.         cout << "-----------" << endl;
  61.         cout << "|";
  62.         for (int col = 0; col < 5; col++) {
  63.             cout << board[row][col] << "|";
  64.         }
  65.         cout << endl;
  66.     }
  67.     cout << "-----------" << endl;
  68. }
  69.  
  70. int main() {
  71.     char board[5][5] = {
  72.         {' ', ' ', ' ', ' ', ' '},
  73.         {' ', ' ', ' ', ' ', ' '},
  74.         {' ', ' ', ' ', ' ', ' '},
  75.         {' ', ' ', ' ', ' ', ' '},
  76.         {' ', ' ', ' ', ' ', ' '}
  77.     };
  78.  
  79.     vector<string> dictionary = {"enter", "hello", "mixed", "shift", "world"};
  80.  
  81.     int task;
  82.  
  83.     cout << "Enter the task you want to execute:" << endl
  84.          << "1 for populate board " << endl
  85.          << "2 for check word is existed in the dictionary " << endl
  86.          << "3 for counting how many operations are needed for binary search " << endl
  87.          << endl;
  88.     cin >> task;
  89.     if (task == 1) {
  90.         populateBoard(board);
  91.         displayBoard(board);
  92.     } else if (task == 2) {
  93.         populateBoard(board);
  94.         displayBoard(board);
  95.         checkExists(board, dictionary);
  96.     } else if (task == 3) {
  97.         // Uncomment below code block to test Extra Credit Task
  98.         // string candidateWord;
  99.         // cout << "Enter the word :";
  100.         // cin >> candidateWord;
  101.         // countingNumOfComparison(candidateWord, dictionary);
  102.     }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement