0% found this document useful (0 votes)
2 views

assignment3

The document contains multiple programming questions and code snippets related to C programming, including file handling, sorting algorithms, and matrix operations. It provides a C program for reading files, a comparison of binary and text file handling, a table of algorithm complexities, and a matrix multiplication program. Each section includes code examples and explanations of the functionality.

Uploaded by

gamerxzy42
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

assignment3

The document contains multiple programming questions and code snippets related to C programming, including file handling, sorting algorithms, and matrix operations. It provides a C program for reading files, a comparison of binary and text file handling, a table of algorithm complexities, and a matrix multiplication program. Each section includes code examples and explanations of the functionality.

Uploaded by

gamerxzy42
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Ques 1 :

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char* argv[argc +1]){


int ret = EXIT_FAILURE;
for(size_t i = 1; i < argc ; ++i){
FILE* instream = fopen(argv[i] , "r");
if(instream){
char *line = NULL;
size_t len = 0;
size_t line_number = 0;
while(getline(&line, &len, instream) != EOF) {
printf("%lu %s", ++line_number ,line);
}
fclose(instream);
if(line)
free(line);
ret = EXIT_SUCCESS;
} else{
fprintf(stderr, "Could not open %s: ", argv[i]);
perror(0);
errno = 0;
}
}
return ret;
}

ques 2 :
Binary file handling is used to handle binary files i.e. files that consist of only binary data 0 or 1, while
text file handling is used to handle text files i.e. files those contain text encoded data.

Ques 3 :
Algorithm Best case Time Worst case time Average time Worst Case Space complexity
Complexity complexity complexity
Bubble Sort Ω(n) O(n2) θ(n2) O(n)
Insertion Sort Ω(n) O(n2) θ(n2) O(n)
Selection Sort Ω(n2) O(n2) θ(n2) O(n)
Merge Sort Ω(n log n) O(n log n) θ(n log n) O(n)
Binary Search Ω(log n) O(log n) θ(log n) O(n)
Tree
Binary Search Ω(1) O(log n) θ(log n) O(1)
Linear Search Ω(1) O(n) θ(n / 2) O(1) iterative
Ques 4 :
#include<stdlib.h>
#include<stdio.h>
typedef struct Matrix Matrix;
struct Matrix{
// arrays of arrays
size_t rows, cols;
double **arr;
};
Matrix* matrix_init(size_t nrows, size_t ncols)
{
Matrix *return_matrix = malloc(sizeof(Matrix));
return_matrix->cols = ncols;
return_matrix->rows = nrows;
size_t len = sizeof(double*) * nrows + sizeof(double) * nrows * ncols;
return_matrix->arr = malloc(len);
double *mem = (double *)(return_matrix->arr + nrows);
for(size_t i = 0; i < nrows ; ++i)
{
return_matrix->arr[i] = mem + (i * ncols);
}
return return_matrix;
}

void print_matrix(Matrix* m)
{
for(size_t i = 0; i < m->rows; ++i)
{
printf("[%f",m->arr[i][0]);
for(size_t j = 1; j < m->cols; ++j)
{
printf(",%f",m->arr[i][j]);
}
printf("]\n");
}
}

void scan_matrix(Matrix *m)


{
for(size_t i = 0 ; i < m->rows; ++i)
{
for(size_t j = 0 ; j < m->cols; ++j)
{
scanf("%lf", &m->arr[i][j]);
}
}
}
void free_matrix(Matrix *m)
{
free(m->arr);
free(m);
}

// multiplies the two matrix and returns the result


Matrix* matrix_mul(Matrix *m1, Matrix *m2)
{
if(m1->cols != m2->rows){
perror("Cannot multiply matrix!\n");
return NULL;
}
Matrix* answer = matrix_init(m1->rows, m2->cols);
for(size_t i = 0 ; i < m1->rows; ++i){
for(size_t j = 0 ; j < m2->cols; ++j){
answer->arr[i][j] = 0;
for(size_t k = 0 ; k < m1->cols; ++k){
answer->arr[i][j] += m1->arr[i][k] * m2->arr[k][j];
}
}
}
return answer;
}

int main()
{
Matrix* m1 = matrix_init(2, 2);
Matrix* m2 = matrix_init(2, 2);
printf("Enter matrix one : \n");
scan_matrix(m1);
printf("Enter matrix two : \n");
scan_matrix(m2);
printf("Matrix one is : \n");
print_matrix(m1);
printf("Matrix two is : \n");
print_matrix(m1);
Matrix *m3 = matrix_mul(m1, m2);
printf("\nAnswer is : \n");
print_matrix(m3);
free(m1);
free(m2);
free(m3);
return 0;
}

You might also like