I am trying to figure out how to print all the combinations in c++.
Given input is {"abc","xyz"} and desired output is {"ax", "ay", "az", "bx", "by", "bz", "cx", "cy","cz"}
I found this recursion code snippet :
`#include <bits/stdc++.h>
using namespace std;
void printKLengthString(char set[], string sequence, int n, int k) {
if (k == 0){
cout<<sequence<<"\t";
return;
}
for (int i = 0; i < n; i++){
string newSequence;
newSequence=sequence+set[i];
printKLengthString(set, newSequence, n, k - 1);
}
}
int main() {
char set[] = {'a', 'b'};
int n = 2;
int k = 3;
printKLengthString(set, "", n, k);
}`
but I am not able to manipulate it according to my desired inputs
Update 1:
Here is my code:
`#include <bits/stdc++.h>
using namespace std;
void printKLengthString(vector<char> set, string sequence, int n, int k) {
if (k == 0){
cout<<sequence<<"\t";
return;
}
for (int i = 0; i < n; i++){
string newSequence;
newSequence=sequence+set.at(i);
printKLengthString(set, newSequence, n, k - 1);
}
}
int main() {
vector<string> stringIn = {"ab", "xy"};
// int n = 2;
// int k = 2;
// for (int i = 0; i < set.size(); i++) {
// cout << set[i] << "\n";
// }
vector<char> set;
for (int i = 0; i < stringIn.size(); i++) {
for (int j = 0; j < stringIn[0].size(); j++) {
// cout << stringIn[i].at(j) << "\n";
// str += char(set[i].at(j));
set.push_back(stringIn[i].at(j));
}
}
// for (char k: set) {
// cout << k << "\t";
// }
cout << "\n";
// cout << "stringIn Size : " << stringIn.size() << "\n";
// cout << "set Size : " << set.size() << "\n";
int k = stringIn.size();
int n = set.size();
printKLengthString(set, "", n, k);
}`
I am getting output as :
aa ab ax ay ba bb bx by xa xb xx xy ya yb yx yy
which is permutation but I just want the combination , which I am not able to figure out..
Anyone could guide me?
Update 2: I want to scale this for multiple inputs, e.g. {"abc","def","ghi","xyz"}
const unsigned int n1 = strlen(s1);
const unsigned int n2 = strlen(s2);
for (unsigned int i1=0;i1<n1;i1++)
{
for (unsigned int i2=0;i2<n2;i2++)
{
printf("%c%c\n",s1[i1],s2[i2]);
}
}
Related
If input A < 10^1000 , b = common int <100000
how can Ii know that A is multiple of B or not?
int main()
{
int testcase = 0;
cin >> testcase;
for (int i = 0; i < testcase; i++)
{
long long num;
int div = 0;
cin >> num >> div;
if (num % div == 0)
{
cout << 1 << endl;
}
else
{
cout << 0 << endl;
}
}
return 0;
}
this is what i tried.
The first step is to read (or set) the large number as a string.
The second step consists in convert this string to a vector of int, each int being less than 10000.
The last step is to calculate a modulo b, in the same way we are performing a division by hand
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
std::vector<int> conver_to_vect_int (const std::string &a) {
std::vector<int> x;
int n = a.size();
x.reserve (n/5 + 1);
for (int i = 0; i < n; i += 5) {
int max = std::min (i+5, n);
std::string s (a.begin() + i, a.begin() + max);
int number = std::stoi (s);
x.push_back (number);
}
return x;
}
int modulo (const std::string &a_string, int b) {
std::vector<int> a = conver_to_vect_int (a_string);
int mod = 0;
for (int val: a) {
int val_with_mod = mod * 100000 + val;
mod = val_with_mod % b;
}
return mod;
}
int main() {
std::vector<std::string> list_s = {"29", "111111111", "111111111111111111111111111111111111", "111111111111111211111111111111111111"};
int b = 9;
for (auto &s: list_s) {
int mod = modulo (s, b);
bool divisibility = mod == 0;
std::cout << "divisibility of " << s << " by " << b << " = " << divisibility << "\n";
}
}
I have written the code for RSA in C++ on Ubuntu. It was working fine on that, it's working fine on Windows Dev C++ as well, but it doesn't show the character properly.
Here is the code :
#include<iostream>
#include<stdlib.h> // for rand()
#include<math.h> // for floor function
#include<string.h>
using namespace std;
//function to check whether a number is prime or not
int check_prime(int number)
{
int count = 0;
for(int i = 2; i<number + 1; i++)
{
if(number%i == 0)
{
count++;
}
}
if(count>2)
{
return 0;
}
else
{
return 1;
}
}
//function to generate a random prime number
int generate_random_prime()
{
int temp;
while(1)
{
temp = rand() % 50;
if(check_prime(temp) == 1)
{
return temp;
}
}
}
int gcd(int a, int b)
{
int temp;
while(b != 0)
{
temp = b;
b = a%b;
a = temp;
}
return a;
}
// Extended Euclid GCD to find d such de congruent to 1
int extended_gcd(int a, int b)
{
int d, x, y, r, q;
if(b == 0)
{
d = a;
x = 1;
y = 0;
cout << "\n d= " << d << " x= " << x << " y= " << y << "\n";
}
int x2, x1, y2, y1;
x2 = 1;
x1 = 0;
y2 = 0;
y1 = 1;
while(b > 0)
{
q = floor(a / b);
r = a - q*b;
x = x2 - q*x1;
y = y2 - q*y1;
a = b;
b = r;
x2 = x1;
x1 = x;
y2 = y1;
y1 = y;
}
d = a;
x = x2;
y = y2;
return x2;
}
//returns a^b mod n using square and multiply method
int modular_exponentiation(int a, int b, int n)
{
if(a == 1)
{
return 0;
}
int c = 1;
for(int i = 1; i < b + 1; i++)
{
c = (c*a) % n;
}
return c;
}
//cipher text = (message^e) %n
int cipher_text(int m, int e, int n)
{
return modular_exponentiation(m, e, n);
}
//decrypted_text= (cipher^d)%n
int decrypt_cipher(int c, int d, int n)
{
return modular_exponentiation(c, d, n);
}
int main()
{
// generating two random prime p and q
int p = generate_random_prime();
int q = generate_random_prime();
cout << "Prime p : " << p << "and q : " << q << "\n";
int n = p*q;
cout << "n=p*q = " << n << "\n";
//calculating Euler Totient for prime p and q
int euler_phi = (p - 1)*(q - 1);
cout << "Euler totient is : " << euler_phi << "\n";
int d, e;
// calculating e such that 1<e<euler_phi and gcd(n,euler_phi)=1
while(1)
{
e = rand() % (euler_phi - 1 + 1) + 1;
if(gcd(euler_phi, e) == 1)
{
break;
}
}
cout << "e value is : " << e << "\n";
//calculating d such that ed congruent 1, ed=1
d = extended_gcd(e, euler_phi);
//d=5;
cout << "d value is : " << d << "\n";
//storing the message to be encrypted as char array and encrypting each char element
char message[20];
int cipher[20];
cout << "Enter the message to be encrypted : ";
cin >> message;
cout << "Message to be encrypted is : " << message << "\n";
int size = strlen(message);
//calculating cipher text c
for(int i = 0; i < size; i++)
{
cipher[i] = cipher_text(int(message[i]), e, n);
}
cout << "Cipher text is : ";
for(int i = 0; i < size; i++)
{
cout << cipher[i] << " ";
}
char message_decrypted[size];
//decrypting cipher text
for(int i = 0; i < size; i++)
{
message_decrypted[i] = decrypt_cipher(cipher[i], d, n);
}
cout << "\nDecrypted message is : ";
for(int i = 0; i < size; i++)
{
cout << message_decrypted[i];
}
cout << "\n";
return 0;
}
I have tried the code on DevC++ and using g++.
Check the images :
Image using g++ compiler
I need a way to print the char to be displayed properly.
I think that message_decrypted[i]=decrypt_cipher(cipher[i],d,n); needs to be changed to print the character properly in Devcpp
Here is the link to the code in online IDE where it works fine https://repl.it/#shubhamjohar/RSA
When your main routine invokes
decrypt_cipher(cipher[i], d, n);
cipher[0] is 386 as matching your output above. d is -179. And n is 697
The corresponding call into modular_exponentiation(a=386, b=-179, n=697) results in this for-loop getting skipped:
for (int i = 1; i<b + 1; i++) {
c = (c*a) % n;
}
Because i < (b + 1) evaluates to (1 < -178), which evaluates to false.
Therefore, your modular_exponentiation returns 1, which is an unprintable character.
Same applies for the subsequent calls to decrypt_cipher from main.
I don't know enough about the RSA algorithm to know if your implementation is correct. But when d is negative, that for-loop isn't going to do any loops.
Maybe it is incurred by the following expression in your program:
char message_decrypted[size];
There is some standard change related to this usage. please read the following page for more details.
https://www.geeksforgeeks.org/variable-length-arrays-in-c-and-c/
Or try to use something like new char[size] to allocate memory dynamically.
I am working on visual studio 2013, with a windows8 hp.My code is trying to add two int arrays of size [20] and output the sum. I know I am out or range some where ,but I can't seem to find where. I am the first digit from each array during my convert function and my answer[i] output is only 19 digits when it should be 21digits.
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;
int globalnum[20];
int total[21];
int i;
void convert(char[], int);
void add(int[], int[], int);
void printAnswer(int[], int);
int main()
{
char n1[20];
char n2[20];
int num1[20];
int num2[20];
int answer[21];
cin >> n1 >> n2;
int l1 = strlen(n1);
int l2 = strlen(n2);
int max = fmax(l1, l2);
convert(n1, l1);
for (int i = 0; i < max - 1; i++)
num1[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num1[i];
cout << endl;
convert(n2, l2);
for (int i = 0; i < max - 1; i++)
num2[i] = globalnum[i];
for (int i = 0; i < max; i++)
cout << num2[i];
cout << endl;
add(num1, num2, max);
for (int i = 0; i < max - 1; i++)
answer[i] = total[i];
// printAnswer(answer,max);
for (int i = 0; i < max - 1; i++)
cout << answer[i];
return 0;
}
void convert(char c1[], int size)
{
for (int i = 0; i < size - 1; i++)
globalnum[i] = c1[size - 1 - i] - '0';
}
void add(int add1[], int add2[], int s1)
{
int sum[21];
int remain = 0;
for (i = 0; i < s1 - 1; i++)// This starts to add the numbers.
{
sum[i] = (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain) % 10;
if (add1[s1 - 1 - i] + add2[s1 - 1 - i] + remain >= 10)
remain = 1;
else
remain = 0;
if (remain != 0)
total[s1 - 1 - i] = 1;
else total[s1 - 1 - i] = 0;
total[s1 - 1 - i] = sum[i];
}
if (remain != 0)
total[0] = 1;
}
//void printAnswer(int t[], int b)
// {
// for (int i = b - 1; i < 0; i--)
// cout << t[i];
// }
// cout << endl;
//}
There's too many problems with your code to give a simple answer. It's maybe easier to work backwards from working code. You haven't fully specified the problem but this was my best guess at what you're trying to do:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
vector<int> toInts(const string& s) {
vector<int> v(s.size());
transform(cbegin(s), cend(s), begin(v), [](int c) { return c - '0'; });
return v;
}
int main() {
string a, b;
cin >> a >> b;
auto n = toInts(a);
auto m = toInts(b);
const auto size = max(n.size(), m.size());
n.resize(size);
m.resize(size);
vector<int> sums(size);
transform(cbegin(n), cend(n), cbegin(m), begin(sums), plus<>{});
copy(cbegin(sums), cend(sums), ostream_iterator<int>{cout, ", "});
cout << endl;
}
I have a problem with generating a pascal triangle in c++, same algorithm works good in java and in c++ it only works for the first two numbers of every line of the triangle in any other it generates way to big numbers. For example in java it generates:
1 5 10 10 5 1 and in C++: 1 5 1233124 1241241585 32523523500 etc
Here is code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Pascal {
private:
int* tab;
int prev1;
int prev2;
public:
Pascal(int n) {
tab = new int[n+1];
prev1=0;
prev2=0;
for(int i = 0; i <= n; i++) {
for(int k = 0; k <= i; k++) {
if (k == 0) {
tab[k] = 1;
prev2 = 1;
} else {
prev1 = tab[k-1] + tab[k];
tab[k-1] = prev2;
prev2 = prev1;
}
}
}
}
int wspolczynnik(int m) {
return tab[m];
}
};
int main (int argc, char* argv[]) {
int n = 0, m = 0;
n = atoi(argv[1]); // konwersja string na int
if (n >= 0)
for (int i = 2; i < argc; i++) {
Pascal *wiersz = new Pascal(n);
m = atoi(argv[i]);
int result = wiersz->wspolczynnik(m);
if (m < 0 || m > n)
cout << m << " - element poza zakresem" << endl;
else
cout << m << " : " << result << endl;
delete[] wiersz;
}
return 0;
}
See if initializing the tab array helps:
tab = new int[n+1]();
sorry for my english.
I wrote a programm that add two large numbers.
Number 1 is read from file data1.in, the same with the second, data2.in.
The problem was when I tryed to add 68925579999999999990+79925579999999999990 I am getting the wrong result:48951159999999999980.
using python to add these numbers I have got 148851159999999999980.
Where I got wrong ??
#include <iostream.h>
#include <fstream.h>
#include<math.h>
int n = 0;
int m = 0;
const int zerou = 9000;
//using namespace std;
void zero(int*a)
{
for(int i = 0;i<zerou;i++)
a[i]=0;
}
void zero(int*a,int*b)
{
for(int i = 0;i<zerou;i++)
a[i]=b[i]=0;
}
void rebuild(int* a)
{
int temp[9000];
zero(temp);
int i;
int delta = abs(m-n)+1;//k1 -dim a
for(i = delta;i<n+delta;i++)
{
temp[i] = a[i-delta];
a[i-delta] = 0;
}
n += delta;
for(i =0;i<n;i++)
a[i] = temp[i];
}
void rebuildS(int* a)
{
int temp[9000];
zero(temp);
int i;
int delta = abs(m-n)+1;//k1 -dim a
for(i = delta;i<m+delta;i++)
{
temp[i] = a[i-delta];
}
m += delta;
for(i =0;i<m;i++)
a[i] = temp[i];
}
void citirea(int* ar){
ifstream f;
f.open("data1.in");
int data;
while (f>>data){
ar[n++] = data;
}
f.close();
}
void citirea_(int* ar){
ifstream f("data2.in");
int data;
while (f>>data){
ar[m++] = data;
}
f.close();
}
/*
void perDig(int*a, int*b,int *t,int i)
{
*t += (a[i]+b[i])/10;
a[i] = (a[i]+b[i]+*t)%10;
}*/
void adunarea(int*a, int* b)
{
int transport = 0;
int sum;
for(int i = n;i>=0;i--)
{
sum = a[i]+b[i]+transport;
//sum = perDig(a,b,transport,i);
if(sum >9)
{
transport = (sum)/10;
sum %=10;
}
a[i] = sum;
}
}
int main()
{
int a[9000],b[9000];
zero(a,b);
citirea(a);
citirea_(b);
if(n > m)
rebuildS(b);
else if(m > n)
rebuild(a);
adunarea(a,b);
ofstream rez;
rez.open("data.out");
for(int i = 0;i<m;i++)
{
rez<<a[i]<<" ";
}
rez.close();
cin.get();
return 0;
}
There are two errors in your answer.
if (!sum > 9) you don't zero the transport, so numbers carry forever.
You are going to be one digit short because you add N digits and don't account for an Nth+1 digit in the answer if transport != 0.
You have a bigger problem, though. You don't know how to debug the code yourself. If in doubt, make the program tell you each step it performs, and read what it says, and see where it goes wrong. Knowing how to do this is worth more than 10 correct versions of your code.
void adunarea(int*a, int* b)
{
int transport = 0;
int sum;
for(int i = n;i>=0;i--)
{
cout << a[i] << " + " << b[i] << " + " << transport << endl;
sum = a[i]+b[i]+transport;
if(sum >9)
{
transport = (sum)/10;
sum %=10;
}
a[i] = sum;
cout << " = " << sum << " ( " << transport << " )" << endl;
}
}
All I've done is add two print statements, and with that and the correct answer you can look for the first incorrect digit and see absolutely everything about how the wrong digit was made, and then fix it.