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

Looking for answer in C++ ---------------------------- Program 1: #include <iost

ID: 3682315 • Letter: L

Question

Looking for answer in C++

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

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

Hi I have done coding. Please test it.

#include <iostream>
#include <string>
#include <time.h>

using namespace std;

bool checkUniqueMethod1(string);
bool checkUniqueMethod2(string);

int main() {
string input;
bool unique;
clock_t start;
clock_t end;
double firstMethodTime[9]= {0};
double seconMethodTime[9] = {0};

int i=0;
  
// for string of size 5
input = "abcde";

start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of length 10;
   string input1 = "abcdefghij";
   start = clock();
unique = checkUniqueMethod1(input1);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input1);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of length 20
   input="abcdefghijklmnopqrst";
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of lenght 50
   unique = "";
   char c = '1';
   for( int i=1; i<=50; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of lenght 80
   unique = "";
   c = '1';
   for( int i=1; i<=80; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of lenght 100
   unique = "";
   c = '1';
   for( int i=1; i<=100; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of lenght 150
   unique = "";
   c = '1';
   for( int i=1; i<=150; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;

   // for input of lenght 200
   unique = "";
   c = '1';
   for( int i=1; i<=200; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // for input of lenght 256
   unique = "";
   c = '1';
   for( int i=1; i<=256; i++){
       input = input + c;
       c++;
   }
   start = clock();
unique = checkUniqueMethod1(input);
end = clock();
firstMethodTime[i] = (double)(end-start);

start = clock();
unique = checkUniqueMethod2(input);
end = clock();
seconMethodTime[i] = (double)(end-start);
   i++;
  
   // printing time taken by both methods
   i=0;
   cout<<"n       program1       profram2"<<endl;
   cout<<"5       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"10       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"20       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"80       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"100       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"150       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"200       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl; i++;
   cout<<"256       "<<firstMethodTime[i]<<"       "<<seconMethodTime[i]<<endl;
  
return 0;
}

bool checkUniqueMethod1(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;
}

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

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote