Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question to compare strings ---------------------------- Program 1: #include

ID: 3682180 • Letter: C

Question

C++ Question to compare strings

----------------------------

Program 1:

#include <iostream>
#include <string>

using namespace std;

bool checkUnique(string);

int main() {
   string input;
   bool unique;

   cout << "Please enter a string: ";
   cin >> input;

   unique = checkUnique(input);

   if (unique) {
       cout << "Unique string!" << endl;
   }
   else cout << "Not unique!" << endl;

   getchar();
   getchar();
   return 0;
}

bool checkUnique(string input) {
   int unique = 0;
   int i = 0;

   while (input[i] != '') {
       int position = (int)input[i] - 'a';

       if ((unique & (1 << position)) >0)
           return false;

       unique |= (1 << position);

       i++;
   }
  
   return true;
}

--------------------------------

Program 2:

#include <iostream>
#include <string>

using namespace std;

bool checkUnique(string);

int main() {
   string input;
   bool unique;

   cout << "Please enter a string: ";
   cin >> input;

   unique = checkUnique(input);

   if (unique) {
       cout << "Unique string!" << endl;
   }
   else cout << "Not unique!" << endl;

   getchar();
   getchar();
   return 0;
}

bool checkUnique(string input) {
   bool unique[256] = { false };
   int i = 0;

   while (unique[i] != '') {
       if (unique[(int)input[i]])
           return false;
       else {
           unique[(int)input[i]] = true;
           i++;
       }
   }
}

Compare program 1 and program 2 using preset strings with unique characters (Do not ask the strings to the users). Use strings of size 5, 10, 20, 50, 80, 100, 150, 200, and 256. Compute the time that program 1 and program 2 take to process each one of the strings. Show for console the time that each program takes in a table like this: n ogram 1 program 2 time time time 10 time time time 50 time time 800 time time 100 time time 150 time time time 200 time time 256 time To compute running times, retrieve system time before and after execution and compute the difference. Do do not use boost or any other non standard libraries. You can use the function clock as shown in this example: 1 include K time.h> 3 int main clock-t start clock //your code here clock-t end clock printf Elapsed %f seconds n", (double) (start end) CLOCKS-PER-SEC); return 0; 10 This example and a complete reference the function time() can be found at http:// cppreference. com/w/cpp/chrono/C/Clock:

Explanation / Answer

n Program1 Program2 5 4.6e-05 4.3e-05 10 4.1e-05 4.4e-05 20 4.7e-05 4.1e-05 50 4.7e-05 4.3e-05 80 4.3e-05 4e-05 100 4.6e-05 4.1e-05 150 5.1e-05 4.4e-05 200 5e-05 4.4e-05 256 5.1e-05 4.6e-05

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote