Compression in c++ using bitwise operators - c++

We have been given an assignment in which we have to compress 4 bytes into 3 bytes. It wants us to have a compression of 25%, by packing chars into 6 bits instead of 8 bits. It should be 25% exact compression, but my program is doing approx 50%. Code Book is my own "ASCII" sort of table, The bitwise operators are used to perform compression. Does anyone have any idea why it is compressing it by 50% rather than 25%? I know namespace std isn't a good practice, but we are asked to use it. Thanks!`
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
unsigned char CodeBook[53][2];
unsigned char FindCharacterCode(unsigned char C)
{
for (int j = 0; j < 53; j++)
if (CodeBook[j][0] == C)
return CodeBook[j][1];
return 0;
}
void MyCodeBook()
{
CodeBook[0][0] = 'a'; CodeBook[0][1] = 1;
CodeBook[1][0] = 'b'; CodeBook[1][1] = 2;
CodeBook[2][0] = 'c'; CodeBook[2][1] = 3;
CodeBook[3][0] = 'd'; CodeBook[3][1] = 4;
CodeBook[4][0] = 'e'; CodeBook[4][1] = 5;
CodeBook[5][0] = 'f'; CodeBook[5][1] = 6;
CodeBook[6][0] = 'g'; CodeBook[6][1] = 7;
CodeBook[7][0] = 'h'; CodeBook[7][1] = 8;
CodeBook[8][0] = 'i'; CodeBook[8][1] = 9;
CodeBook[9][0] = 'j'; CodeBook[9][1] = 10;
CodeBook[10][0] = 'k'; CodeBook[10][1] = 11;
CodeBook[11][0] = 'l'; CodeBook[11][1] = 12;
CodeBook[12][0] = 'm'; CodeBook[12][1] = 13;
CodeBook[13][0] = 'n'; CodeBook[13][1] = 14;
CodeBook[14][0] = 'o'; CodeBook[14][1] = 15;
CodeBook[15][0] = 'p'; CodeBook[15][1] = 16;
CodeBook[16][0] = 'q'; CodeBook[16][1] = 17;
CodeBook[17][0] = 'r'; CodeBook[17][1] = 18;
CodeBook[18][0] = 's'; CodeBook[18][1] = 19;
CodeBook[19][0] = 't'; CodeBook[19][1] = 20;
CodeBook[20][0] = 'u'; CodeBook[20][1] = 21;
CodeBook[21][0] = 'v'; CodeBook[21][1] = 22;
CodeBook[2][0] = 'w'; CodeBook[22][1] = 23;
CodeBook[23][0] = 'x'; CodeBook[23][1] = 24;
CodeBook[24][0] = 'y'; CodeBook[24][1] = 25;
CodeBook[25][0] = 'z'; CodeBook[25][1] = 26;
CodeBook[26][0] = '0'; CodeBook[26][1] = 27;
CodeBook[27][0] = '1'; CodeBook[27][1] = 28;
CodeBook[28][0] = '2'; CodeBook[28][1] = 29;
CodeBook[29][0] = '3'; CodeBook[29][1] = 30;
CodeBook[30][0] = '4'; CodeBook[30][1] = 31;
CodeBook[31][0] = '5'; CodeBook[31][1] = 32;
CodeBook[32][0] = '6'; CodeBook[32][1] = 33;
CodeBook[33][0] = '7'; CodeBook[33][1] = 34;
CodeBook[34][0] = '8'; CodeBook[34][1] = 35;
CodeBook[35][0] = '9'; CodeBook[35][1] = 36;
CodeBook[36][0] = '!'; CodeBook[36][1] = 37;
CodeBook[37][0] = '$'; CodeBook[37][1] = 38;
CodeBook[38][0] = '('; CodeBook[38][1] = 39;
CodeBook[39][0] = ')'; CodeBook[39][1] = 40;
CodeBook[40][0] = '#'; CodeBook[40][1] = 41;
CodeBook[41][0] = '&'; CodeBook[41][1] = 42;
CodeBook[42][0] = '%'; CodeBook[42][1] = 43;
CodeBook[43][0] = '-'; CodeBook[43][1] = 44;
CodeBook[44][0] = '.'; CodeBook[44][1] = 45;
CodeBook[45][0] = ','; CodeBook[45][1] = 46;
CodeBook[46][0] = '\''; CodeBook[46][1] = 47;
CodeBook[47][0] = ';'; CodeBook[47][1] = 48;
CodeBook[48][0] = ':'; CodeBook[48][1] = 49;
CodeBook[49][0] = '?'; CodeBook[49][1] = 50;
CodeBook[50][0] = ' '; CodeBook[50][1] = 51;
CodeBook[51][0] = ' '; CodeBook[51][1] = 52;
CodeBook[52][0] = 'ΒΆ'; CodeBook[52][1] = 53;
}
int main()
{
MyCodeBook();
ifstream In;
ofstream Out;
In.open("C://Users//osama//Desktop//code.txt");
Out.open("C://Users//osama//Desktop//compressed.txt");
unsigned char Data[4] = { 0 },
Compressed[3] = { 0 }, Code[4] = { 0 };
int i;
while (!In.eof())
{
if (In.is_open())
{
for (i = 0; i < 4; i++)
{
In >> Data[i];
Code[i] = FindCharacterCode(Data[i]);
}
}
else
{
cout << "Not open!" << endl << endl;
return -1;
}
Compressed[0] = Code[0] << 2;
Compressed[0] = (Compressed[0] | Code[1] >> 4);
Compressed[1] = (Code[1] << 4);
Compressed[1] = (Compressed[1] | Code[2] >> 2);
Compressed[2] = (Code[2] << 6);
Compressed[2] = Compressed[2] | Code[3];
for (i = 0; i < 3; i++)
{
Out << Compressed[i];
}
}
In.close();
Out.close();
return 0;
}

Hint:
For any given input of your compression algorithm you will get a number between 1 and 53 (let's call it 0 and 53).
How many bits do we need to fit a number this large?
1? in one bit we can store between 0 and 1 - too small.
2? in 2 bits we can store between 0 and 3 - too small.
...
5? in 5 bits we can store between 0 and 31 - too small.
6? in 6 bits we can store between 0 and 63 - big enough.
the inputs are chars, or bytes - 8 bits.
6 / 8 * 100 = 75%
Here's your 25% compression.
Now you need to figure out how to encode 4 lots of 6 bits into 3 bytes of 8 bits.
That's where your bitwise operators (and some bit-shifting) will come in.

Related

Wrong cost adjacency matrix

I try to obtain the adjacency matrix of weights, and then use it in the calculation of the minimum weight path. There is a problem, when I try to display it, I get a wrong result :
By logic, the diagonal must have only 0, and in the places where the vertices are adjacent, must be the weight of the edge
//set the source and destination of each edge
g->edge[0]->src = 0;
g->edge[0]->dest = 1;
g->edge[0]->weight = 9;
g->edge[1]->src = 0;
g->edge[1]->dest = 10;
g->edge[1]->weight = 6;
g->edge[2]->src = 1;
g->edge[2]->dest = 2;
g->edge[2]->weight = 3;
g->edge[3]->src = 1;
g->edge[3]->dest = 10;
g->edge[3]->weight = 2;
g->edge[4]->src = 2;
g->edge[4]->dest = 3;
g->edge[4]->weight = 2;
g->edge[5]->src = 2;
g->edge[5]->dest = 6;
g->edge[5]->weight = 3;
g->edge[6]->src = 2;
g->edge[6]->dest = 5;
g->edge[6]->weight = 3;
g->edge[7]->src = 3;
g->edge[7]->dest = 4;
g->edge[7]->weight = 5;
g->edge[8]->src = 4;
g->edge[8]->dest = 5;
g->edge[8]->weight = 4;
g->edge[9]->src = 6;
g->edge[9]->dest = 10;
g->edge[9]->weight = 2;
g->edge[10]->src = 6;
g->edge[10]->dest = 7;
g->edge[10]->weight = 9;
g->edge[11]->src = 7;
g->edge[11]->dest = 8;
g->edge[11]->weight = 7;
g->edge[12]->src = 7;
g->edge[12]->dest = 9;
g->edge[12]->weight = 2;
g->edge[13]->src = 8;
g->edge[13]->dest = 9;
g->edge[13]->weight = 7;
g->edge[14]->src = 9;
g->edge[14]->dest = 10;
g->edge[14]->weight = 5;
My code :
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
What's wrong?
for (i = 0; i < numberOfVertices; i++)
{
adjacency_matrix[i][i] = 0;
for (j = i + 1; j < numberOfVertices; j++)
{
adjacency_matrix[i][j] = g->edge[i]->weight;
adjacency_matrix[j][i] = g->edge[i]->weight;
}
}
In this code you are setting every edge from vertex i to every other vertex to the same weight. I do not think this is what you want.
( Note: It is hard to know what you do want. When reporting a problem you need to include a description of both what happens AND what you wanted to happen. "It's wrong!" is almost useless as a bug report. )

Programming Homework: Robot Genetic Algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This program is built to run 10000 generations of 200 robots, allowing evolution to shape the digits in the robot's "DNA". The problem I'm having is that, even though there is nothing but numbers going into the 'robotGenes' array, the resulting DNA that prints is a random collection of letters, numbers and symbols. I have absolutly no idea why. I have tried switching up where I declare and assign my variables, unfortunatly its all been in vane. Plz halp.
P.s. I'm posting the entire program code here, as I am unsure what is causing the problem, though I am positive that I am simply being daft. Any help is greatly appreciated.
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cmath>
using namespace std;
int randBattery = 0,
randRobot = 0,
randDirect = 0,
board[144] = { },
testDirect = 0,
randTurn = 0,
randTurnDirect = 0,
genAdd = 0,
masterFit = 0,
abc = 0;
double fitness[10000] = { },
winners[5] = { };
std::string startCond;
unsigned char robotGenes[200][12] = { },
test[12] = { };
class Display {
public:
int results() {
cout << "\n\n\n-Evolution Winning Avg. Fitness Ratings-\n\n";
cout << "1. " << winners[1];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
cout << "\n\n2. " << winners[2];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[41][G];
}
cout << "\n\n3. " << winners[3];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[81][G];
}
cout << "\n\n4. " << winners[4];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[121][G];
}
cout << "\n\n5. " << winners[5];
cout << "\n DNA: ";
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[161][G];
}
return 0;
};
};
class RobotSex {
public:
int bang() {
int newGen, newGen2, B;
newGen = 101;
newGen2 = 102;
B = 2;
for (int C = 1; C <= 100; C += 2)
{
robotGenes[newGen][6] = robotGenes[C][6];
robotGenes[newGen][7] = robotGenes[C][7];
robotGenes[newGen][8] = robotGenes[C][8];
robotGenes[newGen][9] = robotGenes[C][9];
robotGenes[newGen][10] = robotGenes[B][10];
robotGenes[newGen][11] = robotGenes[B][11];
robotGenes[newGen2][6] = robotGenes[B][6];
robotGenes[newGen2][7] = robotGenes[B][7];
robotGenes[newGen2][8] = robotGenes[B][8];
robotGenes[newGen2][9] = robotGenes[B][9];
robotGenes[newGen2][10] = robotGenes[C][10];
robotGenes[newGen2][11] = robotGenes[C][11];
newGen += 2;
newGen2 += 2;
B += 2;
}
};
};
class Genocide {
public:
int ethnicCleansing() {
for (int x = 101; x <= 200; x++)
{
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
}
};
};
class Sorting {
public:
int sortPower() {
for (int P = 0; P <= 200; P++)
{
for (int x = 1; x <= 200; x++)
{
int y;
y = x + 1;
if (robotGenes[x][12] < robotGenes[y][12])
for (int Q = 1; Q <= 12; Q++)
swap (robotGenes[x][Q], robotGenes[y][Q]);
}
}
};
int addGenFit() {
for (int o = 1; o <= 200; o++)
{
genAdd += robotGenes[o][12];
}
fitness[masterFit] = genAdd / 200;
::masterFit += 1;
};
int sortGenFit() {
for (int P = 0; P <= 10000; P++)
{
for (int x = 1; x <= 9999; x++)
{
int y;
y = x + 1;
if (fitness[x] < fitness[y])
swap (fitness[x], fitness[y]);
}
}
for (int o = 1; o <= 2000; o++)
{
genAdd += fitness[o];
}
winners[1] = genAdd / 2000;
for (int o = 2001; o <= 4000; o++)
{
genAdd += fitness[o];
}
winners[2] = genAdd / 2000;
for (int o = 4001; o <= 6000; o++)
{
genAdd += fitness[o];
}
winners[3] = genAdd / 2000;
for (int o = 6001; o <= 8000; o++)
{
genAdd += fitness[o];
}
winners[4] = genAdd / 2000;
for (int o = 8001; o <= 10000; o++)
{
genAdd += fitness[o];
}
winners[5] = genAdd / 2000;
};
};
class Establishing {
public:
int clearBoard() {
for (int c = 0; c <= 144; c++) //Clearing board
{
board[c] = 0;
}
for (int x = 0; x <= 12; x++) //Establishing walls
{
board[x] = 9;
}
for (int x = 132; x <= 144; x++)
{
board[x] = 9;
}
for (int x = 13; x <= 121; x += 12)
{
board[x] = 9;
}
for (int x = 24; x <= 132; x += 12)
{
board[x] = 9;
}
};
};
class Randomizer {
public:
int randBattery() {
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R1
::randBattery = (rand() % 14+23);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R2
::randBattery = (rand() % 26+35);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R3
::randBattery = (rand() % 38+47);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R4
::randBattery = (rand() % 50+59);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R5
::randBattery = (rand() % 62+71);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R6
::randBattery = (rand() % 74+82);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R7
::randBattery = (rand() % 85+95);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R8
::randBattery = (rand() % 98+107);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R9
::randBattery = (rand() % 110+119);
board[::randBattery] = 1;
}
for (int t = 1; t <= 4; t++)
{
srand(time(NULL)); //Random battery placement R10
::randBattery = (rand() % 122+131);
board[::randBattery] = 1;
}
};
int randPlacement() {
for (int t = 1; t <= 200; t++)
{
srand(time(NULL)); //Random robot placement & resets power level
randRobot = (rand() % 62+71);
robotGenes[t][1] = randRobot;
robotGenes[t][12] = 5;
}
};
char randDirect() {
for (int d = 1; d <= 200; d++)
{
srand(time(NULL)); //Random robot direction
::randDirect = (rand() % 62+71);
robotGenes[d][5] = ::randDirect;
}
};
};
class Reader {
public:
int readDNA() {
do {
if (board[test[1]] == 1) //Pickup battery
test[12] += 5;
if (test[9] == 1) //Check for wall
{
if (board[test[2]] == 9)
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
if (board[test[2]] == 9 && board[test[3]] == 9)
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
}
if (test[7] == test[6]) //Changing direction after # of moves
if (test[6] % 2 == 0) //Turning left
{
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
}
else //Turning right
{
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
}
if (test[10] == 1) //Left sensor checking for battery, TURN LEFT
if (test[5] == 1)
test[5] = 4;
if (test[5] == 2)
test[5] = 1;
if (test[5] == 3)
test[5] = 2;
if (test[5] == 4)
test[5] = 3;
if (test[11] == 1) //Right sensor checking for battery, TURN RIGHT
if (test[5] == 1)
test[5] = 2;
if (test[5] == 2)
test[5] = 3;
if (test[5] == 3)
test[5] = 4;
if (test[5] == 4)
test[5] = 1;
if (test[5] == 1) //Face north
{
test[2] = test[1] + 12;
::test[3] = test[1] - 1;
test[4] = test[1] + 1;
test[1] += 12; //Move north
test[2] += 12;
::test[3] += 12;
test[4] += 12;
}
if (test[5] == 2) //Face east
{
test[2] = test[1] + 1;
::test[3] = test[1] + 12;
test[4] = test[1] - 12;
test[1] += 1; //Move east
test[2] += 1;
::test[3] += 1;
test[4] += 1;
}
if (test[5] == 3) //Face south
{
test[2] = test[1] - 12;
::test[3] = test[1] + 1;
test[4] = test[1] - 1;
test[1] -= 12; //Move south
test[2] -= 12;
::test[3] -= 12;
test[4] -= 12;
}
if (test[5] == 4) //Face west
{
test[2] = test[1] - 1;
::test[3] = test[1] - 12;
test[4] = test[1] + 12;
test[1] -= 1; //Move west
test[2] -= 1;
::test[3] -= 1;
test[4] -= 1;
}
test[7] += 1;
test[12] -= 1;
} while (test[12] > 1 && test[7] < 24);
};
};
class Running {
public:
void runRobot() {
for (int q = 1; q <= 200; q++)
{
for (int z = 1; z <= 12; z++)
{
::test[z] = robotGenes[q][z];
}
Reader objectDNA;
objectDNA.readDNA();
for (int z = 1; z <= 12; z++)
{
::robotGenes[q][z] = test[z];
}
}
};
};
int main()
{
//System Greeting
cout << "Enter anything to start simulation.\n\n";
cin >> startCond;
masterFit = 1;
//Setting starting genes
for (int t = 1; t <= 200; t++)
{
srand(time(NULL));
randTurn = (rand() % 10+1);
robotGenes[t][6] = randTurn;
}
for (int o = 1; o <= 40; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 41; o <= 80; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 1;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
for (int o = 81; o <= 120; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 1;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 121; o <= 160; o++)
{
robotGenes[o][8] = 0;
robotGenes[o][9] = 0;
robotGenes[o][10] = 0;
robotGenes[o][11] = 0;
}
for (int o = 161; o <= 200; o++)
{
robotGenes[o][8] = 1;
robotGenes[o][9] = 0;
robotGenes[o][10] = 1;
robotGenes[o][11] = 1;
}
//Running sims (not the game)
Randomizer objectRaDirect;
objectRaDirect.randDirect();
Randomizer objectRaPlace;
objectRaPlace.randPlacement();
for (int sims = 1; sims <= 10000; sims++)
{
Establishing objectClear;
objectClear.clearBoard();
Randomizer objectRaBattery;
objectRaBattery.randBattery();
Running objectRun;
objectRun.runRobot();
abc += 1;
cout << "\n\nGeneration " << abc << " is complete.\n";
Sorting objectPower;
objectPower.sortPower();
Sorting objectGenFit;
objectGenFit.addGenFit();
Genocide objectHitler;
objectHitler.ethnicCleansing();
RobotSex objectSex;
objectSex.bang();
}
Sorting objectWin;
objectWin.sortGenFit();
for (int B = 1; B <= 5; B++)
{
winners[B] = abs (winners[B]);
}
Display objectEnd;
objectEnd.results();
};
You have two major issues with your code:
1) You have an out-of-bounds array access here:
unsigned char robotGenes[200][12] = { }, test[12] = { };
//...
for (int G = 1; G <= 12; G++)
{
cout << robotGenes[1][G];
}
On the last iteration, you are printing a garbage value. The program actually invokes undefined behavior, since robotGenes[1][G] is out-of-bounds when G is 12.
You are also making the same mistake in the ethnicCleansing function, but probably even worse since you are not just reading an invalid location, you are writing to it:
for (int y = 6; y <= 12; y++)
{
robotGenes[x][y] = 0;
}
When y is 12, you are writing to an invalid index.
Array indices start from 0 up to n-1, where n is the total number of entries in the array. Thus G ranges from 0 to 11, inclusive. Going to index 12 is an out-of-bounds access.
2) You are calling functions that are supposed to return a value, but fail to do so. For example,
int Genocide::ethnicCleansing
This function is supposed to return an int, but doesn't return anything. Thus your program invokes undefined behavior. Not returning a value from a function that is supposed to return a value is undefined behavior. Note that you have several other functions with the same issue.
Please see your full code here.
Fix the warnings, fix the out-of-bounds accesses first. I don't know if these will be the only problems, but they are the two that stick out like sore thumbs.
You are most likely printing the data as a char array instead of an unsigned char array. This causes ASCII characters represented by the numbers to be printed instead of the actual numbers.

Hill Cipher Encrypt

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

RNA to protein. Program compiles but won't stop running

I recently learned about multidimensional arrays and was given the task of analyzing strands of RNA and translating them into sequences of protein. I decided to use my knowledge of multidimensional arrays to create a definition of each amino acid a codon (group of 3 RNA bases) would translate to.
//RNA codon to amino acid mapping
char aminoAcid[4][4][4];
//A = 0, C = 1, G = 2, U = 3
//phenylalanine - F
aminoAcid[3][3][3] = 'F';
aminoAcid[3][3][1] = 'F';
//Leucine - L
aminoAcid[3][3][0] = 'L';
aminoAcid[3][3][2] = 'L';
//Serine - S
aminoAcid[3][1][3] = 'S';
aminoAcid[3][1][1] = 'S';
aminoAcid[3][1][0] = 'S';
aminoAcid[3][1][2] = 'S';
//tyrosine - Y
aminoAcid[3][0][3] = 'Y';
aminoAcid[3][0][1] = 'Y';
//stop codon
aminoAcid[3][0][0] = '-';
aminoAcid[3][0][2] = '-';
//cysteine - C
aminoAcid[3][2][3] = 'C';
aminoAcid[3][2][1] = 'C';
//stop codon
aminoAcid[3][2][0] = '-';
//tryptophan - W
aminoAcid[3][2][2] = 'W';
//leucine - L
aminoAcid[1][3][3] = 'L';
aminoAcid[1][3][1] = 'L';
aminoAcid[1][3][0] = 'L';
aminoAcid[1][3][2] = 'L';
//proline - P
aminoAcid[1][1][3] = 'P';
aminoAcid[1][1][1] = 'P';
aminoAcid[1][1][0] = 'P';
aminoAcid[1][1][2] = 'P';
//histidine - H
aminoAcid[1][0][3] = 'H';
aminoAcid[1][0][1] = 'H';
//glutamine - Q
aminoAcid[1][0][0] = 'Q';
aminoAcid[1][0][2] = 'Q';
//arginine - R
aminoAcid[1][2][3] = 'R';
aminoAcid[1][2][1] = 'R';
aminoAcid[1][2][0] = 'R';
aminoAcid[1][2][2] = 'R';
//isoleucine - I
aminoAcid[0][3][3] = 'I';
aminoAcid[0][3][1] = 'I';
aminoAcid[0][3][0] = 'I';
//methionine(start codon) - M
aminoAcid[0][3][2] = 'M';
//threonine -T
aminoAcid[0][1][3] = 'T';
aminoAcid[0][1][1] = 'T';
aminoAcid[0][1][0] = 'T';
aminoAcid[0][1][2] = 'T';
//asparagine - N
aminoAcid[0][0][3] = 'N';
aminoAcid[0][0][1] = 'N';
//lysine - K
aminoAcid[0][0][0] = 'K';
aminoAcid[0][0][2] - 'K';
//serine - S
aminoAcid[0][2][3] = 'S';
aminoAcid[0][2][1] = 'S';
//arginine - R
aminoAcid[0][2][0] = 'R';
aminoAcid[0][2][2] = 'R';
//valine - V
aminoAcid[2][3][3] = 'V';
aminoAcid[2][3][1] = 'V';
aminoAcid[2][3][0] = 'V';
aminoAcid[2][3][2] = 'V';
//alanine - A
aminoAcid[2][1][3] = 'A';
aminoAcid[2][1][1] = 'A';
aminoAcid[2][1][0] = 'A';
aminoAcid[2][1][2] = 'A';
//aspartic acid - D
aminoAcid[2][0][3] = 'D';
aminoAcid[2][0][1] = 'D';
//glutamic acid - E
aminoAcid[2][0][0] = 'E';
aminoAcid[2][0][2] = 'E';
//glycine - G
aminoAcid[2][2][3] = 'G';
aminoAcid[2][2][1] = 'G';
aminoAcid[2][2][0] = 'G';
aminoAcid[2][2][2] = 'G';
I created the following function to translate the strand. In this case, please note that my rna strand is:
AUGCUUAUUAACUGAAAACAUAUGGGUAGUCGAUGA
string rnaAnalysis::translateRna()
{
string protein = "";
int firstBase, secondBase, thirdBase;
for(int i = 0; i < newSequence.length() - 2; i+3)
{
if(newSequence[i] == 'A')
{
firstBase = 0;
}
else if(newSequence[i] == 'C')
{
firstBase = 1;
}
else if(newSequence[i] == 'G')
{
firstBase = 2;
}
else if(newSequence[i] == 'U')
{
firstBase = 3;
}
if(newSequence[i+1] == 'A')
{
secondBase = 0;
}
else if(newSequence[i+1] == 'C')
{
secondBase = 1;
}
else if(newSequence[i+1] == 'G')
{
secondBase = 2;
}
else if(newSequence[i+1] == 'U')
{
secondBase = 3;
}
if(newSequence[i+2] == 'A')
{
thirdBase = 0;
}
else if(newSequence[i+2] == 'C')
{
thirdBase = 1;
}
else if(newSequence[i+2] == 'G')
{
thirdBase = 2;
}
else if(newSequence[i+2] == 'U')
{
thirdBase = 3;
}
bool readSequence = true;
if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[0][3][2])
{
readSequence = true;
}
else if (aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][0] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][0][2] ||
aminoAcid[firstBase][secondBase][thirdBase] == aminoAcid[3][2][0])
{
readSequence = false;
}
if(readSequence)
{
protein = protein + aminoAcid[firstBase][secondBase][thirdBase] + " ";
}
else
{
continue;
}
}
return protein;
}
The bool is used for "start codons" and "stop codons", basically codons within the strand that will tell you when to record and when to stop. newSequence would be the RNA strand.
EDIT: I'm fairly new at this, so I understand my code may look really ugly. Any feedback on how to clean it up is much appreciated as well.
for(int i = 0; i < newSequence.length() - 2; i+3)
should be
for(int i = 0; i < newSequence.length() - 2; i += 3)
Your code never changes the value of i which is why it never stops running.
Your loop starts with the same piece of code three times, where you convert the letter to the 'base index'. That's an obvious place to use a function
for (int i = 0; i < newSequence.length() - 2; i += 3)
{
int firstBase = baseIndex(newSequence[i]);
int secondBase = baseIndex(newSequence[i + 1]);
int thirdBase = baseIndex(newSequence[i + 2]);
...
I'll leave you to write the baseIndex function.

Convert julian to date c++

I am trying to get the date from a julian date using this two functions that I wrote
int dayepoch(int amin, int acur,int mcur, int dcur){
int daycount = 0;
for(int a=amin;a<acur;a++){
for(int m=mcur;m<=12;m++){
for(int d=1;d<=daymo(a,m);d++) daycount ++;
}
}
for(int m=1;m<mcur-1;m++){
for(int d=1;d<=daymo(acur,m);d++) daycount ++;
}
for(int d=1;d<=dcur;d++) daycount ++;
return daycount;
}
int *dayepochrev(int amin, int e){
int a = amin;
int m = 1;
int d = 1;
while(dayepoch(amin,a,m,d) <= e) a++;
a--;
while(dayepoch(amin,a,m+1,d) <= e) m++;
m--;
while(dayepoch(amin,a,m+1,d) <= e) d++;
d--;
int *epochrev = new int[3];
epochrev[0] = a;
epochrev[1] = m;
epochrev[2] = d;
return epochrev;
delete[] epochrev;
}
where
int daymo(int a, int m){
int dmmax = 0;
if((a % 4 == 0 && a % 100 != 0)||(a % 400 == 0) ) {
if(m==0)dmmax = 0;
if(m==1)dmmax = 31;
if(m==2)dmmax = 29;
if(m==3)dmmax = 31;
if(m==4)dmmax = 30;
if(m==5)dmmax = 31;
if(m==6)dmmax = 30;
if(m==7)dmmax = 31;
if(m==8)dmmax = 31;
if(m==9)dmmax = 30;
if(m==10)dmmax = 31;
if(m==11)dmmax = 30;
if(m==12)dmmax = 31;
}
else {
if(m==0)dmmax = 0;
if(m==1)dmmax = 31;
if(m==2)dmmax = 28;
if(m==3)dmmax = 31;
if(m==4)dmmax = 30;
if(m==5)dmmax = 31;
if(m==6)dmmax = 30;
if(m==7)dmmax = 31;
if(m==8)dmmax = 31;
if(m==9)dmmax = 30;
if(m==10)dmmax = 31;
if(m==11)dmmax = 30;
if(m==12)dmmax = 31;
}
return dmmax;
}
If the starting month is MOSTART = 4 and the starting year = 1945 and the number of months considered are 12, than the total number of days are
EPOCH = 365
(from 1/4/1945 to 31/3/1946) which I can get using
int EPOCH = dayepoch(1945, 1946, MOSTART,31);
which is the TOTAL NUMBER OF DAYS IN THE GIVEN PERIOD (DAYS SINCE YEAR).
Then I would like to come back to the date using the sequence 1-365 applied to the given period
for(int e=1;e<EPOCH+1;e++){
epochrev = dayepochrev(amin,e);
}
but I get the dates 1-1-1945 -> 31-12-1945. Please can you help me to do this?
Many thanks