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

Computer Oriented Numerical Methods

This document summarizes revisions made to labs from a previous course. It includes C code examples for matrix multiplication, reading from and writing to files, using functions, pointers, dynamic memory allocation, and structures. The codes revise various concepts taught in an Advanced Computer Programming and Data Structures course from the previous semester.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Computer Oriented Numerical Methods

This document summarizes revisions made to labs from a previous course. It includes C code examples for matrix multiplication, reading from and writing to files, using functions, pointers, dynamic memory allocation, and structures. The codes revise various concepts taught in an Advanced Computer Programming and Data Structures course from the previous semester.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB 1:

TITLE: REVISION OF LABS PERFORMED IN EE441L

CODE 1: MATRIX MULTIPLICATION


#include <stdio.h>

int main()
{
int i, j, k;
int r1, c1, r2, c2;

//initializing matrix 1
printf("\nEnter the no. of rows and column of the matrix 1: ");
scanf("%d %d", &r1, &c1);

//initializing matrix 2
printf("\nEnter the no. of rows and column of the matrix 2: ");
scanf("%d %d",&r2,&c2);

float mat1[r1][c1];
float mat2[r2][c2];
float mul[c1][r2];

//checking if matrix multiplication is possible


if(c1!=r2)
{
printf("\nMatrix multiplication is not possible");
return -1;
}
//if possible then taking values for matrices
else
{
//mat1
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter the values for matrix1[%d][%d]",i,j);
scanf("%f",&mat1[i][j]);
}
}
printf("\n");
//mat2
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter the values for matrix2[%d][%d]",i,j);
scanf("%f",&mat2[i][j]);
}
}
}
//Displaying the matrices
printf("matrix1\n");
for(i=0;i<r1;i++)

1
{
for(j=0;j<c1;j++)
{
printf(" %0.2f ",mat1[i][j]);
}
printf("\n");
}
printf("matrix2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(" %0.2f ",mat2[i][j]);
}
printf("\n");
}

//matrix multiply
for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
mul[i][j]=0;
for(k=0;k<c2;k++)
{
mul[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
//Result
printf("matrix1 * matrix2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(" %0.2f ",mul[i][j]);
}
printf("\n");
}
return 0;
}

CODE 2: READ FROM A FILE

#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
fp = fopen("FILE.dat","r");//in place of FILE.dat it can be any desired file
while(ch!='\n')
{
ch = getc(fp);
printf("%c",ch);

2
}
fclose(fp);
return 0;
}

CODE 3: WRITE TO A FILE

#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
FILE *fp;
fp = fopen("FILE.dat","w");
if(fp == NULL)
{
printf("Error!");
exit(1);
}
printf("Write something ");
while(ch!='\n')
{
scanf("%c",&ch);
fprintf(fp,"%c",ch);
}
fclose(fp);
return 0;
}

CODE 4: USE OF FUNCTION


///reversiong of number using fuction
#include <stdio.h>
int reverse(int num);
int main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
printf("reverse = %d",reverse(num));
return 0;
}

int reverse(int num)


{
static int rev = 0;
static int base_pos = 1;
if(num > 0)
{
reverse(num/10);
rev += (num%10)*base_pos;
base_pos *= 10;
}
return rev;
}

3
CODE 5: POINTERS
//swapping of numbers using pointers
#include <stdio.h>
void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int main()
{
int a,b;
printf("Enter the value of a and b ");
scanf("%d %d",&a,&b);
printf("\nBefore swap a = %d b = %d",a,b);
swap(&a,&b);
printf("\nafter swap a = %d b = %d",a,b);
return 0;
}

CODE 6: DYNAMIC MEMORY ALLOCATION


#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,num,*pt;
printf("Enter the number of elements ");
scanf("%d ",&n);

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

if(pt == NULL)
{
printf("Sorry could not allocate");
exit(0);
}
printf("Enter elements of array ");
for(i=0;i<n;++i)
{
scanf("%d ",pt+i);
}
printf("\nThe elements are ");
for(i=0;i<n;++i)
{
printf("\n%d ",*(pt+i));
}
free(pt);
return 0;
}

4
CODE 7: STRUCTURES
//use of structure
#include <stdio.h>
struct student{
char name[20];
int age;
};

int main()
{
int n,i;
printf("Enter the number of students ");
scanf("%d",&n);
struct student st[n];

for(i=0;i<n;i++)
{
printf("\nEnter the data of student %d ",i+1);
printf("\n Name ");
scanf("%s",st[i].name);
printf("\n age ");
scanf("%d ",&st[i].age);

for(i=0;i<n;i++)
{
printf("Data of student %d ",i+1);
printf("\n Name %s",st[i].name);
printf("\n age %d",st[i].age);
}

return 0;
}

COMMENTS:
In this lab, we got to revise the C codes that were taught in the previous semester (EE441, Advanced
Computer Programming and Data Structure). Codes for Matrix Multiplication, File operation, Functions,
Pointer, Dynamic allocation of memory, structures and data structures were revised.

You might also like