This is the code I was able to come up with, so far: /* * File: main.cpp * Autho
ID: 3750615 • Letter: T
Question
This is the code I was able to come up with, so far:
/*
* File: main.cpp
* Author: carlosblanco
*https://code.sololearn.com/cKAqC9car7Xx#java
* https://stackoverflow.com/questions/26941470/initializing-2d-char-array-in-c
* https://www.quora.com/Is-it-a-good-practice-to-declare-the-counter-inside-a-loop-or-outside-My-professor-says-it-is-much-better-practice-to-declare-all-variables-at-the-beginning-of-a-block-is-this-true
* Created on September 16, 2018, 11:51 AM
*/
#include <cstdlib>
#include <stdio.h>
#include <string.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
char square[5][5];
square[0][0] = 'c';
square[0][1] = 'r';
square[0][2] = 'a';
square[0][3] = 'z';
square[0][4] = 'y';
square[1][0] = 'b';
square[1][1] = 'd';
square[1][2] = 'e';
square[1][3] = 'f';
square[1][4] = 'g';
square[2][0] = 'h';
square[2][1] = 'i';
square[2][2] = 'j';
square[2][3] = 'k';
square[2][4] = 'l';
square[3][0] = 'm';
square[3][1] = 'n';
square[3][2] = 'o';
square[3][3] = 'p';
square[3][4] = 's';
square[4][0] = 't';
square[4][1] = 'u';
square[4][2] = 'v';
square[4][3] = 'w';
square[4][4] = 'x';
char * size = sizeof (square);
char input[26];
fgets(input, 26, stdin);
while (input != NULL) {
for (int i = 0; i < size; i++) {
char b;
b = square(i);
int j = 0;
for (j = 0; j < 5; j++) {
int k = 0;
for (k = 0; k < 5; k++) {
if (b == square[j][k]) {
int firstLetter = j + 1;
int secondLetter = k + 1;
printf("%d + ", secondLetter);
}
}
}
}
}
return 0;
}
Explanation / Answer
Yes, Its better to write all variables at a beginning of block, except for
sub block level temporaty variables.
Below are the programs for encryption and decryption.
/*
file: polybiusEncrypt.c
sample input: crazvwx
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv) {
char square[5][5];
int size;
char input[50];
char b;
int i,j,k;
int dig1,dig2;
square[0][0] = 'c';
square[0][1] = 'r';
square[0][2] = 'a';
square[0][3] = 'z';
square[0][4] = 'y';
square[1][0] = 'b';
square[1][1] = 'd';
square[1][2] = 'e';
square[1][3] = 'f';
square[1][4] = 'g';
square[2][0] = 'h';
square[2][1] = 'i';
square[2][2] = 'j';
square[2][3] = 'k';
square[2][4] = 'l';
square[3][0] = 'm';
square[3][1] = 'n';
square[3][2] = 'o';
square[3][3] = 'p';
square[3][4] = 's';
square[4][0] = 't';
square[4][1] = 'u';
square[4][2] = 'v';
square[4][3] = 'w';
square[4][4] = 'x';
fgets(input, 50, stdin); //read string from keyboard
size = strlen(input); //calculate its length
if (input != NULL) {
for ( i = 0; i < size-1; i++) {
b = input[i]; // read letter from string
for ( j = 0; j < 5; j++) {
for ( k = 0; k < 5; k++) {
if(b == 'q'){
// if the letter is 'q' then consider it as 'i'
// as per given in specification
// location of i in array is [2][1], so we print it by adding 1,so 32
// because address of 1st item in array is 00. but we print it as 11
printf("32 ");
}
else if (b == square[j][k]) {
//if input letter matches with array element
//then print its location(j,k)
int firstLetter = j + 1;
int secondLetter = k + 1;
printf("%d%d ", firstLetter, secondLetter);
break;
}
}
}
}
}
return 0;
}
------------------------------------------------------
/*
file: polybiusDecrypt.c
sample input: 1112131415424344
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char ** argv) {
char square[5][5];
int size = sizeof(square);
char input[50];
int i, dig1,dig2;
square[0][0] = 'c';
square[0][1] = 'r';
square[0][2] = 'a';
square[0][3] = 'z';
square[0][4] = 'y';
square[1][0] = 'b';
square[1][1] = 'd';
square[1][2] = 'e';
square[1][3] = 'f';
square[1][4] = 'g';
square[2][0] = 'h';
square[2][1] = 'i';
square[2][2] = 'j';
square[2][3] = 'k';
square[2][4] = 'l';
square[3][0] = 'm';
square[3][1] = 'n';
square[3][2] = 'o';
square[3][3] = 'p';
square[3][4] = 's';
square[4][0] = 't';
square[4][1] = 'u';
square[4][2] = 'v';
square[4][3] = 'w';
square[4][4] = 'x';
fgets(input, 26, stdin);//read numeric string in array
size = strlen(input)-1; //calculate its length
if(size%2==1)
return 0; //if string size is odd means wrong input so exit
for ( i = 0; i <size; i=i+2) {
//read two letters from input string
//convert it into two integers dig1, dig2
dig1 = input[i]-48; //convert char to its equivalent digit ( '2' into 2)
dig2 = input[i+1]-48;
printf("%c ",square[dig1-1][dig2-1]);
//display the character specified by the two digits dig1, dig2
}
return 0;
}
--------------------------------------
Note: Question image was not clear & difficult to read, please upload clear image next time :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.