Question

III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if...

III. ASSIGNMENT 2.1

As discussed in class, the example program enumerates all possible strings (or if we interpret as numbers, numbers) of base-b and a given length, say l. The number of strings enumerated is b l . Now if we interpret the outputs as strings, or lists, rather than base-b numbers and decide that we only want to enumerate those strings that have unique members, the number of possible strings reduces from b l to b!. Furthermore, consider a case where b = 2. If we were to enumerate all possible strings where l = 2, we would have the following collection: {00, 01, 10, 11}, that is a collection of size 2 2 or 4. Now, if we were to consider the case where we restrict to what is termed in this problem unique members we have only {01, 10} as in both cases {00, 11} we have either 0 or 1 appear more than once in the string. Lets look at a case where b = 3 and l = 3. The enumeration of all possible strings is listed below.

{000, 001, 002, 010, 011, 012, 020, 021, 022,

100, 101, 102, 110, 111, 112, 120, 121, 122,

200, 201, 202, 210, 211, 212, 220, 221, 222}

This is 3 ^3 or 27 possible strings.

Now lets look at those strings with unique members (per position).

{012, 021, 102, 120, 201, 210}

This is 3! or 6 possible strings.

Programming assignment 2.1 is to produce a list of all possible strings with only unique members or digits given a base b such that 2 ≤ b ≤ 10 and length l where b = l.

IV. ASSIGNMENT 2.2

Programming assignment 2.2 is to produce a list of all possible strings of unique members or digits given a base b such that 2 ≤ b ≤ 10 and length l where b > l.

V. SAMPLE OUTPUT

A sample output for assignments 2.1 and 2.2 is listed below. The solutions should adhere to the below listed output format.

01

10

b: 2 l: 2 runtime: 0 ms.

First line of output is the first string in ascending order of magnitude when evaluated as a base b number. Each consecutive line lists the next string in ascending order of magnitude when evaluated as a base b number. Then two line breaks and a descriptive output of run time execution. The runtime execution output should be exactly in the format listed above.

If possible please use sample code:

#include<iostream>
#include<cmath>
#include <chrono>
using namespace std;
using namespace std::chrono;

int generator(int base, int length){

   /** Algorithm only supports base 2 (binary) to base 9 **/
   if(base < 2 || base > 10){
       cout << "Err: Invalid Base Provided";
       return 1;
   }
   /** Algorithm only supports base 2 (binary) to base 9 **/

   /** Compute number of all possibile strings, this is a string that does not have any uniqueness constraint **/
   int possible = pow(base, length);
   /** Compute number of all possibile strings, this is a string that does not have any uniqueness constraint **/

   /** Instantiate an array that supports all possible strings for given length and base **/
   int generated[possible][length];
   /** Instantiate an array that supports all possible strings for given length and base **/
  
   /** Initialize each element in the array for all possible strings to 0 **/
   for(int i = 0; i < possible; i++){
for(int j = 0; j < length; j++){
generated[i][j] = 0;
}
   }
   /** Initialize each element in the array for all possible strings to 0 **/

    /** An iterator that increments per position in a given string **/
   int iteration = 0;
   /** An iterator that increments per position in a given string **/

   /** An iterator that increments per segment of a given string **/
   int steps = base;
   /** An iterator that increments per segment of a given string **/

   while(iteration < length){
       /** Algorithm logic to be clarified during lecture **/
       int repetitions = possible/steps;
       int value = 0;
       int position = 0;
       for(int s = 0; s < steps; s++){
           for(int r = 0; r < repetitions; r++){
               generated[position][iteration] = value;
               /**
               Begin: DEBUG
               cout << "steps: " << steps << " repetitions: " << repetitions << " value: " << value << " position: " << position <<"\n";
               End: DEBUG
               **/
               position += 1;
           }
           value = (value+1)%base;
          
       }
       steps *= base;
       iteration += 1;
       /** Algorithm logic to be clarified during lecture **/
   }

   /** Outputting all possible strings for a given base and length **/
   for(int i = 0; i < possible; i++){
       for(int j = 0; j < length; j++){
           cout << generated[i][j];
       }
       cout << "\n";
   }
   /** Outputting all possible strings for a given base and length **/
   return 0;
}

int main() {
   //Set value of base and length
   int base = 2;
    int length = 3;
   //Capture Time Prior to Execution
   milliseconds initial = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
   //Execute our algorithm that models a growth rate.
   generator(base, length);
   //Capture Time of Execution Completion
   milliseconds resulting = duration_cast< milliseconds >(system_clock::now().time_since_epoch());
   //Compute the Duration of Execution
   milliseconds duration = resulting - initial;
   //Output Execution Duration
   cout << "b: " << base << " l: " << length << " runtime: " << std::to_string(duration.count()) << " ms.\n";
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ASSIGNMENT 2.1 & 2.2

#include<iostream>
#include<cmath>
#include <chrono>
#include <vector>
using namespace std;
using namespace std::chrono;

/** vector of strings to store the generated strings of base b and length l **/
vector< string > generated;
/** vector of strings to store the generated strings of base b and length l **/

void generateUnique(int base, int length, string s, bool done[11]){
   /** base condition to check if the string is of the required length or not **/
   if(s.length()==length){
      
       /** inserting the string into the collection if done **/
       generated.push_back(s);
       /** inserting the string into the collection if done **/
      
       return;
   }
   /** base condition to check if the string is of the required length or not **/
  
   /** loop to check the numbers which have not been used yet and calling the iterative function for them **/
   for(int i=0;i<base;i++){
       if(!done[i])
       {
           done[i]=1;
           generateUnique(base , length ,s+(char)(i+'0') , done);
           done[i]=0;
       }
   }
   /** loop to check the numbers which have not been used yet and calling the iterative function for them **/
}

int generator(int base, int length){
  
   /** Algorithm only supports base 2 (binary) to base 10 **/
    if(base < 2 || base > 10){
       cout << "Err: Invalid Base Provided";
       return 1;
    }
    /** Algorithm only supports base 2 (binary) to base 10 **/
   
    /** string which will contain the current string **/
    string s;
    /** string which will contain the current string **/
   
    /** boolean array to check if a number from 0 to (b-1) has been already used or not **/
    bool done[11]={0};
    /** boolean array to check if a number from 0 to (b-1) has been already used or not **/
   
    /** recursive function to generate the strings **/
    generateUnique( base, length, s, done);
    /** recursive function to generate the strings **/
   
    /** outputing the collection **/
      for(int i=0;i<generated.size(); i++)
      cout<<generated[i]<<"\n";
   /** outputing the collection **/  
      
   return 0;  
}

int main() {
   //Set value of base and length
   int base = 4;
   int length = 2;

   //Capture Time Prior to Execution
   milliseconds initial = duration_cast< milliseconds >(system_clock::now().time_since_epoch());

   //Execute our algorithm that models a growth rate.
   generator(base, length);

   //Capture Time of Execution Completion
   milliseconds resulting = duration_cast< milliseconds >(system_clock::now().time_since_epoch());

   //Compute the Duration of Execution
   milliseconds duration = resulting - initial;

   //Output Execution Duration
   cout << "b: " << base << " l: " << length << " runtime: " << std::to_string(duration.count()) << " ms.\n";
   return 0;
}

OUTPUT:

01
02
03
10
12
13
20
21
23
30
31
32
b: 4 l: 2 runtime: 16 ms.


The above program recursively generates the required strings with unique characters .This program can be used for both the assignments 2.1 and 2.2. The recursive function maintains an array of boolean to check which characters have been already used and whch not. Thus it generates all the unique strings of the given length having base b.

Add a comment
Know the answer?
Add Answer to:
III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from...

    Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...

  • I am trying to do this assignment: Write a program that reads a list of concepts/strings...

    I am trying to do this assignment: Write a program that reads a list of concepts/strings from the LIST.txt text file (one concept per line), stores them into an array of strings, and then prints the following: 1.Prints a heading “LIST1:” and then prints all the concepts from the array, one concept per line 2.Prints a heading “LIST2:” and then prints all the concepts from the array that do not contain spaces, one concept per line 3.Prints a heading “LIST3:”...

  • 3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That...

    3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...

  • Use the code below to answer the questions that follow. Assume that all proper libraries and...

    Use the code below to answer the questions that follow. Assume that all proper libraries and name spaces are included and that the code will compile without error. Within the main function, for the variable int last_sid , write in the last digit of your SMC student ID. int foo(int a, int b) { //First int c = a+b; while(c>=3) c-=3; return c; } //------------------------------------ char foo(string a, int b) { //Second return a[b]; } //------------------------------------ string foo(int b, string...

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • 2. 9 marks] Strings. Consider the following definitions on strings Let U be the set of all strings Let s be a str...

    2. 9 marks] Strings. Consider the following definitions on strings Let U be the set of all strings Let s be a string. The length or size of a string, denoted Is, is the number of characters in s Let s be a string, and i e N such that 0 < ί < sl. We write s[i] to represent the character of s at index i, where indexing starts at 0 (so s 0] is the first character, and...

  • 2. 9 marks] Strings. Consider the following definitions on strings Let U be the set of all strings. Let s be a stri...

    2. 9 marks] Strings. Consider the following definitions on strings Let U be the set of all strings. Let s be a string. The length or size of a string, denoted Is, is the number of characters in s Let s be a string, and ie N such that 0 Si< Is. We write si] to represent the character of s at index i, where indexing starts at 0 (so s(0 is the first character, and s|s -1 is the...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT