remove old stuff
This commit is contained in:
parent
f4fa9cdf8e
commit
74d8503a20
@ -1,70 +0,0 @@
|
||||
/*This is a C++ Program to perform 2D FFT. A fast Fourier transform (FFT) is an algorithm to compute the discrete Fourier transform (DFT) and its inverse. Fourier analysis converts time (or space) to frequency and vice versa; an FFT rapidly computes such transformations by factorizing the DFT matrix into a product of sparse (mostly zero) factors.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define PI 3.14159265
|
||||
int n;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the size: ";
|
||||
cin >> n;
|
||||
double inputData[n][n];
|
||||
cout << "Enter the 2D elements ";
|
||||
for (int i = 0; i < n; i++)
|
||||
for (int j = 0; j < n; j++)
|
||||
cin >> inputData[i][j];
|
||||
double realOut[n][n];
|
||||
double imagOut[n][n];
|
||||
double amplitudeOut[n][n];
|
||||
int height = n;
|
||||
int width = n;
|
||||
// Two outer loops iterate on output data.
|
||||
for (int yWave = 0; yWave < height; yWave++)
|
||||
{
|
||||
for (int xWave = 0; xWave < width; xWave++)
|
||||
{
|
||||
// Two inner loops iterate on input data.
|
||||
for (int ySpace = 0; ySpace < height; ySpace++)
|
||||
{
|
||||
for (int xSpace = 0; xSpace < width; xSpace++)
|
||||
{
|
||||
// Compute real, imag, and ampltude.
|
||||
realOut[yWave][xWave] += (inputData[ySpace][xSpace] * cos(
|
||||
2 * PI * ((1.0 * xWave * xSpace / width) + (1.0
|
||||
* yWave * ySpace / height)))) / sqrt(
|
||||
width * height);
|
||||
imagOut[yWave][xWave] -= (inputData[ySpace][xSpace] * sin(
|
||||
2 * PI * ((1.0 * xWave * xSpace / width) + (1.0
|
||||
* yWave * ySpace / height)))) / sqrt(
|
||||
width * height);
|
||||
amplitudeOut[yWave][xWave] = sqrt(
|
||||
realOut[yWave][xWave] * realOut[yWave][xWave]
|
||||
+ imagOut[yWave][xWave]
|
||||
* imagOut[yWave][xWave]);
|
||||
}
|
||||
cout << realOut[yWave][xWave] << " + " << imagOut[yWave][xWave]
|
||||
<< " i (" << amplitudeOut[yWave][xWave] << ")\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the size:
|
||||
2
|
||||
Enter the 2D elements
|
||||
2 3
|
||||
4 2
|
||||
|
||||
2.5 + 0.0 i
|
||||
5.5 + 0.0 i
|
||||
-0.5 + -1.8369701987210297E-16 i
|
||||
0.5 + -3.0616169978683826E-16 i
|
||||
2.5 + 0.0 i
|
||||
-0.5 + -3.6739403974420594E-16 i
|
||||
-0.5 + -1.8369701987210297E-16 i
|
||||
-1.5 + -1.8369701987210297E-16 i
|
@ -1,40 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout<<"Enter the dimension of the matrix:\n ";
|
||||
int rowA;
|
||||
cin>>rowA;
|
||||
int colA;
|
||||
cin>>colA;
|
||||
cout<<"Enter the dimension of the other matrix:\n ";
|
||||
int rowB;
|
||||
cin>>rowB;
|
||||
int colB;
|
||||
cin>>colB;
|
||||
if(colA == rowB)
|
||||
{
|
||||
cout<<"Matrices are multipilcable";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"Matrices are not multipilcable";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the dimension of the matrix:
|
||||
2 4
|
||||
Enter the dimension of the other matrix:
|
||||
2 5
|
||||
Matrices are not multipilcable
|
||||
|
||||
Enter the dimension of the matrix:
|
||||
4 5
|
||||
Enter the dimension of the other matrix:
|
||||
5 6
|
||||
Matrices are multipilcable
|
@ -1,76 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
double d = 0;
|
||||
double det(int n, double mat[10][10]);
|
||||
double det(int n, double mat[10][10])
|
||||
{
|
||||
double submat[10][10];
|
||||
if (n == 2)
|
||||
return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
|
||||
else
|
||||
{
|
||||
for (int c = 0; c < n; c++)
|
||||
{
|
||||
int subi = 0; //submatrix's i value
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
int subj = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (j == c)
|
||||
continue;
|
||||
submat[subi][subj] = mat[i][j];
|
||||
subj++;
|
||||
}
|
||||
subi++;
|
||||
}
|
||||
d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the dimension of the matrix:\n";
|
||||
int n;
|
||||
cin >> n;
|
||||
double mat[10][10];
|
||||
cout << "Enter the elements of the matrix:\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> mat[j][i];
|
||||
}
|
||||
}
|
||||
if (det(n, mat) != 0)
|
||||
{
|
||||
cout << "The given matrix is invertible";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "The given matrix is not invertible";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the dimension of the matrix:
|
||||
3
|
||||
Enter the elements of the matrix:
|
||||
1 2 3
|
||||
4 5 6
|
||||
7 8 9
|
||||
The given matrix is not invertible
|
||||
|
||||
Enter the dimension of the matrix:
|
||||
5
|
||||
Enter the elements of the matrix:
|
||||
1 2 3 4 5
|
||||
6 7 8 9 0
|
||||
0 9 8 7 6
|
||||
5 4 3 2 1
|
||||
1 3 5 7 9
|
||||
The given matrix is invertible
|
@ -1,54 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <conio.h>
|
||||
|
||||
using namespace std;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout<<"Enter the dimensions of the matrix: ";
|
||||
int m, n;
|
||||
cin>>m>>n;
|
||||
double mat[m][n];
|
||||
int zeros = 0;
|
||||
cout<<"Enter the elements of the matrix: ";
|
||||
for(int i=0; i<m; i++)
|
||||
{
|
||||
for(int j=0; j<n; j++)
|
||||
{
|
||||
cin>>mat[i][j];
|
||||
if(mat[i][j] == 0)
|
||||
{
|
||||
zeros++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(zeros > (m*n)/2)
|
||||
{
|
||||
cout<<"The matrix is a sparse matrix";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"The matrix is not a sparse matrix";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the dimensions of the matrix:
|
||||
3 3
|
||||
|
||||
Enter the elements of the matrix:
|
||||
1 2 3
|
||||
4 5 6
|
||||
0 0 0
|
||||
|
||||
The matrix is not a sparse matrix
|
||||
|
||||
|
||||
Enter the dimensions of the matrix:
|
||||
3 3
|
||||
|
||||
Enter the elements of the matrix:
|
||||
1 1 0
|
||||
0 0 1
|
||||
1 0 0
|
||||
|
||||
The matrix is a sparse matrix
|
@ -1,65 +0,0 @@
|
||||
/*This is a C++ Program to compute the coefficients of the DFT (Discrete Fourier Transform) directly. In mathematics, the discrete Fourier transform (DFT) converts a finite list of equally spaced samples of a function into the list of coefficients of a finite combination of complex sinusoids, ordered by their frequencies, that has those same sample values. It can be said to convert the sampled function from its original domain (often time or position along a line) to the frequency domain.*/
|
||||
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define PI 3.14159265
|
||||
|
||||
class DFT_Coefficient
|
||||
{
|
||||
public:
|
||||
double real, img;
|
||||
DFT_Coefficient()
|
||||
{
|
||||
real = 0.0;
|
||||
img = 0.0;
|
||||
}
|
||||
};
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int N = 10;
|
||||
cout << "Calculation DFT Coefficients\n";
|
||||
cout << "Enter the coefficient of simple linear function:\n";
|
||||
cout << "ax + by = c\n";
|
||||
double a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
double function[N];
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
function[i] = (((a * (double) i) + (b * (double) i)) - c);
|
||||
//System.out.print( " "+function[i] + " ");
|
||||
}
|
||||
cout << "Enter the max K value: ";
|
||||
int k;
|
||||
cin >> k;
|
||||
double cosine[N];
|
||||
double sine[N];
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
cosine[i] = cos((2 * i * k * PI) / N);
|
||||
sine[i] = sin((2 * i * k * PI) / N);
|
||||
}
|
||||
DFT_Coefficient dft_val;
|
||||
cout << "The coefficients are: ";
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
dft_val.real += function[i] * cosine[i];
|
||||
dft_val.img += function[i] * sine[i];
|
||||
}
|
||||
cout << "(" << dft_val.real << ") - " << "(" << dft_val.img << " i)";
|
||||
}
|
||||
|
||||
/*
|
||||
Calculation DFT Coefficients
|
||||
Enter the coefficient of simple linear funtion:
|
||||
ax + by = c
|
||||
1 2 3
|
||||
Enter the max K value:
|
||||
2
|
||||
The coefficients are: (-15) - (-20.6457 i)
|
||||
|
||||
------------------
|
||||
(program exited with code: 0)
|
||||
Press return to continue
|
@ -1,69 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
double d = 0;
|
||||
double det(int n, double mat[10][10]);
|
||||
double det(int n, double mat[10][10])
|
||||
{
|
||||
double submat[10][10];
|
||||
if (n == 2)
|
||||
return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
|
||||
else
|
||||
{
|
||||
for (int c = 0; c < n; c++)
|
||||
{
|
||||
int subi = 0; //submatrix's i value
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
int subj = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (j == c)
|
||||
continue;
|
||||
submat[subi][subj] = mat[i][j];
|
||||
subj++;
|
||||
}
|
||||
subi++;
|
||||
}
|
||||
d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the dimension of the matrix:\n";
|
||||
int n;
|
||||
cin >> n;
|
||||
double mat[10][10];
|
||||
cout << "Enter the elements of the matrix:\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> mat[j][i];
|
||||
}
|
||||
}
|
||||
cout << "The determinant of the given matrix is: " << det(n, mat);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the dimension of the matrix:
|
||||
3
|
||||
Enter the elements of the matrix:
|
||||
3 5 2
|
||||
8 4 8
|
||||
2 4 7
|
||||
The determinant of the given matrix is: -164
|
||||
|
||||
Enter the dimension of the matrix:
|
||||
4
|
||||
Enter the elements of the matrix:
|
||||
9 5 2 5
|
||||
9 5 3 7
|
||||
6 5 4 8
|
||||
1 5 3 7
|
||||
The determinant of the given matrix is: 0
|
@ -1,84 +0,0 @@
|
||||
/*This is a C++ Program to perform Discrete Fourier Transform using Naive approach. In mathematics, the discrete Fourier transform (DFT) converts a finite list of equally spaced samples of a function into the list of coefficients of a finite combination of complex sinusoids, ordered by their frequencies, that has those same sample values. It can be said to convert the sampled function from its original domain (often time or position along a line) to the frequency domain.*/
|
||||
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define PI 3.14159265
|
||||
|
||||
class DFT_Coefficient
|
||||
{
|
||||
public:
|
||||
double real, img;
|
||||
DFT_Coefficient()
|
||||
{
|
||||
real = 0.0;
|
||||
img = 0.0;
|
||||
}
|
||||
};
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int N = 10;
|
||||
cout << "Discrete Fourier Transform using naive method\n";
|
||||
cout << "Enter the coefficient of simple linear function:\n";
|
||||
cout << "ax + by = c\n";
|
||||
double a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
double function[N];
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
function[i] = (((a * (double) i) + (b * (double) i)) - c);
|
||||
//System.out.print( " "+function[i] + " ");
|
||||
}
|
||||
cout << "Enter the max K value: ";
|
||||
int k;
|
||||
cin >> k;
|
||||
double cosine[N];
|
||||
double sine[N];
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
cosine[i] = cos((2 * i * k * PI) / N);
|
||||
sine[i] = sin((2 * i * k * PI) / N);
|
||||
}
|
||||
DFT_Coefficient dft_val[k];
|
||||
cout << "The coefficients are: ";
|
||||
for (int j = 0; j < k; j++)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
dft_val[j].real += function[i] * cosine[i];
|
||||
dft_val[j].img += function[i] * sine[i];
|
||||
}
|
||||
cout << "(" << dft_val[j].real << ") - " << "(" << dft_val[j].img << " i)\n";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Discrete Fourier Transform using naive method
|
||||
Enter the coefficient of simple linear function:
|
||||
ax + by = c
|
||||
1 2 3
|
||||
Enter the max K value: 20
|
||||
The coefficients are:
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
||||
(105) - (-1.03386e-005 i)
|
@ -1,73 +0,0 @@
|
||||
/*This is a C++ Program to perform Fast Fourier Transform. A fast Fourier transform (FFT) is an algorithm to compute the discrete Fourier transform (DFT) and its inverse. Fourier analysis converts time (or space) to frequency and vice versa; an FFT rapidly computes such transformations by factorizing the DFT matrix into a product of sparse (mostly zero) factors.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <complex>
|
||||
#include <cmath>
|
||||
#include <iterator>
|
||||
using namespace std;
|
||||
|
||||
unsigned int bitReverse(unsigned int x, int log2n)
|
||||
|
||||
{
|
||||
int n = 0;
|
||||
int mask = 0x1;
|
||||
for (int i = 0; i < log2n; i++)
|
||||
{
|
||||
n <<= 1;
|
||||
n |= (x & 1);
|
||||
x >>= 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
const double PI = 3.1415926536;
|
||||
template<class Iter_T>
|
||||
void fft(Iter_T a, Iter_T b, int log2n)
|
||||
{
|
||||
typedef typename iterator_traits<iter_t>::value_type complex;
|
||||
const complex J(0, 1);
|
||||
int n = 1 << log2n;
|
||||
for (unsigned int i = 0; i < n; ++i)
|
||||
{
|
||||
b[bitReverse(i, log2n)] = a[i];
|
||||
}
|
||||
for (int s = 1; s <= log2n; ++s)
|
||||
{
|
||||
int m = 1 << s;
|
||||
int m2 = m >> 1;
|
||||
complex w(1, 0);
|
||||
complex wm = exp(-J * (PI / m2));
|
||||
for (int j = 0; j < m2; ++j)
|
||||
{
|
||||
for (int k = j; k < n; k += m)
|
||||
{
|
||||
complex t = w * b[k + m2];
|
||||
complex u = b[k];
|
||||
b[k] = u + t;
|
||||
b[k + m2] = u - t;
|
||||
}
|
||||
w *= wm;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
typedef complex cx;
|
||||
cx a[] = { cx(0, 0), cx(1, 1), cx(3, 3), cx(4, 4), cx(4, 4), cx(3, 3), cx(
|
||||
1, 1), cx(0, 0)
|
||||
};
|
||||
cx b[8];
|
||||
fft(a, b, 3);
|
||||
for (int i = 0; i < 8; ++i)
|
||||
cout << b[i] << "\n";
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
(16,16)
|
||||
(-4.82843,-11.6569)
|
||||
(0,0)
|
||||
(-0.343146,0.828427)
|
||||
(0,0)
|
||||
(0.828427,-0.343146)
|
||||
(0,0)
|
||||
(-11.6569,-4.82843)
|
@ -1,25 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the number of dice: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << "The values on dice are: ( ";
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << (rand() % 6) + 1<<" ";
|
||||
cout<<")";
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of dice: 5
|
||||
The values on dice are: ( 6 6 5 5 6 )
|
||||
|
||||
Enter the number of dice: 1
|
||||
The values on dice are: ( 6 )
|
||||
|
||||
Enter the number of dice: 3
|
||||
The values on dice are: ( 6 6 5 )
|
@ -1,74 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
|
||||
using namespace std;
|
||||
double d = 0;
|
||||
double det(int n, double mat[10][10]);
|
||||
double det(int n, double mat[10][10])
|
||||
{
|
||||
double submat[10][10];
|
||||
if (n == 2)
|
||||
return ((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
|
||||
else
|
||||
{
|
||||
for (int c = 0; c < n; c++)
|
||||
{
|
||||
int subi = 0; //submatrix's i value
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
int subj = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (j == c)
|
||||
continue;
|
||||
submat[subi][subj] = mat[i][j];
|
||||
subj++;
|
||||
}
|
||||
subi++;
|
||||
}
|
||||
d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the number of vectors:\n";
|
||||
int n;
|
||||
cin >> n;
|
||||
double mat[10][10];
|
||||
cout << "Enter the vectors one by one:\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> mat[j][i];
|
||||
}
|
||||
}
|
||||
d = det(n, mat);
|
||||
if (d != 0)
|
||||
cout << "The vectors forms the basis of R" << n
|
||||
<< " as the determinant is non-zero";
|
||||
else
|
||||
cout << "The vectors doesn't form the basis of R" << n
|
||||
<< " as the determinant is zero";
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of vectors:
|
||||
3
|
||||
Enter the vectors one by one:
|
||||
1 2 3
|
||||
2 3 4
|
||||
3 4 5
|
||||
The vectors doesn't form the basis of R3 as the determinant is zero
|
||||
|
||||
Enter the number of vectors:
|
||||
4
|
||||
Enter the vectors one by one:
|
||||
2 3 5 8
|
||||
1 6 2 9
|
||||
3 4 2 7
|
||||
2 5 3 9
|
||||
The vectors forms the basis of R4 as the determinant is non-zero
|
@ -1,137 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Closest Pair of Points in an Array
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cfloat>
|
||||
#include <cstdlib>
|
||||
#include <cmath>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Point Declaration
|
||||
*/
|
||||
struct Point
|
||||
{
|
||||
int x, y;
|
||||
};
|
||||
|
||||
/*
|
||||
* sort array of points according to X coordinate
|
||||
*/
|
||||
int compareX(const void* a, const void* b)
|
||||
{
|
||||
Point *p1 = (Point *)a, *p2 = (Point *)b;
|
||||
return (p1->x - p2->x);
|
||||
}
|
||||
/*
|
||||
* sort array of points according to Y coordinate
|
||||
*/
|
||||
int compareY(const void* a, const void* b)
|
||||
{
|
||||
Point *p1 = (Point *)a, *p2 = (Point *)b;
|
||||
return (p1->y - p2->y);
|
||||
}
|
||||
/*
|
||||
* find the distance between two points
|
||||
*/
|
||||
float dist(Point p1, Point p2)
|
||||
{
|
||||
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
|
||||
}
|
||||
/*
|
||||
* return the smallest distance between two points
|
||||
*/
|
||||
float small_dist(Point P[], int n)
|
||||
{
|
||||
float min = FLT_MAX;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = i + 1; j < n; ++j)
|
||||
{
|
||||
if (dist(P[i], P[j]) < min)
|
||||
min = dist(P[i], P[j]);
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
/*
|
||||
* find the distance beween the closest points of strip of given size
|
||||
*/
|
||||
float stripClosest(Point strip[], int size, float d)
|
||||
{
|
||||
float min = d;
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int j = i + 1; j < size && (strip[j].y - strip[i].y) < min; ++j)
|
||||
{
|
||||
if (dist(strip[i],strip[j]) < min)
|
||||
min = dist(strip[i], strip[j]);
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
/*
|
||||
* find the smallest distance.
|
||||
*/
|
||||
float closestUtil(Point Px[], Point Py[], int n)
|
||||
{
|
||||
if (n <= 3)
|
||||
return small_dist(Px, n);
|
||||
int mid = n / 2;
|
||||
Point midPoint = Px[mid];
|
||||
Point Pyl[mid + 1];
|
||||
Point Pyr[n - mid - 1];
|
||||
int li = 0, ri = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (Py[i].x <= midPoint.x)
|
||||
Pyl[li++] = Py[i];
|
||||
else
|
||||
Pyr[ri++] = Py[i];
|
||||
}
|
||||
float dl = closestUtil(Px, Pyl, mid);
|
||||
float dr = closestUtil(Px + mid, Pyr, n-mid);
|
||||
float d = min(dl, dr);
|
||||
Point strip[n];
|
||||
int j = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (abs(Py[i].x - midPoint.x) < d)
|
||||
strip[j] = Py[i], j++;
|
||||
}
|
||||
return min(d, stripClosest(strip, j, d));
|
||||
}
|
||||
/*
|
||||
* finds the smallest distance
|
||||
*/
|
||||
float closest(Point P[], int n)
|
||||
{
|
||||
Point Px[n];
|
||||
Point Py[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
Px[i] = P[i];
|
||||
Py[i] = P[i];
|
||||
}
|
||||
qsort(Px, n, sizeof(Point), compareX);
|
||||
qsort(Py, n, sizeof(Point), compareY);
|
||||
return closestUtil(Px, Py, n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
|
||||
int n = sizeof(P) / sizeof(P[0]);
|
||||
cout << "The smallest distance is " << closest(P, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
The smallest distance is 1.41421
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,83 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Factorial of Large Numbers
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
int fact[101][200] = {0};
|
||||
|
||||
/*
|
||||
* Find Factorial of Large Numbers
|
||||
* fact[i][0] is used to store the number of digits
|
||||
*/
|
||||
void fact_large(int n)
|
||||
{
|
||||
int i;
|
||||
fact[1][0] = 1;
|
||||
fact[1][1] = 1;
|
||||
if (fact[n][0] == 0)
|
||||
{
|
||||
for (i = n - 1; i > 0 ; i--)
|
||||
{
|
||||
if (fact[i][0] != 0)
|
||||
break;
|
||||
}
|
||||
for ( ; i < n; i++)
|
||||
{
|
||||
int j = 1;
|
||||
int carry = 0;
|
||||
int len = fact[i][0];
|
||||
while (len--)
|
||||
{
|
||||
int temp = (i + 1) * fact[i][j] + carry;
|
||||
fact[i + 1][j] = temp % 10;
|
||||
carry = temp / 10;
|
||||
j++;
|
||||
}
|
||||
while (carry > 0)
|
||||
{
|
||||
fact[i + 1][j] = carry % 10;
|
||||
carry /= 10;
|
||||
j++;
|
||||
}
|
||||
fact[i + 1][0] = j - 1;
|
||||
}
|
||||
}
|
||||
for (i = fact[n][0]; i > 0; i--)
|
||||
{
|
||||
cout << fact[n][i];
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter interger to compute factorial(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
fact_large(n);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter interger to compute factorial(0 to exit): 100
|
||||
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||
Enter interger to compute factorial(0 to exit): 50
|
||||
30414093201713378043612608166064768844377641568960512000000000000
|
||||
Enter interger to compute factorial(0 to exit): 72
|
||||
61234458376886086861524070385274672740778091784697328983823014963978384987221689274204160000000000000000
|
||||
Enter interger to compute factorial(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Factorial of a Number using Dynamic Programming
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
int result[1000] = {0};
|
||||
/*
|
||||
* Find Factorial of a Number using Dynamic Programming
|
||||
*/
|
||||
ll fact_dp(int n)
|
||||
{
|
||||
if (n >= 0)
|
||||
{
|
||||
result[0] = 1;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
result[i] = i * result[i - 1];
|
||||
}
|
||||
return result[n];
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter interger to compute factorial(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fact_dp(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter interger to compute factorial(0 to exit): 10
|
||||
3628800
|
||||
Enter interger to compute factorial(0 to exit): 20
|
||||
2432902008176640000
|
||||
Enter interger to compute factorial(0 to exit): 15
|
||||
1307674368000
|
||||
Enter interger to compute factorial(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Factorial of a Number using Iteration
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
/*
|
||||
* Find Factorial of a Number using Iteration
|
||||
*/
|
||||
ll fact_iter(int n)
|
||||
{
|
||||
ll result = 1;
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter interger to compute factorial(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fact_iter(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter interger to compute factorial(0 to exit): 10
|
||||
3628800
|
||||
Enter interger to compute factorial(0 to exit): 20
|
||||
2432902008176640000
|
||||
Enter interger to compute factorial(0 to exit): 15
|
||||
1307674368000
|
||||
Enter interger to compute factorial(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,47 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Factorial of a Number using Recursion
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
/*
|
||||
* Find Factorial of a Number using Recursion
|
||||
*/
|
||||
ll fact_recur(int n)
|
||||
{
|
||||
if (n == 0 || n == 1)
|
||||
return 1;
|
||||
else
|
||||
return n * fact_recur(n - 1);
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter interger to compute factorial(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fact_recur(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter interger to compute factorial(0 to exit): 10
|
||||
3628800
|
||||
Enter interger to compute factorial(0 to exit): 20
|
||||
2432902008176640000
|
||||
Enter interger to compute factorial(0 to exit): 15
|
||||
1307674368000
|
||||
Enter interger to compute factorial(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,71 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Fibonacci Numbers using Dynamic Programming
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
ll fib[1000] = {0};
|
||||
/*
|
||||
* Fibonacci Numbers using Dp
|
||||
*/
|
||||
ll fibo_dp(int n)
|
||||
{
|
||||
fib[1] = 1;
|
||||
fib[2] = 1;
|
||||
if (fib[n] == 0)
|
||||
{
|
||||
for (int j = 3; j <= n; ++j)
|
||||
{
|
||||
if (fib[n] == 0)
|
||||
fib[j] = fib[j - 1] + fib[j - 2];
|
||||
else
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return fib[n];
|
||||
}
|
||||
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fibo_dp(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 10
|
||||
55
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
|
||||
34
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
|
||||
21
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
|
||||
13
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
|
||||
8
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
|
||||
5
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
|
||||
3
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
|
||||
2
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,68 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Fibonacci Numbers using Iteration
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Iterative function to find Fibonacci Numbers
|
||||
*/
|
||||
ll fibo_iter(int n)
|
||||
{
|
||||
int previous = 1;
|
||||
int current = 1;
|
||||
int next = 1;
|
||||
for (int i = 3; i <= n; ++i)
|
||||
{
|
||||
next = current + previous;
|
||||
previous = current;
|
||||
current = next;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fibo_iter(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 1
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
|
||||
2
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
|
||||
3
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
|
||||
5
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
|
||||
8
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
|
||||
13
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
|
||||
21
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
|
||||
34
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 10
|
||||
55
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* function to multiply two matrices
|
||||
*/
|
||||
void multiply(ll F[2][2], ll M[2][2])
|
||||
{
|
||||
ll x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
|
||||
ll y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
|
||||
ll z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
|
||||
ll w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
|
||||
F[0][0] = x;
|
||||
F[0][1] = y;
|
||||
F[1][0] = z;
|
||||
F[1][1] = w;
|
||||
}
|
||||
|
||||
/*
|
||||
* function to calculate power of a matrix
|
||||
*/
|
||||
void power(ll F[2][2], int n)
|
||||
{
|
||||
if (n == 0 || n == 1)
|
||||
return;
|
||||
ll M[2][2] = {{1,1},{1,0}};
|
||||
power(F, n / 2);
|
||||
multiply(F, F);
|
||||
if (n % 2 != 0)
|
||||
multiply(F, M);
|
||||
}
|
||||
|
||||
/*
|
||||
* function that returns nth Fibonacci number
|
||||
*/
|
||||
ll fibo_matrix(ll n)
|
||||
{
|
||||
ll F[2][2] = {{1,1},{1,0}};
|
||||
if (n == 0)
|
||||
return 0;
|
||||
power(F, n - 1);
|
||||
return F[0][0];
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fibo_matrix(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 1
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
|
||||
2
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
|
||||
3
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
|
||||
5
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
|
||||
8
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
|
||||
13
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
|
||||
21
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
|
||||
34
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 10
|
||||
55
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Fibonacci Numbers using Recursion
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Recursive function to find Fibonnaci Numbers
|
||||
*/
|
||||
ll fibo_recur(int n)
|
||||
{
|
||||
if (n == 1 || n == 2)
|
||||
return 1;
|
||||
else
|
||||
return fibo_recur(n - 1) + fibo_recur(n - 2);;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
while (1)
|
||||
{
|
||||
cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
|
||||
cin>>n;
|
||||
if (n == 0)
|
||||
break;
|
||||
cout<<fibo_recur(n)<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 1
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
|
||||
1
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
|
||||
2
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
|
||||
3
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
|
||||
5
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
|
||||
8
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
|
||||
13
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
|
||||
21
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
|
||||
34
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 10
|
||||
55
|
||||
Enter the integer n to find nth fibonnaci no.(0 to exit): 0
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,35 +0,0 @@
|
||||
/*This is a C++ Program to find GCD of two numbers using Recursive Euclid Algorithm. In mathematics, the Euclidean algorithm, or Euclid’s algorithm, is a method for computing the greatest common divisor (GCD) of two (usually positive) integers, also known as the greatest common factor (GCF) or highest common factor (HCF). It is named after the Greek mathematician Euclid, who described it in Books VII and X of his Elements.
|
||||
The GCD of two positive integers is the largest integer that divides both of them without leaving a remainder (the GCD of two integers in general is defined in a more subtle way).
|
||||
|
||||
In its simplest form, Euclid’s algorithm starts with a pair of positive integers, and forms a new pair that consists of the smaller number and the difference between the larger and smaller numbers. The process repeats until the numbers in the pair are equal. That number then is the greatest common divisor of the original pair of integers.
|
||||
|
||||
The main principle is that the GCD does not change if the smaller number is subtracted from the larger number. For example, the GCD of 252 and 105 is exactly the GCD of 147 (= 252 – 105) and 105. Since the larger of the two numbers is reduced, repeating this process gives successively smaller numbers, so this repetition will necessarily stop sooner or later — when the numbers are equal (if the process is attempted once more, one of the numbers will become 0).*/
|
||||
|
||||
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
int gcd(int u, int v)
|
||||
{
|
||||
return (v != 0) ? gcd(v, u % v) : u;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int num1, num2, result;
|
||||
cout << "Enter two numbers to find GCD using Euclidean algorithm: ";
|
||||
cin >> num1 >> num2;
|
||||
result = gcd(num1, num2);
|
||||
if (gcd)
|
||||
cout << "\nThe GCD of " << num1 << " and " << num2 << " is: " << result
|
||||
<< endl;
|
||||
else
|
||||
cout << "\nInvalid input!!!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter two numbers to find GCD using Euclidean algorithm: 12 30
|
||||
The GCD of 12 and 30 is: 6
|
@ -1,565 +0,0 @@
|
||||
#if !defined(MATRIX_H)
|
||||
#define MATRIX_H
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <tchar.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
class CMatrix
|
||||
{
|
||||
private:
|
||||
int m_rows;
|
||||
int m_cols;
|
||||
char m_name[128];
|
||||
CMatrix();
|
||||
public:
|
||||
double **m_pData;
|
||||
CMatrix(const char *name, int rows, int cols) :
|
||||
m_rows(rows), m_cols(cols)
|
||||
{
|
||||
strcpy(m_name, name);
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
CMatrix(const CMatrix &other)
|
||||
{
|
||||
strcpy(m_name, other.m_name);
|
||||
m_rows = other.m_rows;
|
||||
m_cols = other.m_cols;
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
~CMatrix()
|
||||
{
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
delete[] m_pData[i];
|
||||
delete[] m_pData;
|
||||
m_rows = m_cols = 0;
|
||||
}
|
||||
void SetName(const char *name)
|
||||
{
|
||||
strcpy(m_name, name);
|
||||
}
|
||||
const char* GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
void GetInput()
|
||||
{
|
||||
std::cin >> *this;
|
||||
}
|
||||
void FillSimulatedInput()
|
||||
{
|
||||
static int factor1 = 1, factor2 = 2;
|
||||
std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: "
|
||||
<< m_rows << " Cols: " << m_cols << "\n";
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
std::cout << "Input For Row: " << i + 1 << " Col: " << j
|
||||
+ 1 << " = ";
|
||||
int data = ((i + 1) * factor1) + (j + 1) * factor2;
|
||||
m_pData[i][j] = data / 10.2;
|
||||
std::cout << m_pData[i][j] << "\n";
|
||||
factor1 += (rand() % 4);
|
||||
factor2 += (rand() % 3);
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
double Determinant()
|
||||
{
|
||||
double det = 0;
|
||||
double **pd = m_pData;
|
||||
switch (m_rows)
|
||||
{
|
||||
case 2:
|
||||
{
|
||||
det = pd[0][0] * pd[1][1] - pd[0][1] * pd[1][0];
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
/***
|
||||
a b c
|
||||
d e f
|
||||
g h i
|
||||
|
||||
a b c a b c
|
||||
d e f d e f
|
||||
g h i g h i
|
||||
|
||||
// det (A) = aei + bfg + cdh - afh - bdi - ceg.
|
||||
***/
|
||||
double a = pd[0][0];
|
||||
double b = pd[0][1];
|
||||
double c = pd[0][2];
|
||||
double d = pd[1][0];
|
||||
double e = pd[1][1];
|
||||
double f = pd[1][2];
|
||||
double g = pd[2][0];
|
||||
double h = pd[2][1];
|
||||
double i = pd[2][2];
|
||||
double det = (a * e * i + b * f * g + c * d * h);
|
||||
det = det - a * f * h;
|
||||
det = det - b * d * i;
|
||||
det = det - c * e * g;
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
CMatrix *temp[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
temp[i] = new CMatrix("", 3, 3);
|
||||
for (int k = 0; k < 4; k++)
|
||||
{
|
||||
for (int i = 1; i < 4; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = this->m_pData[0][0] * temp[0]->Determinant()
|
||||
- this->m_pData[0][1] * temp[1]->Determinant()
|
||||
+ this->m_pData[0][2] * temp[2]->Determinant()
|
||||
- this->m_pData[0][3] * temp[3]->Determinant();
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
{
|
||||
CMatrix *temp[5];
|
||||
for (int i = 0; i < 5; i++)
|
||||
temp[i] = new CMatrix("", 4, 4);
|
||||
for (int k = 0; k < 5; k++)
|
||||
{
|
||||
for (int i = 1; i < 5; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = this->m_pData[0][0] * temp[0]->Determinant()
|
||||
- this->m_pData[0][1] * temp[1]->Determinant()
|
||||
+ this->m_pData[0][2] * temp[2]->Determinant()
|
||||
- this->m_pData[0][3] * temp[3]->Determinant()
|
||||
+ this->m_pData[0][4] * temp[4]->Determinant();
|
||||
return det;
|
||||
}
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
default:
|
||||
{
|
||||
int DIM = m_rows;
|
||||
CMatrix **temp = new CMatrix*[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
temp[i] = new CMatrix("", DIM - 1, DIM - 1);
|
||||
for (int k = 0; k < DIM; k++)
|
||||
{
|
||||
for (int i = 1; i < DIM; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < DIM; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = 0;
|
||||
for (int k = 0; k < DIM; k++)
|
||||
{
|
||||
if ((k % 2) == 0)
|
||||
det = det + (this->m_pData[0][k]
|
||||
* temp[k]->Determinant());
|
||||
else
|
||||
det = det - (this->m_pData[0][k]
|
||||
* temp[k]->Determinant());
|
||||
}
|
||||
for (int i = 0; i < DIM; i++)
|
||||
delete temp[i];
|
||||
delete[] temp;
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
CMatrix& operator =(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";
|
||||
}
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
delete[] m_pData[i];
|
||||
delete[] m_pData;
|
||||
m_rows = m_cols = 0;
|
||||
strcpy(m_name, other.m_name);
|
||||
m_rows = other.m_rows;
|
||||
m_cols = other.m_cols;
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
CMatrix CoFactor()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return cofactor;
|
||||
if (m_rows < 2)
|
||||
return cofactor;
|
||||
else if (m_rows == 2)
|
||||
{
|
||||
cofactor.m_pData[0][0] = m_pData[1][1];
|
||||
cofactor.m_pData[0][1] = -m_pData[1][0];
|
||||
cofactor.m_pData[1][0] = -m_pData[0][1];
|
||||
cofactor.m_pData[1][1] = m_pData[0][0];
|
||||
return cofactor;
|
||||
}
|
||||
else if (m_rows >= 3)
|
||||
{
|
||||
int DIM = m_rows;
|
||||
CMatrix ***temp = new CMatrix**[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
temp[i] = new CMatrix*[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
for (int j = 0; j < DIM; j++)
|
||||
temp[i][j] = new CMatrix("", DIM - 1, DIM - 1);
|
||||
for (int k1 = 0; k1 < DIM; k1++)
|
||||
{
|
||||
for (int k2 = 0; k2 < DIM; k2++)
|
||||
{
|
||||
int i1 = 0;
|
||||
for (int i = 0; i < DIM; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < DIM; j++)
|
||||
{
|
||||
if (k1 == i || k2 == j)
|
||||
continue;
|
||||
temp[k1][k2]->m_pData[i1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
if (k1 != i)
|
||||
i1++;
|
||||
}
|
||||
}
|
||||
}
|
||||
bool flagPositive = true;
|
||||
for (int k1 = 0; k1 < DIM; k1++)
|
||||
{
|
||||
flagPositive = ((k1 % 2) == 0);
|
||||
for (int k2 = 0; k2 < DIM; k2++)
|
||||
{
|
||||
if (flagPositive == true)
|
||||
{
|
||||
cofactor.m_pData[k1][k2]
|
||||
= temp[k1][k2]->Determinant();
|
||||
flagPositive = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
cofactor.m_pData[k1][k2]
|
||||
= -temp[k1][k2]->Determinant();
|
||||
flagPositive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < DIM; i++)
|
||||
for (int j = 0; j < DIM; j++)
|
||||
delete temp[i][j];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
delete[] temp[i];
|
||||
delete[] temp;
|
||||
}
|
||||
return cofactor;
|
||||
}
|
||||
CMatrix Adjoint()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
CMatrix adj("ADJ", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return adj;
|
||||
cofactor = this->CoFactor();
|
||||
// adjoint is transpose of a cofactor of a matrix
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
adj.m_pData[j][i] = cofactor.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return adj;
|
||||
}
|
||||
CMatrix Transpose()
|
||||
{
|
||||
CMatrix trans("TR", m_cols, m_rows);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
trans.m_pData[j][i] = m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return trans;
|
||||
}
|
||||
CMatrix Inverse()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
CMatrix inv("INV", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return inv;
|
||||
// to find out Determinant
|
||||
double det = Determinant();
|
||||
cofactor = this->CoFactor();
|
||||
// inv = transpose of cofactor / Determinant
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
inv.m_pData[j][i] = cofactor.m_pData[i][j] / det;
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
CMatrix operator +(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Addition could not take place because number of rows and columns are different between the two matrices";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
result.m_pData[i][j] = this->m_pData[i][j]
|
||||
+ other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
CMatrix operator -(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Subtraction could not take place because number of rows and columns are different between the two matrices";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
result.m_pData[i][j] = this->m_pData[i][j]
|
||||
- other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
CMatrix operator *(const CMatrix &other)
|
||||
{
|
||||
if (this->m_cols != other.m_rows)
|
||||
{
|
||||
std::cout
|
||||
<< "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", this->m_rows, other.m_cols);
|
||||
for (int i = 0; i < this->m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < other.m_cols; j++)
|
||||
{
|
||||
for (int k = 0; k < this->m_cols; k++)
|
||||
{
|
||||
result.m_pData[i][j] += this->m_pData[i][k]
|
||||
* other.m_pData[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
bool operator ==(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Comparision could not take place because number of rows and columns are different between the two matrices";
|
||||
return false;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
bool bEqual = true;
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
if (this->m_pData[i][j] != other.m_pData[i][j])
|
||||
bEqual = false;
|
||||
}
|
||||
}
|
||||
return bEqual;
|
||||
}
|
||||
friend std::istream& operator >>(std::istream &is, CMatrix &m);
|
||||
friend std::ostream& operator <<(std::ostream &os, const CMatrix &m);
|
||||
};
|
||||
std::istream& operator >>(std::istream &is, CMatrix &m)
|
||||
{
|
||||
std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: "
|
||||
<< m.m_rows << " Cols: " << m.m_cols << "\n";
|
||||
for (int i = 0; i < m.m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m.m_cols; j++)
|
||||
{
|
||||
std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1
|
||||
<< " = ";
|
||||
is >> m.m_pData[i][j];
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return is;
|
||||
}
|
||||
std::ostream& operator <<(std::ostream &os, const CMatrix &m)
|
||||
{
|
||||
os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: "
|
||||
<< m.m_cols << "\n\n";
|
||||
for (int i = 0; i < m.m_rows; i++)
|
||||
{
|
||||
os << " | ";
|
||||
for (int j = 0; j < m.m_cols; j++)
|
||||
{
|
||||
char buf[32];
|
||||
double data = m.m_pData[i][j];
|
||||
if (m.m_pData[i][j] > -0.00001 && m.m_pData[i][j] < 0.00001)
|
||||
data = 0;
|
||||
sprintf(buf, "%10.2lf ", data);
|
||||
os << buf;
|
||||
}
|
||||
os << "|\n";
|
||||
}
|
||||
os << "\n\n";
|
||||
return os;
|
||||
}
|
||||
#endif
|
||||
int main()
|
||||
{
|
||||
CMatrix a("A", 5, 5);
|
||||
//std::cin >> a;
|
||||
a.FillSimulatedInput();
|
||||
CMatrix aadj = a.Inverse();
|
||||
std::cout << a;
|
||||
std::cout << aadj;
|
||||
CMatrix unit = (a * aadj);
|
||||
unit.SetName("A * A-Inv");
|
||||
std::cout << unit;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter Input For Matrix :
|
||||
A Rows: 5
|
||||
Cols: 5
|
||||
Input For Row: 1 Col: 1 = 0.294118
|
||||
Input For Row: 1 Col: 2 = 0.980392
|
||||
Input For Row: 1 Col: 3 = 1.86275
|
||||
Input For Row: 1 Col: 4 = 2.84314
|
||||
Input For Row: 1 Col: 5 = 3.62745
|
||||
|
||||
Input For Row: 2 Col: 1 = 2.54902
|
||||
Input For Row: 2 Col: 2 = 3.92157
|
||||
Input For Row: 2 Col: 3 = 5.09804
|
||||
Input For Row: 2 Col: 4 = 7.05882
|
||||
Input For Row: 2 Col: 5 = 9.80392
|
||||
|
||||
Input For Row: 3 Col: 1 = 6.66667
|
||||
Input For Row: 3 Col: 2 = 8.92157
|
||||
Input For Row: 3 Col: 3 = 10.8824
|
||||
Input For Row: 3 Col: 4 = 12.6471
|
||||
Input For Row: 3 Col: 5 = 15.3922
|
||||
|
||||
Input For Row: 4 Col: 1 = 12.0588
|
||||
Input For Row: 4 Col: 2 = 15.098
|
||||
Input For Row: 4 Col: 3 = 18.1373
|
||||
Input For Row: 4 Col: 4 = 20.7843
|
||||
Input For Row: 4 Col: 5 = 24.4118
|
||||
|
||||
Input For Row: 5 Col: 1 = 21.1765
|
||||
Input For Row: 5 Col: 2 = 24.7059
|
||||
Input For Row: 5 Col: 3 = 27.7451
|
||||
Input For Row: 5 Col: 4 = 31.0784
|
||||
Input For Row: 5 Col: 5 = 34.3137
|
||||
|
||||
Matrix : A Rows: 5 Cols: 5
|
||||
|
||||
| 0.29 0.98 1.86 2.84 3.63 |
|
||||
| 2.55 3.92 5.10 7.06 9.80 |
|
||||
| 6.67 8.92 10.88 12.65 15.39 |
|
||||
| 12.06 15.10 18.14 20.78 24.41 |
|
||||
| 21.18 24.71 27.75 31.08 34.31 |
|
||||
|
||||
Matrix : INV Rows: 5 Cols: 5
|
||||
|
||||
| -0.93 0.80 -3.74 2.86 -0.49 |
|
||||
| 0.37 -0.32 5.35 -4.91 1.14 |
|
||||
| -0.78 -0.93 -1.46 2.96 -1.10 |
|
||||
| 2.37 -0.10 0.25 -1.65 0.84 |
|
||||
| -1.21 0.57 -0.58 0.87 -0.36 |
|
||||
|
||||
Matrix : A * A-Inv Rows: 5 Cols: 5
|
||||
|
||||
| 1.00 0.00 0.00 0.00 0.00 |
|
||||
| 0.00 1.00 0.00 0.00 0.00 |
|
||||
| 0.00 0.00 1.00 0.00 0.00 |
|
||||
| 0.00 0.00 0.00 1.00 0.00 |
|
||||
| 0.00 0.00 0.00 0.00 1.00 |
|
@ -1,109 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// This class represents a directed graph using adjacency list representation
|
||||
class Graph
|
||||
{
|
||||
int V; // No. of vertices
|
||||
list<int> *adj; // Pointer to an array containing adjacency lists
|
||||
public:
|
||||
Graph(int V); // Constructor
|
||||
void addEdge(int v, int w); // function to add an edge to graph
|
||||
bool isReachable(int s, int d); // returns true if there is a path from s to d
|
||||
};
|
||||
|
||||
Graph::Graph(int V)
|
||||
{
|
||||
this->V = V;
|
||||
adj = new list<int> [V];
|
||||
}
|
||||
|
||||
void Graph::addEdge(int v, int w)
|
||||
{
|
||||
adj[v].push_back(w); // Add w to v’s list.
|
||||
}
|
||||
|
||||
// A BFS based function to check whether d is reachable from s.
|
||||
bool Graph::isReachable(int s, int d)
|
||||
{
|
||||
// Base case
|
||||
if (s == d)
|
||||
return true;
|
||||
// Mark all the vertices as not visited
|
||||
bool *visited = new bool[V];
|
||||
for (int i = 0; i < V; i++)
|
||||
visited[i] = false;
|
||||
// Create a queue for BFS
|
||||
list<int> queue;
|
||||
// Mark the current node as visited and enqueue it
|
||||
visited[s] = true;
|
||||
queue.push_back(s);
|
||||
// it will be used to get all adjacent vertices of a vertex
|
||||
list<int>::iterator i;
|
||||
while (!queue.empty())
|
||||
{
|
||||
// Dequeue a vertex from queue and print it
|
||||
s = queue.front();
|
||||
queue.pop_front();
|
||||
// Get all adjacent vertices of the dequeued vertex s
|
||||
// If a adjacent has not been visited, then mark it visited
|
||||
// and enqueue it
|
||||
for (i = adj[s].begin(); i != adj[s].end(); ++i)
|
||||
{
|
||||
// If this adjacent node is the destination node, then return true
|
||||
if (*i == d)
|
||||
return true;
|
||||
// Else, continue to do BFS
|
||||
if (!visited[*i])
|
||||
{
|
||||
visited[*i] = true;
|
||||
queue.push_back(*i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Driver program to test methods of graph class
|
||||
int main()
|
||||
{
|
||||
// Create a graph given in the above diagram
|
||||
Graph g(4);
|
||||
g.addEdge(0, 1);
|
||||
g.addEdge(0, 2);
|
||||
g.addEdge(1, 2);
|
||||
g.addEdge(2, 0);
|
||||
g.addEdge(2, 3);
|
||||
g.addEdge(3, 3);
|
||||
cout << "Enter the source and destination vertices: (0-3)";
|
||||
int u, v;
|
||||
cin >> u >> v;
|
||||
if (g.isReachable(u, v))
|
||||
cout << "\nThere is a path from " << u << " to " << v;
|
||||
else
|
||||
cout << "\nThere is no path from " << u << " to " << v;
|
||||
int temp;
|
||||
temp = u;
|
||||
u = v;
|
||||
v = temp;
|
||||
if (g.isReachable(u, v))
|
||||
cout << "\nThere is a path from " << u << " to " << v;
|
||||
else
|
||||
cout << "\nThere is no path from " << u << " to " << v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the source and destination vertices: (0-3)
|
||||
1 3
|
||||
|
||||
There is a path from 1 to 3
|
||||
There is no path from 3 to 1
|
||||
|
||||
Enter the source and destination vertices: (0-3)
|
||||
2 3
|
||||
|
||||
There is a path from 2 to 3
|
||||
There is no path from 3 to 2
|
@ -1,57 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int gcd(int x, int y)
|
||||
{
|
||||
int r = 0, a, b;
|
||||
a = (x > y) ? x : y; // a is greater number
|
||||
b = (x < y) ? x : y; // b is smaller number
|
||||
r = b;
|
||||
while (a % b != 0)
|
||||
{
|
||||
r = a % b;
|
||||
a = b;
|
||||
b = r;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int lcm(int x, int y)
|
||||
{
|
||||
int a;
|
||||
a = (x > y) ? x : y; // a is greater number
|
||||
while (true)
|
||||
{
|
||||
if (a % x == 0 && a % y == 0)
|
||||
return a;
|
||||
++a;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the two numbers: ";
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
cout << "The GCD of two numbers is: " << gcd(x, y) << endl;
|
||||
;
|
||||
cout << "The LCM of two numbers is: " << lcm(x, y) << endl;
|
||||
;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the two numbers:
|
||||
5
|
||||
8
|
||||
The GCD of two numbers is: 1
|
||||
The LCM of two numbers is: 40
|
||||
|
||||
Enter the two numbers:
|
||||
100
|
||||
50
|
||||
The GCD of two numbers is: 50
|
||||
The LCM of two numbers is: 100
|
@ -1,179 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void permute(int *a, int k, int size)
|
||||
{
|
||||
if (k == size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
cout << *(a + i);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = k; i < size; i++)
|
||||
{
|
||||
int temp = a[k];
|
||||
a[k] = a[i];
|
||||
a[i] = temp;
|
||||
permute(a, k + 1, size);
|
||||
temp = a[k];
|
||||
a[k] = a[i];
|
||||
a[i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the length of the password: ";
|
||||
int m;
|
||||
cin >> m;
|
||||
int a[m];
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
/*generates random number between 1 and 10*/
|
||||
a[i] = rand() % 10;
|
||||
}
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
cout << a[i] << ", ";
|
||||
}
|
||||
cout << "The Passwords are: ";
|
||||
permute(a, 0, m);
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the length of the password: 3
|
||||
1, 7, 4, The Passwords are: 174
|
||||
147
|
||||
714
|
||||
741
|
||||
471
|
||||
417
|
||||
|
||||
Enter the length of the password: 5
|
||||
1, 7, 4, 0, 9, The Passwords are: 17409
|
||||
17490
|
||||
17049
|
||||
17094
|
||||
17904
|
||||
17940
|
||||
14709
|
||||
14790
|
||||
14079
|
||||
14097
|
||||
14907
|
||||
14970
|
||||
10479
|
||||
10497
|
||||
10749
|
||||
10794
|
||||
10974
|
||||
10947
|
||||
19407
|
||||
19470
|
||||
19047
|
||||
19074
|
||||
19704
|
||||
19740
|
||||
71409
|
||||
71490
|
||||
71049
|
||||
71094
|
||||
71904
|
||||
71940
|
||||
74109
|
||||
74190
|
||||
74019
|
||||
74091
|
||||
74901
|
||||
74910
|
||||
70419
|
||||
70491
|
||||
70149
|
||||
70194
|
||||
70914
|
||||
70941
|
||||
79401
|
||||
79410
|
||||
79041
|
||||
79014
|
||||
79104
|
||||
79140
|
||||
47109
|
||||
47190
|
||||
47019
|
||||
47091
|
||||
47901
|
||||
47910
|
||||
41709
|
||||
41790
|
||||
41079
|
||||
41097
|
||||
41907
|
||||
41970
|
||||
40179
|
||||
40197
|
||||
40719
|
||||
40791
|
||||
40971
|
||||
40917
|
||||
49107
|
||||
49170
|
||||
49017
|
||||
49071
|
||||
49701
|
||||
49710
|
||||
07419
|
||||
07491
|
||||
07149
|
||||
07194
|
||||
07914
|
||||
07941
|
||||
04719
|
||||
04791
|
||||
04179
|
||||
04197
|
||||
04917
|
||||
04971
|
||||
01479
|
||||
01497
|
||||
01749
|
||||
01794
|
||||
01974
|
||||
01947
|
||||
09417
|
||||
09471
|
||||
09147
|
||||
09174
|
||||
09714
|
||||
09741
|
||||
97401
|
||||
97410
|
||||
97041
|
||||
97014
|
||||
97104
|
||||
97140
|
||||
94701
|
||||
94710
|
||||
94071
|
||||
94017
|
||||
94107
|
||||
94170
|
||||
90471
|
||||
90417
|
||||
90741
|
||||
90714
|
||||
90174
|
||||
90147
|
||||
91407
|
||||
91470
|
||||
91047
|
||||
91074
|
||||
91704
|
||||
91740
|
@ -1,72 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Welcome to the Sieve of Sundaram\n" << endl;
|
||||
int arraySize;
|
||||
int numberPrimes = 0;
|
||||
cout << "Input a positive integer to find all the prime numbers up to and "
|
||||
<< "\nincluding that number: ";
|
||||
cin >> arraySize;
|
||||
int n = arraySize / 2;
|
||||
/* array to start off with that will eventually get
|
||||
all the composite numbers removed and the remaining
|
||||
ones output to the screen */
|
||||
int isPrime[arraySize + 1];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
isPrime[i] = i;
|
||||
}
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
for (int j = i; j <= (n - i) / (2 * i + 1); j++)
|
||||
{
|
||||
isPrime[i + j + 2 * i * j] = 0;/*From this list, remove all
|
||||
numbers of the form i + j + 2ij */
|
||||
}
|
||||
}
|
||||
int TheseArePrime = 0;
|
||||
if (arraySize > 2)
|
||||
{
|
||||
isPrime[TheseArePrime++] = 2;/*this IF statement adds 2 to the output */
|
||||
}
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
if (isPrime[i] != 0)
|
||||
{
|
||||
isPrime[TheseArePrime++] = i * 2 + 1;
|
||||
}
|
||||
}
|
||||
int size = sizeof isPrime / sizeof(int);//total size of array/size of array data type
|
||||
for (int x = 0; x <= size; x++)
|
||||
{
|
||||
if (isPrime[x] != 0)
|
||||
{
|
||||
cout << isPrime[x] << "\t";//outputs all prime numbers found
|
||||
numberPrimes++;// the counter of the number of primes found
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << "\nNumber of Primes: " << numberPrimes << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Welcome to the Sieve of Sundaram
|
||||
|
||||
Input a positive integer to find all the prime numbers up to and
|
||||
including that number: 10
|
||||
2 3 5 7
|
||||
Number of Primes: 4
|
||||
|
||||
Welcome to the Sieve of Sundaram
|
||||
|
||||
Input a positive integer to find all the prime numbers up to and
|
||||
including that number: 100
|
||||
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
|
||||
Number of Primes: 25
|
@ -1,18 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int val = rand();
|
||||
char Hex[33];
|
||||
itoa(val, Hex, 16);
|
||||
cout<< "Random Decimal Byte:" << val;
|
||||
cout << "\nEquivalent Hex Byte: " << Hex;
|
||||
}
|
||||
|
||||
/*
|
||||
Random Decimal Byte:41
|
||||
Equivalent Hex Byte: 29
|
@ -1,45 +0,0 @@
|
||||
/*This is a C++ Program to generate random numbers using Middle Square method. In mathematics, the middle-square method is a method of generating pseudorandom numbers. In practice it is not a good method, since its period is usually very short and it has some severe weaknesses, such as the output sequence almost always converging to zero.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int a[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000 };
|
||||
int middleSquareNumber(int numb, int dig)
|
||||
{
|
||||
int sqn = numb * numb, next_num = 0;
|
||||
int trim = (dig / 2);
|
||||
sqn = sqn / a[trim];
|
||||
for (int i = 0; i < dig; i++)
|
||||
{
|
||||
next_num += (sqn % (a[trim])) * (a[i]);
|
||||
sqn = sqn / 10;
|
||||
}
|
||||
return next_num;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the #-digit random numbers you want: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
int start = 1, end = 1;
|
||||
start = a[n - 1];
|
||||
end = a[n];
|
||||
int number = ((rand()) % (end - start)) + start;
|
||||
cout << "The random numbers are:\n" << number << ", ";
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
number = middleSquareNumber(number, n);
|
||||
cout << number << ", ";
|
||||
}
|
||||
cout << "...";
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter the #-digit random numbers you want: 5
|
||||
The random numbers are:
|
||||
10041, 16426, 796264, -276041, -115546, ...
|
@ -1,31 +0,0 @@
|
||||
/*This is a C++ Program to generate random numbers using Multiply with Carry method. In computer science, multiply-with-carry (MWC) is a method invented by George Marsaglia for generating sequences of random integers based on an initial set from two to many thousands of randomly chosen seed values. The main advantages of the MWC method are that it invokes simple computer integer arithmetic and leads to very fast generation of sequences of random numbers with immense periods, ranging from around 260 to 22000000.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int max_Sequence_Elements = 10;
|
||||
int base_b = 2000;
|
||||
int multiplier_a = rand() % base_b;
|
||||
int r = 1;
|
||||
int c[max_Sequence_Elements];
|
||||
int x[max_Sequence_Elements];
|
||||
c[0] = rand() % multiplier_a;
|
||||
x[0] = rand() % base_b;
|
||||
cout << "The random number sequence is: " << x[0];
|
||||
//generating sequence
|
||||
for (int i = 1; i < max_Sequence_Elements; i++)
|
||||
{
|
||||
x[i] = (multiplier_a * x[i - r] + c[i - 1]) % base_b;
|
||||
c[i] = (multiplier_a * x[i - r] + c[i - 1]) / base_b;
|
||||
cout << " " << x[i];
|
||||
}
|
||||
cout << "...";
|
||||
}
|
||||
|
||||
/*
|
||||
The random number sequence is: 334 1711 157 472 1355 1564 151 223 1146 990...
|
@ -1,34 +0,0 @@
|
||||
/*This is a C++ Program to generate random numbers using Probability Distribution Function. Probability distribution is based on probability density function. a probability density function (pdf), or density of a continuous random variable, is a function that describes the relative likelihood for this random variable to take on a given value. The probability of the random variable falling within a particular range of values is given by the integral of this variable’s density over that range—that is, it is given by the area under the density function but above the horizontal axis and between the lowest and greatest values of the range.*/
|
||||
|
||||
//pdf(x) = 1 if x>360
|
||||
// = 0 if x<0
|
||||
// = x/360 otherwise
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//This is a sample program to generate a random numbers based on probability desity function of spiner
|
||||
//pdf(x) = 1 if x>360
|
||||
// = 0 if x<0
|
||||
// = x/360 otherwise
|
||||
int N = 10;
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int p = 0;
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
p = rand() % 400;
|
||||
if (p > 360)
|
||||
cout << 0 << " ";
|
||||
else if (p < 0)
|
||||
cout << 0 << " ";
|
||||
else
|
||||
cout << p * 0.1 / 360 << " ";
|
||||
}
|
||||
cout << "...";
|
||||
}
|
||||
|
||||
/*
|
||||
0.0113889 0.0186111 0.0927778 0.0277778 0 0.0344444 0.0772222 0.0438889 0.045 0.0177778 ...
|
@ -1,26 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <time.h>
|
||||
|
||||
const int LOW = 1;
|
||||
const int HIGH = 32000;
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int randomNumber;
|
||||
time_t seconds;
|
||||
time(&seconds);
|
||||
srand((unsigned int) seconds);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
randomNumber = rand() % (HIGH - LOW + 1) + LOW;
|
||||
cout << randomNumber << " ";
|
||||
}
|
||||
cout << "...";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
312 7423 23444 16008 31816 1823 29315 17424 11753 18384 ...
|
@ -1,141 +0,0 @@
|
||||
/*This is a C++ Program to multiply two signed numbers using booth’s algorithm. Booth’s multiplication algorithm is a multiplication algorithm that multiplies two signed binary numbers in two’s complement notation. Booth used desk calculators that were faster at shifting than adding and created the algorithm to increase their speed. Booth’s algorithm is of interest in the study of computer architecture.*/
|
||||
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void add(int a[], int x[], int qrn);
|
||||
void complement(int a[], int n)
|
||||
{
|
||||
int i;
|
||||
int x[8] = { NULL };
|
||||
x[0] = 1;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
a[i] = (a[i] + 1) % 2;
|
||||
}
|
||||
add(a, x, n);
|
||||
}
|
||||
|
||||
void add(int ac[], int x[], int qrn)
|
||||
{
|
||||
int i, c = 0;
|
||||
for (i = 0; i < qrn; i++)
|
||||
{
|
||||
ac[i] = ac[i] + x[i] + c;
|
||||
if (ac[i] > 1)
|
||||
{
|
||||
ac[i] = ac[i] % 2;
|
||||
c = 1;
|
||||
}
|
||||
else
|
||||
c = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void ashr(int ac[], int qr[], int &qn, int qrn)
|
||||
{
|
||||
int temp, i;
|
||||
temp = ac[0];
|
||||
qn = qr[0];
|
||||
cout << "\t\tashr\t\t";
|
||||
for (i = 0; i < qrn - 1; i++)
|
||||
{
|
||||
ac[i] = ac[i + 1];
|
||||
qr[i] = qr[i + 1];
|
||||
}
|
||||
qr[qrn - 1] = temp;
|
||||
}
|
||||
|
||||
void display(int ac[], int qr[], int qrn)
|
||||
{
|
||||
int i;
|
||||
for (i = qrn - 1; i >= 0; i--)
|
||||
cout << ac[i];
|
||||
cout << " ";
|
||||
for (i = qrn - 1; i >= 0; i--)
|
||||
cout << qr[i];
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int mt[10], br[10], qr[10], sc, ac[10] = { 0 };
|
||||
int brn, qrn, i, qn, temp;
|
||||
cout
|
||||
<< "\n--Enter the multiplicand and multipier in signed 2's complement form if negative--";
|
||||
cout << "\n Number of multiplicand bit=";
|
||||
cin >> brn;
|
||||
cout << "\nmultiplicand=";
|
||||
for (i = brn - 1; i >= 0; i--)
|
||||
cin >> br[i]; //multiplicand
|
||||
for (i = brn - 1; i >= 0; i--)
|
||||
mt[i] = br[i]; // copy multipier to temp array mt[]
|
||||
complement(mt, brn);
|
||||
cout << "\nNo. of multiplier bit=";
|
||||
cin >> qrn;
|
||||
sc = qrn; //sequence counter
|
||||
cout << "Multiplier=";
|
||||
for (i = qrn - 1; i >= 0; i--)
|
||||
cin >> qr[i]; //multiplier
|
||||
qn = 0;
|
||||
temp = 0;
|
||||
cout << "qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";
|
||||
cout << "\t\t\tinitial\t\t";
|
||||
display(ac, qr, qrn);
|
||||
cout << "\t\t" << sc << "\n";
|
||||
while (sc != 0)
|
||||
{
|
||||
cout << qr[0] << "\t" << qn;
|
||||
if ((qn + qr[0]) == 1)
|
||||
{
|
||||
if (temp == 0)
|
||||
{
|
||||
add(ac, mt, qrn);
|
||||
cout << "\t\tsubtracting BR\t";
|
||||
for (i = qrn - 1; i >= 0; i--)
|
||||
cout << ac[i];
|
||||
temp = 1;
|
||||
}
|
||||
else if (temp == 1)
|
||||
{
|
||||
add(ac, br, qrn);
|
||||
cout << "\t\tadding BR\t";
|
||||
for (i = qrn - 1; i >= 0; i--)
|
||||
cout << ac[i];
|
||||
temp = 0;
|
||||
}
|
||||
cout << "\n\t";
|
||||
ashr(ac, qr, qn, qrn);
|
||||
}
|
||||
else if (qn - qr[0] == 0)
|
||||
ashr(ac, qr, qn, qrn);
|
||||
display(ac, qr, qrn);
|
||||
cout << "\t";
|
||||
sc--;
|
||||
cout << "\t" << sc << "\n";
|
||||
}
|
||||
cout << "Result=";
|
||||
display(ac, qr, qrn);
|
||||
}
|
||||
|
||||
/*
|
||||
--Enter the multiplicand and multipier in signed 2's complement form if negative--
|
||||
Number of multiplicand bit=5
|
||||
Multiplicand=1 0 1 1 1
|
||||
|
||||
Number of multiplier bit=5
|
||||
Multiplier=1 0 0 1 1
|
||||
|
||||
qn q[n+1] BR AC QR sc
|
||||
initial 00000 10011 5
|
||||
1 0 subtracting BR 01001
|
||||
ashr 00100 11001 4
|
||||
1 1 ashr 00010 01100 3
|
||||
0 1 adding BR 11001
|
||||
ashr 11100 10110 2
|
||||
0 0 ashr 11110 01011 1
|
||||
1 0 subtracting BR 00111
|
||||
ashr 00011 10101 0
|
||||
|
||||
Result=00011 10101
|
@ -1,129 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the dimension of the matrices: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << "Enter the 1st matrix: ";
|
||||
double a[n][n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
cout << "Enter the 2nd matrix: ";
|
||||
double b[n][n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> b[i][j];
|
||||
}
|
||||
}
|
||||
cout << "Enter the result matrix: ";
|
||||
double c[n][n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> c[i][j];
|
||||
}
|
||||
}
|
||||
//random generation of the r vector containing only 0/1 as its elements
|
||||
double r[n][1];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
r[i][0] = rand() % 2;
|
||||
cout << r[i][0] << " ";
|
||||
}
|
||||
//test A * (b*r) - (C*) = 0
|
||||
double br[n][1];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < 1; j++)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
br[i][j] = br[i][j] + b[i][k] * r[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double cr[n][1];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < 1; j++)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
cr[i][j] = cr[i][j] + c[i][k] * r[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double abr[n][1];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < 1; j++)
|
||||
{
|
||||
for (int k = 0; k < n; k++)
|
||||
{
|
||||
abr[i][j] = abr[i][j] + a[i][k] * br[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
// br = multiplyVector(b, r, n);
|
||||
// cr = multiplyVector(c, r, n);
|
||||
// abr = multiplyVector(a, br, n);
|
||||
//abr-cr
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
abr[i][0] -= cr[i][0];
|
||||
}
|
||||
bool flag = true;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (abr[i][0] == 0)
|
||||
continue;
|
||||
else
|
||||
flag = false;
|
||||
}
|
||||
if (flag == true)
|
||||
cout << "Yes";
|
||||
else
|
||||
cout << "No";
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter the dimension of the matrices: 2
|
||||
Enter the 1st matrix:
|
||||
1 2
|
||||
2 3
|
||||
Enter the 2nd matrix:
|
||||
1 3
|
||||
3 4
|
||||
Enter the result matrix:
|
||||
9 9
|
||||
14 15
|
||||
|
||||
Yes
|
||||
|
||||
Enter the dimesion of the matrices:
|
||||
2
|
||||
Enter the 1st matrix:
|
||||
2 3
|
||||
3 4
|
||||
Enter the 2st matrix:
|
||||
1 0
|
||||
1 2
|
||||
Enter the result matrix:
|
||||
6 5
|
||||
8 7
|
||||
|
||||
Yes
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Euler Theorem
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
vector<int> inverseArray(int n, int m)
|
||||
{
|
||||
vector<int> modInverse(n + 1, 0);
|
||||
modInverse[1] = 1;
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
modInverse[i] = (-(m / i) * modInverse[m % i]) % m + m;
|
||||
}
|
||||
return modInverse;
|
||||
}
|
||||
//Main
|
||||
int main()
|
||||
{
|
||||
vector<int>::iterator it;
|
||||
int a, m;
|
||||
cout<<"Enter number to find modular multiplicative inverse: ";
|
||||
cin>>a;
|
||||
cout<<"Enter Modular Value: ";
|
||||
cin>>m;
|
||||
cout<<inverseArray(a, m)[a]<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter number to find modular multiplicative inverse: 5
|
||||
Enter Modular Value: 7
|
||||
3
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Extended Eucledian Algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
using namespace std;
|
||||
/* return the gcd of a and b followed by the pair x and y of
|
||||
equation ax + by = gcd(a,b)
|
||||
*/
|
||||
pair<int, pair<int, int> > extendedEuclid(int a, int b)
|
||||
{
|
||||
int x = 1, y = 0;
|
||||
int xLast = 0, yLast = 1;
|
||||
int q, r, m, n;
|
||||
while (a != 0)
|
||||
{
|
||||
q = b / a;
|
||||
r = b % a;
|
||||
m = xLast - q * x;
|
||||
n = yLast - q * y;
|
||||
xLast = x;
|
||||
yLast = y;
|
||||
x = m;
|
||||
y = n;
|
||||
b = a;
|
||||
a = r;
|
||||
}
|
||||
return make_pair(b, make_pair(xLast, yLast));
|
||||
}
|
||||
|
||||
int modInverse(int a, int m)
|
||||
{
|
||||
return (extendedEuclid(a, m).second.first + m) % m;
|
||||
}
|
||||
|
||||
//Main
|
||||
int main()
|
||||
{
|
||||
int a, m;
|
||||
cout<<"Enter number to find modular multiplicative inverse: ";
|
||||
cin>>a;
|
||||
cout<<"Enter Modular Value: ";
|
||||
cin>>m;
|
||||
cout<<modInverse(a, m)<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter number to find modular multiplicative inverse: 133
|
||||
Enter Modular Value: 135
|
||||
67
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Fermat Primality Test
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
/*
|
||||
* modular exponentiation
|
||||
*/
|
||||
ll modulo(ll base, ll exponent, ll mod)
|
||||
{
|
||||
ll x = 1;
|
||||
ll y = base;
|
||||
while (exponent > 0)
|
||||
{
|
||||
if (exponent % 2 == 1)
|
||||
x = (x * y) % mod;
|
||||
y = (y * y) % mod;
|
||||
exponent = exponent / 2;
|
||||
}
|
||||
return x % mod;
|
||||
}
|
||||
|
||||
/*
|
||||
* Fermat's test for checking primality
|
||||
*/
|
||||
bool Fermat(ll p, int iterations)
|
||||
{
|
||||
if (p == 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
ll a = rand() % (p - 1) + 1;
|
||||
if (modulo(a, p - 1, p) != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int iteration = 50;
|
||||
ll num;
|
||||
cout<<"Enter integer to test primality: ";
|
||||
cin>>num;
|
||||
if (Fermat(num, iteration))
|
||||
cout<<num<<" is prime"<<endl;
|
||||
else
|
||||
cout<<num<<" is not prime"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter integer to test primality: 479001599
|
||||
479001599 is prime
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Fermat's Little Theorem
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/* calculates (a^b)%MOD */
|
||||
int pow(int a, int b, int MOD)
|
||||
{
|
||||
int x = 1, y = a;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2 == 1)
|
||||
{
|
||||
x = (x * y);
|
||||
if (x > MOD)
|
||||
x %= MOD;
|
||||
}
|
||||
y = (y * y);
|
||||
if (y > MOD)
|
||||
y %= MOD;
|
||||
b /= 2;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int modInverse(int a, int m)
|
||||
{
|
||||
return pow(a, m - 2, m);
|
||||
}
|
||||
//Main
|
||||
int main()
|
||||
{
|
||||
int a, m;
|
||||
cout<<"Enter number to find modular multiplicative inverse: ";
|
||||
cin>>a;
|
||||
cout<<"Enter Modular Value: ";
|
||||
cin>>m;
|
||||
cout<<modInverse(a, m)<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter number to find modular multiplicative inverse: 1111
|
||||
Enter Modular Value: 331
|
||||
216
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,48 +0,0 @@
|
||||
/*This is a C++ Program to shuffle array using Fisher-Yates algorithm. The Fisher–Yates shuffle (named after Ronald Fisher and Frank Yates), also known as the Knuth shuffle (after Donald Knuth), is an algorithm for generating a random permutation of a finite set—in plain terms, for randomly shuffling the set. A variant of the Fisher–Yates shuffle, known as Sattolo’s algorithm, may be used to generate random cycles of length n instead. The Fisher–Yates shuffle is unbiased, so that every permutation is equally likely. The modern version of the algorithm is also rather efficient, requiring only time proportional to the number of items being shuffled and no additional storage space.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void fisherYatesShuffling(int *arr, int n)
|
||||
{
|
||||
int a[n];
|
||||
int ind[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
ind[i] = 0;
|
||||
int index;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
do
|
||||
{
|
||||
index = rand() % n;
|
||||
}
|
||||
while (ind[index] != 0);
|
||||
ind[index] = 1;
|
||||
a[i] = *(arr + index);
|
||||
}
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the array size: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << "Enter the array elements: ";
|
||||
int a[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cin >> a[i];
|
||||
}
|
||||
fisherYatesShuffling(a, n);
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the array size: 7
|
||||
Enter the array elements: 12 23 34 45 56 67 78
|
||||
78 23 67 45 34 12 56
|
@ -1,80 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
float a[10][10], b[10], x[10], y[10];
|
||||
int n = 0, m = 0, i = 0, j = 0;
|
||||
cout << "Enter size of 2d array(Square matrix) : ";
|
||||
cin >> n;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
cout << "Enter values no :(" << i << ", " << j << ") ";
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
cout << "\nEnter Values to the right side of equation\n";
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cout << "Enter values no :(" << i << ", " << j << ") ";
|
||||
cin >> b[i];
|
||||
}
|
||||
cout << "Enter initial values of x\n";
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cout << "Enter values no. :(" << i<<"):";
|
||||
cin >> x[i];
|
||||
}
|
||||
cout << "\nEnter the no. of iteration : ";
|
||||
cin >> m;
|
||||
while (m > 0)
|
||||
{
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
y[i] = (b[i] / a[i][i]);
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
if (j == i)
|
||||
continue;
|
||||
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
|
||||
x[i] = y[i];
|
||||
}
|
||||
printf("x%d = %f ", i + 1, y[i]);
|
||||
}
|
||||
cout << "\n";
|
||||
m--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter size of 2d array(Square matrix) : 3
|
||||
Enter values no :(0, 0) 2
|
||||
Enter values no :(0, 1) 3
|
||||
Enter values no :(0, 2) 1
|
||||
Enter values no :(1, 0) 5
|
||||
Enter values no :(1, 1) 4
|
||||
Enter values no :(1, 2) 6
|
||||
Enter values no :(2, 0) 8
|
||||
Enter values no :(2, 1) 7
|
||||
Enter values no :(2, 2) 9
|
||||
|
||||
Enter Values to the right side of equation
|
||||
Enter values no :(0, 3) 2
|
||||
Enter values no :(1, 3) 3
|
||||
Enter values no :(2, 3) 4
|
||||
|
||||
Enter initial values of x
|
||||
Enter values no. :(0): 0
|
||||
Enter values no. :(1): 0
|
||||
Enter values no. :(2): 0
|
||||
|
||||
Enter the no. of iteration : 4
|
||||
x1 = 1.000000 x2 = -0.500000 x3 = -0.055556
|
||||
x1 = 1.777778 x2 = -1.388889 x3 = -0.055556
|
||||
x1 = 3.111111 x2 = -3.055555 x3 = 0.055555
|
||||
x1 = 5.555555 x2 = -6.277777 x3 = 0.388889
|
@ -1,98 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Miller Rabin Primality Test
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* calculates (a * b) % c taking into account that a * b might overflow
|
||||
*/
|
||||
ll mulmod(ll a, ll b, ll mod)
|
||||
{
|
||||
ll x = 0,y = a % mod;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2 == 1)
|
||||
{
|
||||
x = (x + y) % mod;
|
||||
}
|
||||
y = (y * 2) % mod;
|
||||
b /= 2;
|
||||
}
|
||||
return x % mod;
|
||||
}
|
||||
/*
|
||||
* modular exponentiation
|
||||
*/
|
||||
ll modulo(ll base, ll exponent, ll mod)
|
||||
{
|
||||
ll x = 1;
|
||||
ll y = base;
|
||||
while (exponent > 0)
|
||||
{
|
||||
if (exponent % 2 == 1)
|
||||
x = (x * y) % mod;
|
||||
y = (y * y) % mod;
|
||||
exponent = exponent / 2;
|
||||
}
|
||||
return x % mod;
|
||||
}
|
||||
|
||||
/*
|
||||
* Miller-Rabin primality test, iteration signifies the accuracy
|
||||
*/
|
||||
bool Miller(ll p,int iteration)
|
||||
{
|
||||
if (p < 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (p != 2 && p % 2==0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
ll s = p - 1;
|
||||
while (s % 2 == 0)
|
||||
{
|
||||
s /= 2;
|
||||
}
|
||||
for (int i = 0; i < iteration; i++)
|
||||
{
|
||||
ll a = rand() % (p - 1) + 1, temp = s;
|
||||
ll mod = modulo(a, temp, p);
|
||||
while (temp != p - 1 && mod != 1 && mod != p - 1)
|
||||
{
|
||||
mod = mulmod(mod, mod, p);
|
||||
temp *= 2;
|
||||
}
|
||||
if (mod != p - 1 && temp % 2 == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//Main
|
||||
int main()
|
||||
{
|
||||
int iteration = 5;
|
||||
ll num;
|
||||
cout<<"Enter integer to test primality: ";
|
||||
cin>>num;
|
||||
if (Miller(num, iteration))
|
||||
cout<<num<<" is prime"<<endl;
|
||||
else
|
||||
cout<<num<<" is not prime"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter integer to test primality: 127
|
||||
127 is prime
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Modular Exponentiation Algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Function to calculate modulus of x raised to the power y
|
||||
*/
|
||||
ll modular_pow(ll base, ll exponent, int modulus)
|
||||
{
|
||||
ll result = 1;
|
||||
while (exponent > 0)
|
||||
{
|
||||
if (exponent % 2 == 1)
|
||||
result = (result * base) % modulus;
|
||||
exponent = exponent >> 1;
|
||||
base = (base * base) % modulus;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
ll x, y;
|
||||
int mod;
|
||||
cout<<"Enter Base Value: ";
|
||||
cin>>x;
|
||||
cout<<"Enter Exponent: ";
|
||||
cin>>y;
|
||||
cout<<"Enter Modular Value: ";
|
||||
cin>>mod;
|
||||
cout<<modular_pow(x, y, mod);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter Base Value: 2
|
||||
Enter Exponent: 5
|
||||
Enter Modular Value: 23
|
||||
9
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,35 +0,0 @@
|
||||
/*This is a C++ Program to genrate random numbers using Naor-Reingold random function. Moni Naor and Omer Reingold described efficient constructions for various cryptographic primitives in private key as well as public-key cryptography. Their result is the construction of an efficient pseudorandom function. Let p and l be prime numbers with l |p-1. Select an element g ? {\mathbb F_p}^* of multiplicative order l. Then for each n-dimensional vector a = (a1, …, an)? (\mathbb F_{l})^{n} they define the function
|
||||
f_{a}(x) = g^{a_{1}^{x_{1}} a_{2}^{x_{2}}…a_{n}^{x_{n}}} \in \mathbb F_p
|
||||
|
||||
where x = x1 … xn is the bit representation of integer x, 0 = x = 2^n-1, with some extra leading zeros if necessary.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int p = 7, l = 3, g = 2, n = 4, x;
|
||||
int a[] = { 1, 2, 2, 1 };
|
||||
int bin[4];
|
||||
cout << "The Random numbers are: ";
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
x = rand() % 16;
|
||||
for (int j = 3; j >= 0; j--)
|
||||
{
|
||||
bin[j] = x % 2;
|
||||
x /= 2;
|
||||
}
|
||||
int mul = 1;
|
||||
for (int k = 0; k < 4; k++)
|
||||
mul *= pow(a[k], bin[k]);
|
||||
cout << pow(g, mul)<<" ";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The Random numbers are:
|
||||
2 4 16 4 2 4 16 16 4 2
|
@ -1,51 +0,0 @@
|
||||
/*This is a C++ Program to generate random numbers using Park-Miller algorithm. A general formula of a random number generator (RNG) of this type is:
|
||||
X_{k+1} = g X(k) mod n
|
||||
where the modulus n is a prime number or a power of a prime number, the multiplier g is an element of high multiplicative order modulo n (e.g., a primitive root modulo n), and the seed X0 is co-prime to n.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const long m = 2147483647L;
|
||||
const long a = 48271L;
|
||||
const long q = 44488L;
|
||||
const long r = 3399L;
|
||||
|
||||
static long r_seed = 12345678L;
|
||||
|
||||
double uniform()
|
||||
{
|
||||
long hi = r_seed / q;
|
||||
long lo = r_seed - q * hi;
|
||||
long t = a * lo - r * hi;
|
||||
if (t > 0)
|
||||
r_seed = t;
|
||||
else
|
||||
r_seed = t + m;
|
||||
return r_seed;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
double A[10];
|
||||
for (int i = 0; i < 10; i++)
|
||||
A[i] = uniform();
|
||||
cout<<"Random numbers are:\n";
|
||||
for (int i = 0; i < 10; i++)
|
||||
cout << A[i]<<endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Random numbers are:
|
||||
1.08525e+009
|
||||
5.0826e+008
|
||||
1.35229e+009
|
||||
1.56324e+009
|
||||
8.90733e+008
|
||||
1.81003e+009
|
||||
1.50959e+009
|
||||
8.62973e+008
|
||||
1.85299e+009
|
||||
6.77684e+008
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Russian Peasant Multiplication
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
* multiply two numbers using Russian Peasant method
|
||||
*/
|
||||
unsigned int russianPeasant(unsigned int a, unsigned int b)
|
||||
{
|
||||
int res = 0;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b & 1)
|
||||
res = res + a;
|
||||
a = a << 1;
|
||||
b = b >> 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
cout << russianPeasant(15, 5) << endl;
|
||||
cout << russianPeasant(13, 6) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
75
|
||||
78
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Segmented Sieve
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#define MAX 46656
|
||||
#define LMT 216
|
||||
#define LEN 4830
|
||||
#define RNG 100032
|
||||
#define sq(x) ((x)*(x))
|
||||
#define mset(x,v) memset(x, v , sizeof(x))
|
||||
#define chkC(x,n) (x[n >> 6] & (1 << ((n >> 1) & 31)))
|
||||
#define setC(x,n) (x[n >> 6] |= (1 << ((n >> 1) & 31)))
|
||||
using namespace std;
|
||||
unsigned base[MAX/64], segment[RNG/64], primes[LEN];
|
||||
|
||||
/*
|
||||
* Generates all the necessary prime numbers and marks them in base[]
|
||||
*/
|
||||
void sieve()
|
||||
{
|
||||
unsigned i, j, k;
|
||||
for (i = 3; i < LMT; i += 2)
|
||||
{
|
||||
if (!chkC(base, i))
|
||||
{
|
||||
for (j = i * i, k = i << 1; j < MAX; j += k)
|
||||
setC(base, j);
|
||||
}
|
||||
}
|
||||
for (i = 3, j = 0; i < MAX; i += 2)
|
||||
{
|
||||
if (!chkC(base, i))
|
||||
primes[j++] = i;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the prime-count within range [a,b] and marks them in segment[]
|
||||
*/
|
||||
int segmented_sieve(int a, int b)
|
||||
{
|
||||
unsigned i, j, k, cnt = (a <= 2 && 2 <=b )? 1 : 0;
|
||||
if (b < 2)
|
||||
return 0;
|
||||
if (a < 3)
|
||||
a = 3;
|
||||
if (a % 2 == 0)
|
||||
a++;
|
||||
mset (segment, 0);
|
||||
for (i = 0; sq(primes[i]) <= b; i++)
|
||||
{
|
||||
j = primes[i] * ((a + primes[i] - 1) / primes[i]);
|
||||
if (j % 2 == 0) j += primes[i];
|
||||
for (k = primes[i] << 1; j <= b; j += k)
|
||||
{
|
||||
if (j != primes[i])
|
||||
setC(segment, (j - a));
|
||||
}
|
||||
}
|
||||
for (i = 0; i <= b - a; i += 2)
|
||||
{
|
||||
if (!chkC(segment, i))
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
sieve();
|
||||
int a, b;
|
||||
cout<<"Enter Lower Bound: ";
|
||||
cin>>a;
|
||||
cout<<"Enter Upper Bound: ";
|
||||
cin>>b;
|
||||
cout<<"Number of primes between "<<a<<" and "<<b<<": ";
|
||||
cout<<segmented_sieve(a, b)<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter Lower Bound: 7
|
||||
Enter Upper Bound: 600
|
||||
Number of primes between 7 and 600: 106
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Sieve of Atkins
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#define ll long long
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Sieve of Atkins
|
||||
*/
|
||||
void sieve_atkins(ll int n)
|
||||
{
|
||||
vector<bool> is_prime(n + 1);
|
||||
is_prime[2] = true;
|
||||
is_prime[3] = true;
|
||||
for (ll int i = 5; i <= n; i++)
|
||||
{
|
||||
is_prime[i] = false;
|
||||
}
|
||||
ll int lim = ceil(sqrt(n));
|
||||
for (ll int x = 1; x <= lim; x++)
|
||||
{
|
||||
for (ll int y = 1; y <= lim; y++)
|
||||
{
|
||||
ll int num = (4 * x * x + y * y);
|
||||
if (num <= n && (num % 12 == 1 || num % 12 == 5))
|
||||
{
|
||||
is_prime[num] = true;
|
||||
}
|
||||
num = (3 * x * x + y * y);
|
||||
if (num <= n && (num % 12 == 7))
|
||||
{
|
||||
is_prime[num] = true;
|
||||
}
|
||||
if (x > y)
|
||||
{
|
||||
num = (3 * x * x - y * y);
|
||||
if (num <= n && (num % 12 == 11))
|
||||
{
|
||||
is_prime[num] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ll int i = 5; i <= lim; i++)
|
||||
{
|
||||
if (is_prime[i])
|
||||
{
|
||||
for (ll int j = i * i; j <= n; j += i)
|
||||
{
|
||||
is_prime[j] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ll int i = 2; i <= n; i++)
|
||||
{
|
||||
if (is_prime[i])
|
||||
{
|
||||
cout<<i<<"\t";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
ll int n;
|
||||
n = 300;
|
||||
cout<<"Following are the prime numbers below "<<n<<endl;
|
||||
sieve_atkins(n);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Following are the prime numbers below 300
|
||||
2 3 5 7 11 13 17 19 23 29
|
||||
31 37 41 43 47 53 59 61 67 71
|
||||
73 79 83 89 97 101 103 107 109 113
|
||||
127 131 137 139 149 151 157 163 167 173
|
||||
179 181 191 193 197 199 211 223 227 229
|
||||
233 239 241 251 257 263 269 271 277 281
|
||||
283 293
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,30 +0,0 @@
|
||||
/*This C++ program to implement Sieve of Eratosthenes. The program initializes an integer array with all the elements initialized to 0. Then the algorithm follows where the each non-prime element’s index is marked as 1 inside the nested loops. The prime numbers are those whose value of index is marked as 0.*/
|
||||
|
||||
/*
|
||||
* C++ Program to implement Sieve of Eratosthenes
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
const int len = 100;
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[100] = {0};
|
||||
for (int i = 2; i < 100; i++)
|
||||
{
|
||||
for (int j = i * i; j < 100; j+=i)
|
||||
{
|
||||
arr[j - 1] = 1;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i < 100; i++)
|
||||
{
|
||||
if (arr[i - 1] == 0)
|
||||
std::cout << i << "\t";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
1 2 3 5 7 11 13 17 19 23
|
||||
29 31 37 41 43 47 53 59 61 67
|
||||
71 73 79 83 89 97
|
@ -1,110 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Solovay-Strassen Primality Test
|
||||
*/
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#define ll long long
|
||||
using namespace std;
|
||||
/*
|
||||
* modular exponentiation
|
||||
*/
|
||||
ll modulo(ll base, ll exponent, ll mod)
|
||||
{
|
||||
ll x = 1;
|
||||
ll y = base;
|
||||
while (exponent > 0)
|
||||
{
|
||||
if (exponent % 2 == 1)
|
||||
x = (x * y) % mod;
|
||||
y = (y * y) % mod;
|
||||
exponent = exponent / 2;
|
||||
}
|
||||
return x % mod;
|
||||
}
|
||||
/*
|
||||
* calculates Jacobian(a/n) n>0 and n is odd
|
||||
*/
|
||||
int calculateJacobian(ll a,ll n)
|
||||
{
|
||||
if (!a)
|
||||
return 0;
|
||||
int ans = 1;
|
||||
ll temp;
|
||||
if (a < 0)
|
||||
{
|
||||
a = -a;
|
||||
if (n % 4 == 3)
|
||||
ans=-ans;
|
||||
}
|
||||
if (a == 1)
|
||||
return ans;
|
||||
while (a)
|
||||
{
|
||||
if (a < 0)
|
||||
{
|
||||
a = -a;
|
||||
if (n % 4 == 3)
|
||||
ans = -ans;
|
||||
}
|
||||
while (a % 2 == 0)
|
||||
{
|
||||
a = a / 2;
|
||||
if (n % 8 == 3 || n % 8 == 5)
|
||||
ans = -ans;
|
||||
}
|
||||
swap(a, n);
|
||||
if (a % 4 == 3 && n % 4 == 3)
|
||||
ans = -ans;
|
||||
a = a % n;
|
||||
if (a > n / 2)
|
||||
a = a - n;
|
||||
}
|
||||
if (n == 1)
|
||||
return ans;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Solovay-Strassen Primality Test
|
||||
* Iterations determine the accuracy of the test
|
||||
*/
|
||||
bool Solovoy(ll p, int iteration)
|
||||
{
|
||||
if (p < 2)
|
||||
return false;
|
||||
if (p != 2 && p % 2 == 0)
|
||||
return false;
|
||||
for (int i = 0; i < iteration; i++)
|
||||
{
|
||||
ll a = rand() % (p - 1) + 1;
|
||||
ll jacobian = (p + calculateJacobian(a, p)) % p;
|
||||
ll mod = modulo(a, (p - 1) / 2, p);
|
||||
if (!jacobian || mod != jacobian)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//Main
|
||||
int main()
|
||||
{
|
||||
int iteration = 50;
|
||||
ll num;
|
||||
cout<<"Enter integr to test primality: ";
|
||||
cin>>num;
|
||||
if (Solovoy(num, iteration))
|
||||
cout<<num<<" is prime"<<endl;
|
||||
else
|
||||
cout<<num<<" is not prime"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter integr to test primality: 219891801103773
|
||||
219891801103773 is not prime
|
||||
|
||||
------------------
|
||||
(program exited with code: 1)
|
||||
Press return to continue
|
@ -1,229 +0,0 @@
|
||||
/*This is a C++ Program to implement Strassen’s algorithm for matrix multiplication. In the mathematical discipline of linear algebra, the Strassen algorithm, named after Volker Strassen, is an algorithm used for matrix multiplication. It is faster than the standard matrix multiplication algorithm and is useful in practice for large matrices, but would be slower than the fastest known algorithms for extremely large matrices.*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#define M 2
|
||||
#define N (1<<M)
|
||||
|
||||
typedef double datatype;
|
||||
#define DATATYPE_FORMAT "%4.2g"
|
||||
typedef datatype mat[N][N]; // mat[2**M,2**M] for divide and conquer mult.
|
||||
typedef struct
|
||||
{
|
||||
int ra, rb, ca, cb;
|
||||
} corners; // for tracking rows and columns.
|
||||
// A[ra..rb][ca..cb] .. the 4 corners of a matrix.
|
||||
|
||||
// set A[a] = I
|
||||
void identity(mat A, corners a)
|
||||
{
|
||||
int i, j;
|
||||
for (i = a.ra; i < a.rb; i++)
|
||||
for (j = a.ca; j < a.cb; j++)
|
||||
A[i][j] = (datatype) (i == j);
|
||||
}
|
||||
|
||||
// set A[a] = k
|
||||
void set(mat A, corners a, datatype k)
|
||||
{
|
||||
int i, j;
|
||||
for (i = a.ra; i < a.rb; i++)
|
||||
for (j = a.ca; j < a.cb; j++)
|
||||
A[i][j] = k;
|
||||
}
|
||||
|
||||
// set A[a] = [random(l..h)].
|
||||
void randk(mat A, corners a, double l, double h)
|
||||
{
|
||||
int i, j;
|
||||
for (i = a.ra; i < a.rb; i++)
|
||||
for (j = a.ca; j < a.cb; j++)
|
||||
A[i][j] = (datatype) (l + (h - l) * (rand() / (double) RAND_MAX));
|
||||
}
|
||||
|
||||
// Print A[a]
|
||||
void print(mat A, corners a, char *name)
|
||||
{
|
||||
int i, j;
|
||||
printf("%s = {\n", name);
|
||||
for (i = a.ra; i < a.rb; i++)
|
||||
{
|
||||
for (j = a.ca; j < a.cb; j++)
|
||||
printf(DATATYPE_FORMAT ", ", A[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
printf("}\n");
|
||||
}
|
||||
|
||||
// C[c] = A[a] + B[b]
|
||||
void add(mat A, mat B, mat C, corners a, corners b, corners c)
|
||||
{
|
||||
int rd = a.rb - a.ra;
|
||||
int cd = a.cb - a.ca;
|
||||
int i, j;
|
||||
for (i = 0; i < rd; i++)
|
||||
{
|
||||
for (j = 0; j < cd; j++)
|
||||
{
|
||||
C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] + B[i + b.ra][j
|
||||
+ b.ca];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// C[c] = A[a] - B[b]
|
||||
void sub(mat A, mat B, mat C, corners a, corners b, corners c)
|
||||
{
|
||||
int rd = a.rb - a.ra;
|
||||
int cd = a.cb - a.ca;
|
||||
int i, j;
|
||||
for (i = 0; i < rd; i++)
|
||||
{
|
||||
for (j = 0; j < cd; j++)
|
||||
{
|
||||
C[i + c.ra][j + c.ca] = A[i + a.ra][j + a.ca] - B[i + b.ra][j
|
||||
+ b.ca];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return 1/4 of the matrix: top/bottom , left/right.
|
||||
void find_corner(corners a, int i, int j, corners *b)
|
||||
{
|
||||
int rm = a.ra + (a.rb - a.ra) / 2;
|
||||
int cm = a.ca + (a.cb - a.ca) / 2;
|
||||
*b = a;
|
||||
if (i == 0)
|
||||
b->rb = rm; // top rows
|
||||
else
|
||||
b->ra = rm; // bot rows
|
||||
if (j == 0)
|
||||
b->cb = cm; // left cols
|
||||
else
|
||||
b->ca = cm; // right cols
|
||||
}
|
||||
|
||||
// Multiply: A[a] * B[b] => C[c], recursively.
|
||||
void mul(mat A, mat B, mat C, corners a, corners b, corners c)
|
||||
{
|
||||
corners aii[2][2], bii[2][2], cii[2][2], p;
|
||||
mat P[7], S, T;
|
||||
int i, j, m, n, k;
|
||||
// Check: A[m n] * B[n k] = C[m k]
|
||||
m = a.rb - a.ra;
|
||||
assert(m==(c.rb-c.ra));
|
||||
n = a.cb - a.ca;
|
||||
assert(n==(b.rb-b.ra));
|
||||
k = b.cb - b.ca;
|
||||
assert(k==(c.cb-c.ca));
|
||||
assert(m>0);
|
||||
if (n == 1)
|
||||
{
|
||||
C[c.ra][c.ca] += A[a.ra][a.ca] * B[b.ra][b.ca];
|
||||
return;
|
||||
}
|
||||
// Create the 12 smaller matrix indexes:
|
||||
// A00 A01 B00 B01 C00 C01
|
||||
// A10 A11 B10 B11 C10 C11
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
for (j = 0; j < 2; j++)
|
||||
{
|
||||
find_corner(a, i, j, &aii[i][j]);
|
||||
find_corner(b, i, j, &bii[i][j]);
|
||||
find_corner(c, i, j, &cii[i][j]);
|
||||
}
|
||||
}
|
||||
p.ra = p.ca = 0;
|
||||
p.rb = p.cb = m / 2;
|
||||
#define LEN(A) (sizeof(A)/sizeof(A[0]))
|
||||
for (i = 0; i < LEN(P); i++)
|
||||
set(P[i], p, 0);
|
||||
#define ST0 set(S,p,0); set(T,p,0)
|
||||
// (A00 + A11) * (B00+B11) = S * T = P0
|
||||
ST0;
|
||||
add(A, A, S, aii[0][0], aii[1][1], p);
|
||||
add(B, B, T, bii[0][0], bii[1][1], p);
|
||||
mul(S, T, P[0], p, p, p);
|
||||
// (A10 + A11) * B00 = S * B00 = P1
|
||||
ST0;
|
||||
add(A, A, S, aii[1][0], aii[1][1], p);
|
||||
mul(S, B, P[1], p, bii[0][0], p);
|
||||
// A00 * (B01 - B11) = A00 * T = P2
|
||||
ST0;
|
||||
sub(B, B, T, bii[0][1], bii[1][1], p);
|
||||
mul(A, T, P[2], aii[0][0], p, p);
|
||||
// A11 * (B10 - B00) = A11 * T = P3
|
||||
ST0;
|
||||
sub(B, B, T, bii[1][0], bii[0][0], p);
|
||||
mul(A, T, P[3], aii[1][1], p, p);
|
||||
// (A00 + A01) * B11 = S * B11 = P4
|
||||
ST0;
|
||||
add(A, A, S, aii[0][0], aii[0][1], p);
|
||||
mul(S, B, P[4], p, bii[1][1], p);
|
||||
// (A10 - A00) * (B00 + B01) = S * T = P5
|
||||
ST0;
|
||||
sub(A, A, S, aii[1][0], aii[0][0], p);
|
||||
add(B, B, T, bii[0][0], bii[0][1], p);
|
||||
mul(S, T, P[5], p, p, p);
|
||||
// (A01 - A11) * (B10 + B11) = S * T = P6
|
||||
ST0;
|
||||
sub(A, A, S, aii[0][1], aii[1][1], p);
|
||||
add(B, B, T, bii[1][0], bii[1][1], p);
|
||||
mul(S, T, P[6], p, p, p);
|
||||
// P0 + P3 - P4 + P6 = S - P4 + P6 = T + P6 = C00
|
||||
add(P[0], P[3], S, p, p, p);
|
||||
sub(S, P[4], T, p, p, p);
|
||||
add(T, P[6], C, p, p, cii[0][0]);
|
||||
// P2 + P4 = C01
|
||||
add(P[2], P[4], C, p, p, cii[0][1]);
|
||||
// P1 + P3 = C10
|
||||
add(P[1], P[3], C, p, p, cii[1][0]);
|
||||
// P0 + P2 - P1 + P5 = S - P1 + P5 = T + P5 = C11
|
||||
add(P[0], P[2], S, p, p, p);
|
||||
sub(S, P[1], T, p, p, p);
|
||||
add(T, P[5], C, p, p, cii[1][1]);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
mat A, B, C;
|
||||
corners ai = { 0, N, 0, N };
|
||||
corners bi = { 0, N, 0, N };
|
||||
corners ci = { 0, N, 0, N };
|
||||
srand(time(0));
|
||||
// identity(A,bi); identity(B,bi);
|
||||
// set(A,ai,2); set(B,bi,2);
|
||||
randk(A, ai, 0, 2);
|
||||
randk(B, bi, 0, 2);
|
||||
print(A, ai, "A");
|
||||
print(B, bi, "B");
|
||||
set(C, ci, 0);
|
||||
// add(A,B,C, ai, bi, ci);
|
||||
mul(A, B, C, ai, bi, ci);
|
||||
print(C, ci, "C");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
A = {
|
||||
1.2, 0.83, 0.39, 0.41,
|
||||
1.8, 1.9, 0.49, 0.23,
|
||||
0.38, 0.72, 1.8, 1.9,
|
||||
0.13, 1.8, 0.48, 0.82,
|
||||
}
|
||||
B = {
|
||||
1.2, 1.6, 1.4, 1.6,
|
||||
0.27, 0.63, 0.3, 0.79,
|
||||
0.58, 1.2, 1.1, 0.07,
|
||||
2, 1.9, 0.47, 0.47,
|
||||
}
|
||||
C = {
|
||||
2.7, 3.7, 2.6, 2.9,
|
||||
3.4, 5, 3.7, 4.5,
|
||||
5.3, 6.7, 3.6, 2.2,
|
||||
2.5, 3.5, 1.6, 2.1,
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/*This is a C++ Program to implement Bin packing algorithm. This is a sample program to illustrate the Bin-Packing algorithm using next fit heuristics. In the bin packing problem, objects of different volumes must be packed into a finite number of bins or containers each of volume V in a way that minimizes the number of bins used. In computational complexity theory, it is a combinatorial NP-hard problem.
|
||||
There are many variations of this problem, such as 2D packing, linear packing, packing by weight, packing by cost, and so on. They have many applications, such as filling up containers, loading trucks with weight capacity constraints, creating file backups in media and technology mapping in Field-programmable gate array semiconductor chip design.
|
||||
|
||||
The bin packing problem can also be seen as a special case of the cutting stock problem. When the number of bins is restricted to 1 and each item is characterised by both a volume and a value, the problem of maximising the value of items that can fit in the bin is known as the knapsack problem.*/
|
||||
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void binPacking(int *a, int size, int n)
|
||||
{
|
||||
int binCount = 1;
|
||||
int s = size;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (s - *(a + i) > 0)
|
||||
{
|
||||
s -= *(a + i);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
binCount++;
|
||||
s = size;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
cout << "Number of bins required: " << binCount;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "BIN - PACKING Algorithm\n";
|
||||
cout << "Enter the number of items in Set: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << "Enter " << n << " items:";
|
||||
int a[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
cin >> a[i];
|
||||
cout << "Enter the bin size: ";
|
||||
int size;
|
||||
cin >> size;
|
||||
binPacking(a, size, n);
|
||||
}
|
||||
|
||||
/*
|
||||
BIN - PACKING Algorithm
|
||||
Enter the number of items in Set: 5
|
||||
Enter 5 items:12 23 34 45 56
|
||||
Enter the bin size: 70
|
||||
Number of bins required: 3
|
@ -1,62 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int noOfDigit(long a)
|
||||
{
|
||||
int n = 0;
|
||||
while (a > 0)
|
||||
{
|
||||
a /= 10;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
void schonhageStrassenMultiplication(long x, long y, int n, int m)
|
||||
{
|
||||
int linearConvolution[n + m - 1];
|
||||
for (int i = 0; i < (n + m - 1); i++)
|
||||
linearConvolution[i] = 0;
|
||||
long p = x;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
x = p;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
linearConvolution[i + j] += (y % 10) * (x % 10);
|
||||
x /= 10;
|
||||
}
|
||||
y /= 10;
|
||||
}
|
||||
cout << "The Linear Convolution is: ( ";
|
||||
for (int i = (n + m - 2); i >= 0; i--)
|
||||
{
|
||||
cout << linearConvolution[i] << " ";
|
||||
}
|
||||
cout << ")";
|
||||
long product = 0;
|
||||
int nextCarry = 0, base = 1;
|
||||
;
|
||||
for (int i = 0; i < n + m - 1; i++)
|
||||
{
|
||||
linearConvolution[i] += nextCarry;
|
||||
product = product + (base * (linearConvolution[i] % 10));
|
||||
nextCarry = linearConvolution[i] / 10;
|
||||
base *= 10;
|
||||
}
|
||||
cout << "\nThe Product of the numbers is: " << product;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the numbers:";
|
||||
long a, b;
|
||||
cin >> a >> b;
|
||||
int n = noOfDigit(a);
|
||||
int m = noOfDigit(b);
|
||||
schonhageStrassenMultiplication(a, b, n, m);
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the numbers:3452 1245
|
||||
The Linear Convolution is: ( 3 10 25 43 44 33 10 )
|
||||
Product of the numbers is: 4297740
|
@ -1,95 +0,0 @@
|
||||
/*This is a C++ Program to generate random numbers using Linear Congruential Generator. A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation. The method represents one of the oldest and best-known pseudorandom number generator algorithms. The theory behind them is relatively easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide modulo arithmetic by storage-bit truncation.*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class mRND
|
||||
{
|
||||
public:
|
||||
void seed(unsigned int s)
|
||||
{
|
||||
_seed = s;
|
||||
}
|
||||
|
||||
protected:
|
||||
mRND() :
|
||||
_seed(0), _a(0), _c(0), _m(2147483648)
|
||||
{
|
||||
}
|
||||
int rnd()
|
||||
{
|
||||
return (_seed = (_a * _seed + _c) % _m);
|
||||
}
|
||||
|
||||
int _a, _c;
|
||||
unsigned int _m, _seed;
|
||||
};
|
||||
|
||||
class MS_RND: public mRND
|
||||
{
|
||||
public:
|
||||
MS_RND()
|
||||
{
|
||||
_a = 214013;
|
||||
_c = 2531011;
|
||||
}
|
||||
int rnd()
|
||||
{
|
||||
return mRND::rnd() >> 16;
|
||||
}
|
||||
};
|
||||
|
||||
class BSD_RND: public mRND
|
||||
{
|
||||
public:
|
||||
BSD_RND()
|
||||
{
|
||||
_a = 1103515245;
|
||||
_c = 12345;
|
||||
}
|
||||
int rnd()
|
||||
{
|
||||
return mRND::rnd();
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
BSD_RND bsd_rnd;
|
||||
MS_RND ms_rnd;
|
||||
cout << "MS RAND:" << endl << "========" << endl;
|
||||
for (int x = 0; x < 10; x++)
|
||||
cout << ms_rnd.rnd() << endl;
|
||||
cout << endl << "BSD RAND:" << endl << "=========" << endl;
|
||||
for (int x = 0; x < 10; x++)
|
||||
cout << bsd_rnd.rnd() << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
MS RAND:
|
||||
========
|
||||
38
|
||||
7719
|
||||
21238
|
||||
2437
|
||||
8855
|
||||
11797
|
||||
8365
|
||||
32285
|
||||
10450
|
||||
30612
|
||||
|
||||
BSD RAND:
|
||||
=========
|
||||
12345
|
||||
1406932606
|
||||
654583775
|
||||
1449466924
|
||||
229283573
|
||||
1109335178
|
||||
1051550459
|
||||
1293799192
|
||||
794471793
|
||||
551188310
|
@ -1,84 +0,0 @@
|
||||
/*This is a C++ Program to find prime number between a given range using Wheel Seive method. Wheel factorization is a graphical method for manually performing a preliminary to the Sieve of Eratosthenes that separates prime numbers from composites. Start by writing the natural numbers around circles as shown below. Prime numbers in the innermost circle have their multiples in similar positions as themselves in the other circles, forming spokes of primes and their multiples. Multiples of the prime numbers in the innermost circle form spokes of composite numbers in the outer circles.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define MAX_NUM 50
|
||||
// array will be initialized to 0 being global
|
||||
int primes[MAX_NUM];
|
||||
|
||||
void gen_sieve_primes(void)
|
||||
{
|
||||
for (int p = 2; p < MAX_NUM; p++) // for all elements in array
|
||||
{
|
||||
if (primes[p] == 0) // it is not multiple of any other prime
|
||||
primes[p] = 1; // mark it as prime
|
||||
// mark all multiples of prime selected above as non primes
|
||||
int c = 2;
|
||||
int mul = p * c;
|
||||
for (; mul < MAX_NUM;)
|
||||
{
|
||||
primes[mul] = -1;
|
||||
c++;
|
||||
mul = p * c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_all_primes()
|
||||
{
|
||||
int c = 0;
|
||||
for (int i = 0; i < MAX_NUM; i++)
|
||||
{
|
||||
if (primes[i] == 1)
|
||||
{
|
||||
c++;
|
||||
if (c < 4)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 1:
|
||||
cout << c << "st prime is: " << i << endl;
|
||||
break;
|
||||
case 2:
|
||||
cout << c << "nd prime is: " << i << endl;
|
||||
break;
|
||||
case 3:
|
||||
cout << c << "rd prime is: " << i << endl;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
cout << c << "th prime is: " << i << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
gen_sieve_primes();
|
||||
print_all_primes();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
1st prime is: 2
|
||||
2nd prime is: 3
|
||||
3rd prime is: 5
|
||||
4th prime is: 7
|
||||
5th prime is: 11
|
||||
6th prime is: 13
|
||||
7th prime is: 17
|
||||
8th prime is: 19
|
||||
9th prime is: 23
|
||||
10th prime is: 29
|
||||
11th prime is: 31
|
||||
12th prime is: 37
|
||||
13th prime is: 41
|
||||
14th prime is: 43
|
||||
15th prime is: 47
|
@ -1,113 +0,0 @@
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Number of components in the graph
|
||||
#define V 9
|
||||
|
||||
// A utility function to find the component with minimum distance value, from
|
||||
// the set of components not yet included in shortest path tree
|
||||
int minDistance(int dist[], bool sptSet[])
|
||||
{
|
||||
// Initialize min value
|
||||
int min = INT_MAX, min_index;
|
||||
for (int v = 0; v < V; v++)
|
||||
if (sptSet[v] == false && dist[v] <= min)
|
||||
min = dist[v], min_index = v;
|
||||
return min_index;
|
||||
}
|
||||
|
||||
// A utility function to print the constructed distance array
|
||||
void printSolution(int dist[], int n)
|
||||
{
|
||||
cout << "Component\tDistance from other component\n";
|
||||
for (int i = 0; i < V; i++)
|
||||
printf("%d\t\t%d\n", i, dist[i]);
|
||||
}
|
||||
|
||||
// Funtion that implements Dijkstra's single source shortest path algorithm
|
||||
// for a graph represented using adjacency matrix representation
|
||||
void optimizeLength(int graph[V][V], int src)
|
||||
{
|
||||
int dist[V]; // The output array. dist[i] will hold the shortest
|
||||
// distance from src to i
|
||||
bool sptSet[V]; // sptSet[i] will true if component i is included in shortest
|
||||
// path tree or shortest distance from src to i is finalized
|
||||
// Initialize all distances as INFINITE and stpSet[] as false
|
||||
for (int i = 0; i < V; i++)
|
||||
dist[i] = INT_MAX, sptSet[i] = false;
|
||||
// Distance of source component from itself is always 0
|
||||
dist[src] = 0;
|
||||
// Find shortest path for all components
|
||||
for (int count = 0; count < V - 1; count++)
|
||||
{
|
||||
// Pick the minimum distance component from the set of components not
|
||||
// yet processed. u is always equal to src in first iteration.
|
||||
int u = minDistance(dist, sptSet);
|
||||
// Mark the picked component as processed
|
||||
sptSet[u] = true;
|
||||
// Update dist value of the adjacent components of the picked component.
|
||||
for (int v = 0; v < V; v++)
|
||||
// Update dist[v] only if is not in sptSet, there is an edge from
|
||||
// u to v, and total weight of path from src to v through u is
|
||||
// smaller than current value of dist[v]
|
||||
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]
|
||||
+ graph[u][v] < dist[v])
|
||||
dist[v] = dist[u] + graph[u][v];
|
||||
}
|
||||
// print the constructed distance array
|
||||
printSolution(dist, V);
|
||||
}
|
||||
|
||||
// driver program to test above function
|
||||
int main()
|
||||
{
|
||||
/* Let us create the example graph discussed above */
|
||||
int graph[V][V] =
|
||||
{
|
||||
{ 0, 4, 0, 0, 0, 0, 0, 8, 0 }, { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, {
|
||||
0, 8, 0, 7, 0, 4, 0, 0, 2
|
||||
},
|
||||
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 }, {
|
||||
0, 0, 0, 9, 0, 10, 0, 0,
|
||||
0
|
||||
}, { 0, 0, 4, 0, 10, 0, 2, 0, 0 }, {
|
||||
0, 0, 0, 14,
|
||||
0, 2, 0, 1, 6
|
||||
}, { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, {
|
||||
0, 0, 2, 0, 0, 0, 6, 7, 0
|
||||
}
|
||||
};
|
||||
cout << "Enter the starting component: ";
|
||||
int s;
|
||||
cin >> s;
|
||||
optimizeLength(graph, s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the starting component: 1
|
||||
Component Distance from other component
|
||||
0 4
|
||||
1 0
|
||||
2 8
|
||||
3 15
|
||||
4 22
|
||||
5 12
|
||||
6 12
|
||||
7 11
|
||||
8 10
|
||||
|
||||
Enter the starting component: 6
|
||||
Component Distance from other component
|
||||
0 9
|
||||
1 12
|
||||
2 6
|
||||
3 13
|
||||
4 12
|
||||
5 2
|
||||
6 0
|
||||
7 1
|
||||
8 6
|
@ -1,27 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
int add(int x, int y)
|
||||
{
|
||||
int carry;
|
||||
while (y != 0)
|
||||
{
|
||||
carry = x & y;
|
||||
x = x ^ y;
|
||||
y = carry << 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the numbers to be added:";
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
cout << "The Summation is: " << add(x, y);
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the numbers to be added:23 24
|
||||
The Summation is: 47
|
@ -1,77 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int a[10][10], b[10][10], c[10][10];
|
||||
int x, y, i, j;
|
||||
cout << "\nEnter the number of rows and columns for Message Matrix:\n\n";
|
||||
cin >> x >> y;
|
||||
// x denotes number rows in matrix A
|
||||
// y denotes number columns in matrix A
|
||||
cout << "\n\nEnter elements for Matrix :::\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < y; j++)
|
||||
{
|
||||
cin >> a[i][j];
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
cout << "\n\nMatrix :\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < y; j++)
|
||||
{
|
||||
cout << "\t" << a[i][j];
|
||||
}
|
||||
cout << "\n\n";
|
||||
}
|
||||
for (i = 0; i < y; i++)
|
||||
{
|
||||
for (j = 0; j < x; j++)
|
||||
{
|
||||
b[i][j]=x+y;
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < x; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for (int k = 0; k < y; k++)
|
||||
{
|
||||
c[i][j] = c[i][j] + a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
cout
|
||||
<< "\n-----------------------------------------------------------\n";
|
||||
cout << "\n\nEncoded Matrix :\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < x; j++)
|
||||
{
|
||||
cout << "\t" << c[i][j];
|
||||
}
|
||||
cout << "\n\n";
|
||||
}
|
||||
getch();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of rows and columns for Message Matrix:
|
||||
2 2
|
||||
|
||||
Enter elements for Matrix :::
|
||||
1 2
|
||||
3 4
|
||||
Matrix :
|
||||
1 2
|
||||
3 4
|
||||
-----------------------------------------------------------
|
||||
Encoded Matrix :
|
||||
12 12
|
||||
28 28
|
@ -1,103 +0,0 @@
|
||||
/*
|
||||
This is a C++ Program to perform LU Decomposition of any matrix. In numerical analysis, LU decomposition (where ‘LU’ stands for ‘Lower Upper’, and also called LU factorization) factors a matrix as the product of a lower triangular matrix and an upper triangular matrix. The product sometimes includes a permutation matrix as well. The LU decomposition can be viewed as the matrix form of Gaussian elimination. Computers usually solve square systems of linear equations using the LU decomposition, and it is also a key step when inverting a matrix, or computing the determinant of a matrix
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include<cstdio>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
void lu(float[][10], float[][10], float[][10], int n);
|
||||
void output(float[][10], int);
|
||||
float a[10][10], l[10][10], u[10][10];
|
||||
int n = 0, i = 0, j = 0;
|
||||
cout << "Enter size of 2d array(Square matrix) : ";
|
||||
cin >> n;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
cout << "Enter values no:" << i << ", " << j << ": ";
|
||||
cin >> a[i][j];
|
||||
}
|
||||
}
|
||||
lu(a, l, u, n);
|
||||
cout << "\nL Decomposition\n\n";
|
||||
output(l, n);
|
||||
cout << "\nU Decomposition\n\n";
|
||||
output(u, n);
|
||||
return 0;
|
||||
}
|
||||
void lu(float a[][10], float l[][10], float u[][10], int n)
|
||||
{
|
||||
int i = 0, j = 0, k = 0;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
if (j < i)
|
||||
l[j][i] = 0;
|
||||
else
|
||||
{
|
||||
l[j][i] = a[j][i];
|
||||
for (k = 0; k < i; k++)
|
||||
{
|
||||
l[j][i] = l[j][i] - l[j][k] * u[k][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
if (j < i)
|
||||
u[i][j] = 0;
|
||||
else if (j == i)
|
||||
u[i][j] = 1;
|
||||
else
|
||||
{
|
||||
u[i][j] = a[i][j] / l[i][i];
|
||||
for (k = 0; k < i; k++)
|
||||
{
|
||||
u[i][j] = u[i][j] - ((l[i][k] * u[k][j]) / l[i][i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void output(float x[][10], int n)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
printf("%f ", x[i][j]);
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Enter size of 2d array(Square matrix) : 3
|
||||
Enter values no:0, 0: 1
|
||||
Enter values no:0, 1: 1
|
||||
Enter values no:0, 2: -1
|
||||
Enter values no:1, 0: 2
|
||||
Enter values no:1, 1: -1
|
||||
Enter values no:1, 2: 3
|
||||
Enter values no:2, 0: 3
|
||||
Enter values no:2, 1: 1
|
||||
Enter values no:2, 2: -1
|
||||
|
||||
L Decomposition
|
||||
|
||||
1.000000 0.000000 0.000000
|
||||
2.000000 -3.000000 0.000000
|
||||
3.000000 -2.000000 -1.333333
|
||||
|
||||
U Decomposition
|
||||
|
||||
1.000000 1.000000 -1.000000
|
||||
0.000000 1.000000 -1.666667
|
||||
0.000000 0.000000 1.000000
|
@ -1,112 +0,0 @@
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int a[10][10], b[10][10], c[10][10];
|
||||
int x, y, i, j, m, n;
|
||||
cout << "\nEnter the number of rows and columns for Matrix A:::\n\n";
|
||||
cin >> x >> y;
|
||||
// x denotes number rows in matrix A
|
||||
// y denotes number columns in matrix A
|
||||
cout << "\n\nEnter elements for Matrix A :::\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < y; j++)
|
||||
{
|
||||
cin >> a[i][j];
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
cout << "\n\nMatrix A :\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < y; j++)
|
||||
{
|
||||
cout << "\t" << a[i][j];
|
||||
}
|
||||
cout << "\n\n";
|
||||
}
|
||||
cout << "\n-----------------------------------------------------------\n";
|
||||
cout << "\nEnter the number of rows and columns for Matrix B:::\n\n";
|
||||
cin >> m >> n;
|
||||
// m denotes number rows in matrix B
|
||||
// n denotes number columns in matrix B
|
||||
cout << "\n\nEnter elements for Matrix B :::\n\n";
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
cin >> b[i][j];
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
cout << "\n\nMatrix B :\n\n";
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
cout << "\t" << b[i][j];
|
||||
}
|
||||
cout << "\n\n";
|
||||
}
|
||||
if (y == m)
|
||||
{
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for (int k = 0; k < m; k++)
|
||||
{
|
||||
c[i][j] = c[i][j] + a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
cout
|
||||
<< "\n-----------------------------------------------------------\n";
|
||||
cout << "\n\nMultiplication of Matrix A and Matrix B :\n\n";
|
||||
for (i = 0; i < x; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
cout << "\t" << c[i][j];
|
||||
}
|
||||
cout << "\n\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\n\nMultiplication is not possible";
|
||||
}
|
||||
getch();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of rows and columns for Matrix A:::
|
||||
2 2
|
||||
|
||||
Enter elements for Matrix A :::
|
||||
1 2
|
||||
3 4
|
||||
|
||||
Matrix A :
|
||||
1 2
|
||||
3 4
|
||||
-----------------------------------------------------------
|
||||
Enter the number of rows and columns for Matrix B:::
|
||||
2 2
|
||||
|
||||
Enter elements for Matrix B :::
|
||||
4 5
|
||||
6 7
|
||||
|
||||
Matrix B :
|
||||
4 5
|
||||
6 7
|
||||
-----------------------------------------------------------
|
||||
|
||||
Multiplication of Matrix A and Matrix B :
|
||||
16 19
|
||||
36 43
|
@ -1,65 +0,0 @@
|
||||
#include<stdio.h>
|
||||
#include<limits.h>
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
|
||||
|
||||
int MatrixChainOrder(int p[], int n)
|
||||
{
|
||||
/* For simplicity of the program, one extra row and one extra column are
|
||||
allocated in m[][]. 0th row and 0th column of m[][] are not used */
|
||||
int m[n][n];
|
||||
int s[n][n];
|
||||
int i, j, k, L, q;
|
||||
/* m[i,j] = Minimum number of scalar multiplications needed to compute
|
||||
the matrix A[i]A[i+1]...A[j] = A[i..j] where dimention of A[i] is
|
||||
p[i-1] x p[i] */
|
||||
// cost is zero when multiplying one matrix.
|
||||
for (i = 1; i < n; i++)
|
||||
m[i][i] = 0;
|
||||
// L is chain length.
|
||||
for (L = 2; L < n; L++)
|
||||
{
|
||||
for (i = 1; i <= n - L + 1; i++)
|
||||
{
|
||||
j = i + L - 1;
|
||||
m[i][j] = INT_MAX;
|
||||
for (k = i; k <= j - 1; k++)
|
||||
{
|
||||
// q = cost/scalar multiplications
|
||||
q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
|
||||
if (q < m[i][j])
|
||||
{
|
||||
m[i][j] = q;
|
||||
s[i][j] = k;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return m[1][n - 1];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cout
|
||||
<< "Enter the array p[], which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]";
|
||||
cout << "Enter the total length:";
|
||||
int n;
|
||||
cin >> n;
|
||||
int array[n];
|
||||
cout << "Enter the dimensions: ";
|
||||
for (int var = 0; var < n; ++var)
|
||||
{
|
||||
cin >> array[var];
|
||||
}
|
||||
cout << "Minimum number of multiplications is: " << MatrixChainOrder(array,
|
||||
n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter the array p[], which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]Enter the total length:4
|
||||
Enter the dimensions: 2 4 3 5
|
||||
Minimum number of multiplications is: 54
|
@ -1,80 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
// A utility function to print an array p[] of size 'n'
|
||||
void printArray(int p[], int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << p[i] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void printAllUniqueParts(int n)
|
||||
{
|
||||
int p[n]; // An array to store a partition
|
||||
int k = 0; // Index of last element in a partition
|
||||
p[k] = n; // Initialize first partition as number itself
|
||||
// This loop first prints current partition, then generates next
|
||||
// partition. The loop stops when the current partition has all 1s
|
||||
while (true)
|
||||
{
|
||||
// print current partition
|
||||
printArray(p, k + 1);
|
||||
// Generate next partition
|
||||
// Find the rightmost non-one value in p[]. Also, update the
|
||||
// rem_val so that we know how much value can be accommodated
|
||||
int rem_val = 0;
|
||||
while (k >= 0 && p[k] == 1)
|
||||
{
|
||||
rem_val += p[k];
|
||||
k--;
|
||||
}
|
||||
// if k < 0, all the values are 1 so there are no more partitions
|
||||
if (k < 0)
|
||||
return;
|
||||
// Decrease the p[k] found above and adjust the rem_val
|
||||
p[k]--;
|
||||
rem_val++;
|
||||
// If rem_val is more, then the sorted order is violeted. Divide
|
||||
// rem_val in differnt values of size p[k] and copy these values at
|
||||
// different positions after p[k]
|
||||
while (rem_val > p[k])
|
||||
{
|
||||
p[k + 1] = p[k];
|
||||
rem_val = rem_val - p[k];
|
||||
k++;
|
||||
}
|
||||
// Copy rem_val to next position and increment position
|
||||
p[k + 1] = rem_val;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
cout << "All Unique Partitions of 2 \n";
|
||||
printAllUniqueParts(2);
|
||||
cout << "\nAll Unique Partitions of 3 \n";
|
||||
printAllUniqueParts(3);
|
||||
cout << "\nAll Unique Partitions of 4 \n";
|
||||
printAllUniqueParts(4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
All Unique Partitions of 2
|
||||
2
|
||||
1 1
|
||||
|
||||
All Unique Partitions of 3
|
||||
3
|
||||
2 1
|
||||
1 1 1
|
||||
|
||||
All Unique Partitions of 4
|
||||
4
|
||||
3 1
|
||||
2 2
|
||||
2 1 1
|
||||
1 1 1 1
|
@ -1,81 +0,0 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
// A utility function to print an array p[] of size 'n'
|
||||
void printArray(int p[], int n)
|
||||
{
|
||||
for (int i = 0; i < n; i++)
|
||||
cout << p[i] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void printAllUniqueParts(int n)
|
||||
{
|
||||
int p[n]; // An array to store a partition
|
||||
int k = 0; // Index of last element in a partition
|
||||
p[k] = n; // Initialize first partition as number itself
|
||||
// This loop first prints current partition, then generates next
|
||||
// partition. The loop stops when the current partition has all 1s
|
||||
while (true)
|
||||
{
|
||||
// print current partition
|
||||
printArray(p, k + 1);
|
||||
// Generate next partition
|
||||
// Find the rightmost non-one value in p[]. Also, update the
|
||||
// rem_val so that we know how much value can be accommodated
|
||||
int rem_val = 0;
|
||||
while (k >= 0 && p[k] == 1)
|
||||
{
|
||||
rem_val += p[k];
|
||||
k--;
|
||||
}
|
||||
// if k < 0, all the values are 1 so there are no more partitions
|
||||
if (k < 0)
|
||||
return;
|
||||
// Decrease the p[k] found above and adjust the rem_val
|
||||
p[k]--;
|
||||
rem_val++;
|
||||
// If rem_val is more, then the sorted order is violeted. Divide
|
||||
// rem_val in differnt values of size p[k] and copy these values at
|
||||
// different positions after p[k]
|
||||
while (rem_val > p[k])
|
||||
{
|
||||
p[k + 1] = p[k];
|
||||
rem_val = rem_val - p[k];
|
||||
k++;
|
||||
}
|
||||
// Copy rem_val to next position and increment position
|
||||
p[k + 1] = rem_val;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
// Driver program to test above functions
|
||||
int main()
|
||||
{
|
||||
cout << "All Unique Partitions of 2 \n";
|
||||
printAllUniqueParts(2);
|
||||
cout << "\nAll Unique Partitions of 3 \n";
|
||||
printAllUniqueParts(3);
|
||||
cout << "\nAll Unique Partitions of 4 \n";
|
||||
printAllUniqueParts(4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
All Unique Partitions of 2
|
||||
2
|
||||
1 1
|
||||
|
||||
All Unique Partitions of 3
|
||||
3
|
||||
2 1
|
||||
1 1 1
|
||||
|
||||
All Unique Partitions of 4
|
||||
4
|
||||
3 1
|
||||
2 2
|
||||
2 1 1
|
||||
1 1 1 1
|
@ -1,49 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char var[] = { 'x', 'y', 'z', 'w' };
|
||||
cout << "Enter the number of variables in the equations: ";
|
||||
int n;
|
||||
cin >> n;
|
||||
cout << "\nEnter the coefficients of each variable for each equations";
|
||||
cout << "\nax + by + cz + ... = d";
|
||||
int mat[n][n];
|
||||
int constants[n][1];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cin >> mat[i][j];
|
||||
}
|
||||
cin >> constants[i][0];
|
||||
}
|
||||
cout << "Matrix representation is: ";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cout << " " << mat[i][j];
|
||||
}
|
||||
cout << " " << var[i];
|
||||
cout << " = " << constants[i][0];
|
||||
cout << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of variables in the equations: 3
|
||||
|
||||
Enter the coefficients of each variable for each equations
|
||||
ax + by + cz + ... = d
|
||||
1 2 3 4
|
||||
1 2 6 8
|
||||
2 3 9 8
|
||||
Matrix representation is:
|
||||
1 2 3 x = 4
|
||||
1 2 6 y = 8
|
||||
2 3 9 z = 8
|
@ -1,65 +0,0 @@
|
||||
/*This is a C++ Program to knapsack problem using dynamic programming. The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.*/
|
||||
|
||||
// A Dynamic Programming based solution for 0-1 Knapsack problem
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// A utility function that returns maximum of two integers
|
||||
int max(int a, int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
// Returns the maximum value that can be put in a knapsack of capacity W
|
||||
int knapSack(int W, int wt[], int val[], int n)
|
||||
{
|
||||
int i, w;
|
||||
int K[n + 1][W + 1];
|
||||
// Build table K[][] in bottom up manner
|
||||
for (i = 0; i <= n; i++)
|
||||
{
|
||||
for (w = 0; w <= W; w++)
|
||||
{
|
||||
if (i == 0 || w == 0)
|
||||
K[i][w] = 0;
|
||||
else if (wt[i - 1] <= w)
|
||||
K[i][w]
|
||||
= max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
|
||||
else
|
||||
K[i][w] = K[i - 1][w];
|
||||
}
|
||||
}
|
||||
return K[n][W];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Enter the number of items in a Knapsack:";
|
||||
int n, W;
|
||||
cin >> n;
|
||||
int val[n], wt[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "Enter value and weight for item " << i << ":";
|
||||
cin >> val[i];
|
||||
cin >> wt[i];
|
||||
}
|
||||
// int val[] = { 60, 100, 120 };
|
||||
// int wt[] = { 10, 20, 30 };
|
||||
// int W = 50;
|
||||
cout << "Enter the capacity of knapsack";
|
||||
cin >> W;
|
||||
cout << knapSack(W, wt, val, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of items in a Knapsack:5
|
||||
Enter value and weight for item 0:11 111
|
||||
Enter value and weight for item 1:22 121
|
||||
Enter value and weight for item 2:33 131
|
||||
Enter value and weight for item 3:44 141
|
||||
Enter value and weight for item 4:55 151
|
||||
Enter the capacity of knapsack 300
|
||||
99
|
@ -1,647 +0,0 @@
|
||||
#if !defined(MATRIX_H)
|
||||
#define MATRIX_H
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <tchar.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
class CMatrix
|
||||
{
|
||||
private:
|
||||
int m_rows;
|
||||
int m_cols;
|
||||
char m_name[128];
|
||||
CMatrix();
|
||||
public:
|
||||
double **m_pData;
|
||||
CMatrix(const char *name, int rows, int cols) :
|
||||
m_rows(rows), m_cols(cols)
|
||||
{
|
||||
strcpy(m_name, name);
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
CMatrix(const CMatrix &other)
|
||||
{
|
||||
strcpy(m_name, other.m_name);
|
||||
m_rows = other.m_rows;
|
||||
m_cols = other.m_cols;
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
~CMatrix()
|
||||
{
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
delete[] m_pData[i];
|
||||
delete[] m_pData;
|
||||
m_rows = m_cols = 0;
|
||||
}
|
||||
void SetName(const char *name)
|
||||
{
|
||||
strcpy(m_name, name);
|
||||
}
|
||||
const char* GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
void GetInput()
|
||||
{
|
||||
std::cin >> *this;
|
||||
}
|
||||
void FillSimulatedInput()
|
||||
{
|
||||
static int factor1 = 1, factor2 = 2;
|
||||
std::cout << "\n\nEnter Input For Matrix : " << m_name << " Rows: "
|
||||
<< m_rows << " Cols: " << m_cols << "\n";
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
std::cout << "Input For Row: " << i + 1 << " Col: " << j
|
||||
+ 1 << " = ";
|
||||
int data = ((i + 1) * factor1) + (j + 1) * factor2;
|
||||
m_pData[i][j] = data / 10.2;
|
||||
std::cout << m_pData[i][j] << "\n";
|
||||
factor1 += (rand() % 4);
|
||||
factor2 += (rand() % 3);
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
double Determinant()
|
||||
{
|
||||
double det = 0;
|
||||
double **pd = m_pData;
|
||||
switch (m_rows)
|
||||
{
|
||||
case 2:
|
||||
{
|
||||
det = pd[0][0] * pd[1][1] - pd[0][1] * pd[1][0];
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
/***
|
||||
a b c
|
||||
d e f
|
||||
g h i
|
||||
|
||||
a b c a b c
|
||||
d e f d e f
|
||||
g h i g h i
|
||||
|
||||
|
||||
// det (A) = aei + bfg + cdh - afh - bdi - ceg.
|
||||
***/
|
||||
double a = pd[0][0];
|
||||
double b = pd[0][1];
|
||||
double c = pd[0][2];
|
||||
double d = pd[1][0];
|
||||
double e = pd[1][1];
|
||||
double f = pd[1][2];
|
||||
double g = pd[2][0];
|
||||
double h = pd[2][1];
|
||||
double i = pd[2][2];
|
||||
double det = (a * e * i + b * f * g + c * d * h); // - a*f*h - b*d*i - c*e*g);
|
||||
det = det - a * f * h;
|
||||
det = det - b * d * i;
|
||||
det = det - c * e * g;
|
||||
//std::cout << *this;
|
||||
//std::cout << "deter: " << det << " \n";
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
CMatrix *temp[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
temp[i] = new CMatrix("", 3, 3);
|
||||
for (int k = 0; k < 4; k++)
|
||||
{
|
||||
for (int i = 1; i < 4; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = this->m_pData[0][0] * temp[0]->Determinant()
|
||||
- this->m_pData[0][1] * temp[1]->Determinant()
|
||||
+ this->m_pData[0][2] * temp[2]->Determinant()
|
||||
- this->m_pData[0][3] * temp[3]->Determinant();
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
{
|
||||
CMatrix *temp[5];
|
||||
for (int i = 0; i < 5; i++)
|
||||
temp[i] = new CMatrix("", 4, 4);
|
||||
for (int k = 0; k < 5; k++)
|
||||
{
|
||||
for (int i = 1; i < 5; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = this->m_pData[0][0] * temp[0]->Determinant()
|
||||
- this->m_pData[0][1] * temp[1]->Determinant()
|
||||
+ this->m_pData[0][2] * temp[2]->Determinant()
|
||||
- this->m_pData[0][3] * temp[3]->Determinant()
|
||||
+ this->m_pData[0][4] * temp[4]->Determinant();
|
||||
return det;
|
||||
}
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
case 9:
|
||||
case 10:
|
||||
case 11:
|
||||
case 12:
|
||||
default:
|
||||
{
|
||||
int DIM = m_rows;
|
||||
CMatrix **temp = new CMatrix*[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
temp[i] = new CMatrix("", DIM - 1, DIM - 1);
|
||||
for (int k = 0; k < DIM; k++)
|
||||
{
|
||||
for (int i = 1; i < DIM; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < DIM; j++)
|
||||
{
|
||||
if (k == j)
|
||||
continue;
|
||||
temp[k]->m_pData[i - 1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
double det = 0;
|
||||
for (int k = 0; k < DIM; k++)
|
||||
{
|
||||
if ((k % 2) == 0)
|
||||
det = det + (this->m_pData[0][k]
|
||||
* temp[k]->Determinant());
|
||||
else
|
||||
det = det - (this->m_pData[0][k]
|
||||
* temp[k]->Determinant());
|
||||
}
|
||||
for (int i = 0; i < DIM; i++)
|
||||
delete temp[i];
|
||||
delete[] temp;
|
||||
return det;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
CMatrix& operator =(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "WARNING: Assignment is taking place with by changing the number of rows and columns of the matrix";
|
||||
}
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
delete[] m_pData[i];
|
||||
delete[] m_pData;
|
||||
m_rows = m_cols = 0;
|
||||
strcpy(m_name, other.m_name);
|
||||
m_rows = other.m_rows;
|
||||
m_cols = other.m_cols;
|
||||
m_pData = new double*[m_rows];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
m_pData[i] = new double[m_cols];
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
m_pData[i][j] = other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
CMatrix CoFactor()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return cofactor;
|
||||
if (m_rows < 2)
|
||||
return cofactor;
|
||||
else if (m_rows == 2)
|
||||
{
|
||||
cofactor.m_pData[0][0] = m_pData[1][1];
|
||||
cofactor.m_pData[0][1] = -m_pData[1][0];
|
||||
cofactor.m_pData[1][0] = -m_pData[0][1];
|
||||
cofactor.m_pData[1][1] = m_pData[0][0];
|
||||
return cofactor;
|
||||
}
|
||||
else if (m_rows >= 3)
|
||||
{
|
||||
int DIM = m_rows;
|
||||
CMatrix ***temp = new CMatrix**[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
temp[i] = new CMatrix*[DIM];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
for (int j = 0; j < DIM; j++)
|
||||
temp[i][j] = new CMatrix("", DIM - 1, DIM - 1);
|
||||
for (int k1 = 0; k1 < DIM; k1++)
|
||||
{
|
||||
for (int k2 = 0; k2 < DIM; k2++)
|
||||
{
|
||||
int i1 = 0;
|
||||
for (int i = 0; i < DIM; i++)
|
||||
{
|
||||
int j1 = 0;
|
||||
for (int j = 0; j < DIM; j++)
|
||||
{
|
||||
if (k1 == i || k2 == j)
|
||||
continue;
|
||||
temp[k1][k2]->m_pData[i1][j1++]
|
||||
= this->m_pData[i][j];
|
||||
}
|
||||
if (k1 != i)
|
||||
i1++;
|
||||
}
|
||||
}
|
||||
}
|
||||
bool flagPositive = true;
|
||||
for (int k1 = 0; k1 < DIM; k1++)
|
||||
{
|
||||
flagPositive = ((k1 % 2) == 0);
|
||||
for (int k2 = 0; k2 < DIM; k2++)
|
||||
{
|
||||
if (flagPositive == true)
|
||||
{
|
||||
cofactor.m_pData[k1][k2]
|
||||
= temp[k1][k2]->Determinant();
|
||||
flagPositive = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
cofactor.m_pData[k1][k2]
|
||||
= -temp[k1][k2]->Determinant();
|
||||
flagPositive = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < DIM; i++)
|
||||
for (int j = 0; j < DIM; j++)
|
||||
delete temp[i][j];
|
||||
for (int i = 0; i < DIM; i++)
|
||||
delete[] temp[i];
|
||||
delete[] temp;
|
||||
}
|
||||
return cofactor;
|
||||
}
|
||||
CMatrix Adjoint()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
CMatrix adj("ADJ", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return adj;
|
||||
cofactor = this->CoFactor();
|
||||
// adjoint is transpose of a cofactor of a matrix
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
adj.m_pData[j][i] = cofactor.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return adj;
|
||||
}
|
||||
CMatrix Transpose()
|
||||
{
|
||||
CMatrix trans("TR", m_cols, m_rows);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
trans.m_pData[j][i] = m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return trans;
|
||||
}
|
||||
CMatrix Inverse()
|
||||
{
|
||||
CMatrix cofactor("COF", m_rows, m_cols);
|
||||
CMatrix inv("INV", m_rows, m_cols);
|
||||
if (m_rows != m_cols)
|
||||
return inv;
|
||||
// to find out Determinant
|
||||
double det = Determinant();
|
||||
cofactor = this->CoFactor();
|
||||
// inv = transpose of cofactor / Determinant
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
inv.m_pData[j][i] = cofactor.m_pData[i][j] / det;
|
||||
}
|
||||
}
|
||||
return inv;
|
||||
}
|
||||
CMatrix operator +(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Addition could not take place because number of rows and columns are different between the two matrices";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
result.m_pData[i][j] = this->m_pData[i][j]
|
||||
+ other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
CMatrix operator -(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Subtraction could not take place because number of rows and columns are different between the two matrices";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
result.m_pData[i][j] = this->m_pData[i][j]
|
||||
- other.m_pData[i][j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
CMatrix operator *(const CMatrix &other)
|
||||
{
|
||||
if (this->m_cols != other.m_rows)
|
||||
{
|
||||
std::cout
|
||||
<< "Multiplication could not take place because number of columns of 1st Matrix and number of rows in 2nd Matrix are different";
|
||||
return *this;
|
||||
}
|
||||
CMatrix result("", this->m_rows, other.m_cols);
|
||||
for (int i = 0; i < this->m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < other.m_cols; j++)
|
||||
{
|
||||
for (int k = 0; k < this->m_cols; k++)
|
||||
{
|
||||
result.m_pData[i][j] += this->m_pData[i][k]
|
||||
* other.m_pData[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
bool operator ==(const CMatrix &other)
|
||||
{
|
||||
if (this->m_rows != other.m_rows || this->m_cols != other.m_cols)
|
||||
{
|
||||
std::cout
|
||||
<< "Comparision could not take place because number of rows and columns are different between the two matrices";
|
||||
return false;
|
||||
}
|
||||
CMatrix result("", m_rows, m_cols);
|
||||
bool bEqual = true;
|
||||
for (int i = 0; i < m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m_cols; j++)
|
||||
{
|
||||
if (this->m_pData[i][j] != other.m_pData[i][j])
|
||||
bEqual = false;
|
||||
}
|
||||
}
|
||||
return bEqual;
|
||||
}
|
||||
friend std::istream& operator >>(std::istream &is, CMatrix &m);
|
||||
friend std::ostream& operator <<(std::ostream &os, const CMatrix &m);
|
||||
};
|
||||
std::istream& operator >>(std::istream &is, CMatrix &m)
|
||||
{
|
||||
std::cout << "\n\nEnter Input For Matrix : " << m.m_name << " Rows: "
|
||||
<< m.m_rows << " Cols: " << m.m_cols << "\n";
|
||||
for (int i = 0; i < m.m_rows; i++)
|
||||
{
|
||||
for (int j = 0; j < m.m_cols; j++)
|
||||
{
|
||||
std::cout << "Input For Row: " << i + 1 << " Col: " << j + 1
|
||||
<< " = ";
|
||||
is >> m.m_pData[i][j];
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return is;
|
||||
}
|
||||
std::ostream& operator <<(std::ostream &os, const CMatrix &m)
|
||||
{
|
||||
os << "\n\nMatrix : " << m.m_name << " Rows: " << m.m_rows << " Cols: "
|
||||
<< m.m_cols << "\n\n";
|
||||
for (int i = 0; i < m.m_rows; i++)
|
||||
{
|
||||
os << " | ";
|
||||
for (int j = 0; j < m.m_cols; j++)
|
||||
{
|
||||
char buf[32];
|
||||
double data = m.m_pData[i][j];
|
||||
if (m.m_pData[i][j] > -0.00001 && m.m_pData[i][j] < 0.00001)
|
||||
data = 0;
|
||||
sprintf(buf, "%10.2lf ", data);
|
||||
os << buf;
|
||||
}
|
||||
os << "|\n";
|
||||
}
|
||||
os << "\n\n";
|
||||
return os;
|
||||
}
|
||||
#endif
|
||||
int main()
|
||||
{
|
||||
CMatrix a("A", 6, 6);
|
||||
CMatrix b("B", 6, 1);
|
||||
a.FillSimulatedInput();
|
||||
b.FillSimulatedInput();
|
||||
std::cout << a << "\n Determinant : ";
|
||||
std::cout << a.Determinant() << "\n";
|
||||
std::cout << b << "\n Determinant : ";
|
||||
std::cout << b.Determinant() << "\n";
|
||||
CMatrix ainv = a.Inverse();
|
||||
CMatrix q = a * ainv;
|
||||
q.SetName("A * A'");
|
||||
std::cout << q << "\n";
|
||||
CMatrix x = ainv * b;
|
||||
x.SetName("X");
|
||||
std::cout << x << "\n";
|
||||
CMatrix y = a * x; // we will get B
|
||||
y.SetName("Y");
|
||||
std::cout << y << "\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter Input For Matrix : A Rows: 6 Cols: 6
|
||||
Input For Row: 1 Col: 1 = 0.294118
|
||||
Input For Row: 1 Col: 2 = 0.980392
|
||||
Input For Row: 1 Col: 3 = 1.86275
|
||||
Input For Row: 1 Col: 4 = 2.84314
|
||||
Input For Row: 1 Col: 5 = 3.62745
|
||||
Input For Row: 1 Col: 6 = 5.58824
|
||||
|
||||
Input For Row: 2 Col: 1 = 2.94118
|
||||
Input For Row: 2 Col: 2 = 4.11765
|
||||
Input For Row: 2 Col: 3 = 5.88235
|
||||
Input For Row: 2 Col: 4 = 8.43137
|
||||
Input For Row: 2 Col: 5 = 10.3922
|
||||
Input For Row: 2 Col: 6 = 12.3529
|
||||
|
||||
Input For Row: 3 Col: 1 = 8.13725
|
||||
Input For Row: 3 Col: 2 = 9.70588
|
||||
Input For Row: 3 Col: 3 = 12.0588
|
||||
Input For Row: 3 Col: 4 = 15.098
|
||||
Input For Row: 3 Col: 5 = 17.8431
|
||||
Input For Row: 3 Col: 6 = 20.5882
|
||||
|
||||
Input For Row: 4 Col: 1 = 14.902
|
||||
Input For Row: 4 Col: 2 = 18.2353
|
||||
Input For Row: 4 Col: 3 = 21.4706
|
||||
Input For Row: 4 Col: 4 = 24.7059
|
||||
Input For Row: 4 Col: 5 = 27.549
|
||||
Input For Row: 4 Col: 6 = 31.1765
|
||||
|
||||
Input For Row: 5 Col: 1 = 24.902
|
||||
Input For Row: 5 Col: 2 = 27.9412
|
||||
Input For Row: 5 Col: 3 = 32.451
|
||||
Input For Row: 5 Col: 4 = 36.0784
|
||||
Input For Row: 5 Col: 5 = 39.7059
|
||||
Input For Row: 5 Col: 6 = 43.9216
|
||||
|
||||
Input For Row: 6 Col: 1 = 36.3725
|
||||
Input For Row: 6 Col: 2 = 39.6078
|
||||
Input For Row: 6 Col: 3 = 43.8235
|
||||
Input For Row: 6 Col: 4 = 47.2549
|
||||
Input For Row: 6 Col: 5 = 51.3725
|
||||
Input For Row: 6 Col: 6 = 55.2941
|
||||
|
||||
|
||||
|
||||
|
||||
Enter Input For Matrix : B Rows: 6 Cols: 1
|
||||
Input For Row: 1 Col: 1 = 9.41176
|
||||
|
||||
Input For Row: 2 Col: 1 = 15.7843
|
||||
|
||||
Input For Row: 3 Col: 1 = 22.7451
|
||||
|
||||
Input For Row: 4 Col: 1 = 29.902
|
||||
|
||||
Input For Row: 5 Col: 1 = 37.1569
|
||||
|
||||
Input For Row: 6 Col: 1 = 44.6078
|
||||
|
||||
|
||||
|
||||
|
||||
Matrix : A Rows: 6 Cols: 6
|
||||
|
||||
| 0.29 0.98 1.86 2.84 3.63 5.59 |
|
||||
| 2.94 4.12 5.88 8.43 10.39 12.35 |
|
||||
| 8.14 9.71 12.06 15.10 17.84 20.59 |
|
||||
| 14.90 18.24 21.47 24.71 27.55 31.18 |
|
||||
| 24.90 27.94 32.45 36.08 39.71 43.92 |
|
||||
| 36.37 39.61 43.82 47.25 51.37 55.29 |
|
||||
|
||||
|
||||
|
||||
Determinant : -11.9339
|
||||
|
||||
|
||||
Matrix : B Rows: 6 Cols: 1
|
||||
|
||||
| 9.41 |
|
||||
| 15.78 |
|
||||
| 22.75 |
|
||||
| 29.90 |
|
||||
| 37.16 |
|
||||
| 44.61 |
|
||||
|
||||
|
||||
|
||||
Determinant : -1.#IND
|
||||
|
||||
|
||||
Matrix : A * A' Rows: 6 Cols: 6
|
||||
|
||||
| 1.00 0.00 0.00 0.00 0.00 0.00 |
|
||||
| 0.00 1.00 0.00 0.00 0.00 0.00 |
|
||||
| 0.00 0.00 1.00 0.00 0.00 0.00 |
|
||||
| 0.00 0.00 0.00 1.00 0.00 0.00 |
|
||||
| 0.00 0.00 0.00 0.00 1.00 0.00 |
|
||||
| 0.00 0.00 0.00 0.00 0.00 1.00 |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Matrix : X Rows: 6 Cols: 1
|
||||
|
||||
| 0.82 |
|
||||
| 3.47 |
|
||||
| -9.38 |
|
||||
| 7.71 |
|
||||
| -5.76 |
|
||||
| 3.98 |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Matrix : Y Rows: 6 Cols: 1
|
||||
|
||||
| 9.41 |
|
||||
| 15.78 |
|
||||
| 22.75 |
|
||||
| 29.90 |
|
||||
| 37.16 |
|
||||
| 44.61 |
|
@ -1,61 +0,0 @@
|
||||
/*This is a C++ Program to solve 0-1 knapsack problem. The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.*/
|
||||
|
||||
#include<stdio.h>
|
||||
#include<conio.h>
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// A utility function that returns maximum of two integers
|
||||
int max(int a, int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
// Returns the maximum value that can be put in a knapsack of capacity W
|
||||
int knapSack(int W, int wt[], int val[], int n)
|
||||
{
|
||||
// Base Case
|
||||
if (n == 0 || W == 0)
|
||||
return 0;
|
||||
// If weight of the nth item is more than Knapsack capacity W, then
|
||||
// this item cannot be included in the optimal solution
|
||||
if (wt[n - 1] > W)
|
||||
return knapSack(W, wt, val, n - 1);
|
||||
// Return the maximum of two cases: (1) nth item included (2) not included
|
||||
else
|
||||
return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
|
||||
knapSack(W, wt, val, n - 1));
|
||||
}
|
||||
|
||||
// Driver program to test above function
|
||||
int main()
|
||||
{
|
||||
cout << "Enter the number of items in a Knapsack:";
|
||||
int n, W;
|
||||
cin >> n;
|
||||
int val[n], wt[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "Enter value and weight for item " << i << ":";
|
||||
cin >> val[i];
|
||||
cin >> wt[i];
|
||||
}
|
||||
// int val[] = { 60, 100, 120 };
|
||||
// int wt[] = { 10, 20, 30 };
|
||||
// int W = 50;
|
||||
cout << "Enter the capacity of knapsack";
|
||||
cin >> W;
|
||||
cout << knapSack(W, wt, val, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the number of items in a Knapsack:5
|
||||
Enter value and weight for item 0:11 111
|
||||
Enter value and weight for item 1:22 121
|
||||
Enter value and weight for item 2:33 131
|
||||
Enter value and weight for item 3:44 141
|
||||
Enter value and weight for item 4:55 151
|
||||
Enter the capacity of knapsack 300
|
||||
99
|
@ -1,78 +0,0 @@
|
||||
/*This is a C++ Program to solve fractional knapsack. The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items.*/
|
||||
|
||||
/* program to implement fractional knapsack problem using greedy programming */
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
int array[2][100], n, w, i, curw, used[100], maxi = -1, totalprofit = 0;
|
||||
//input number of objects
|
||||
cout << "Enter number of objects: ";
|
||||
cin >> n;
|
||||
//input max weight of knapsack
|
||||
cout << "Enter the weight of the knapsack: ";
|
||||
cin >> w;
|
||||
/* Array's first row is to store weights
|
||||
second row is to store profits */
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
cin >> array[0][i] >> array[1][i];
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
used[i] = 0;
|
||||
}
|
||||
curw = w;
|
||||
//loop until knapsack is full
|
||||
while (curw >= 0)
|
||||
{
|
||||
maxi = -1;
|
||||
//loop to find max profit object
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
if ((used[i] == 0) && ((maxi == -1) || (((float) array[1][i]
|
||||
/ (float) array[0][i]) > ((float) array[1][maxi]
|
||||
/ (float) array[0][maxi]))))
|
||||
{
|
||||
maxi = i;
|
||||
}
|
||||
}
|
||||
used[maxi] = 1;
|
||||
//decrease current wight
|
||||
curw -= array[0][maxi];
|
||||
//increase total profit
|
||||
totalprofit += array[1][maxi];
|
||||
if (curw >= 0)
|
||||
{
|
||||
cout << "\nAdded object " << maxi + 1 << " Weight: "
|
||||
<< array[0][maxi] << " Profit: " << array[1][maxi]
|
||||
<< " completely in the bag, Space left: " << curw;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\nAdded object " << maxi + 1 << " Weight: "
|
||||
<< (array[0][maxi] + curw) << " Profit: "
|
||||
<< (array[1][maxi] / array[0][maxi]) * (array[0][maxi]
|
||||
+ curw) << " partially in the bag, Space left: 0"
|
||||
<< " Weight added is: " << curw + array[0][maxi];
|
||||
totalprofit -= array[1][maxi];
|
||||
totalprofit += ((array[1][maxi] / array[0][maxi]) * (array[0][maxi]
|
||||
+ curw));
|
||||
}
|
||||
}
|
||||
//print total worth of objects filled in knapsack
|
||||
cout << "\nBags filled with objects worth: " << totalprofit;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter number of objects: 3
|
||||
Enter the weight of the knapsack: 50
|
||||
10 60
|
||||
20 100
|
||||
30 120
|
||||
|
||||
Added object 1 Weight: 10 Profit: 60 completely in the bag, Space left: 40
|
||||
Added object 2 Weight: 20 Profit: 100 completely in the bag, Space left: 20
|
||||
Added object 3 Weight: 20 Profit: 80 partially in the bag, Space left: 0 Weight added is: 20
|
||||
Bags filled with objects worth: 240
|
@ -1,20 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "First number: " << rand() % 100;
|
||||
srand(time(NULL));
|
||||
cout << "\nRandom number: " << rand() % 100;
|
||||
srand(1);
|
||||
cout << "\nAgain the first number: " << rand() % 100;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
First number: 41
|
||||
Random number: 98
|
||||
Again the first number: 41
|
@ -1,82 +0,0 @@
|
||||
/*/This is a C++ Program to implement Gauss Jordan Elimination algorithm. In linear algebra, Gaussian elimination (also known as row reduction) is an algorithm for solving systems of linear equations. It is usually understood as a sequence of operations performed on the associated matrix of coefficients. This method can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix.*/
|
||||
|
||||
|
||||
#include<iostream>
|
||||
#include<conio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, j, k, n;
|
||||
float a[10][10] = { 0 }, d;
|
||||
cout << "No of equations ? ";
|
||||
cin >> n;
|
||||
cout << "Read all coefficients of matrix with b matrix too " << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
for (j = 1; j <= n; j++)
|
||||
cin >> a[i][j];
|
||||
for (i = 1; i <= n; i++)
|
||||
for (j = 1; j <= 2 * n; j++)
|
||||
if (j == (i + n))
|
||||
a[i][j] = 1;
|
||||
/************** partial pivoting **************/
|
||||
for (i = n; i > 1; i--)
|
||||
{
|
||||
if (a[i - 1][1] < a[i][1])
|
||||
for (j = 1; j <= n * 2; j++)
|
||||
{
|
||||
d = a[i][j];
|
||||
a[i][j] = a[i - 1][j];
|
||||
a[i - 1][j] = d;
|
||||
}
|
||||
}
|
||||
cout << "pivoted output: " << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
for (j = 1; j <= n * 2; j++)
|
||||
cout << a[i][j] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
/********** reducing to diagonal matrix ***********/
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
for (j = 1; j <= n * 2; j++)
|
||||
if (j != i)
|
||||
{
|
||||
d = a[j][i] / a[i][i];
|
||||
for (k = 1; k <= n * 2; k++)
|
||||
a[j][k] -= a[i][k] * d;
|
||||
}
|
||||
}
|
||||
/************** reducing to unit matrix *************/
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
d = a[i][i];
|
||||
for (j = 1; j <= n * 2; j++)
|
||||
a[i][j] = a[i][j] / d;
|
||||
}
|
||||
cout << "your solutions: " << endl;
|
||||
for (i = 1; i <= n; i++)
|
||||
{
|
||||
for (j = n + 1; j <= n * 2; j++)
|
||||
cout << a[i][j] << " ";
|
||||
cout << endl;
|
||||
}
|
||||
getch();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*No of equations ? 3
|
||||
Read all coefficients of matrix with b matrix too
|
||||
2 3 4
|
||||
5 6 3
|
||||
9 8 6
|
||||
pivoted output:
|
||||
9 8 6 0 0 1
|
||||
2 3 4 1 0 0
|
||||
5 6 3 0 1 0
|
||||
your solutions:
|
||||
-0.292683 -0.341463 0.365854
|
||||
0.0731707 0.585366 -0.341463
|
||||
0.341463 -0.268293 0.0731708*/
|
@ -1,189 +0,0 @@
|
||||
/*This is a C++ Program to convert NFA to DFA. A DFA (Deterministic Finite Automaton) is a finite state machine where from each state and a given input symbol, the next possible state is uniquely determined. On the other hand, an NFA (Non-Deterministic Finite Automaton) can move to several possible next states from a given state and a given input symbol. However, this does not add any more power to the machine. It still accepts the same set of languages, namely the regular languages. It is possible to convert an NFA to an equivalent DFA using the powerset construction.
|
||||
The intuition behind this scheme is that an NFA can be in several possible states at any time. We can simulate it with a DFA whose states correspond to sets of states of the underlying NFA.*/
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#define MAX_NFA_STATES 10
|
||||
#define MAX_ALPHABET_SIZE 10
|
||||
using namespace std;
|
||||
// Representation of an NFA state
|
||||
class NFAstate
|
||||
{
|
||||
public:
|
||||
int transitions[MAX_ALPHABET_SIZE][MAX_NFA_STATES];
|
||||
NFAstate()
|
||||
{
|
||||
for (int i = 0; i < MAX_ALPHABET_SIZE; i++)
|
||||
for (int j = 0; j < MAX_NFA_STATES; j++)
|
||||
transitions[i][j] = -1;
|
||||
}
|
||||
}*NFAstates;
|
||||
// Representation of a DFA state
|
||||
struct DFAstate
|
||||
{
|
||||
bool finalState;
|
||||
bitset<MAX_NFA_STATES> constituentNFAstates;
|
||||
bitset<MAX_NFA_STATES> transitions[MAX_ALPHABET_SIZE];
|
||||
int symbolicTransitions[MAX_ALPHABET_SIZE];
|
||||
};
|
||||
set<int> NFA_finalStates;
|
||||
vector<int> DFA_finalStates;
|
||||
vector<DFAstate*> DFAstates;
|
||||
queue<int> incompleteDFAstates;
|
||||
int N, M; // N -> No. of stattes, M -> Size of input alphabet
|
||||
// finds the epsilon closure of the NFA state "state" and stores it into "closure"
|
||||
void epsilonClosure(int state, bitset<MAX_NFA_STATES> &closure)
|
||||
{
|
||||
for (int i = 0; i < N && NFAstates[state].transitions[0][i] != -1; i++)
|
||||
if (closure[NFAstates[state].transitions[0][i]] == 0)
|
||||
{
|
||||
closure[NFAstates[state].transitions[0][i]] = 1;
|
||||
epsilonClosure(NFAstates[state].transitions[0][i], closure);
|
||||
}
|
||||
}
|
||||
// finds the epsilon closure of a set of NFA states "state" and stores it into "closure"
|
||||
void epsilonClosure(bitset<MAX_NFA_STATES> state,
|
||||
bitset<MAX_NFA_STATES> &closure)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
if (state[i] == 1)
|
||||
epsilonClosure(i, closure);
|
||||
}
|
||||
// returns a bitset representing the set of states the NFA could be in after moving
|
||||
// from state X on input symbol A
|
||||
void NFAmove(int X, int A, bitset<MAX_NFA_STATES> &Y)
|
||||
{
|
||||
for (int i = 0; i < N && NFAstates[X].transitions[A][i] != -1; i++)
|
||||
Y[NFAstates[X].transitions[A][i]] = 1;
|
||||
}
|
||||
// returns a bitset representing the set of states the NFA could be in after moving
|
||||
// from the set of states X on input symbol A
|
||||
void NFAmove(bitset<MAX_NFA_STATES> X, int A, bitset<MAX_NFA_STATES> &Y)
|
||||
{
|
||||
for (int i = 0; i < N; i++)
|
||||
if (X[i] == 1)
|
||||
NFAmove(i, A, Y);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int i, j, X, Y, A, T, F, D;
|
||||
// read in the underlying NFA
|
||||
ifstream fin("NFA.txt");
|
||||
fin >> N >> M;
|
||||
NFAstates = new NFAstate[N];
|
||||
fin >> F;
|
||||
for (i = 0; i < F; i++)
|
||||
{
|
||||
fin >> X;
|
||||
NFA_finalStates.insert(X);
|
||||
}
|
||||
fin >> T;
|
||||
while (T--)
|
||||
{
|
||||
fin >> X >> A >> Y;
|
||||
for (i = 0; i < Y; i++)
|
||||
{
|
||||
fin >> j;
|
||||
NFAstates[X].transitions[A][i] = j;
|
||||
}
|
||||
}
|
||||
fin.close();
|
||||
// construct the corresponding DFA
|
||||
D = 1;
|
||||
DFAstates.push_back(new DFAstate);
|
||||
DFAstates[0]->constituentNFAstates[0] = 1;
|
||||
epsilonClosure(0, DFAstates[0]->constituentNFAstates);
|
||||
for (j = 0; j < N; j++)
|
||||
if (DFAstates[0]->constituentNFAstates[j] == 1 && NFA_finalStates.find(
|
||||
j) != NFA_finalStates.end())
|
||||
{
|
||||
DFAstates[0]->finalState = true;
|
||||
DFA_finalStates.push_back(0);
|
||||
break;
|
||||
}
|
||||
incompleteDFAstates.push(0);
|
||||
while (!incompleteDFAstates.empty())
|
||||
{
|
||||
X = incompleteDFAstates.front();
|
||||
incompleteDFAstates.pop();
|
||||
for (i = 1; i <= M; i++)
|
||||
{
|
||||
NFAmove(DFAstates[X]->constituentNFAstates, i,
|
||||
DFAstates[X]->transitions[i]);
|
||||
epsilonClosure(DFAstates[X]->transitions[i],
|
||||
DFAstates[X]->transitions[i]);
|
||||
for (j = 0; j < D; j++)
|
||||
if (DFAstates[X]->transitions[i]
|
||||
== DFAstates[j]->constituentNFAstates)
|
||||
{
|
||||
DFAstates[X]->symbolicTransitions[i] = j;
|
||||
break;
|
||||
}
|
||||
if (j == D)
|
||||
{
|
||||
DFAstates[X]->symbolicTransitions[i] = D;
|
||||
DFAstates.push_back(new DFAstate);
|
||||
DFAstates[D]->constituentNFAstates
|
||||
= DFAstates[X]->transitions[i];
|
||||
for (j = 0; j < N; j++)
|
||||
if (DFAstates[D]->constituentNFAstates[j] == 1
|
||||
&& NFA_finalStates.find(j) != NFA_finalStates.end())
|
||||
{
|
||||
DFAstates[D]->finalState = true;
|
||||
DFA_finalStates.push_back(D);
|
||||
break;
|
||||
}
|
||||
incompleteDFAstates.push(D);
|
||||
D++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// write out the corresponding DFA
|
||||
ofstream fout("DFA.txt");
|
||||
fout << D << " " << M << "\n" << DFA_finalStates.size();
|
||||
for (vector<int>::iterator it = DFA_finalStates.begin(); it
|
||||
!= DFA_finalStates.end(); it++)
|
||||
fout << " " << *it;
|
||||
fout << "\n";
|
||||
for (i = 0; i < D; i++)
|
||||
{
|
||||
for (j = 1; j <= M; j++)
|
||||
fout << i << " " << j << " "
|
||||
<< DFAstates[i]->symbolicTransitions[j] << "\n";
|
||||
}
|
||||
fout.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Input file
|
||||
NFA.txt
|
||||
4 2
|
||||
2 0 1
|
||||
4
|
||||
0 1 2 1 2
|
||||
1 1 2 1 2
|
||||
2 2 2 1 3
|
||||
3 1 2 1 2
|
||||
|
||||
Output file
|
||||
DFA.txt
|
||||
4 2
|
||||
3 0 1 3
|
||||
0 1 1
|
||||
0 2 2
|
||||
1 1 1
|
||||
1 2 3
|
||||
2 1 2
|
||||
2 2 2
|
||||
3 1 1
|
||||
3 2 2
|
@ -1,121 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Decode a Message Encoded Using Playfair Cipher
|
||||
This C++ program decodes any message encoded using the technique of traditional playfair cipher. The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher. Input is not case sensitive and works only for characters from ‘a’ to ‘z’ and ‘A’ to ‘Z’.
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
const char encoder[5][5] = {{'A','B','C','D','E'},
|
||||
{'F','G','H','I','K'},
|
||||
{'L','M','N','O','P'},
|
||||
{'Q','R','S','T','U'},
|
||||
{'V','W','X','Y','Z'}
|
||||
};
|
||||
|
||||
void input_string(vector<char>& a)
|
||||
{
|
||||
char c;
|
||||
while (1)
|
||||
{
|
||||
c=getchar();
|
||||
if (c >= 97 && c <= 122)
|
||||
c -= 32;
|
||||
if (c == '\n')
|
||||
break;
|
||||
else if (c==' ')
|
||||
continue;
|
||||
else if (c == 'J')
|
||||
a.push_back('I');
|
||||
a.push_back(c);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void get_pos(char p, int& r, int& c)
|
||||
{
|
||||
if (p < 'J')
|
||||
{
|
||||
r = (p - 65) / 5;
|
||||
c = (p - 65) % 5;
|
||||
}
|
||||
else if (p > 'J')
|
||||
{
|
||||
r = (p - 66) / 5;
|
||||
c = (p - 66) % 5;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void same_row(int r, vector<char>& code, int c1, int c2)
|
||||
{
|
||||
code.push_back(encoder[r][(c1 + 4) % 5]);
|
||||
code.push_back(encoder[r][(c2 + 4) % 5]);
|
||||
return;
|
||||
}
|
||||
|
||||
void same_column(int c, vector<char>& code, int r1, int r2)
|
||||
{
|
||||
code.push_back(encoder[(r1 + 4) % 5][c]);
|
||||
code.push_back(encoder[(r2 + 4) % 5][c]);
|
||||
return;
|
||||
}
|
||||
|
||||
void diff_col_row(int r1, int c1, vector<char>& code, int r2, int c2)
|
||||
{
|
||||
code.push_back(encoder[r1][c2]);
|
||||
code.push_back(encoder[r2][c1]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void encode(vector<char> msgx, int len)
|
||||
{
|
||||
vector<char> code;
|
||||
int i = 0, j = 0;
|
||||
int r1, c1, r2, c2;
|
||||
while (i < len)
|
||||
{
|
||||
get_pos(msgx[i], r1, c1);
|
||||
i++;
|
||||
get_pos(msgx[i], r2, c2);
|
||||
if (r1 == r2)
|
||||
{
|
||||
same_row(r1, code, c1, c2);
|
||||
}
|
||||
else if (c1 == c2)
|
||||
{
|
||||
same_column(c1, code, r1, r2);
|
||||
}
|
||||
else
|
||||
{
|
||||
diff_col_row(r1, c1, code, r2, c2);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
cout<<"\nCODE: ";
|
||||
for (j = 0; j < code.size(); j++)
|
||||
{
|
||||
if (code[j] == 'X')
|
||||
continue;
|
||||
cout<<code[j];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<char> msg;
|
||||
std::cout<<"Enter the Encrypted Message:";
|
||||
input_string(msg);
|
||||
int len=msg.size();
|
||||
encode(msg,len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the Encrypted Message:CBNVMPPO
|
||||
CODE: BALLOON
|
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Encode a Message Using Playfair Cipher
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
void get_pos(char, int&, int&);
|
||||
void same_row(int, vector<char>&, int, int);
|
||||
void same_column(int, vector<char>&, int, int);
|
||||
void diff_col_row(int, int, vector<char>&, int, int);
|
||||
void encode(vector<char>, int);
|
||||
void get_input(vector<char>&);
|
||||
void convert_string(vector<char>&, vector<char>&);
|
||||
|
||||
const char encoder[5][5]= {{'A','B','C','D','E'},
|
||||
{'F','G','H','I','K'},
|
||||
{'L','M','N','O','P'},
|
||||
{'Q','R','S','T','U'},
|
||||
{'V','W','X','Y','Z'}
|
||||
};
|
||||
|
||||
void get_pos(char p, int& r, int& c)
|
||||
{
|
||||
if (p < 'J')
|
||||
{
|
||||
r = (p - 65) / 5;
|
||||
c = (p - 65) % 5;
|
||||
}
|
||||
else if (p > 'J')
|
||||
{
|
||||
r = (p - 66) / 5;
|
||||
c = (p - 66) % 5;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void same_row(int r, vector<char>& code, int c1, int c2)
|
||||
{
|
||||
code.push_back(encoder[r][(c1 + 1) % 5]);
|
||||
code.push_back(encoder[r][(c2 + 1) % 5]);
|
||||
return;
|
||||
}
|
||||
|
||||
void same_column(int c, vector<char>& code, int r1, int r2)
|
||||
{
|
||||
code.push_back(encoder[(r1 + 1) % 5][c]);
|
||||
code.push_back(encoder[(r2 + 1) % 5][c]);
|
||||
return;
|
||||
}
|
||||
|
||||
void diff_col_row(int r1, int c1, vector<char>& code, int r2, int c2)
|
||||
{
|
||||
code.push_back(encoder[r1][c2]);
|
||||
code.push_back(encoder[r2][c1]);
|
||||
return;
|
||||
}
|
||||
|
||||
void encode(vector<char> msgx, int len)
|
||||
{
|
||||
vector<char> code;
|
||||
int i = 0, j = 0;
|
||||
int r1, c1, r2, c2;
|
||||
while (i < len)
|
||||
{
|
||||
get_pos(msgx[i], r1, c1);
|
||||
i++;
|
||||
get_pos(msgx[i], r2, c2);
|
||||
if (r1 == r2)
|
||||
{
|
||||
same_row(r1, code, c1, c2);
|
||||
}
|
||||
else if (c1 == c2)
|
||||
{
|
||||
same_column(c1, code, r1, r2);
|
||||
}
|
||||
else
|
||||
{
|
||||
diff_col_row(r1, c1, code, r2, c2);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
cout<<"\nCODE: ";
|
||||
for (j = 0; j < code.size(); j++)
|
||||
{
|
||||
cout<<code[j];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void get_input(vector<char>& a)
|
||||
{
|
||||
char c;
|
||||
while (1)
|
||||
{
|
||||
c = getchar();
|
||||
if (c >= 97 && c <= 122)
|
||||
c- =32;
|
||||
if (c == '\n')
|
||||
break;
|
||||
else if (c==' ')
|
||||
continue;
|
||||
else if (c == 'J')
|
||||
a.push_back('I');
|
||||
a.push_back(c);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void convert_string(vector<char>& msg, vector<char>& msgx)
|
||||
{
|
||||
int i, j;
|
||||
i = j = 0;
|
||||
while (i < msg.size())
|
||||
{
|
||||
msgx.push_back(msg[i]);
|
||||
i++;
|
||||
if (i == msg.size())
|
||||
{
|
||||
msgx.push_back('X');
|
||||
break;
|
||||
}
|
||||
if (msg[i] == msgx[j])
|
||||
{
|
||||
msgx.push_back('X');
|
||||
j++;
|
||||
}
|
||||
else if(msg[i] != msgx[j])
|
||||
{
|
||||
j++;
|
||||
msgx.push_back(msg[i]);
|
||||
i+ = 1;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<char> msg;
|
||||
vector<char> msgx;
|
||||
int i, j;
|
||||
cout<<"Enter Message to Encrypt:";
|
||||
get_input(msg);
|
||||
convert_string(msg, msgx);
|
||||
int len = msgx.size();
|
||||
/*
|
||||
cout<<"\n\n";
|
||||
for (i = 0;i < len;i++)
|
||||
cout<<msgx[i];
|
||||
*///this is the string after making pairs of 2
|
||||
encode(msgx, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter Message to Encrypt:Balloon
|
||||
CODE: CBNVMPPO
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Find Length of Longest Common Substring
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
|
||||
// A utility function to find maximum of two integers
|
||||
int max(int a, int b)
|
||||
{
|
||||
return (a > b)? a : b;
|
||||
}
|
||||
|
||||
/* Returns length of longest common substring of X[0..m-1] and Y[0..n-1] */
|
||||
int LCSubStr(char *X, char *Y, int m, int n)
|
||||
{
|
||||
int LCSuff[m + 1][n + 1];
|
||||
int result = 0;
|
||||
for (int i = 0; i <= m; i++)
|
||||
{
|
||||
for (int j=0; j<=n; j++)
|
||||
{
|
||||
if (i == 0 || j == 0)
|
||||
LCSuff[i][j] = 0;
|
||||
else if (X[i-1] == Y[j-1])
|
||||
{
|
||||
LCSuff[i][j] = LCSuff[i-1][j-1] + 1;
|
||||
result = max(result, LCSuff[i][j]);
|
||||
}
|
||||
else LCSuff[i][j] = 0;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*Main */
|
||||
int main()
|
||||
{
|
||||
char X[] = "Sanfoundry";
|
||||
char Y[] = "foundation";
|
||||
int m = strlen(X);
|
||||
int n = strlen(Y);
|
||||
cout << "Length of Longest Common Substring is " << LCSubStr(X, Y, m, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Length of Longest Common Substring is 5
|
@ -1,59 +0,0 @@
|
||||
/*This is a C++ Program to implement LCS. The longest common subsequence (LCS) problem is to find the longest subsequence common to all sequences in a set of sequences (often just two). (Note that a subsequence is different from a substring, for the terms of the former need not be consecutive terms of the original sequence.) It is a classic computer science problem, the basis of data comparison programs such as the diff utility, and has applications in bioinformatics.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#define ARRAY_SIZE(A) sizeof(A)/sizeof(A[0])
|
||||
// Binary search (note boundaries in the caller)
|
||||
// A[] is ceilIndex in the caller
|
||||
int CeilIndex(int A[], int l, int r, int key)
|
||||
{
|
||||
int m;
|
||||
while (r - l > 1)
|
||||
{
|
||||
m = l + (r - l) / 2;
|
||||
(A[m] >= key ? r : l) = m; // ternary expression returns an l-value
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int LongestIncreasingSubsequenceLength(int A[], int size)
|
||||
{
|
||||
// Add boundary case, when array size is one
|
||||
int *tailTable = new int[size];
|
||||
int len; // always points empty slot
|
||||
memset(tailTable, 0, sizeof(tailTable[0]) * size);
|
||||
tailTable[0] = A[0];
|
||||
len = 1;
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
if (A[i] < tailTable[0])
|
||||
// new smallest value
|
||||
tailTable[0] = A[i];
|
||||
else if (A[i] > tailTable[len - 1])
|
||||
// A[i] wants to extend largest subsequence
|
||||
tailTable[len++] = A[i];
|
||||
else
|
||||
// A[i] wants to be current end candidate of an existing subsequence
|
||||
// It will replace ceil value in tailTable
|
||||
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
|
||||
}
|
||||
delete[] tailTable;
|
||||
return len;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int A[] = { 2, 5, 3, 7, 11, 8, 10, 13, 6 };
|
||||
int n = ARRAY_SIZE(A);
|
||||
printf("Length of Longest Increasing Subsequence is %d\n",
|
||||
LongestIncreasingSubsequenceLength(A, n));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Length of Longest Increasing Subsequence is 6
|
@ -1,57 +0,0 @@
|
||||
/*This is a C++ Program to implement Affine Cipher. The affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function (ax+b)mod(26), where b is the magnitude of the shift.*/
|
||||
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
using namespace std;
|
||||
string encryptionMessage(string Msg)
|
||||
{
|
||||
string CTxt = "";
|
||||
int a = 3;
|
||||
int b = 6;
|
||||
for (int i = 0; i < Msg.length(); i++)
|
||||
{
|
||||
CTxt = CTxt + (char) ((((a * Msg[i]) + b) % 26) + 65);
|
||||
}
|
||||
return CTxt;
|
||||
}
|
||||
|
||||
string decryptionMessage(string CTxt)
|
||||
{
|
||||
string Msg = "";
|
||||
int a = 3;
|
||||
int b = 6;
|
||||
int a_inv = 0;
|
||||
int flag = 0;
|
||||
for (int i = 0; i < 26; i++)
|
||||
{
|
||||
flag = (a * i) % 26;
|
||||
if (flag == 1)
|
||||
{
|
||||
a_inv = i;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < CTxt.length(); i++)
|
||||
{
|
||||
Msg = Msg + (char) (((a_inv * ((CTxt[i] - b)) % 26)) + 65);
|
||||
}
|
||||
return Msg;
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Enter the message: ";
|
||||
string message;
|
||||
cin >> message;
|
||||
cout << "Message is :" << message;
|
||||
cout << "\nEncrypted Message is : " << encryptionMessage(message);
|
||||
cout << "\nDecrypted Message is: " << decryptionMessage(
|
||||
encryptionMessage(message));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
Enter the message: SANFOUNDRY
|
||||
Message is :SANFOUNDRY
|
||||
Encrypted Message is : VTGIJBGCSN
|
||||
Decrypted Message is: SANFOUNDRY
|
@ -1,146 +0,0 @@
|
||||
/*n computer science, the Aho–Corasick string matching algorithm is a string searching algorithm, it is a kind of dictionary-matching algorithm that locates elements of a finite set of strings (the “dictionary”) within an input text. It matches all patterns simultaneously. The complexity of the algorithm is linear in the length of the patterns plus the length of the searched text plus the number of output matches. Note that because all matches are found, there can be a quadratic number of matches if every substring matches (e.g. dictionary = a, aa, aaa, aaaa and input string is aaaa).*/
|
||||
|
||||
using namespace std;
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <cassert>
|
||||
#include <climits>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <queue>
|
||||
#include <deque>
|
||||
#include <stack>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
|
||||
#define For(i, a, b) for (int i=(a); i<(b); ++i)
|
||||
#define D(x) cout << #x " is " << x << endl
|
||||
|
||||
const int MAXS = 6 * 50 + 10; // Max number of states in the matching machine.
|
||||
// Should be equal to the sum of the length of all keywords.
|
||||
|
||||
const int MAXC = 26; // Number of characters in the alphabet.
|
||||
|
||||
int out[MAXS]; // Output for each state, as a bitwise mask.
|
||||
int f[MAXS]; // Failure function
|
||||
int g[MAXS][MAXC]; // Goto function, or -1 if fail.
|
||||
|
||||
int buildMatchingMachine(const vector<string> &words, char lowestChar = 'a',
|
||||
char highestChar = 'z')
|
||||
{
|
||||
memset(out, 0, sizeof out);
|
||||
memset(f, -1, sizeof f);
|
||||
memset(g, -1, sizeof g);
|
||||
int states = 1; // Initially, we just have the 0 state
|
||||
for (int i = 0; i < words.size(); ++i)
|
||||
{
|
||||
const string &keyword = words[i];
|
||||
int currentState = 0;
|
||||
for (int j = 0; j < keyword.size(); ++j)
|
||||
{
|
||||
int c = keyword[j] - lowestChar;
|
||||
if (g[currentState][c] == -1)
|
||||
{
|
||||
// Allocate a new node
|
||||
g[currentState][c] = states++;
|
||||
}
|
||||
currentState = g[currentState][c];
|
||||
}
|
||||
out[currentState] |= (1 << i); // There's a match of keywords[i] at node currentState.
|
||||
}
|
||||
// State 0 should have an outgoing edge for all characters.
|
||||
for (int c = 0; c < MAXC; ++c)
|
||||
{
|
||||
if (g[0][c] == -1)
|
||||
{
|
||||
g[0][c] = 0;
|
||||
}
|
||||
}
|
||||
// Now, let's build the failure function
|
||||
queue<int> q;
|
||||
for (int c = 0; c <= highestChar - lowestChar; ++c)
|
||||
{
|
||||
// Iterate over every possible input
|
||||
// All nodes s of depth 1 have f[s] = 0
|
||||
if (g[0][c] != -1 and g[0][c] != 0)
|
||||
{
|
||||
f[g[0][c]] = 0;
|
||||
q.push(g[0][c]);
|
||||
}
|
||||
}
|
||||
while (q.size())
|
||||
{
|
||||
int state = q.front();
|
||||
q.pop();
|
||||
for (int c = 0; c <= highestChar - lowestChar; ++c)
|
||||
{
|
||||
if (g[state][c] != -1)
|
||||
{
|
||||
int failure = f[state];
|
||||
while (g[failure][c] == -1)
|
||||
{
|
||||
failure = f[failure];
|
||||
}
|
||||
failure = g[failure][c];
|
||||
f[g[state][c]] = failure;
|
||||
out[g[state][c]] |= out[failure]; // Merge out values
|
||||
q.push(g[state][c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return states;
|
||||
}
|
||||
int findNextState(int currentState, char nextInput, char lowestChar = 'a')
|
||||
{
|
||||
int answer = currentState;
|
||||
int c = nextInput - lowestChar;
|
||||
while (g[answer][c] == -1)
|
||||
answer = f[answer];
|
||||
return g[answer][c];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<string> keywords;
|
||||
keywords.push_back("he");
|
||||
keywords.push_back("she");
|
||||
keywords.push_back("hers");
|
||||
keywords.push_back("his");
|
||||
string text = "ahishers";
|
||||
buildMatchingMachine(keywords, 'a', 'z');
|
||||
int currentState = 0;
|
||||
for (int i = 0; i < text.size(); ++i)
|
||||
{
|
||||
currentState = findNextState(currentState, text[i], 'a');
|
||||
if (out[currentState] == 0)
|
||||
continue; // Nothing new, let's move on to the next character.
|
||||
for (int j = 0; j < keywords.size(); ++j)
|
||||
{
|
||||
if (out[currentState] & (1 << j))
|
||||
{
|
||||
// Matched keywords[j]
|
||||
cout << "Keyword " << keywords[j] << " appears from " << i
|
||||
- keywords[j].size() + 1 << " to " << i << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
Keyword his appears from 1 to 3
|
||||
Keyword he appears from 4 to 5
|
||||
Keyword she appears from 3 to 5
|
||||
Keyword hers appears from 4 to 7
|
@ -1,64 +0,0 @@
|
||||
/*This is a C++ Program to Implement Bitap Algorithm. The bitap algorithm (also known as the shift-or, shift-and or Baeza-Yates–Gonnet algorithm) is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is “approximately equal” to a given pattern, where approximate equality is defined in terms of Levenshtein distance — if the substring and pattern are within a given distance k of each other, then the algorithm considers them equal. The algorithm begins by precomputing a set of bitmasks containing one bit for each element of the pattern. Then it is able to do most of the work with bitwise operations, which are extremely fast.*/
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
int bitap_search(string text, string pattern)
|
||||
{
|
||||
int m = pattern.length();
|
||||
long pattern_mask[256];
|
||||
/** Initialize the bit array R **/
|
||||
long R = ~1;
|
||||
if (m == 0)
|
||||
return -1;
|
||||
if (m > 63)
|
||||
{
|
||||
cout<<"Pattern is too long!";
|
||||
return -1;
|
||||
}
|
||||
/** Initialize the pattern bitmasks **/
|
||||
for (int i = 0; i <= 255; ++i)
|
||||
pattern_mask[i] = ~0;
|
||||
for (int i = 0; i < m; ++i)
|
||||
pattern_mask[pattern[i]] &= ~(1L << i);
|
||||
for (int i = 0; i < text.length(); ++i)
|
||||
{
|
||||
/** Update the bit array **/
|
||||
R |= pattern_mask[text[i]];
|
||||
R <<= 1;
|
||||
if ((R & (1L << m)) == 0)
|
||||
return i - m + 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
void findPattern(string t, string p)
|
||||
{
|
||||
int pos = bitap_search(t, p);
|
||||
if (pos == -1)
|
||||
cout << "\nNo Match\n";
|
||||
else
|
||||
cout << "\nPattern found at position : " << pos;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
cout << "Bitap Algorithm Test\n";
|
||||
cout << "Enter Text\n";
|
||||
string text;
|
||||
cin >> text;
|
||||
cout << "Enter Pattern\n";
|
||||
string pattern;
|
||||
cin >> pattern;
|
||||
findPattern(text, pattern);
|
||||
}
|
||||
|
||||
/*
|
||||
Bitap Algorithm Test
|
||||
Enter Text
|
||||
DharmendraHingu
|
||||
Enter Pattern
|
||||
Hingu
|
||||
|
||||
Pattern found at position : 10
|
@ -1,61 +0,0 @@
|
||||
/*This is a C++ Program to implement Boyer-Moore algorithm. The idea of bad character heuristic is simple. The character of the text which doesn’t match with the current character of pattern is called the Bad Character. Whenever a character doesn’t match, we slide the pattern in such a way that aligns the bad character with the last occurrence of it in pattern. We preprocess the pattern and store the last occurrence of every possible character in an array of size equal to alphabet size. If the character is not present at all, then it may result in a shift by m (length of pattern). Therefore, the bad character heuristic takes O(n/m) time in the best case.*/
|
||||
|
||||
/* Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm */
|
||||
|
||||
# include <limits.h>
|
||||
# include <string.h>
|
||||
# include <stdio.h>
|
||||
|
||||
# define NO_OF_CHARS 256
|
||||
|
||||
// A utility function to get maximum of two integers
|
||||
int max(int a, int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
// The preprocessing function for Boyer Moore's bad character heuristic
|
||||
void badCharHeuristic(char *str, int size, int badchar[NO_OF_CHARS])
|
||||
{
|
||||
int i;
|
||||
// Initialize all occurrences as -1
|
||||
for (i = 0; i < NO_OF_CHARS; i++)
|
||||
badchar[i] = -1;
|
||||
// Fill the actual value of last occurrence of a character
|
||||
for (i = 0; i < size; i++)
|
||||
badchar[(int) str[i]] = i;
|
||||
}
|
||||
|
||||
void search(char *txt, char *pat)
|
||||
{
|
||||
int m = strlen(pat);
|
||||
int n = strlen(txt);
|
||||
int badchar[NO_OF_CHARS];
|
||||
badCharHeuristic(pat, m, badchar);
|
||||
int s = 0; // s is shift of the pattern with respect to text
|
||||
while (s <= (n - m))
|
||||
{
|
||||
int j = m - 1;
|
||||
while (j >= 0 && pat[j] == txt[s + j])
|
||||
j--;
|
||||
if (j < 0)
|
||||
{
|
||||
printf("\n pattern occurs at shift = %d", s);
|
||||
s += (s + m < n) ? m - badchar[txt[s + m]] : 1;
|
||||
}
|
||||
else
|
||||
s += max(1, j - badchar[txt[s + j]]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver program to test above funtion */
|
||||
int main()
|
||||
{
|
||||
char txt[] = "ABAAABCD";
|
||||
char pat[] = "ABC";
|
||||
search(txt, pat);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
pattern occurs at shift = 4
|
@ -1,40 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
char caesar(char);
|
||||
int main()
|
||||
{
|
||||
string input;
|
||||
do
|
||||
{
|
||||
cout << "Enter cipertext and press enter to continue." << endl;
|
||||
cout << "Enter blank line to quit." << endl;
|
||||
getline(cin, input);
|
||||
string output = "";
|
||||
for (int x = 0; x < input.length(); x++)
|
||||
{
|
||||
output += caesar(input[x]);
|
||||
}
|
||||
cout << output << endl;
|
||||
}
|
||||
while (!input.length() == 0);
|
||||
} //end main
|
||||
|
||||
char caesar(char c)
|
||||
{
|
||||
if (isalpha(c))
|
||||
{
|
||||
c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
|
||||
c = (((c - 65) + 13) % 26) + 65;
|
||||
}
|
||||
//if c isn't alpha, just send it back.
|
||||
return c;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter cipertext and press enter to continue.
|
||||
Enter blank line to quit.
|
||||
Sanfoundry
|
||||
FNASBHAQEL
|
||||
Enter cipertext and press enter to continue.
|
||||
Enter blank line to quit.
|
@ -1,39 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
#define MAX(X, Y) (X > Y) ? X : Y
|
||||
#define POS(X) (X > 0) ? X : 0
|
||||
|
||||
int maxSum = INT_MIN;
|
||||
int N;
|
||||
int kadane(int* row, int len)
|
||||
{
|
||||
int x, sum, maxSum = INT_MIN;
|
||||
for (sum = POS(row[0]), x = 0; x < N; ++x, sum = POS(sum + row[x]))
|
||||
maxSum = MAX(sum, maxSum);
|
||||
return maxSum;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Enter the array length: ";
|
||||
cin >> N;
|
||||
int arr[N];
|
||||
cout << "Enter the array: ";
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
cin >> arr[i];
|
||||
}
|
||||
cout << "The Max Sum is: "<<kadane(arr, N) << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter the array length: 5
|
||||
Enter the array: 1 -5 2 -1 3
|
||||
The Max Sum is: 4
|
||||
|
||||
Enter the array length: 9
|
||||
Enter the array: -2 1 -3 4 -1 2 1 -5 4
|
||||
The Max Sum is: 6
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Knuth–Morris–Pratt Algorithm (KMP)
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
using namespace std;
|
||||
void preKMP(string pattern, int f[])
|
||||
{
|
||||
int m = pattern.length(), k;
|
||||
f[0] = -1;
|
||||
for (int i = 1; i < m; i++)
|
||||
{
|
||||
k = f[i - 1];
|
||||
while (k >= 0)
|
||||
{
|
||||
if (pattern[k] == pattern[i - 1])
|
||||
break;
|
||||
else
|
||||
k = f[k];
|
||||
}
|
||||
f[i] = k + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//check whether target string contains pattern
|
||||
bool KMP(string pattern, string target)
|
||||
{
|
||||
int m = pattern.length();
|
||||
int n = target.length();
|
||||
int f[m];
|
||||
preKMP(pattern, f);
|
||||
int i = 0;
|
||||
int k = 0;
|
||||
while (i < n)
|
||||
{
|
||||
if (k == -1)
|
||||
{
|
||||
i++;
|
||||
k = 0;
|
||||
}
|
||||
else if (target[i] == pattern[k])
|
||||
{
|
||||
i++;
|
||||
k++;
|
||||
if (k == m)
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
k = f[k];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string tar = "san and linux training";
|
||||
string pat = "lin";
|
||||
if (KMP(pat, tar))
|
||||
cout<<"'"<<pat<<"' found in string '"<<tar<<"'"<<endl;
|
||||
else
|
||||
cout<<"'"<<pat<<"' not found in string '"<<tar<<"'"<<endl;
|
||||
pat = "sanfoundry";
|
||||
if (KMP(pat, tar))
|
||||
cout<<"'"<<pat<<"' found in string '"<<tar<<"'"<<endl;
|
||||
else
|
||||
cout<<"'"<<pat<<"' not found in string '"<<tar<<"'"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
'lin' found in string 'san and linux training'
|
||||
'sanfoundry' not found in string 'san and linux training'
|
@ -1,42 +0,0 @@
|
||||
/*This is a C++ Program to find Levenshtein Distance. The Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
|
||||
For example, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
int d[100][100];
|
||||
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
||||
int main()
|
||||
{
|
||||
int i,j,m,n,temp,tracker;
|
||||
char s[] = "Sanfoundry";
|
||||
char t[] = "Education";
|
||||
m = strlen(s);
|
||||
n = strlen(t);
|
||||
for(i=0; i<=m; i++)
|
||||
d[0][i] = i;
|
||||
for(j=0; j<=n; j++)
|
||||
d[j][0] = j;
|
||||
for (j=1; j<=m; j++)
|
||||
{
|
||||
for(i=1; i<=n; i++)
|
||||
{
|
||||
if(s[i-1] == t[j-1])
|
||||
{
|
||||
tracker = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tracker = 1;
|
||||
}
|
||||
temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
|
||||
d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
|
||||
}
|
||||
}
|
||||
printf("the Levinstein distance is %d\n",d[n][m]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
the Levinstein distance is 9
|
@ -1,120 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Longest Prefix Matching
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<cstdlib>
|
||||
#include<cstring>
|
||||
#include<stack>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* node Declaration
|
||||
*/
|
||||
struct node
|
||||
{
|
||||
char data;
|
||||
node *child[128];
|
||||
node()
|
||||
{
|
||||
for (int i = 0; i < 128; i++)
|
||||
child[i] = NULL;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* trie class Declaration
|
||||
*/
|
||||
class trie
|
||||
{
|
||||
private:
|
||||
node *root;
|
||||
public:
|
||||
trie()
|
||||
{
|
||||
root = new_node(0);
|
||||
}
|
||||
|
||||
node *new_node(int data)
|
||||
{
|
||||
node *Q = new node;
|
||||
Q->data = data;
|
||||
return Q;
|
||||
}
|
||||
|
||||
void add(string S)
|
||||
{
|
||||
node *cur = root;
|
||||
for (int i = 0; i < S.length(); i++)
|
||||
{
|
||||
if (!cur->child[S[i] - 'A'])
|
||||
cur->child[S[i] - 'A'] = new_node(S[i]);
|
||||
cur = cur->child[S[i] - 'A'];
|
||||
}
|
||||
}
|
||||
|
||||
void check(node *cur, string S, int i)
|
||||
{
|
||||
if (cur)
|
||||
{
|
||||
cout<<cur->data;
|
||||
if (i < S.length())
|
||||
check(cur->child[S[i] - 'A'], S, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void checkroot(string S)
|
||||
{
|
||||
if (root && S.length() > 0 && S[0] > 'A')
|
||||
check(root->child[S[0] - 'A'],S,1);
|
||||
else
|
||||
cout<<"\nEmpty root \n";
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Main
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
trie dict;
|
||||
dict.add("are");
|
||||
dict.add("area");
|
||||
dict.add("base");
|
||||
dict.add("cat");
|
||||
dict.add("cater");
|
||||
dict.add("basement");
|
||||
string input;
|
||||
input = "caterer";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
input = "basement";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
input = "are";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
input = "arex";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
input = "basemexz";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
input = "xyz";
|
||||
cout<<input << ": ";
|
||||
dict.checkroot(input);
|
||||
cout<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
caterer: cater
|
||||
basement: basement
|
||||
are: are
|
||||
arex: are
|
||||
basemexz: baseme
|
||||
xyz:
|
@ -1,63 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Rabin-Karp Algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
using namespace std;
|
||||
#define d 256
|
||||
/*
|
||||
* search a substring in a string
|
||||
*/
|
||||
void search(char *pat, char *txt, int q)
|
||||
{
|
||||
int M = strlen(pat);
|
||||
int N = strlen(txt);
|
||||
int i, j;
|
||||
int p = 0;
|
||||
int t = 0;
|
||||
int h = 1;
|
||||
for (i = 0; i < M - 1; i++)
|
||||
h = (h * d) % q;
|
||||
for (i = 0; i < M; i++)
|
||||
{
|
||||
p = (d *p + pat[i]) % q;
|
||||
t = (d * t + txt[i]) % q;
|
||||
}
|
||||
for (i = 0; i <= N - M; i++)
|
||||
{
|
||||
if (p == t)
|
||||
{
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (txt[i + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M)
|
||||
{
|
||||
cout<<"Pattern found at index: "<<i<<endl;
|
||||
}
|
||||
}
|
||||
if (i < N - M)
|
||||
{
|
||||
t = (d * (t - txt[i] * h) + txt[i + M]) % q;
|
||||
if (t < 0)
|
||||
t = (t + q);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Main */
|
||||
int main()
|
||||
{
|
||||
char *txt = "Sanfoundry Linux Training";
|
||||
char *pat = "nux";
|
||||
int q = 101;
|
||||
search(pat, txt, q);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Pattern found at index: 13
|
@ -1,97 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement String Matching Using Vectors
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
void input_string(vector<char>& str)
|
||||
{
|
||||
char a;
|
||||
while (1)
|
||||
{
|
||||
a = getchar();
|
||||
if (a == '\n')
|
||||
break;
|
||||
str.push_back(a);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void print_string(vector<char> strn)
|
||||
{
|
||||
for (std::vector<char>::iterator it = strn.begin(); it != strn.end(); ++it)
|
||||
{
|
||||
cout<<*it;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int match_string(vector<char>& original, vector<char> match)
|
||||
{
|
||||
vector<char>::iterator p,q, r;
|
||||
int i = 0;
|
||||
p = original. begin();
|
||||
while (r <= match.end() && p <= original.end())
|
||||
{
|
||||
r = match.begin();
|
||||
while (*p != *r && p < original.end())
|
||||
{
|
||||
p++;
|
||||
i++;
|
||||
}
|
||||
q = p;
|
||||
while (*p == *r && r <= match.end() && p<=original.end())
|
||||
{
|
||||
p++;
|
||||
i++;
|
||||
r++;
|
||||
}
|
||||
if (r >= match.end())
|
||||
{
|
||||
original.erase(original.begin(), q + 1);
|
||||
return (i - match.size() + 1);
|
||||
}
|
||||
if (p >= original.end())
|
||||
return 0;
|
||||
p = ++q;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<char> original,match;
|
||||
int i,result,k=0,sum=0;
|
||||
cout<<"Enter String:";
|
||||
input_string(original);
|
||||
cout<<"Enter Search Pattern:";
|
||||
input_string(match);
|
||||
if (match.size() > original.size())
|
||||
{
|
||||
cout<<"Error:Original string too small.";
|
||||
}
|
||||
do
|
||||
{
|
||||
result = match_string(original, match);
|
||||
sum += result; //to store previous found position
|
||||
if (result > 0)
|
||||
{
|
||||
k++;
|
||||
cout<<"\nMatch found from Position = "<<sum;
|
||||
}
|
||||
}
|
||||
while (result > 0); //loop to find all patterns
|
||||
if (k == 0)
|
||||
cout<<"Error:Match Not Found";
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter String:all men went to apall mall
|
||||
Enter Search Pattern:all
|
||||
|
||||
Match found from Position = 1
|
||||
Match found from Position = 19
|
||||
Match found from Position = 24
|
@ -1,41 +0,0 @@
|
||||
/*This is a C++ Program to implement online search. The Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
|
||||
For example, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits.*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
int d[100][100];
|
||||
#define MIN(x,y) ((x) < (y) ? (x) : (y))
|
||||
int main()
|
||||
{
|
||||
int i,j,m,n,temp,tracker;
|
||||
char s[] = "Sanfoundry";
|
||||
char t[] = "Education";
|
||||
m = strlen(s);
|
||||
n = strlen(t);
|
||||
for(i=0; i<=m; i++)
|
||||
d[0][i] = i;
|
||||
for(j=0; j<=n; j++)
|
||||
d[j][0] = j;
|
||||
for (j=1; j<=m; j++)
|
||||
{
|
||||
for(i=1; i<=n; i++)
|
||||
{
|
||||
if(s[i-1] == t[j-1])
|
||||
{
|
||||
tracker = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tracker = 1;
|
||||
}
|
||||
temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
|
||||
d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
|
||||
}
|
||||
}
|
||||
printf("the Levinstein distance is %d\n",d[n][m]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
the Levinstein distance is 9
|
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement Z-Algorithm
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
bool zAlgorithm(string pattern, string target)
|
||||
{
|
||||
string s = pattern + '$' + target;
|
||||
int n = s.length();
|
||||
vector<int> z(n, 0);
|
||||
int goal = pattern.length();
|
||||
int r = 0, l = 0, i;
|
||||
for (int k = 1; k < n; k++)
|
||||
{
|
||||
if (k > r)
|
||||
{
|
||||
for (i = k; i < n && s[i] == s[i - k]; i++);
|
||||
if (i > k)
|
||||
{
|
||||
z[k] = i - k;
|
||||
l = k;
|
||||
r = i - 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int kt = k - l, b = r - k + 1;
|
||||
if (z[kt] > b)
|
||||
{
|
||||
for (i = r + 1; i < n && s[i] == s[i - k]; i++);
|
||||
z[k] = i - k;
|
||||
l = k;
|
||||
r = i - 1;
|
||||
}
|
||||
}
|
||||
if (z[k] == goal)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string tar = "san and linux training";
|
||||
string pat = "lin";
|
||||
if (zAlgorithm(pat, tar))
|
||||
cout<<"'"<<pat<<"' found in string '"<<tar<<"'"<<endl;
|
||||
else
|
||||
cout<<"'"<<pat<<"' not found in string '"<<tar<<"'"<<endl;
|
||||
pat = "sanfoundry";
|
||||
if (zAlgorithm(pat, tar))
|
||||
cout<<"'"<<pat<<"' found in string '"<<tar<<"'"<<endl;
|
||||
else
|
||||
cout<<"'"<<pat<<"' not found in string '"<<tar<<"'"<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
'lin' found in string 'san and linux training'
|
||||
'sanfoundry' not found in string 'san and linux training'
|
@ -1,110 +0,0 @@
|
||||
/*This is a C++ Program to implement hill cipher. In classical cryptography, the Hill cipher is a polygraphic substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once. The following discussion assumes an elementary knowledge of matrices.*/
|
||||
|
||||
#include<stdio.h>
|
||||
#include<iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int check(int x)
|
||||
{
|
||||
if (x % 3 == 0)
|
||||
return 0;
|
||||
int a = x / 3;
|
||||
int b = 3 * (a + 1);
|
||||
int c = b - x;
|
||||
return c;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int l, i, j;
|
||||
int temp1;
|
||||
int k[3][3];
|
||||
int p[3][1];
|
||||
int c[3][1];
|
||||
char ch;
|
||||
cout
|
||||
<< "\nThis cipher has a key of length 9. ie. a 3*3 matrix.\nEnter the 9 character key. ";
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
for (j = 0; j < 3; ++j)
|
||||
{
|
||||
scanf("%c", &ch);
|
||||
if (65 <= ch && ch <= 91)
|
||||
k[i][j] = (int) ch % 65;
|
||||
else
|
||||
k[i][j] = (int) ch % 97;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
for (j = 0; j < 3; ++j)
|
||||
{
|
||||
cout << k[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << "\nEnter the length of string to be encoded(without spaces). ";
|
||||
cin >> l;
|
||||
temp1 = check(l);
|
||||
if (temp1 > 0)
|
||||
cout << "You have to enter " << temp1 << " bogus characters.";
|
||||
char pi[l + temp1];
|
||||
cout << "\nEnter the string. ";
|
||||
for (i = -1; i < l + temp1; ++i)
|
||||
{
|
||||
cin >> pi[i];
|
||||
}
|
||||
int temp2 = l;
|
||||
int n = (l + temp1) / 3;
|
||||
int temp3;
|
||||
int flag = 0;
|
||||
int count;
|
||||
cout << "\n\nThe encoded cipher is : ";
|
||||
while (n > 0)
|
||||
{
|
||||
count = 0;
|
||||
for (i = flag; i < flag + 3; ++i)
|
||||
{
|
||||
if (65 <= pi[i] && pi[i] <= 91)
|
||||
temp3 = (int) pi[i] % 65;
|
||||
else
|
||||
temp3 = (int) pi[i] % 97;
|
||||
p[count][0] = temp3;
|
||||
count = count + 1;
|
||||
}
|
||||
int k1;
|
||||
for (i = 0; i < 3; ++i)
|
||||
c[i][0] = 0;
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
for (j = 0; j < 1; ++j)
|
||||
{
|
||||
for (k1 = 0; k1 < 3; ++k1)
|
||||
c[i][j] += k[i][k1] * p[k1][j];
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
c[i][0] = c[i][0] % 26;
|
||||
printf("%c ", (char) (c[i][0] + 65));
|
||||
}
|
||||
n = n - 1;
|
||||
flag = flag + 3;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
This cipher has a key of length 9. ie. a 3*3 matrix.
|
||||
Enter the 9 character key. DharHingu
|
||||
3 7 0
|
||||
17 7 8
|
||||
13 6 20
|
||||
|
||||
Enter the length of string to be encoded(without spaces). 10
|
||||
You have to enter 2 bogus characters.
|
||||
Enter the string. Sanfoundry
|
||||
|
||||
The encoded cipher is : N B W A O Q Y Y X X D O
|
@ -1,88 +0,0 @@
|
||||
/*This is a C++ Program to implement monoalphaetic cipher. In cryptography, a substitution cipher is a method of encoding by which units of plaintext are replaced with ciphertext, according to a regular system; the “units” may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by performing an inverse substitution.
|
||||
Substitution ciphers can be compared with transposition ciphers. In a transposition cipher, the units of the plaintext are rearranged in a different and usually quite complex order, but the units themselves are left unchanged. By contrast, in a substitution cipher, the units of the plaintext are retained in the same sequence in the ciphertext, but the units themselves are altered.
|
||||
|
||||
There are a number of different types of substitution cipher. If the cipher operates on single letters, it is termed a simple substitution cipher; a cipher that operates on larger groups of letters is termed polygraphic. A monoalphabetic cipher uses fixed substitution over the entire message, whereas a polyalphabetic cipher uses a number of substitutions at different positions in the message, where a unit from the plaintext is mapped to one of several possibilities in the ciphertext and vice versa.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
// the rot13 function
|
||||
std::string rot13(std::string s)
|
||||
{
|
||||
static std::string const lcalph = "abcdefghijklmnopqrstuvwxyz", ucalph =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
std::string result;
|
||||
std::string::size_type pos;
|
||||
result.reserve(s.length());
|
||||
for (std::string::iterator it = s.begin(); it != s.end(); ++it)
|
||||
{
|
||||
if ((pos = lcalph.find(*it)) != std::string::npos)
|
||||
result.push_back(lcalph[(pos + 13) % 26]);
|
||||
else if ((pos = ucalph.find(*it)) != std::string::npos)
|
||||
result.push_back(ucalph[(pos + 13) % 26]);
|
||||
else
|
||||
result.push_back(*it);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// function to output the rot13 of a file on std::cout
|
||||
// returns false if an error occurred processing the file, true otherwise
|
||||
// on entry, the argument is must be open for reading
|
||||
int rot13_stream(std::istream& is)
|
||||
{
|
||||
std::string line;
|
||||
while (std::getline(is, line))
|
||||
{
|
||||
if (!(std::cout << rot13(line) << "\n"))
|
||||
return false;
|
||||
}
|
||||
return is.eof();
|
||||
}
|
||||
|
||||
// the main program
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
if (argc == 1) // no arguments given
|
||||
return rot13_stream(std::cin) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
std::ifstream file;
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
file.open(argv[i], std::ios::in);
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << argv[0] << ": could not open for reading: " << argv[i]
|
||||
<< "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (!rot13_stream(file))
|
||||
{
|
||||
if (file.eof())
|
||||
// no error occurred for file, so the error must have been in output
|
||||
std::cerr << argv[0] << ": error writing to stdout\n";
|
||||
else
|
||||
std::cerr << argv[0] << ": error reading from " << argv[i]
|
||||
<< "\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
file.clear();
|
||||
file.close();
|
||||
if (!file)
|
||||
std::cerr << argv[0] << ": warning: closing failed for " << argv[i]
|
||||
<< "\n";
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Dharmendra
|
||||
Qunezraqen
|
||||
Hingu
|
||||
Uvath
|
||||
Sanfoundry
|
||||
Fnasbhaqel
|
@ -1,92 +0,0 @@
|
||||
/*This C++ program encodes any message using the technique of one time pad cipher technique. Input is not case sensitive and works only for all characters. White spaces are not ignored but are produced as random characters in the decoded message.
|
||||
Note:Since the key is required for decryption, it is printed on stdout. However, it is not safe to make the key public.*/
|
||||
|
||||
/*
|
||||
* C++ Program to Implement the One Time Pad Algorithm.
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include<stdlib.h>
|
||||
using namespace std;
|
||||
void to_upper_case(vector<char>& text, int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
if (text[i] >= 97 && text[i] <= 122)
|
||||
text[i] -= 32;
|
||||
}
|
||||
}
|
||||
void print_string(vector<char> text, int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
cout << (char) (text[i] + 65);
|
||||
}
|
||||
cout << endl;
|
||||
return;
|
||||
}
|
||||
size_t get_input(vector<char>& msg)
|
||||
{
|
||||
char a;
|
||||
while (1)
|
||||
{
|
||||
a = getchar();
|
||||
if (a == '\n')
|
||||
break;
|
||||
msg.push_back(a);
|
||||
}
|
||||
return msg.size();
|
||||
}
|
||||
int main()
|
||||
{
|
||||
vector<char> msg;
|
||||
vector<char> enc_msg;
|
||||
vector<char> dec_msg;
|
||||
int *p;
|
||||
int i;
|
||||
size_t len;
|
||||
cout << "Enter Message to Encrypt:";
|
||||
len = get_input(msg);
|
||||
to_upper_case(msg, len);
|
||||
p = (int*) malloc(msg.size() * sizeof(int));
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
p[i] = rand() % 26;
|
||||
if (msg[i] >= 65 && msg[i] <= 90)
|
||||
enc_msg.push_back((char) ((msg[i] - 65 + p[i]) % 26));
|
||||
else if (msg[i] >= 97 && msg[i] <= 122)
|
||||
enc_msg.push_back((char) ((msg[i] - 97 + p[i]) % 26));
|
||||
else
|
||||
enc_msg.push_back((char) msg[i]);
|
||||
}
|
||||
cout << "\nEncoded Message:";
|
||||
print_string(enc_msg, len);
|
||||
cout << "\nKey for decryption:\n";
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
cout << (char) (p[i] + 65);
|
||||
}
|
||||
cout << endl;
|
||||
cout << "\nDecrypted Message:";
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
if ((enc_msg[i] - p[i]) < 0)
|
||||
dec_msg.push_back((char) (enc_msg[i] - p[i] + 26));
|
||||
else if ((enc_msg[i] - p[i]) >= 0)
|
||||
dec_msg.push_back((char) (enc_msg[i] - p[i]));
|
||||
else
|
||||
dec_msg.push_back((char) enc_msg[i]);
|
||||
}
|
||||
print_string(dec_msg, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Enter Message to Encrypt: This is the demonstration of OTP algorithm
|
||||
Encoded Message:IOYYaCEaTFPaOJPLSAKTVLKLTaPBaTGFaUICTENHGH
|
||||
|
||||
Key for decryption:
|
||||
PHQGHUMEAYLNLFDXFIRCVSCXGGBWKFNQDUXWFNFOZV
|
||||
|
||||
Decrypted Message:THISZIS]THETDEMONSTRATION[OFWOTP^ALGORITHM
|
@ -1,183 +0,0 @@
|
||||
/*This C++ program encodes any message using RSA Algorithm. Input is case sensitive and works only for all characters. RSA is one of the first practicable public-key cryptosystems and is widely used for secure data transmission. In such a cryptosystem, the encryption key is public and differs from the decryption key which is kept secret. In RSA, this asymmetry is based on the practical difficulty of factoring the product of two large prime numbers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman.*/
|
||||
|
||||
/*
|
||||
* C++ Program to Implement the RSA Algorithm
|
||||
*/
|
||||
#include<iostream>
|
||||
#include<math.h>
|
||||
#include<string.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
long int p, q, n, t, flag, e[100], d[100], temp[100], j, m[100], en[100], i;
|
||||
char msg[100];
|
||||
int prime(long int);
|
||||
void ce();
|
||||
long int cd(long int);
|
||||
void encrypt();
|
||||
void decrypt();
|
||||
int prime(long int pr)
|
||||
{
|
||||
int i;
|
||||
j = sqrt(pr);
|
||||
for (i = 2; i <= j; i++)
|
||||
{
|
||||
if (pr % i == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
cout << "\nENTER FIRST PRIME NUMBER\n";
|
||||
cin >> p;
|
||||
flag = prime(p);
|
||||
if (flag == 0)
|
||||
{
|
||||
cout << "\nWRONG INPUT\n";
|
||||
exit(1);
|
||||
}
|
||||
cout << "\nENTER ANOTHER PRIME NUMBER\n";
|
||||
cin >> q;
|
||||
flag = prime(q);
|
||||
if (flag == 0 || p == q)
|
||||
{
|
||||
cout << "\nWRONG INPUT\n";
|
||||
exit(1);
|
||||
}
|
||||
cout << "\nENTER MESSAGE\n";
|
||||
fflush(stdin);
|
||||
cin >> msg;
|
||||
for (i = 0; msg[i] != NULL; i++)
|
||||
m[i] = msg[i];
|
||||
n = p * q;
|
||||
t = (p - 1) * (q - 1);
|
||||
ce();
|
||||
cout << "\nPOSSIBLE VALUES OF e AND d ARE\n";
|
||||
for (i = 0; i < j - 1; i++)
|
||||
cout << e[i] << "\t" << d[i] << "\n";
|
||||
encrypt();
|
||||
decrypt();
|
||||
return 0;
|
||||
}
|
||||
void ce()
|
||||
{
|
||||
int k;
|
||||
k = 0;
|
||||
for (i = 2; i < t; i++)
|
||||
{
|
||||
if (t % i == 0)
|
||||
continue;
|
||||
flag = prime(i);
|
||||
if (flag == 1 && i != p && i != q)
|
||||
{
|
||||
e[k] = i;
|
||||
flag = cd(e[k]);
|
||||
if (flag > 0)
|
||||
{
|
||||
d[k] = flag;
|
||||
k++;
|
||||
}
|
||||
if (k == 99)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
long int cd(long int x)
|
||||
{
|
||||
long int k = 1;
|
||||
while (1)
|
||||
{
|
||||
k = k + t;
|
||||
if (k % x == 0)
|
||||
return (k / x);
|
||||
}
|
||||
}
|
||||
void encrypt()
|
||||
{
|
||||
long int pt, ct, key = e[0], k, len;
|
||||
i = 0;
|
||||
len = strlen(msg);
|
||||
while (i != len)
|
||||
{
|
||||
pt = m[i];
|
||||
pt = pt - 96;
|
||||
k = 1;
|
||||
for (j = 0; j < key; j++)
|
||||
{
|
||||
k = k * pt;
|
||||
k = k % n;
|
||||
}
|
||||
temp[i] = k;
|
||||
ct = k + 96;
|
||||
en[i] = ct;
|
||||
i++;
|
||||
}
|
||||
en[i] = -1;
|
||||
cout << "\nTHE ENCRYPTED MESSAGE IS\n";
|
||||
for (i = 0; en[i] != -1; i++)
|
||||
printf("%c", en[i]);
|
||||
}
|
||||
void decrypt()
|
||||
{
|
||||
long int pt, ct, key = d[0], k;
|
||||
i = 0;
|
||||
while (en[i] != -1)
|
||||
{
|
||||
ct = temp[i];
|
||||
k = 1;
|
||||
for (j = 0; j < key; j++)
|
||||
{
|
||||
k = k * ct;
|
||||
k = k % n;
|
||||
}
|
||||
pt = k + 96;
|
||||
m[i] = pt;
|
||||
i++;
|
||||
}
|
||||
m[i] = -1;
|
||||
cout << "\nTHE DECRYPTED MESSAGE IS\n";
|
||||
for (i = 0; m[i] != -1; i++)
|
||||
printf("%c", m[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
ENTER FIRST PRIME NUMBER
|
||||
47
|
||||
|
||||
ENTER ANOTHER PRIME NUMBER
|
||||
53
|
||||
|
||||
ENTER MESSAGE
|
||||
Dharmendra
|
||||
|
||||
POSSIBLE VALUES OF e AND d ARE
|
||||
3 1595
|
||||
5 957
|
||||
7 1367
|
||||
11 435
|
||||
17 985
|
||||
19 1259
|
||||
29 165
|
||||
31 463
|
||||
37 1293
|
||||
41 2217
|
||||
43 1947
|
||||
59 1419
|
||||
61 549
|
||||
67 2035
|
||||
71 1415
|
||||
73 1409
|
||||
79 1847
|
||||
83 2075
|
||||
89 2177
|
||||
97 1233
|
||||
101 1421
|
||||
103 2183
|
||||
|
||||
THE ENCRYPTED MESSAGE IS
|
||||
x`a???]??a
|
||||
THE DECRYPTED MESSAGE IS
|
||||
Dharmendra
|
@ -1,52 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Implement the String Search Algorithm for
|
||||
* Short Text Sizes
|
||||
*/
|
||||
|
||||
//enter string without spaces
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
char org[100], dup[100];
|
||||
int i, j, k = 0, len_org, len_dup;
|
||||
cout<<"NOTE:Strings are accepted only till blank space.";
|
||||
cout<<"\nEnter Original String:";
|
||||
fflush(stdin);
|
||||
cin>>org;
|
||||
fflush(stdin);
|
||||
cout<<"Enter Pattern to Search:";
|
||||
cin>>dup;
|
||||
len_org = strlen(org);
|
||||
len_dup = strlen(dup);
|
||||
for (i = 0; i <= (len_org - len_dup); i++)
|
||||
{
|
||||
for (j = 0; j < len_dup; j++)
|
||||
{
|
||||
//cout<<"comparing '"<<org[i + j]<<"' and '"<<dup[j]<<"'.";
|
||||
if (org[i + j] != dup[j])
|
||||
break ;
|
||||
}
|
||||
if (j == len_dup)
|
||||
{
|
||||
k++;
|
||||
cout<<"\nPattern Found at Position: "<<i;
|
||||
}
|
||||
}
|
||||
if (k == 0)
|
||||
cout<<"\nError:No Match Found!";
|
||||
else
|
||||
cout<<"\nTotal Instances Found = "<<k;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
NOTE:Strings are accepted only till blank space.
|
||||
Enter Original String:allmenwenttoapall mall
|
||||
Enter Pattern to Search:all
|
||||
|
||||
Pattern Found at Position: 0
|
||||
Pattern Found at Position: 14
|
||||
Total Instances Found = 2
|
@ -1,71 +0,0 @@
|
||||
/*This is a C++ Program to implement Vigenere cipher. The Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Vigenere
|
||||
{
|
||||
public:
|
||||
string key;
|
||||
|
||||
Vigenere(string key)
|
||||
{
|
||||
for (int i = 0; i < key.size(); ++i)
|
||||
{
|
||||
if (key[i] >= 'A' && key[i] <= 'Z')
|
||||
this->key += key[i];
|
||||
else if (key[i] >= 'a' && key[i] <= 'z')
|
||||
this->key += key[i] + 'A' - 'a';
|
||||
}
|
||||
}
|
||||
|
||||
string encrypt(string text)
|
||||
{
|
||||
string out;
|
||||
for (int i = 0, j = 0; i < text.length(); ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c >= 'a' && c <= 'z')
|
||||
c += 'A' - 'a';
|
||||
else if (c < 'A' || c > 'Z')
|
||||
continue;
|
||||
out += (c + key[j] - 2 * 'A') % 26 + 'A';
|
||||
j = (j + 1) % key.length();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
string decrypt(string text)
|
||||
{
|
||||
string out;
|
||||
for (int i = 0, j = 0; i < text.length(); ++i)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c >= 'a' && c <= 'z')
|
||||
c += 'A' - 'a';
|
||||
else if (c < 'A' || c > 'Z')
|
||||
continue;
|
||||
out += (c - key[j] + 26) % 26 + 'A';
|
||||
j = (j + 1) % key.length();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Vigenere cipher("VIGENERECIPHER");
|
||||
string original =
|
||||
"Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
|
||||
string encrypted = cipher.encrypt(original);
|
||||
string decrypted = cipher.decrypt(encrypted);
|
||||
cout << original << endl;
|
||||
cout << "Encrypted: " << encrypted << endl;
|
||||
cout << "Decrypted: " << decrypted << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Beware the Jabberwock, my son! The jaws that bite, the claws that catch!
|
||||
Encrypted: WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
|
||||
Decrypted: BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
|
@ -1,163 +0,0 @@
|
||||
/*This is a C++ Program to implement transposition technique. In cryptography, a transposition cipher is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed (the plaintext is reordered). Mathematically a bijective function is used on the characters’ positions to encrypt and an inverse function to decrypt.*/
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
|
||||
void cipher(int i, int c);
|
||||
int findMin();
|
||||
void makeArray(int, int);
|
||||
|
||||
char arr[22][22], darr[22][22], emessage[111], retmessage[111], key[55];
|
||||
char temp[55], temp2[55];
|
||||
int k = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
char *message;
|
||||
int i, j, klen, emlen, flag = 0;
|
||||
int r, c, index, rows;
|
||||
printf("Enter the key\n");
|
||||
fflush(stdin);
|
||||
gets(key);
|
||||
printf("\nEnter message to be ciphered\n");
|
||||
fflush(stdin);
|
||||
gets(message);
|
||||
strcpy(temp, key);
|
||||
klen = strlen(key);
|
||||
k = 0;
|
||||
for (i = 0;; i++)
|
||||
{
|
||||
if (flag == 1)
|
||||
break;
|
||||
for (j = 0; key[j] != NULL; j++)
|
||||
{
|
||||
if (message[k] == NULL)
|
||||
{
|
||||
flag = 1;
|
||||
arr[i][j] = '-';
|
||||
}
|
||||
else
|
||||
{
|
||||
arr[i][j] = message[k++];
|
||||
}
|
||||
}
|
||||
}
|
||||
r = i;
|
||||
c = j;
|
||||
for (i = 0; i < r; i++)
|
||||
{
|
||||
for (j = 0; j < c; j++)
|
||||
{
|
||||
printf("%c ", arr[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
k = 0;
|
||||
for (i = 0; i < klen; i++)
|
||||
{
|
||||
index = findMin();
|
||||
cipher(index, r);
|
||||
}
|
||||
emessage[k] = '\0';
|
||||
printf("\nEncrypted message is\n");
|
||||
for (i = 0; emessage[i] != NULL; i++)
|
||||
printf("%c", emessage[i]);
|
||||
printf("\n\n");
|
||||
//deciphering
|
||||
emlen = strlen(emessage);
|
||||
//emlen is length of encrypted message
|
||||
strcpy(temp, key);
|
||||
rows = emlen / klen;
|
||||
//rows is no of row of the array to made from ciphered message
|
||||
j = 0;
|
||||
for (i = 0, k = 1; emessage[i] != NULL; i++, k++)
|
||||
{
|
||||
//printf("\nEmlen=%d",emlen);
|
||||
temp2[j++] = emessage[i];
|
||||
if ((k % rows) == 0)
|
||||
{
|
||||
temp2[j] = '\0';
|
||||
index = findMin();
|
||||
makeArray(index, rows);
|
||||
j = 0;
|
||||
}
|
||||
}
|
||||
printf("\nArray Retrieved is\n");
|
||||
k = 0;
|
||||
for (i = 0; i < r; i++)
|
||||
{
|
||||
for (j = 0; j < c; j++)
|
||||
{
|
||||
printf("%c ", darr[i][j]);
|
||||
//retrieving message
|
||||
retmessage[k++] = darr[i][j];
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
retmessage[k] = '\0';
|
||||
printf("\nMessage retrieved is\n");
|
||||
for (i = 0; retmessage[i] != NULL; i++)
|
||||
printf("%c", retmessage[i]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void cipher(int i, int r)
|
||||
{
|
||||
int j;
|
||||
for (j = 0; j < r; j++)
|
||||
{
|
||||
{
|
||||
emessage[k++] = arr[j][i];
|
||||
}
|
||||
}
|
||||
// emessage[k]='\0';
|
||||
}
|
||||
|
||||
void makeArray(int col, int row)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < row; i++)
|
||||
{
|
||||
darr[i][col] = temp2[i];
|
||||
}
|
||||
}
|
||||
|
||||
int findMin()
|
||||
{
|
||||
int i, j, min, index;
|
||||
min = temp[0];
|
||||
index = 0;
|
||||
for (j = 0; temp[j] != NULL; j++)
|
||||
{
|
||||
if (temp[j] < min)
|
||||
{
|
||||
min = temp[j];
|
||||
index = j;
|
||||
}
|
||||
}
|
||||
temp[index] = 123;
|
||||
return (index);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Enter the key
|
||||
hello
|
||||
|
||||
Enter the message to be ciphered
|
||||
how are you
|
||||
|
||||
h o w a
|
||||
r e y o
|
||||
u - - - -
|
||||
|
||||
Encrypted message is
|
||||
oe-hruw - y-ao-
|
||||
|
||||
Array Retrieved is
|
||||
h o w a
|
||||
r e y o
|
||||
u - - - -
|
||||
|
||||
Message retrieved is
|
||||
how are you----
|
@ -1,74 +0,0 @@
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
#define NO_OF_CHARS 256
|
||||
|
||||
int getNextState(char *pat, int M, int state, int x)
|
||||
{
|
||||
// If the character c is same as next character in pattern,
|
||||
// then simply increment state
|
||||
if (state < M && x == pat[state])
|
||||
return state + 1;
|
||||
int ns, i; // ns stores the result which is next state
|
||||
// ns finally contains the longest prefix which is also suffix
|
||||
// in "pat[0..state-1]c"
|
||||
// Start from the largest possible value and stop when you find
|
||||
// a prefix which is also suffix
|
||||
for (ns = state; ns > 0; ns--)
|
||||
{
|
||||
if (pat[ns - 1] == x)
|
||||
{
|
||||
for (i = 0; i < ns - 1; i++)
|
||||
{
|
||||
if (pat[i] != pat[state - ns + 1 + i])
|
||||
break;
|
||||
}
|
||||
if (i == ns - 1)
|
||||
return ns;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* This function builds the TF table which represents Finite Automata for a
|
||||
given pattern */
|
||||
void computeTF(char *pat, int M, int TF[][NO_OF_CHARS])
|
||||
{
|
||||
int state, x;
|
||||
for (state = 0; state <= M; ++state)
|
||||
for (x = 0; x < NO_OF_CHARS; ++x)
|
||||
TF[state][x] = getNextState(pat, M, state, x);
|
||||
}
|
||||
|
||||
/* Prints all occurrences of pat in txt */
|
||||
void search(char *pat, char *txt)
|
||||
{
|
||||
int M = strlen(pat);
|
||||
int N = strlen(txt);
|
||||
int TF[M + 1][NO_OF_CHARS];
|
||||
computeTF(pat, M, TF);
|
||||
// Process txt over FA.
|
||||
int i, state = 0;
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
state = TF[state][txt[i]];
|
||||
if (state == M)
|
||||
{
|
||||
printf("\n pattern found at index %d", i - M + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Driver program to test above function
|
||||
int main()
|
||||
{
|
||||
char *txt = "AABAACAADAABAAABAA";
|
||||
char *pat = "AABA";
|
||||
search(pat, txt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
pattern found at index 0
|
||||
pattern found at index 9
|
||||
pattern found at index 13
|
||||
------------------
|
@ -1,38 +0,0 @@
|
||||
/*This is a C++ Program to perform Naive String matching algorithm. In computer science, string searching algorithms, sometimes called string matching algorithms, are an important class of string algorithms that try to find a place where one or several strings (also called patterns) are found within a larger string or text.*/
|
||||
|
||||
#include<stdio.h>
|
||||
#include<string.h>
|
||||
void search(char *pat, char *txt)
|
||||
{
|
||||
int M = strlen(pat);
|
||||
int N = strlen(txt);
|
||||
/* A loop to slide pat[] one by one */
|
||||
for (int i = 0; i <= N - M; i++)
|
||||
{
|
||||
int j;
|
||||
/* For current index i, check for pattern match */
|
||||
for (j = 0; j < M; j++)
|
||||
{
|
||||
if (txt[i + j] != pat[j])
|
||||
break;
|
||||
}
|
||||
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
|
||||
{
|
||||
printf("Pattern found at index %d \n", i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver program to test above function */
|
||||
int main()
|
||||
{
|
||||
char *txt = "AABAACAADAABAAABAA";
|
||||
char *pat = "AABA";
|
||||
search(pat, txt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Pattern found at index 0
|
||||
Pattern found at index 9
|
||||
Pattern found at index 13
|
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* C++ Program to Perform String Matching Using String Library
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
std::string org, dup;
|
||||
int result = -1, i = 1;
|
||||
std::cout<<"Enter Original String:";
|
||||
getline(std::cin, org);
|
||||
std::cout<<"Enter Pattern String:";
|
||||
getline(std::cin, dup);
|
||||
do
|
||||
{
|
||||
result = org.find(dup, result + 1);
|
||||
if (result != -1)
|
||||
std::cout<<"\nInstance:"<<i<<"\tPosition:"<<result<<"\t";
|
||||
i++;
|
||||
}
|
||||
while (result >= 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Enter Original String:All men went to the appall mall
|
||||
Enter Pattern String:all
|
||||
|
||||
Instance:1 Position:23
|
||||
Instance:2 Position:28
|
@ -1,49 +0,0 @@
|
||||
//enter string without spaces
|
||||
#include<iostream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
char org[100], dup[100];
|
||||
int i, j, k = 0, len_org, len_dup;
|
||||
cout << "NOTE:Strings are accepted only till blank space.";
|
||||
cout << "\nEnter Original String:";
|
||||
fflush(stdin);
|
||||
cin >> org;
|
||||
fflush(stdin);
|
||||
cout << "Enter Pattern to Search:";
|
||||
cin >> dup;
|
||||
len_org = strlen(org);
|
||||
len_dup = strlen(dup);
|
||||
for (i = 0; i <= (len_org - len_dup); i++)
|
||||
{
|
||||
for (j = 0; j < len_dup; j++)
|
||||
{
|
||||
//cout<<"comparing '"<<org[i + j]<<"' and '"<<dup[j]<<"'.";
|
||||
if (org[i + j] != dup[j])
|
||||
break;
|
||||
}
|
||||
if (j == len_dup)
|
||||
{
|
||||
k++;
|
||||
cout << "\nPattern Found at Position: " << i;
|
||||
}
|
||||
}
|
||||
if (k == 0)
|
||||
cout << "\nError:No Match Found!";
|
||||
else
|
||||
cout << "\nTotal Instances Found = " << k;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
NOTE:Strings are accepted only till blank space.
|
||||
Enter Original String:ThisC++programperformsnaivestringmatchingwithoutusinganyspecificlibraryfunctions.Atextandapatternisgivenasinput.Thepatternissearchedforinthetextandallinstancesofthepatternaregivenasoutput.h
|
||||
Enter Pattern to Search:in
|
||||
|
||||
Pattern Found at Position: 30
|
||||
Pattern Found at Position: 38
|
||||
Pattern Found at Position: 50
|
||||
Pattern Found at Position: 100
|
||||
Total Instances Found = 4
|
@ -1,31 +0,0 @@
|
||||
|
||||
|
||||
#include<iostream>
|
||||
#include<stdio.h>
|
||||
using namespace std;
|
||||
main() {
|
||||
char str1[50],str2[50];
|
||||
int str_cmp(char*,char*);
|
||||
cout<<“Enter first string:”;
|
||||
gets(str1);
|
||||
cout<<“Enter second string:”;
|
||||
gets(str2);
|
||||
if(str_cmp(str1,str2))
|
||||
cout<<“nStrings are equal”; else
|
||||
cout<<“nStrings are not equal”;
|
||||
return 0;
|
||||
}
|
||||
int str_cmp(char *s1,char *s2) {
|
||||
while(*s1==*s2) {
|
||||
if(*s1==’’||*s2==’’)
|
||||
break;
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
if(*s1==’’&&*s2==’’)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,31 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class complex
|
||||
{
|
||||
public :
|
||||
int real, img;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
complex a, b, c;
|
||||
cout << "Enter a and b where a + ib is the first complex number.";
|
||||
cout << "\na = ";
|
||||
cin >> a.real;
|
||||
cout << "b = ";
|
||||
cin >> a.img;
|
||||
cout << "Enter c and d where c + id is the second complex number.";
|
||||
cout << "\nc = ";
|
||||
cin >> b.real;
|
||||
cout << "d = ";
|
||||
cin >> b.img;
|
||||
c.real = a.real + b.real;
|
||||
c.img = a.img + b.img;
|
||||
if ( c.img >= 0 )
|
||||
cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
|
||||
else
|
||||
cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
|
||||
return 0;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
#include<iostream>
|
||||
#include<cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
main()
|
||||
{
|
||||
int n, max, num, c;
|
||||
cout << "Enter the number of random numbers you want ";
|
||||
cin >> n;
|
||||
cout << "Enter the maximum value of random number ";
|
||||
cin >> max;
|
||||
cout << "random numbers from 0 to " << max << " are :-" << endl;
|
||||
for ( c = 1 ; c <= n ; c++ )
|
||||
{
|
||||
num = random(max);
|
||||
cout << num << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Operations
|
||||
{
|
||||
long c;
|
||||
|
||||
public:
|
||||
void inputNumber()
|
||||
{
|
||||
cout << "Input a number\n";
|
||||
cin >> c;
|
||||
}
|
||||
|
||||
long reverseNumber()
|
||||
{
|
||||
long invert = 0;
|
||||
while (c != 0)
|
||||
{
|
||||
invert = invert * 10;
|
||||
invert = invert + c%10;
|
||||
c = c/10;
|
||||
}
|
||||
return invert;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
long result;
|
||||
Operations t;
|
||||
t.inputNumber();
|
||||
result = t.reverseNumber();
|
||||
cout << "Number obtained on reversal = " << result;
|
||||
return 0;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user