In this assignment, you will do a number of C programming exercises. 1. Matrix m
ID: 3746171 • Letter: I
Question
In this assignment, you will do a number of C programming exercises.
1. Matrix multiplication (matrixmul.c):
Given an n x k matrix A and an k x m matrix B, with 1n,m,k300, write a C program that computes the matrix product C=AB. All entries in matrices A and B are integers with abolute value less than 1000, so you don't need to worry about overflow. If matrices A and B do not have the right dimensions to be multiplied, the product matrix C should have its number of rows and columns both set to zero. Input/Output: please use scanf and printf to handle the data input and output.
Input format: Line 1: Two space-separated integers, n and k. Line 2 to n+1: line i+1 contains k space-separated integers: row i of matrix A. Line n+2: Two space-separated integers, k and m. Line n+3 to n+k+4: Line i+n+3 contains m space-separated integers: row i of matrix B.
Sample Input:
3 2 1 1 1 2 -4 0 2 3 1 2 1 3 2 1
Output format:
Line 1: two space-separated n and m, the dimension of matrix C.
Line 2 to m+1: Line i+1 contains m space-separated integers: row i of matrix C.
Sample Output:
3 3
4 4 2
7 6 3
-4 -8 -4
2. Transposition Cipher (encrypt.c):
A very simple transposition cipher encrypt(S) can be described by the following rule:
* If the length of S is 1 or 2, then encrypt(S) is S.
* If S is a string of N characters s1 s2...sN and k=N/2, then enc(S)=encrypt(sksk-1...s2s1)+encrypt(sNsN-1...sk+1) where + indicates string cancatenation. For example, encrypt("OK")="OK" and encrypt("12345678")="34127856".
Write a program to implement this cipher, given an arbitary text string from keyboard, up to 8192 characters.
It's better to write a separate encryption function, similar to the following: char* encrypt(char *string, size_t length) { // you fill this out }
Input Format:
an abitary string (with the length up to 8192 characters).
Sample Input:
Test early and often!
Output Format
Line 1: One integer: the toal number of characters in the string.
Line 2: The enciphered string. Sample Output: 21 aeyrleT sttf!enn aod
Explanation / Answer
// I am writing two different .c files,one for matrix multiplication and other for encryption
/* Code for Question 1 below */
//Attaching matrixmul.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void matrixMul(int **A,int **B,int **C,int n,int k,int m){
int i,j,l,sum;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
sum = 0;
for(l=0;l<k;l++){
sum += A[i][l]*B[l][j];
}
C[i][j] = sum;
}
}
printf("%d %d ",n,m);
for(i=0;i<n;i++){
for(j=0;j<m;j++)
printf("%d ",C[i][j]);
printf(" ");
}
}
int main(){
int n,m,k1,k2,i,j;
scanf("%d%d",&n,&k1);
int **A = (int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++){
A[i] = (int*)malloc(sizeof(int)*k1);
}
for(i=0;i<n;i++){
for(j=0;j<k1;j++)
scanf("%d",&A[i][j]);
}
scanf("%d%d",&k2,&m);
int **B = (int**)malloc(sizeof(int*)*k2);
for(i=0;i<k2;i++)
B[i] = (int*)malloc(sizeof(int)*m);
for(i=0;i<k2;i++){
for(j=0;j<m;j++)
scanf("%d",&B[i][j]);
}
if(k1 != k2 ){
printf("0 0 ");
}else {
int **C = (int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++){
C[i] = (int*)malloc(sizeof(int)*m);
}
matrixMul(A,B,C,n,k1,m);
}
return(0);
}
/* Code for Question 2 below */
//Attaching encrypt.c
//Encrypt function
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// Using int instead of size_t as it's simpler to handle
// length denotes the number of characters in string
// length = strlen(string)
char *encrypt(char *string,int length){
int i;
if(length <= 2){
char *newStr = (char*)calloc(3,sizeof(char));
newStr[0] = string[0];
newStr[1] = string[1];
return newStr;
}
else {
//find the middle element 'mid'
int mid = length/2;
char temp;
int start,end;
start = 0;end = mid-1;
//reverse the elements in the string[0..mid-1]
while(start < end) {
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;end--;
}
//reverse the elements in the string[mid...length-1]
start = mid;end = length-1;
while(start < end) {
temp = string[start];
string[start] = string[end];
string[end] = temp;
start++;end--;
}
//pass them into two separate functions
char *str1 = encrypt(string,mid);
char *str2 = encrypt(string+mid,length-mid);
//concatenate them and return them in a new string
char *newStr = (char*)calloc(length+1,sizeof(char));
for(i=0;i<mid;i++)
newStr[i] = str1[i];
for(i=mid;i<length;i++)
newStr[i] = str2[i-mid];
//free the older return strings in order to ave memory
free(str1);
free(str2);
return newStr;
}
}
int main(){
int i;
char *input = (char*)calloc(8194,sizeof(char));
char *encrypted = (char*)calloc(8194,sizeof(char));
fgets(input,8194,stdin);
int length = strlen(input);
//since fgets also reads the newline character hence sending only first length-1 chars
encrypted = encrypt(input,length-1);
printf("%d ",length-1);
for(i=0;i<length-1;i++)
printf("%c",encrypted[i]);
}