Hill Cipher Encrypt - c++

So I am trying to use the hill cipher to encrypt my 3x3 matrix with a given key. It works correctly for the first value outputting n which it should, but then after that value I get large values and it never takes the mod of them. I added the cout statements to help me debug and see what's going wrong, but I still can't fix it. Also the second mod 26 is there because when I didn't have it there I was getting negative 13 instead of positive 13. This is a homework program, our key was given to us as numbers in case that is of any importance.
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
using std::endl;
using std::string;
void inverse_matrix();
string encryption(string x);
int main()
{
std::string one = "paymoremoney";
// inverse_matrix();
encryption(one);
system("pause");
return 0;
}
string encryption(string x)
{
int encrypted[4][4];
int key[3][3];
key[0][0] = 4;
key[0][1] = 9;
key[0][2] = 15;
key[1][0] = 15;
key[1][1] = 17;
key[1][2] = 6;
key[2][0] = 24;
key[2][1] = 0;
key[2][2] = 17;
int test = 0;
char str[] = "";
char c;
int length = (int)x.length();
for (int i = 0; i < length; i++)
{
x[i] = tolower(x[i]);
}
/*
while (str[test])
{
c = str[test];
putchar(tolower(c));
test++;
}
*/
int encrypt[4][4];
encrypt[0][0] = x[0];
encrypt[0][1] = x[1];
encrypt[0][2] = x[2];
encrypt[1][0] = x[3];
encrypt[1][1] = x[4];
encrypt[1][2] = x[5];
encrypt[2][0] = x[6];
encrypt[2][1] = x[7];
encrypt[2][2] = x[8];
encrypt[3][0] = x[9];
encrypt[3][1] = x[10];
encrypt[3][2] = x[11];
encrypted[0][0] = (key[0][0] * encrypt[0][0]) + (key[1][0] * encrypt[0][1]) + (key[3][0] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;
encrypted[0][1] = (key[0][1] * encrypt[0][0]) + (key[1][1] * encrypt[0][1]) + (key[2][1] * encrypt[0][2])%26;
encrypted[0][0] %= 26;
encrypted[0][2] = (key[0][2] * encrypt[0][0]) + (key[1][2] * encrypt[0][1]) + (key[2][2] * encrypt[0][2]) % 26;
encrypted[0][0] %= 26;
std::cout << encrypted[0][0];
std::cout << endl;
std::cout << encrypted[0][1];
std::cout << endl;
std::cout << encrypted[0][2];
std::cout << endl;
}

As this is homework, I'll point in the right direction rather than give a direct answer. You're assigning to encrypted[0][1]. What do you do with that value after assigning to it?

Related

C++ Code segmentation fault only in vscode

My C++ code (shown below) works on this site:
GDB Online but not in Visual Studio, where it crashes at
iterations[imag_times][real_times] = i % (iter / 2);
when imag_times is 1 and real_times is 0 with the exception being Exception has occurred. Segmentation fault
I have installed GDB version 7.6.1.
My Question: Does anybody know how to fix that and why this is happening?
#include <iostream>
using namespace std;
int main()
{
// initialization
const double real_min = -1;
const double real_max = 1;
const double imag_min = -1;
const double imag_max = 1;
const int iter = 30;
const double real_offs = 0.01;
const double imag_offs = 0.01;
double z_real = 0;
double z_imag = 0;
double c_real = real_min;
double c_imag = imag_max;
int real_times = 0;
int imag_times = 0;
int** iterations = new int*[1];
iterations[0] = new int;
int i = 0;
// start
while(c_imag >= imag_min)
{
iterations = (int**)realloc(iterations, sizeof(int*) * (imag_times + 1));
real_times = 0;
c_real = real_min;
while(c_real <= real_max)
{
iterations[imag_times] = (int*)realloc(iterations[imag_times], sizeof(int) * (real_times + 1));
z_real = 0;
z_imag = 0;
for(i = 0; i < iter; i++)
{
double z_imag2 = z_imag * z_imag;
z_imag = 2 * z_real * z_imag + c_imag;
z_real = z_real * z_real - z_imag2 + c_real;
if(z_real * z_real + z_imag * z_imag > 4)
{
break;
}
}
iterations[imag_times][real_times] = i % (iter / 2);
real_times++;
c_real = real_min + real_offs * real_times;
}
imag_times++;
c_imag = imag_max - imag_offs * imag_times;
}
// output
for(int i = 0; i < imag_times; i++)
{
for(int j = 0; j < real_times; j++)
{
cout << iterations[i][j];
cout << ",";
}
cout << "\n";
}
cout << "done";
std::cin.get(); // pause so the program doesnt exit instantly
return 0;
}
Thanks in advance!

Using codeblocks code in visual studio

I am used to write C++ project in CodeBlocks, but for some stupid reasons I have to show it to my teacher in VisualStudio. I tried to make a console app or an empty project, and copied my main file there, but with the first one I get bunch of erorrs and the second one I get 'The system cannot find the way specified'. What is different in VisualStudio? I don't understand at all what is wrong.
here is my code
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
const int kroku = 1000;
const double aa = 0; //pocatecni bod intervalu
const double bb = 1; //konečný bod intervalu
double a; //parametr
const double h = (bb - aa) / kroku; //krok
double p(double t) { //(py')' - qy = f
return exp(a*pow(t, 2));
}
double q(double t) {
return -exp(a*pow(t, 2))*pow(a, 2)*pow(t, 2);
}
double dp(double t) {
return 2 * t*a*exp(a*pow(t, 2));
}
double y[kroku + 1]; //řešení původní rce
double dydx[kroku + 1];
double z[kroku + 1]; //řešení dílčí rce
double dzdx[kroku + 1];
double x[kroku + 1]; //rozdělení intervalu (aa, bb) po krocích h
void generateX() { //generuje hodnoty x
for (int k = 0; k <= kroku; k++) {
x[k] = aa + k*h;
}
}
double partial(double pp1, double pp2, double w[kroku + 1], double dwdx[kroku + 1], double v)//řešení rce (pw')' - qw = g s pp
{
w[v] = pp1; //inicializace - počáteční podmínka
dwdx[v] = pp2; //inicialzace - počáteční podmínka
for (int i = 0; i <= kroku; i++) { //substituce dwdx proměnná -> dwdx = (w_(n+1) - w_n)/h) && dwdx =
w[i + 1] = h*dwdx[i] + w[i];
dwdx[i + 1] = (h / p(aa + h*i))*(q(aa + h*i)*w[i] - dp(aa + h*i)*dwdx[i]) + dwdx[i];
}
return 0;
}
double omega1, omega2; //nové počáteční podmínky omega1 = y(x0), omega2 = y'(x0)
void print(double N[kroku + 1])
{
fstream file;
file.open("data.dat", ios::out | ios::in | ios::trunc);//otevření/vytvoření(trunc) souboru
if (file.is_open()) //zápis do souboru
{
cout << "Writing";
file << "#" << "X" << " " << "Y" << endl;
for (int j = 0; j <= kroku; j++) {
file << x[j] << " " << N[j] << endl;
}
file << "#end";
}
else
{
cout << "Somethinq went wrong!";
}
file.close();
}
int main()
{
double alpha; //pocatecni podminka y(aa) = alpha
double beta; //y(bb) = beta
cout << "Assign the value of beta " << endl;
cin >> beta;
cout << "Assign the value of alpha " << endl;
cin >> alpha;
cout << "Assign the value of parameter a" << endl;
cin >> a;
double alpha1 = 0; //alpha1*p(aa)*y'(aa) - beta1*y(aa) = gamma1
//double alpha2 = 0; //alpha2*p(bb)*y'(bb) + beta2*y(bb) = gamma2
double beta1 = -1;
double beta2 = 1;
double gamma1 = alpha;
double gamma2 = beta;
generateX();
partial(alpha1, beta1 / p(aa), z, dzdx, aa); //(pz')'-qz = 0
omega1 = gamma2 / beta2;
omega2 = 1 / (z[kroku] * p(bb))*(gamma1 + dzdx[kroku] * p(bb));
partial(omega1, omega2, y, dydx, aa);//(py')' - qy = f = 0
print(y);
return 0;
strong text}
when I add
#include "stdafx.h"
I get four errors
2x 'Expression must have integral or unscoped enum type'
2x 'subscript is not of integral type'
for these lines
w[v] = pp1;
dwdx[v] = pp2;
Could anyone please help me? Thank you a lot
array subscript v in your line
w[v]
can not be double. It must be of interger type.

C++ Eigen Library

I'm having trouble compiling this program with #include. I see that if I comment out this line it compiles.
MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
I am unable to change the header since I need to run this code in ROS and I have to use the Eigen library built within. I am using the code as described in this link
How to fit a bounding ellipse around a set of 2D points.
Any help is greatly appricated.
pound include iostream
pound include Eigen/Dense
using namespace std;
using Eigen::MatrixXd;
int main ( )
{
//The tolerance for error in fitting the ellipse
double tolerance = 0.2;
int n = 12; // number of points
int d = 2; // dimension
MatrixXd p(d,n); //Fill matrix with random points
p(0,0) = -2.644722;
p(0,1) = -2.644961;
p(0,2) = -2.647504;
p(0,3) = -2.652942;
p(0,4) = -2.652745;
p(0,5) = -2.649508;
p(0,6) = -2.651345;
p(0,7) = -2.654530;
p(0,8) = -2.651370;
p(0,9) = -2.653966;
p(0,10) = -2.661322;
p(0,11) = -2.648208;
p(1,0) = 4.764553;
p(1,1) = 4.718605;
p(1,2) = 4.676985;
p(1,3) = 4.640509;
p(1,4) = 4.595640;
p(1,5) = 4.546657;
p(1,6) = 4.506177;
p(1,7) = 4.468277;
p(1,8) = 4.421263;
p(1,9) = 4.383508;
p(1,10) = 4.353276;
p(1,11) = 4.293307;
cout << p << endl;
MatrixXd q = p;
q.conservativeResize(p.rows() + 1, p.cols());
for(size_t i = 0; i < q.cols(); i++)
{
q(q.rows() - 1, i) = 1;
}
int count = 1;
double err = 1;
const double init_u = 1.0 / (double) n;
MatrixXd u = MatrixXd::Constant(n, 1, init_u);
while(err > tolerance)
{
MatrixXd Q_tr = q.transpose();
cout << "1 " << endl;
MatrixXd X = q * u.asDiagonal() * Q_tr;
cout << "1a " << endl;
MatrixXd M = (Q_tr * X.inverse() * q).diagonal();
cout << "1b " << endl;
int j_x, j_y;
double maximum = M.maxCoeff(&j_x, &j_y);
double step_size = (maximum - d - 1) / ((d + 1) * (maximum + 1));
MatrixXd new_u = (1 - step_size) * u;
new_u(j_x, 0) += step_size;
cout << "2 " << endl;
//Find err
MatrixXd u_diff = new_u - u;
for(size_t i = 0; i < u_diff.rows(); i++)
{
for(size_t j = 0; j < u_diff.cols(); j++)
u_diff(i, j) *= u_diff(i, j); // Square each element of the matrix
}
err = sqrt(u_diff.sum());
count++;
u = new_u;
}
cout << "3 " << endl;
MatrixXd U = u.asDiagonal();
MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
MatrixXd c = p * u;
cout << A << endl;
cout << c << endl;
return 0;
}
If I replace the obvious pound include bogus by
#include <iostream>
#include <Eigen/Dense>
it compiles just fine. It also runs, prints some numbers and returns 0.

C++ Functions in loop/struct

Here is my unaltered working code:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance << ". The amount with interest is: " << Total[i] << endl;
system("pause");
}
}
Here is what i need to do: You must add a function to your program called CalcInterest. This function will take as its ONLY parameter an Account, and return the Interest calculated as shown in Part 1. Your main program should now use this function instead, to generate the display as in Part 1.
This is what i tried:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int CalcInterest(Account);
struct Account { //declare struct outside of main
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int AccountNumber[MAXACCOUNTS] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
double Balance[MAXACCOUNTS] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
int DaysSinceDebited[MAXACCOUNTS] = { 20, 35, 2, 14, 5, 360, 1, 45 };
double Total[MAXACCOUNTS] = {};
//add your code here
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
CalcInterest(Account);
}
int CalcInterest(Account) {
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited > 30))
Total[i] = accounts[i].Balance * 1.06;
else Total[i] = accounts[i].Balance * 1.03;
cout << accounts[i].Number << "has a balance of " << accounts[i].Balance << ". The amount with interest is : " << Total[i] << endl;
}
system("pause");
return 0;
}
There were many errors with this, mostly of things becoming undefined, such as .DaysSinceDebited etc PLEASE HELP!
I believe that by "there are many errors", you are talking about compilation errors. A quick look at your code confirms this.
The first mistake you made is that this function only works on a single account. You therefore cannot loop over your accounts array inside that function, neither can you access Total. You are also a little confused about the syntax of passing arguments, as well as the datatype that should be returned. I can help you with that.
Change your function definition to:
double CalcInterest( const Account & account )
{
// Do your interest calculation on 'account' here, then return it from the function.
double interest = 0.0; //<-- For you to do.
return interest;
}
Then you can simplify your loop in main...
for (int i = 0; i < MAXACCOUNTS; i++)
{
// Calculate the interest on the account, then do something with it.
double interest = CalcInterest( accounts[i] );
Total[i] = 0.0; //<-- For you to do.
}
Note that I've only provided the language structure here, since this is obviously an assignment of some sort. I have indicated the parts where you need to do some work.

C++ Program Crashing

Hi guys I am kinda new on c++ so I was writing a program and it works fine, but there is a problem. Every time I type number bigger than 100 my program crashes and I don't know why. Could anyone help me?
Program code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i = 10;
while(i > 0)
{
i--;
int b = 0, c = 1, d = 0, e, number, how = 0, number1, start, to, number2, split1, split2, mass, start1 = 0, start2 = 0, number3, how1, number4, number5;
cout << "\nIveskite skaiciu \n";
cin >> number;
cout << "\n";
number1 = number;
while(number1 > 0)
{
number1 = number1 / 10;
how = how + 1;
}
how1 = how - 1;
start = pow(10, (how - 1));
to = pow(10, how);
mass = to - start;
number2 = start - 1;
int split[mass][mass], numbers[mass], ok[mass];
while(start1 < mass)
{
start1++;
e = number2 + start1;
numbers[start1] = e;
split[start1][0] = e;
}
while(start2 < mass)
{
start2++;
number3 = numbers[start2];
d = 0;
b = 0;
c = 1;
while(d <= how1)
{
d++;
split1 = number3%10;
split2 = number3 / 10;
number3 = split2;
split[start2][d] = split1;
number4 = b + split[start2][d];
b = number4;
number5 = c * split[start2][d];;
c = number5;
}
if(number4 == number5)
{
ok[mass] = numbers[start2];
cout << number4 << " " << number5 << " >" << ok[mass] << endl;
}
}
}
Seems to me its the 2D array split that gets too large for your stack. You could probably try to allocate it dynamically as suggested here: how to deal with large 2D arrays