For any help regarding Electrical Engineering Assignment Help
visit :- https://www.programminghomeworkhelp.com/
Email :- support@programminghomeworkhelp.com
or call us at :- +1 678 648 4277
programminghomeworkhelp.com
Problem
Averysimpletranspositioncipherencrypt(S )canbedescribedbythefollowingrules:
1. Ifthelengthof Sis1 or2, thenencrypt(S )is S.
2. IfSisastringof N characterss1s2s3. . . sNandk = IN /2j, then
enc(S) = encrypt(sk sk−1 ... s2s1)+ encrypt(sN sN−1 ... sk+1)
where+ indicatesstringconcatenation.
Forexample,encrypt("Ok") = "Ok" andencrypt("12345678") ="34127856".
Writeaprogramto implementthiscipher,givenanarbitrarytextfileinputupto 16 MB insize.Start withthe
templateprogramfoundatprovided in the file loop.data.zip as a basis for your program. Inthisprogram,youwill
seeamostlycompletefunctionto readafileinto adynamicallyallocated stringasrequiredforthisproblem.
programminghomeworkhelp.com
size t getstr( char **str, FILE *input ) {
size t chars to read = BLOCK SIZE; size t length = 0;
II ...snipped... see template file size t chars = 0;
while( ( chars = fread( *str + length, 1, chars to read, input ) ) ) { II you fill this out
}
II ...snipped... see template file return length;
}
Read through the code carefully, make sure you understand it, and complete the inner part of the while loop.
Look up realloc andthe <string.h> header. If you have any questions about the provided code or don’tknow
why something is structured the way it is, pleaseaskabout it on Piazza.
Youwill alsoseeanemptyfunction“encrypt”,whichyoushouldfillout.
void encrypt( char *string, size t length ) {
II you fill this out
}
Resource Limits
Forthisproblemyouareallotted3secondsof runtimeandupto 32MB of RAM.
Input Format
Lines 1. ..: The whole file (can be any number of lines) should be read in asa string.
21
aeyrleT sttf!enn aod
Test
early
and often!
Output Format
Line1: One integer:thetotalnumberof charactersinthestring Lines
2. . . :The encipheredstring.
Output Explanation
Here’seachcharacterinthestringaswearesupposedto readit in, separatedwith ‘.’ sowecanseethe newlines
andspaces:
.T.e.s.t.n.e.a.r.l.y.n.a.n.d. .o.f.t.e.n.!.
The stringisfirstsplitinhalfandtheneachhalfreversed,andthefunctioncalledrecursively;youcan seethe
recursiongoingonhere:
.y.l.r.a.e.n.t.s.e.T. .!.n.e.t..f.o. .d.n.a.n.
.n.a.n.d. .o.
.e.a.r.l.y.
.a.e. .y.l.r
I 
.T.e.s.t.n.
.e.T. .n.t.s.
I 
.f.t.e.n.!.
.t.f. .!.n.e
I 
.y. .r.l. .n. .s.t. .!. .e.n. I 
.n.a.n. .o. .d.


I 
.n. .n.a. .o. .d. .
This diagrammakesit lookabit morecomplicatedthanit actuallyis.Youcanseethatthesampleis correct by
reading off the leaves of the tree from left to right–it’s the enciphered string we want.
.a.e.y.r.l.e.T.n.s.t.t.f.!.e.n.n.n.a.o.d. .
programminghomeworkhelp.com
Solution
/*PROG: matrix2
LANG: C
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Matrix_s {
size_t R, C;
int *index;
} Matrix;
Matrix*
allocate_matrix( size_t R, siz
e_t C ) {
Matrix *matrix
= malloc( sizeof( Matrix ) );
matrix->R = R;
matrix->C = C;
matrix->index = malloc( R *
C * sizeof( int ) );
return matrix;
}
programminghomeworkhelp.com
void destroy_matrix(
Matrix *matrix ) {
free( matrix->index );
free( matrix );
}
typedef enum {
REGULAR = 0,
TRANSPOSE = 1
} Transpose;
// Allowing reading a
matrix in as either
regular or transposed
Matrix*
read_matrix( FILE *inp
ut, Transpose orient ) {
size_t R, C;
fscanf( input, "%zu
%zu", &R, &C );
Matrix *matrix =
NULL;
programminghomeworkhelp.com
if( orient == REGULAR
) {
matrix =
allocate_matrix( R, C );
for( size_t r = 0; r <
matrix->R; ++r ) {
for( size_t c = 0; c <
matrix->C; ++c ) {
fscanf( input, "%d",
&matrix->index[c + r *
C] );
}
}
} else if( orient ==
TRANSPOSE ) {
matrix =
allocate_matrix( C, R );
for( size_t r = 0; r <
matrix->C; ++r ) {
for( size_t c = 0; c <
matrix->R; ++c ) {
fscanf( input, "%d",
&matrix->index[r + c *
R] );
}
}
programminghomeworkhelp.com
} else {
fprintf( stderr, "Error: unknown
orientation %d.n", orient );
exit( EXIT_FAILURE );
}
return matrix;
}
void print_matrix( FILE *output,
Matrix *matrix ) {
fprintf( output, "%zu %zun", matrix-
>R, matrix->C );
for( size_t r = 0; r < matrix->R; ++r )
{
for( size_t c = 0; c < matrix->C - 1;
++c ) {
fprintf( output, "%d ", matrix-
>index[c + r * matrix->C] );
}
fprintf( output, "%dn", matrix-
>index[matrix->C - 1 + r * matrix->C]
);
}
}
programminghomeworkhelp.com
Matrix* product_matrix(
Matrix *a, Matrix *b ) {
if( a->C != b->C ) {
printf( "Error: tried to
multiply
(%zux%zu)x(%zux%zu)n",
a->R, a->C, b->C, b->R );
exit( EXIT_FAILURE );
}
Matrix *prod =
allocate_matrix( a->R, b->R
);
size_t nRows = prod->R,
nCols = prod->C, nInner = a-
>C;
for( size_t r = 0; r < nRows;
++r ) {
for( size_t c = 0; c <
nCols; ++c ) {
prod->index[c + r *
nCols] = 0;
for( size_t i = 0; i <
nInner; ++i ) {
prod->index[c + r *
nCols] += a->index[i + r *
programminghomeworkhelp.com
>index[i + c * nInner];
}
}
}
return prod;
}
int main(void) {
FILE *fin
= fopen( "matrix2.in", "r" );
if( fin == NULL ) {
printf( "Error: could not
open matrix2.inn" );
exit( EXIT_FAILURE );
}
Matrix *a = read_matrix(
fin, REGULAR );
Matrix *b = read_matrix(
fin, TRANSPOSE );
fclose( fin );
Matrix *c = product_matrix(
programminghomeworkhelp.com
a, b );
FILE *output
= fopen( "matrix2.out", "w" );
if( output == NULL ) {
printf( "Error: could not
open matrix2.outn" );
exit( EXIT_FAILURE );
}
print_matrix( output, c );
fclose( output );
destroy_matrix( a );
destroy_matrix( b );
destroy_matrix( c );
return 0;
}
programminghomeworkhelp.com
Below is the output using the
test data:
matrix2: 1: OK [0.006
seconds] 2: OK [0.007
seconds] 3: OK [0.007
seconds] 4: OK [0.019
seconds] 5: OK [0.017
seconds] 6: OK [0.109
seconds] 7: OK [0.178
seconds] 8: OK [0.480
seconds] 9: OK [0.791
seconds]10: OK [1.236
seconds]11: OK [2.088
seconds]
programminghomeworkhelp.com

Programming Assignment Help

  • 1.
    For any helpregarding Electrical Engineering Assignment Help visit :- https://www.programminghomeworkhelp.com/ Email :- support@programminghomeworkhelp.com or call us at :- +1 678 648 4277 programminghomeworkhelp.com
  • 2.
    Problem Averysimpletranspositioncipherencrypt(S )canbedescribedbythefollowingrules: 1. IfthelengthofSis1 or2, thenencrypt(S )is S. 2. IfSisastringof N characterss1s2s3. . . sNandk = IN /2j, then enc(S) = encrypt(sk sk−1 ... s2s1)+ encrypt(sN sN−1 ... sk+1) where+ indicatesstringconcatenation. Forexample,encrypt("Ok") = "Ok" andencrypt("12345678") ="34127856". Writeaprogramto implementthiscipher,givenanarbitrarytextfileinputupto 16 MB insize.Start withthe templateprogramfoundatprovided in the file loop.data.zip as a basis for your program. Inthisprogram,youwill seeamostlycompletefunctionto readafileinto adynamicallyallocated stringasrequiredforthisproblem. programminghomeworkhelp.com size t getstr( char **str, FILE *input ) { size t chars to read = BLOCK SIZE; size t length = 0; II ...snipped... see template file size t chars = 0; while( ( chars = fread( *str + length, 1, chars to read, input ) ) ) { II you fill this out } II ...snipped... see template file return length; } Read through the code carefully, make sure you understand it, and complete the inner part of the while loop. Look up realloc andthe <string.h> header. If you have any questions about the provided code or don’tknow why something is structured the way it is, pleaseaskabout it on Piazza. Youwill alsoseeanemptyfunction“encrypt”,whichyoushouldfillout. void encrypt( char *string, size t length ) { II you fill this out } Resource Limits Forthisproblemyouareallotted3secondsof runtimeandupto 32MB of RAM. Input Format Lines 1. ..: The whole file (can be any number of lines) should be read in asa string.
  • 3.
    21 aeyrleT sttf!enn aod Test early andoften! Output Format Line1: One integer:thetotalnumberof charactersinthestring Lines 2. . . :The encipheredstring. Output Explanation Here’seachcharacterinthestringaswearesupposedto readit in, separatedwith ‘.’ sowecanseethe newlines andspaces: .T.e.s.t.n.e.a.r.l.y.n.a.n.d. .o.f.t.e.n.!. The stringisfirstsplitinhalfandtheneachhalfreversed,andthefunctioncalledrecursively;youcan seethe recursiongoingonhere: .y.l.r.a.e.n.t.s.e.T. .!.n.e.t..f.o. .d.n.a.n. .n.a.n.d. .o. .e.a.r.l.y. .a.e. .y.l.r I .T.e.s.t.n. .e.T. .n.t.s. I .f.t.e.n.!. .t.f. .!.n.e I .y. .r.l. .n. .s.t. .!. .e.n. I .n.a.n. .o. .d. I .n. .n.a. .o. .d. . This diagrammakesit lookabit morecomplicatedthanit actuallyis.Youcanseethatthesampleis correct by reading off the leaves of the tree from left to right–it’s the enciphered string we want. .a.e.y.r.l.e.T.n.s.t.t.f.!.e.n.n.n.a.o.d. . programminghomeworkhelp.com
  • 4.
    Solution /*PROG: matrix2 LANG: C */ #include<stdio.h> #include <stdlib.h> #include <string.h> typedef struct Matrix_s { size_t R, C; int *index; } Matrix; Matrix* allocate_matrix( size_t R, siz e_t C ) { Matrix *matrix = malloc( sizeof( Matrix ) ); matrix->R = R; matrix->C = C; matrix->index = malloc( R * C * sizeof( int ) ); return matrix; } programminghomeworkhelp.com
  • 5.
    void destroy_matrix( Matrix *matrix) { free( matrix->index ); free( matrix ); } typedef enum { REGULAR = 0, TRANSPOSE = 1 } Transpose; // Allowing reading a matrix in as either regular or transposed Matrix* read_matrix( FILE *inp ut, Transpose orient ) { size_t R, C; fscanf( input, "%zu %zu", &R, &C ); Matrix *matrix = NULL; programminghomeworkhelp.com
  • 6.
    if( orient ==REGULAR ) { matrix = allocate_matrix( R, C ); for( size_t r = 0; r < matrix->R; ++r ) { for( size_t c = 0; c < matrix->C; ++c ) { fscanf( input, "%d", &matrix->index[c + r * C] ); } } } else if( orient == TRANSPOSE ) { matrix = allocate_matrix( C, R ); for( size_t r = 0; r < matrix->C; ++r ) { for( size_t c = 0; c < matrix->R; ++c ) { fscanf( input, "%d", &matrix->index[r + c * R] ); } } programminghomeworkhelp.com
  • 7.
    } else { fprintf(stderr, "Error: unknown orientation %d.n", orient ); exit( EXIT_FAILURE ); } return matrix; } void print_matrix( FILE *output, Matrix *matrix ) { fprintf( output, "%zu %zun", matrix- >R, matrix->C ); for( size_t r = 0; r < matrix->R; ++r ) { for( size_t c = 0; c < matrix->C - 1; ++c ) { fprintf( output, "%d ", matrix- >index[c + r * matrix->C] ); } fprintf( output, "%dn", matrix- >index[matrix->C - 1 + r * matrix->C] ); } } programminghomeworkhelp.com
  • 8.
    Matrix* product_matrix( Matrix *a,Matrix *b ) { if( a->C != b->C ) { printf( "Error: tried to multiply (%zux%zu)x(%zux%zu)n", a->R, a->C, b->C, b->R ); exit( EXIT_FAILURE ); } Matrix *prod = allocate_matrix( a->R, b->R ); size_t nRows = prod->R, nCols = prod->C, nInner = a- >C; for( size_t r = 0; r < nRows; ++r ) { for( size_t c = 0; c < nCols; ++c ) { prod->index[c + r * nCols] = 0; for( size_t i = 0; i < nInner; ++i ) { prod->index[c + r * nCols] += a->index[i + r * programminghomeworkhelp.com
  • 9.
    >index[i + c* nInner]; } } } return prod; } int main(void) { FILE *fin = fopen( "matrix2.in", "r" ); if( fin == NULL ) { printf( "Error: could not open matrix2.inn" ); exit( EXIT_FAILURE ); } Matrix *a = read_matrix( fin, REGULAR ); Matrix *b = read_matrix( fin, TRANSPOSE ); fclose( fin ); Matrix *c = product_matrix( programminghomeworkhelp.com
  • 10.
    a, b ); FILE*output = fopen( "matrix2.out", "w" ); if( output == NULL ) { printf( "Error: could not open matrix2.outn" ); exit( EXIT_FAILURE ); } print_matrix( output, c ); fclose( output ); destroy_matrix( a ); destroy_matrix( b ); destroy_matrix( c ); return 0; } programminghomeworkhelp.com
  • 11.
    Below is theoutput using the test data: matrix2: 1: OK [0.006 seconds] 2: OK [0.007 seconds] 3: OK [0.007 seconds] 4: OK [0.019 seconds] 5: OK [0.017 seconds] 6: OK [0.109 seconds] 7: OK [0.178 seconds] 8: OK [0.480 seconds] 9: OK [0.791 seconds]10: OK [1.236 seconds]11: OK [2.088 seconds] programminghomeworkhelp.com