Computer Oriented Numerical Methods
Computer Oriented Numerical Methods
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];
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;
}
#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;
}
#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;
}
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;
}
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.