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

C++ PROGRAMMING: please make sure program compiles and runs and I will give you

ID: 3814791 • Letter: C

Question

C++ PROGRAMMING: please make sure program compiles and runs and I will give you 5 stars :-)

Assignment: For this assignment you'll be creating three different programs that deal with strings. You can make one multi-purpose program, or three individual programs, but they'll need to perform the following tasks: Given a binary string, your program will have to 'add one' to it, giving the next binary string in sequence, i.e. Given 101011. program returns 101100. Given an input string, print out all PERMUTATIONS of the string, i.e., "cat" would yield "cat, eta, act. ate, tac, tea" NOTE: In a string like "madam", each a and m is considered unique, so there are several different permutations of that string which would output "madam" as well. Given a number between 1 and 6. print out every NON-REPEATING possible COMBINATION of the set {A, B, C, D, E, F}- i.e.. Given 2, the program would return AB, AC, AD, AE, AF, BC, BD, BE, BF, CD, CE, CF, DE, DF, EF. Use your program(s) to handle the following six pieces of input: 10111011 1111 Trick Copycat 3 5

Explanation / Answer

Program 1:

#include <iostream>

using namespace std;

int main()
{
cout << "Enter a binary String: "; // Take input string from user
string num1,num2 = "1",result; // initialize variables
getline(cin,num1); //take string excluding new line
int length = num1.size() - 1; // find length of string
for(int i = 0; i < length; i++) // add zeroes to 1
{
num2 = '0' + num2;
}
int carry = 0; // Initialize carry
// Add all bits one by one
for (int i = length; i >= 0 ; i--)
{
// convert each char to int and add
int firstBit = num1.at(i) - '0';
int secondBit = num2.at(i) - '0';
// boolean expression for sum of 3 bits
int sum = (firstBit ^ secondBit ^ carry)+'0';
// convert to string and store
result = (char)sum + result;
// boolean expression for 3-bit addition
carry = (firstBit & secondBit) | (secondBit & carry) | (firstBit & carry);
}
// if overflow, then add a leading 1
if (carry)
result = '1' + result;
//print output
cout << result << endl;
return 0;
}

Program 2:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// Function to print permutations of input string, each permutation is stored in output
void permute(string input, string output)
{
// When size of input string becomes 0, output has a permutation (length of out is n)
if (input.size() == 0)
{
cout << output << endl;
return;
}

// One be one move all characters at the beginning of output
for (int i = 0; i < input.size(); i++)
{
// Remove first character from input and add it to out
permute(input.substr(1), output + input[0]);

// Rotate string such that second character moves to the beginning.
rotate(input.begin(), input.begin() + 1, input.end());
}
}

// Driver code
int main()
{
string input; // initialize variable
cout << "Enter the string for permutations: "; // prompt user for output
getline(cin, input); // read input without new line
permute(input, ""); // call permutation unction
return 0;
}

Program 3:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Recursive template function
template <class comb, class Func>
//recursive function to print combination
void recursive_combination(comb n_begin, comb n_end, int n_column, comb r_begin, comb r_end, int r_column,int loop, Func func)
{
int r_size=r_end-r_begin; //size of combination
int localloop = loop;
int local_n_column = n_column;
//A different combination is out
if(r_column>(r_size-1))
{
func(r_begin); // displaying each combination
return;
}
for(int i = 0; i <= loop; ++i)
{
comb temp1 = r_begin; // new iteration
for(int count=0; count < r_column; ++count)
{
++temp1;
}
comb temp2 = n_begin;
for(int count2 = 0; count2 < n_column + i; ++count2)
{
++temp2;
}
*temp1=*temp2;
++local_n_column;
recursive_combination(n_begin, n_end, local_n_column, r_begin, r_end, r_column + 1, localloop, func);
--localloop;
}
}
// display each combination
void display(char* begin)
{
cout << begin << endl;
}
//driver code
int main()
{
char str[]="ABCDEF",combination[7];
int n = 6,r,i;
cout << "Enter a number between 1 - 6: ";
cin >> r;
for(i = 0; i < r; i++)
combination[i] = str[i]; // taking first combination
combination[i] = ''; // adding null character to string
recursive_combination(str, str+n, 0, combination, combination+r, 0, n-r, display);
return 0;
}

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