Need a simple c++code. The Computer Science Central Intelligence Agency (CSCIA)
ID: 656158 • Letter: N
Question
Need a simple c++code.
The Computer Science Central Intelligence Agency (CSCIA) employs super paranoid computer spies. In order to communicate with each other, they encode their messages. But they aren't satisfied with a simple encoding of the messages. Messages are made up of ASCII characters. Specifically, all that is needed is the letters, a space, an excamation point, a newline, and a semicolon. Characters are encoded by determining where they are in the list of characters (for instance, if the list were A-Z, a-z, space, excamation point, newline, semicolon, then A would be in position 0. However if the list were space, Z, newline, W, A, excamation point, B, X, . . ., then A would be in position 4). This position number is then encoded in binary. The binary is converted to Gray code. The Gray code word is encoded using a Hamming code because the transmission lines are noisy. To further confuse the enemy, the ordering of the characters is changed every morning.
Note that the spies only need 6 bits to represent the 56 characters in binary. So, the Hamming codes will be 10 bits long. Your mission, should you choose to accept it, is to write the decode procedure. Messages will be received as 10 bit packets (you will need to read from the file packets.dat that has one packet per line). THERE ARE NO SPACES BETWEEN THE BITS! Your program will begin by reading in the day's ordering of characters from the file chars.dat. This will be a string of 56 characters (no spaces except the one legal space). This ordering should be stored in a character array. Then your program should read a packet, determine if an error has occurred in transmission, fix any error, extract the Gray code message, convert the Gray code message to binary, use the binary to look up the character in the array (this is called a table lookup), and print the character. Then repeat this procedure for each packet in the file. Your output should contain ONLY the decoded characters. Any other output at all will cause points to be marked off. This includes cute messages about the CSCIA decode program as well as carriage returns and anything else you can think of (You have a humorless, picky boss who has been a spy for way too long). I will allow a single carriage return to be printed before the return statement in main, but that's it!
Sample packets.dat is given below.
1101010101
1101101000
0101000110
1000101001
1110011111
1000011100
0100100110
1101000011
1001001000
0000101011
1111111000
1000011000
1001010011
1001001101
1000101011
0001010101
0001001011
1000101011
1101100101
1001010011
1001001101
1101010110
0000000000
1000110101
0101100011
1000101011
1110110011
0000000000
0100110110
1101001000
0101001110
0100110110
1000011000
1100000011
1000101011
0101111101
0000000000
1000110101
1000110101
1000011000
0001010101
1000110101
0001100110
1100101110
1011100011
0001010101
0101111101
0100101000
1001010011
1000101011
0001010101
0001001011
1000101011
0010110000
0001010101
1101001000
0000000000
1001010011
0101100011
1000101011
1110101101
1000101011
1101111101
1100011000
0010011110
0001011110
1000111011
0100011111
1000011010
0000011111
0000011100
1000100011
0100001000
0000010101
0001111110
1000101011
1000011000
0001001011
1000101011
0100101000
0001010101
1001111110
1000101011
0100110110
0001010101
1001111110
1100000011
1101001000
1000101011
1101001000
1000011000
1101010110
0001100110
1000101011
1000101011
1100101110
1111010101
1101001000
0101001110
1000101011
1110011110
1000011000
0100110110
1100000011
1101001000
1000101011
1111111000
1000011000
1001010011
1001001101
1000101011
0001010101
0001001011
1000101011
1101100101
1001010011
1001001101
1101010110
0000000000
1000110101
0101100011
1000101011
1111111000
1000011000
0000011110
0000011110
1000101011
1101010110
0101001110
0001100110
1000101011
1111010101
1101001000
0001010101
1001111110
1000101011
0001001011
0001010101
0001010101
0000011110
0001100110
1000101011
0111111110
0001010101
1000101011
0000011110
1000011000
1101111011
1000011000
1001010011
1001001101
1000101011
1101010110
0000000000
1001010011
1000101011
1100000011
0000000000
1001010011
1000101011
1000110011
0100011110
0010000000
0101101000
1000111011
1101011110
0101001111
0001100100
1000100011
1000001011
1100001110
1010100011
0000010101
0001111101
0000101000
0001010011
1000101011
0001010101
0001001011
1000101011
0010110000
0001010101
1101001000
0000000000
1001010011
0101100011
1000101011
1110101101
1000101011
0000000000
1101010110
1000101011
1001010011
0001010101
1000101011
1101010110
0000000000
1001010011
0001100110
1000101011
0010011101
0001010101
1001111110
1000101011
0000011110
0001010101
0001010101
0100011011
1000101011
1001111110
0101010000
0001010101
1001010011
1000101011
0000000000
1000101011
0101111101
0001010101
1101010110
0000000000
1001010011
0001100110
1000101011
1000101011
1100101110
1010100011
0000010101
0001111101
0000101000
0001010011
1000101011
0000110011
0100110110
0000000000
0100000101
0000110011
1000101011
1101001000
1000011000
1101010110
1100101110
Sample chars.dat is given below
abcdefghijklmnopqrstuvwxyz
!:ABCDEFGHIJKLMNOPQRSTUVWXYZ
Note there is a space at the end of line 1
Explanation / Answer
to solve this problem u have to codes :
#include <stdio.h>
#include<conio.h>
#include <math.h>
int binarytogray(int);
int main ()
{
int binary, gray;
printf("Enter a binary number: ");
scanf("%d", &binary);
gray = binarytogray(binary);
printf("The gray code of %d is %d ", binary, gray);
return 0;
}
int binarytogray(int bin)
{
int x, y, result = 0,z = 0;
while (bin != 0)
{
x = bin % 10;
binary = binary / 10;
b = bin % 10;
if ((x && !y) || (!x && y))
{
r = r + pow(10, z);
}
z++;
}
return r;
}
then use :
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
ofstream fout("C:\test.txt",ios :: binary);
ifstream file("E:\mnmn.txt");
string content;
while(file >> content)
{
for (size_t i = 0; i < content.size(); i++)
{
while((content[i] == '0')
&& (content[i+1] == '0')
&& (content[i+2] == '0')
&& (content[i+3] == '0')
&& (content[i+4] == '0')
&& (content[i+5] == '0')
&& (content[i+6] == '0')
&& (content[i+7] == '0')
{
char *data = "00000000";
char c = strtol(data, 0, 2);
cout<<c;
}
}
}
}
this problem is not task specified therfore we provide the solution in two parts (as understable )
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.