SlideShare a Scribd company logo
For any help regarding C++ Homework Help
Visit :- https://www.cpphomeworkhelp.com/ ,
Email :- info@cpphomeworkhelp.com or
Call us at :- +1 678 648 4277
Problem : Transposition Cipher (loop)
A very simple transposition cipher encrypt(S) can be described by the following rules:
1. If the length of S is 1 or 2, then encrypt(S) is S.
2. If S is a string of N characters s 1 s 2 s 3 . . . s N and k = IN /2j, then
enc(S) = encrypt(sk sk−1 . . . s 2 s 1) + encrypt(s N s N −1 . . . sk+1)
where + indicates string concatenation.
For example, encrypt("Ok") = "Ok" and encrypt("12345678") = "34127856".
Write a program to implement this cipher, given an arbitrary text file input up to 16 MB
in size. Start with the template program found at provided in the file loop.data.zip as a
basis for your program. In this program, you will see a mostly complete function to read
a file into a dynamically allocated string as required for this problem.
size t getstr( char **str, FILE *input ) {
size t chars to read = BLOCK SIZE;
size t length = 0;
//...snipped... see template file
size t chars = 0;
while( ( chars = fread( *str + length, 1, chars to read, input ) ) ) {
// you fill this out
}
// ...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 and the header. If you have any questions about the
provided code or don’t know why something is structured the way it is, please ask about it
on Piazza.
You will also see an empty function “encrypt”, which you should fill out.
void encrypt( char *string, size t length ) {
// you fill this out
}
Resource Limits
For this problem you are allotted 3 seconds of runtime and up to 32 MB of RAM.
Input Format
Lines 1. . . : The whole file (can be any number of lines) should be read in as a
string.
Sample Input (file loop.in)
Test
Early
and often!
Output Format
Line 1: One integer: the total number of characters in the string
Lines 2. . . : The enciphered string.
21
aeyrleT
sttf!enn
aod
Output Explanation
Here’s each character in the string as we are supposed to read it in, separated with ‘.’
so we can see the newlines and spaces:
.T.e.s.t.n.e.a.r.l.y.n.a.n.d. .o.f.t.e.n.!.
The string is first split in half and then each half reversed, and the function called
recursively; you can see the recursion going on here:
This diagram makes it look a bit more complicated than it actually is. You can see that
the sample is 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. .
.y.l.r.a.e.n.t.s.e.T. . !.n.e.t..f.o. .d.n.a.n.
.e.a.r.l.y. . T.e.s.t.n. .f.t.e.n.!. .n.a.n.d. .o.
.a.e. .y.l.r .e.T. .n.t.s. .t.f. .!.n.e .n.a.n. .o. . d.
/  /  /  | 
| 
/  / 
.y. .r.l. .n. .s.t. .!. .e.n.
.n. .n.a. .o. .d. .
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, size_t C
) {
Matrix *matrix = malloc( sizeof( Matrix )
);
matrix->R = R;
matrix->C = C;
matrix->index = malloc( R * C * sizeof(
int ) );
return matrix;
}
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 *input, Transpose orient ) {
size_t R, C;
fscanf( input, "%zu %zu", &R, &C );
Matrix *matrix = NULL;
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] );
}
}
} 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] );
}
}
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 * nInner] * b->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( 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;
}
Below is the output using the test data:
Matrix 2:
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]

More Related Content

Similar to Best C++ Programming Homework Help

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
Programming Homework Help
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
JinTaek Seo
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
Logan Chien
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
skilljiolms
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
Thierry Gayet
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
ezonesolutions
 
Data type2 c
Data type2 cData type2 c
Data type2 c
thirumalaikumar3
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docxShad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Sonu62614
 
C language
C languageC language
C language
Priya698357
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
Gradeup
 
Data type in c
Data type in cData type in c
Data type in c
thirumalaikumar3
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
GC University Faisalabad
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Mohan Raj
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 

Similar to Best C++ Programming Homework Help (20)

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 
Functions
FunctionsFunctions
Functions
 
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docxShad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
 
C language
C languageC language
C language
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Data type in c
Data type in cData type in c
Data type in c
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 

More from C++ Homework Help

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
C++ Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
C++ Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
C++ Homework Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
C++ Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
C++ Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
C++ Homework Help
 

More from C++ Homework Help (18)

cpp promo ppt.pptx
cpp promo ppt.pptxcpp promo ppt.pptx
cpp promo ppt.pptx
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
Get Fast C++ Homework Help
Get Fast C++ Homework HelpGet Fast C++ Homework Help
Get Fast C++ Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Online CPP Homework Help
Online CPP Homework HelpOnline CPP Homework Help
Online CPP Homework Help
 
CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
CPP Homework help
CPP Homework helpCPP Homework help
CPP Homework help
 
CPP homework help
CPP homework helpCPP homework help
CPP homework help
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Cpp Homework Help
Cpp Homework Help Cpp Homework Help
Cpp Homework Help
 

Recently uploaded

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 

Recently uploaded (20)

How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

Best C++ Programming Homework Help

  • 1. For any help regarding C++ Homework Help Visit :- https://www.cpphomeworkhelp.com/ , Email :- info@cpphomeworkhelp.com or Call us at :- +1 678 648 4277
  • 2. Problem : Transposition Cipher (loop) A very simple transposition cipher encrypt(S) can be described by the following rules: 1. If the length of S is 1 or 2, then encrypt(S) is S. 2. If S is a string of N characters s 1 s 2 s 3 . . . s N and k = IN /2j, then enc(S) = encrypt(sk sk−1 . . . s 2 s 1) + encrypt(s N s N −1 . . . sk+1) where + indicates string concatenation. For example, encrypt("Ok") = "Ok" and encrypt("12345678") = "34127856". Write a program to implement this cipher, given an arbitrary text file input up to 16 MB in size. Start with the template program found at provided in the file loop.data.zip as a basis for your program. In this program, you will see a mostly complete function to read a file into a dynamically allocated string as required for this problem.
  • 3. size t getstr( char **str, FILE *input ) { size t chars to read = BLOCK SIZE; size t length = 0; //...snipped... see template file size t chars = 0; while( ( chars = fread( *str + length, 1, chars to read, input ) ) ) { // you fill this out } // ...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 and the header. If you have any questions about the provided code or don’t know why something is structured the way it is, please ask about it on Piazza. You will also see an empty function “encrypt”, which you should fill out.
  • 4. void encrypt( char *string, size t length ) { // you fill this out } Resource Limits For this problem you are allotted 3 seconds of runtime and up to 32 MB of RAM. Input Format Lines 1. . . : The whole file (can be any number of lines) should be read in as a string. Sample Input (file loop.in) Test Early and often!
  • 5. Output Format Line 1: One integer: the total number of characters in the string Lines 2. . . : The enciphered string. 21 aeyrleT sttf!enn aod Output Explanation Here’s each character in the string as we are supposed to read it in, separated with ‘.’ so we can see the newlines and spaces: .T.e.s.t.n.e.a.r.l.y.n.a.n.d. .o.f.t.e.n.!. The string is first split in half and then each half reversed, and the function called recursively; you can see the recursion going on here:
  • 6. This diagram makes it look a bit more complicated than it actually is. You can see that the sample is 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. . .y.l.r.a.e.n.t.s.e.T. . !.n.e.t..f.o. .d.n.a.n. .e.a.r.l.y. . T.e.s.t.n. .f.t.e.n.!. .n.a.n.d. .o. .a.e. .y.l.r .e.T. .n.t.s. .t.f. .!.n.e .n.a.n. .o. . d. / / / | | / / .y. .r.l. .n. .s.t. .!. .e.n. .n. .n.a. .o. .d. .
  • 7. 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, size_t C ) { Matrix *matrix = malloc( sizeof( Matrix ) ); matrix->R = R; matrix->C = C; matrix->index = malloc( R * C * sizeof( int ) );
  • 8. return matrix; } 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 *input, Transpose orient ) { size_t R, C; fscanf( input, "%zu %zu", &R, &C ); Matrix *matrix = NULL;
  • 9. 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] ); } } } else { fprintf( stderr, "Error: unknown orientation %d.n", orient ); exit( EXIT_FAILURE ); }
  • 10. 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] ); } } 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 );
  • 11. } 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 * nInner] * b->index[i + c * nInner]; } } } return prod; } int main(void) { FILE *fin = fopen( "matrix2.in", "r" );
  • 12. 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( 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 );
  • 13. destroy_matrix( a ); destroy_matrix( b ); destroy_matrix( c ); return 0; } Below is the output using the test data: Matrix 2: 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]