incrrect result in pow and gcd function in c++ - c++

this is my code for RSA cryptosystem
but I have an error with using the pow function that git an error solution
c = pow(m,e)%n;
and for gcd (greatest common divisor) I'm using #include numeric and algorithm and it does not run
but I create function and will be git a solution
this is a full code
#include<iostream>
#include<cmath>
#include<math.h>
#include <algorithm>
#include<numeric>
using namespace std;
int prime(long int pr)
{
int i;
int j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
/*int power(int a, int b) {
float r = 1;
while (b != 0) {
r *= a;
--b;
}
return r;
}
*/
int main() {
int flag,w,k;
cout << "Welcome to RSA Project\n\n ";
cout << "Enter First larg prime for p ";
int p,q,phi,n,e,d;
cin >> p;
flag = prime(p);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
cout << "Enter Second larg prime for q ";
cin >> q;
flag = prime(q);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
//compute n= p*q
n = p * q;
// comput phi
phi = (p - 1) * (q - 1);
//chose e or select randomly e must be gcd(e,phi)=1
w = 2;
while (gcd(w, phi) != 1)
{
w++;
}
e = w;
//chose d from random numbers
k = 1;
while (((k * e) % phi) != 1)
{
k++;
}
d = k;
cout << "\nThe public key is : " << "{ " << n << " , " << e << " }";
cout << "\nThe private key is : " << "{ " << n << " , " << d << " }";
cout << "\n*****\n\n p=" << p << "\nq=" << q <<"\nn="<<n<<"\nphi = " << phi << "\ne = " << e << "\nd = " << d << "\n *****";
//Encryption
cout << " \nEnter the message for Encrypt : ";
int m , c;
cin >> m;
//c = pow(m,e)%n; ((( here an error )))
c = pow(m, e);
c = c% n;
cout << "\n message for encrypt : " << m;
cout << " \nmessage after encrypt : " << c;
//Decryption
m = pow(c , d);
m = c% n;
cout << " \nmessage after decrypt : " << m;
}
output :
output
it's fully tested but the error with pow function ..
if anyone can help me please ..

Related

Dot in division (1./2)

This code solves the program : Create an algorithm in the form of a flowchart, write and debug the task using recursive and ordinary functions. Corresponding results.
What is dot used for here "x = (1. / 2) * (f + (a / f));" ?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k, e, x, a, d, n, f, s;
d = 1;
do {
e = 1;
do {
cout << "Press 1 to use recurrent solution method, press 2 to use nun recurrent solution
method" << endl;
cin >> k;
if (k == 1 || k == 2) e = 2;
} while (e == 1);
switch (k)
{
case 1:
{
cout << "Enter the numder a:" << endl;
cin >> a;
cout << "Enter the numder n:" << endl;
cin >> n;
x = (1. / 2)*(1 + a);
f = x;
s = f;
for (int i = 1; i < n; i++)
{
x = (1. / 2) * (f + (a / f));
s += x;
f = x;
}
cout << "Result: " << s << endl;
}
break;
case 2:
{
cout << " Enter the numder a:" << endl;
cin >> a;
x = sqrt(a);
cout << "Result: " << x << endl;
}
break;
}
cout << "Press 1 to repeat!" << endl;
cin >> d;
} while (d == 1);
}
The dot in "x = (1. / 2) * (f + (a / f));" is used for assuring that the division is interpreted as a float division instead of an integer division, so the result of (1/2) is 0.5 instead of 0.

Check if multiple points lies in multiple circles

I have this math problem which requires to find if a point lies in all the circles... I have almost all the code but it has one problem that is it checks which point lies in each circle and not if one point lies in all circles.. I think I need to do a minor change but I can't figure it out...
#include<iostream>
#include<stdlib.h>
#include<math.h>
int main()
{
using namespace std;
system("chcp 1251");
int k, m, c, ic;
float x[12], y[12], r[12], xp[12], yp[12], d;
do
{
cout << "Enter how many circles (1..12): ";
cin >> k;
}
while (k < 1 || k > 12);
for (c = 0; c < k; c++)
{
cout << "Enter coordinates of circles № " << 1 + c << endl;
cout << "x= "; cin >> x[c];
cout << "y= "; cin >> y[c];
cout << "r= "; cin >> r[c];
}
do
{
cout << "Enter how many points (1..20): ";
cin >> m;
} while (m < 1 || m > 20);
cout << "Enter coordinates of points:" << endl;
for (c = 0; c < m; c++)
{
cout << "Point № " << 1 + c << endl;
cout << "x= "; cin >> xp[c];
cout << "y= "; cin >> yp[c];
}
for (c = 0; c < k; c++)
{
cout << "Points in circle № " << 1 + c << ": ";
for (ic = 0; ic < m; ic++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d < r[c] )
cout << 1 + ic << " ";
}
cout << endl;
}
system("pause");
return 0;
}
I think I just need to change the if.. so if anyone can help me I would appreciate.
after all if you allow to enter 20 point your arrays xp and yp should have a size of 20, another advice if it's not a school work is using the structers, for solving your problem this is the code for fix it
bool b;
for (ic = 0; ic < m; ic++)
{
b=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
b=b&&(d < r[c]);
}
if(b)
cout << "This point lies "<< 1 + ic << " ";
cout << endl;
}
Testing one point against many circles:
bool inCircle=true;
for (c = 0; c < k; c++)
{
d = sqrtf((x[c] - xp[ic]) * (x[c] - xp[ic]) + (y[c] - yp[ic]) * (y[c] - yp[ic]));
if (d > r[c])
{
inCircle = false;
cout << "the point is outside circle " << c << endl;
break; // this is not strictly needed
}
}
if(inCircle)
cout << "the point is inside all of the circles" << endl;
Once you have this working perfectly, you can put a loop around it to test many points.

How to transfer elements from vector to a new array?

I need to transfer elements from the total costs (int totalCosts) into a new array to be called costs[]. I tried doing this at the end of the code but when I tried to access the first element in the array, it shows ALL the total costs from the output. I only needed to access the first one.
// ******** CREATE "example.txt" IN THE FOLDER THAT HAS THE PROJECT'S .CPP FILE ********
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <iterator>
using namespace std;
class vehicle_capacities {
public:
int lw, vw;
};
double nearest_ten(double n)
{
return round(n / 10.0 + 0.4) * 10.0;
}
bool cmp_fn(int a, int b)
{
return a > b;
}
int main()
{
int cw;
vehicle_capacities cap;
cap.lw = 30;
cap.vw = 10;
ifstream myfile("example.txt");
if (myfile.is_open()) {
myfile >> cw;
myfile.close();
}
else {
cout << "Unable to open file" << endl;
}
cout << "Amount of cargo to be transported: " << cw;
cw = nearest_ten(cw);
cout << "(" << cw << ")" << endl;
int maxl = cw / cap.lw; // maximum no. of lorries that can be there
vector<pair<int, int>> solutions;
//vector<int> costs;
vector<int>::iterator it;
// for the inclusive range of 0 to maxl, find the corresponding no. of vans for each variant of no of lorries
for (int l = 0; l <= maxl; ++l) {
bool is_integer = (cw - l * cap.lw) % cap.vw == 0; // only if this is true, then there is an integer which satisfies for given l
if (is_integer) {
int v = (cw - l * cap.lw) / cap.vw; // no of vans
solutions.push_back(make_pair(l, v));
}
}
cout << "Number of mini-lorries: ";
for (auto& solution : solutions) {
cout << solution.first << " ";
}
cout << "\n";
cout << "Number of vans: ";
for (auto& solution : solutions) {
cout << solution.second << " ";
}
cout << "\n";
cout << "Total cost: ";
// LORRY COST = $200, VAN COST = $45
for (auto& solution : solutions) {
int totalCosts = (solution.first * 200) + (solution.second * 45);
cout << totalCosts << " ";
}
/*for (auto& solution : solutions) {
int totalcosts = (solution.first * 200) + (solution.second * 45);
costs.push_back(totalcosts);
for (it = costs.begin(); it < costs.end(); it++) {
cout << *it << " ";
}
}*/
cout << endl;
// Comparison between both vehicles, highest amount = trips needed
cout << "Trips Needed: ";
for (auto& solution : solutions) {
int a = solution.first;
int b = solution.second;
if (a > b) {
cout << a << " ";
}
else if (b > a) {
cout << b << " ";
}
else if (a == b) {
cout << a << " ";
}
}
cout << endl;
cout << "Lowest #1: ";
for (auto& solution : solutions) {
int totalCosts[] = { (solution.first * 200) + (solution.second * 45) };
int elements = sizeof(totalCosts) / sizeof(totalCosts[0]);
sort(totalCosts, totalCosts + elements, cmp_fn);
for (int i = 0; i < elements; ++i) // print the results
cout << totalCosts[i] << " ";
cout << totalCosts[0] << " ";
}
// *** FOR SORTING ELEMENTS IN ARRAY LOW TO HIGH ***
/*int array[] = { 1,10,21,55,1000,556 };
int elements = sizeof(array) / sizeof(array[0]); // Get number of elements in array
sort(array, array + elements);
for (int i = 0; i < elements; ++i) // print the results
cout << array[i] << ' ';*/
return 0;
}
Do update if you have any solutions, thank you.
(Note that you have to create "example.txt" in project file.
You need to dynamically allocate your array.
int elements = solution.size();
int *totalCosts = new int[elements];
int j= 0;
// transfer data
for (auto& solution : solutions) {
totalCosts[j++] = (solution.first * 200) + (solution.second * 45);
}
sort(totalCosts, totalCosts + elements, cmp_fn);
for (int i = 0; i < elements; ++i) // print the results
cout << totalCosts[i] << " ";
cout << totalCosts[0] << " ";
delete[] totalCosts;

Not able to copy the data from one vector to another vector which is inside the structure

The code basically takes in number of nodes(N) and the number of neighbors(k) as the input. Then in the main function's for loop the node will be created which will have all the variables declared in the structure. Then the main function calls the initialization function.
The initialization function creates a vector g consisting of N-1 node numbers which doesn't have the node creating it.(That is if N=100 and the 1st node has called the initialization function, then vector g will have 2,3,....,100 ,and if node 2 has called the initialization function, then vector g will have 1,3,4,...,100).
After this random_numbers function is called and this function randomly shuffles this vector g and picks the first k elements from it and returns it to the initialization function which in turn returns it to the main function. Then the main function will update the node1.neighbor_list using the vector it has received.
After the code finishes running, the output has to look something like this
suppose, N=5, k=3.
node1
{
userID = 1;
neighbor_list = [2,5,4];
}
node2
{
userID = 2;
neighbor_list = [5,4,1];
}
node3 {
userID = 3;
neighbor_list = [2,5,4];
}
node4
{
userID = 4;
neighbor_list = [2,5,1];
}
node5
{
userID = 5;
neighbor_list = [3,2,4];
}
I am able to trace the working till the initialization function returns the vector back to the main function. But I am unable to copy this vector into the neighbor_list vector of the node. Please help!! I have been trying for long at this point.
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
int N;
int k;
vector<int> g;
int j;
int i;
struct nodes
{
int userID;
vector<int> neighbor_list;
};
vector<int> initialization(int j);
vector<int> random_numbers(vector<int> &r);
int main()
{
int f;
vector<int> received;
cout << "Enter the number of nodes: "; //enter some number
cin >> N;
cout << "Enter the number of neighbors: "; // enter a number less than N
cin >> k;
cout << endl;
for (i = 1; i <= N; i++)
{
nodes node[i]; // creates a node
node[i].userID = i;
cout << "creating " << i << " node" << endl;
cout << "Calling the initialization function" << endl;
received = initialization(i);
cout
<< "The vector received back from the initialization function is: ";
for (f = 1; f <= k; f++)
{
cout << received[f - 1] << " ";
}
cout << endl;
for (f = 1; f <= k; f++)
{
node[i].neighbor_list.push_back(received[f - 1]); //error(guessing)
}
cout << "The neighbor_list for node " << i << " is " << endl;
for (f = 1; f <= k; f++)
{
cout << "f: " << f;
cout << node[i].neighbor_list[f - 1] << " ";
}
cout << endl;
cout << endl;
}
return 0;
}
vector<int> initialization(int j) //passing the value of i
{
cout << "Entered the initialization function" << endl;
cout << "The value of the node created passed in is " << j << endl;
int f;
vector<int> g;
vector<int> receiver;
int x;
int n;
int z;
int rand_array[N][N - 1]; //to store the neighbor list of the node[i]
int d;
int a = 0;
for (z = 1; z <= N; z++)
{
if (j != z)
{
rand_array[j - 1][a] = z; //Put all values into array except i
cout << "The value of rand_array[" << j - 1 << "][" << a << "] is "
<< z << endl;
a++;
}
else
{
continue;
}
}
for (f = 1; f <= N - 1; f++)
{
g.push_back(rand_array[j - 1][f - 1]);
}
cout << "vector g after push back from the rand_array is: ";
for (f = 1; f <= N - 1; f++)
{
cout << g[f - 1] << " ";
}
cout << endl;
cout << "Calling the random function" << endl;
receiver = random_numbers(g);
cout << "value of receiver vector from random_numbers function: ";
for (f = 1; f <= k; f++)
{
cout << receiver[f - 1] << " ";
}
cout << endl;
return receiver;
}
vector<int> random_numbers(vector<int> &r)
{
int h;
int f;
cout << "Entered the random_numbers function" << endl;
cout << "The vector passed by the initialization function is: ";
for (f = 1; f <= N - 1; f++)
{
cout << r[f - 1] << " ";
}
cout << endl;
vector<int> sender;
random_shuffle(r.begin(), r.end()); //random shuffle the vector
for (h = 1; h <= k; h++)
{
sender.push_back(r[h - 1]);
}
return sender;
}

is there a better way i could have written this program? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int d;
int e;
int f;
int aa = 0;
int bb = 0;
int cc = 0;
int dd = 0;
int ee = 0;
int ff = 0;
const string odd = "ODD";
const string even = "EVEN";
cout << "enter 6 numbers " << endl;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
cin >> e;
cin >> f;
aa = a % 2;
bb = b % 2;
cc = c % 2;
dd = d % 2;
ee = e % 2;
ff = f % 2;
if(aa == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(bb == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(cc == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(dd == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(ee == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
if(ff == 0){
cout << even << endl;
}else{
cout << odd << endl;
}
return 0;
}
for example is there a way to make it do the same thing but with less code, anything I should have included?
is there an easier way than having to write 6 if/else statements - is there a way to do all 6 in one statement or loop?
how could i improve its efficiency?
Write this function:
void outputEvenness(int n)
{
static const string odd = "ODD";
static const string even = "EVEN";
if(n % 2){
cout << odd<< endl;
} else {
cout << even << endl;
}
}
then call it using outputEvenness(a); outputEvenness(b); etc.
First of all you should include header <string>if you use class std::string.
Also there is no sense to define these strings when they are used as string literals. Also instead of different variables it would be better to define only one array. The auxiliary variables are also unnecessary.
If to assume that you may not use arrays then I would write the program the following way
#include <iostream>
#include <initializer_list>
int main()
{
const size_t N = 6;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
const char *odd = "ODD";
const char *even = "EVEN";
std::cout << "enter " << N << " numbers: ";
std::cin >> a >> b >> c >> d >> e >> f;
for ( int x : { a, b, c, d, e, f } )
{
if ( x % 2 == 0 )
{
std::cout << x << " is " << even << std::endl;
}
else
{
std::cout << x << " is " << odd << std::endl;
}
}
return 0;
}
If you are allowed to use arrays then the program could look as
#include <iostream>
int main()
{
const size_t N = 6;
int a[N] = {};
const char *odd = "ODD";
const char *even = "EVEN";
std::cout << "enter " << N << " numbers: ";
for ( int &x : a ) std::cin >> x;
for ( int x : a )
{
if ( x % 2 == 0 )
{
std::cout << x << " is " << even << std::endl;
}
else
{
std::cout << x << " is " << odd << std::endl;
}
}
return 0;
}
For this simple program there is no sense to define a separate function that will check whether a number is even or odd because it is this program that is such a function.:)
Use arrays and loops:
int a[6]; // Array of 6 ints
cout << "enter 6 numbers" << endl;
// Input the 6 numbers
for (int i = 0; i < 6; i++)
{
cin >> a[i];
}
// Output the results
for (int i = 0; i < 6; i++)
{
cout << a[i] << " is " << (a[i] & 1 ? "ODD" : "EVEN") << endl;
}
int value = 0;
string response = "";
cout << "enter 6 numbers " << endl;
for(int i=0; i<6; i++)
{
cin >> value;
value % 2 == 0 ? response+="even\n" : response+="odd\n";
}
cout << response;