Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

IT 2001 LAB ASSIGNMENT – 2

Ankur Kumar Karn


20BCS034

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

int** transposeMatrix(int n, int **A, int **T)


{
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++){
T[c][r] = A[r][c];
}
}
return T;
}

void printMatrix(int n, int **A)


{
for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++) {
printf("%5d ", A[r][c]);
}
printf("\n");
}
}

int main()
{
int n,r,c;
printf("Enter the value of n: ");
scanf("%d ", &n);
int **A = (int**)malloc(n*sizeof(int *));
int **T = (int**)malloc(n*sizeof(int *));

for( r = 0; r<n; r++)


{
A[r] = (int *)malloc(n*sizeof(int));
T[r] = (int *)malloc(n*sizeof(int));
}

for( r = 0; r<n; r++){


for( c = 0; c<n; c++){
scanf("%d", &A[r][c]);
}
}

printf("\n");
T = transposeMatrix(n,A,T);

printMatrix(n , A);
printf("\n");

printMatrix (n, T);

free(A);

return 0;
}
2.
#include <stdio.h>
#include <stdlib.h>

void printHalf(int n, int **A){


for (int r = 0; r < n; r++)
{
for (int c = 0; c < r; c++) {
printf("%d ", A[r][c]);
}
printf("\n");
}
}

void printFull(int n, int **A){


for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++) {
printf("%5d ", A[r][c]);
}
printf("\n");
}
}

void symmetric(int n, int **A){


for (int r = 0; r < n; r++)
{
for (int c = 0; c < n; c++) {
if (A[c][r] != A[r][c]){
printHalf(n,A);
return;
}
}
}
printFull(n,A);
}
int main()
{
int n,r,c ;
printf("Enter the value of n: ");
scanf("%d",&n);

int **A = (int**)malloc(n*sizeof(int *));


for( r = 0; r<n; r++)
{
A[r] = (int *)malloc(n*sizeof(int));
}

for( r = 0; r<n; r++){


for( c = 0; c<n; c++){
scanf("%d", &A[r][c]);
}
}
symmetric( n, A);
free(A);

return 0;
}
3.
#include <stdio.h>
#include <stdlib.h>

int** addMatrix(int n, int **A, int **B, int **C){


for (int r = 0; r < n; r++){
for (int c = 0; c < n; c++) {
C[r][c]= A[r][c] + B[r][c];
}
}
return C;
}

int** randomMatrix(int n, int **A){


for (int r = 0; r < n; r++){
for (int c = 0; c < n; c++) {
A[r][c] = rand()%100;
}
}
return A;
}

void printMatrix(int n, int **A){


for (int r = 0; r < n; r++){
for (int c = 0; c < n; c++) {
printf("%5d ", A[r][c]);
}
printf("\n");
}
}

int main()
{
int n,r,c;

printf("Enter the value of n: ");


scanf("%d ", &n);

int **A = (int**)malloc(n*sizeof(int *));


int **B = (int**)malloc(n*sizeof(int *));
int **C = (int**)malloc(n*sizeof(int *));
for( r = 0; r<n; r++)
{
A[r] = (int *)malloc(n*sizeof(int));
B[r] = (int *)malloc(n*sizeof(int));
C[r] = (int *)malloc(n*sizeof(int));
}

printf("\n");

A = randomMatrix(n,A);
B = randomMatrix(n,B);

C = addMatrix(n,A,B,C);

printMatrix(n , A);
printf("\n");

printMatrix(n , B);
printf("\n");

printMatrix(n , C);
printf("\n");

free(A);

return 0;
}
4.
#include<stdio.h>

int compare_string(char*, char*);

int main()
{
char first[100], second[100], result;

printf("Enter first string\n");


fgets(first, 100, stdin);

printf("Enter second string\n");


fgets(second, 100, stdin);

result = compare_string(first, second);

if ( result == 1 )
printf("Both strings are same.\n");
else
printf("Strings are different.\n");

return 0;
}

int compare_string(char *first, char *second)


{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;

first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 1;
else
return 0;
}

You might also like