Beruflich Dokumente
Kultur Dokumente
PROJECT FILE
AMAR MALIK
217/COE/14
COE- I
Programs List:
1
2
3
4
5
6
7
8
INVERSE OF A MATRIX
#include<stdio.h>
#include<math.h>
#define N 100
void input(float a[][N],int n)
{
int i,j;
printf("Enter %d integers:",n*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}
void show(float a[][N],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%4g",a[i][j]);
printf("\n");
}
}
float det(float a[][N],int n)
{
int i,j,k;
float b[N][N],d=0;
if(n==1)
return a[0][0];
if(n>1)
{
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
for(k=0;k<n-1;k++)
{
b[j][k]=a[j+1][k+1];
if(k<=i-1)
b[j][k]=a[j+1][k];
}
d+=a[0][i]*det(b,n-1)*pow(-1,i);
}
return d;
}
}
void Adj(float a[][N],float c[][N],int n)
{
int i,j,k,l;
float b[N][N];
if(n==1)
c[0][0]=1;
else
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
for(k=0;k<n-1;k++)
for(l=0;l<n-1;l++)
{
b[k][l]=a[k+1][l+1];
if(l<=j-1)
b[k][l]=a[k+1][l];
if(k<=i-1)
b[k][l]=a[k][l+1];
if(l<=j-1&&k<=i-1)
b[k][l]=a[k][l];
}
c[j][i]=det(b,n-1)*pow(-1,i+j);
}
}
int main()
{
float a[N][N],b[N][N],x;
int n,i,j;
printf("Enter the order of square matrix:");
scanf("%d",&n);
input(a,n);
printf("Matrix is \n");
show(a,n);
x=det(a,n);
if(x==0)
printf("Inverse of matrix does not exist.");
else
{
Adj(a,b,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
b[i][j]=b[i][j]/x;
RANK OF A MATRIX
# include<stdio.h>
int R,C;
int i, j;
int mat[10][10];
void display( int, int);
void input( int, int);
int Rank_Mat(int , int);
void swap(int, int, int);
/* This function exchange two rows of a matrix */
void swap( int row1,int row2, int col)
{
for( i = 0; i < col; i++)
{
int temp = mat[row1][i];
mat[row1][i] = mat[row2][i];
mat[row2][i] = temp;
}
}
/* This function find rank of matrix */
int Rank_Mat(int row1, int col1)
{
int r, c;
for(r = 0; r< col1; r++)
{
display(R,C);
if( mat[r][r] ) // Diagonal element is not zero
for(c = 0; c < row1; c++)
if(c != r)
{
/* Make all the elements above and below the current principal
diagonal element zero */
float ratio = mat[c][r]/ mat[r][r];
for( i = 0; i < col1; i++)
mat[c][i] -= ratio * mat[r][i];
}
else
printf("\n");
/* Principal Diagonal elment is zero */
else
{
for(c = r+1 ; c < row1; c++)
if (mat[c][r])
{
/* Find non zero elements in the same column */
swap(r,c,col1);
break ;
}
if(c == row1)
{
-- col1;
for(c = 0; c < row1; c ++)
mat[c][r] = mat[c][col1];
}
--r;
}
}
return col1;
}
/* Output function */
void display( int row, int col)
{
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
printf(" %d", mat[i][j]);
}
printf("\n");
}
}
/* Input function */
void input( int row, int col)
{
int value;
for(i = 0 ; i< row; i++)
{
for(j = 0 ; j<col; j++)
{
printf("Input Value for: %d: %d: ", i+1, j+1);
scanf("%d", &value);
mat[i][j] = value;
}
}
}
/* main function */
int main()
{
int rank;
printf("\n Input number of rows:");
scanf("%d", &R);
printf("\n Input number of cols:");
scanf("%d", &C);
input(R, C);
printf("\n Row is : %d", R);
printf("\n Column is : %d \n", C);
printf("\n Entered Two Dimensional array is as follows:\n");
display(R,C);
printf("\n Row is : %d", R);
printf("\n Column is : %d\n", C);
rank = Rank_Mat(R, C);
printf("\n Rank of above matrix is : %d", rank);
return 0;
}
POWER OF A MATRIX
#include<stdio.h>
#include<conio.h>
void mul();
int a[10][10],c[10][10],r;
main()
{
int i,j,n;
printf("Enter the order of the matrix:");
scanf("%d",&r);
printf("Enter the Matrix\n");
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
scanf("%d",&a[i][j]);
if(i==j)
c[i][j]=1;
else
c[i][j]=0;
}
}
printf("Enter the power : ");
scanf("%d",&n);
for(i=0;i < n;i++)
mul();
printf("\n\nAnswer\n");
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
printf(" %d",c[i][j]);
printf("\n");
}
getch();
}
void mul()
{
int b[10][10],i,j,k;
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
{
b[i][j]=0;
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<string.h>
<math.h>
{
choice = 0;
printf("Menu:\n1) Add Student\n2) Record Attendance\n3) View Students
having short attendance\n4) Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
fp = fopen(FILENAME, "a+");
if (fp == NULL){ printf("Something went wrong\n"); break;}
rewind(fp);
printf("Please enter student's name: ");
scanf("%s", tmp.name);
printf("Roll Number: ");
scanf("%s", tmp.roll);
int i, j;
for (i = 0; i < 12; i++)
for (j = 0; j < 32; j++)
tmp.attendance[i][j] = N;
fwrite(&tmp, sizeof(tmp), 1, fp);
fclose(fp);
break;
case 2:
fp = fopen(FILENAME, "r+");
if (fp == NULL){ printf("Something went wrong\n"); break;}
rewind(fp);
int dd, mm;
printf("Please enter date (DD/MM): ");
scanf("%d%d%d", &dd, &mm);
dd--, mm--;
printf("Name\tRoll No\tStatus\n");
while(fread(&tmp, sizeof(tmp), 1, fp) == 1)
{
printf("%s\t%s\t", tmp.name, tmp.roll);
char status = '\n';
while(status != 'A' && status != 'P' && status != 'M')
status = getchar();
switch(status)
{
case 'A': tmp.attendance[dd][mm] = A; break;
case 'P': tmp.attendance[dd][mm] = P; break;
case 'M': tmp.attendance[dd][mm] = M; break;
}
fseek(fp, -sizeof(student), SEEK_CUR);
#include<stdio.h>
#define N 100
void input(int a[N][N],int row,int col)
{
int i,j;
printf("Enter %d integers:",row*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
void mult(int a[N][N],int b[N][N],int c[N][N],int row,int col,int colu)
{
int i,j,k;
for(i=0;i<row;i++)
for(j=0;j<colu;j++)
{
c[i][j]=0;
for(k=0;k<col;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
void show(int a[N][N],int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%4d",a[i][j]);
printf("\n");
}
}
int main()
{
int a[N][N],b[N][N],c[N][N],row1,row2,col1,col2;
printf("Enter the no. of rows and columns of first
matrix:");
scanf("%d %d",&row1,&col1);
input(a,row1,col1);
printf("Enter the no. of rows and columns of second
matrix:");
scanf("%d %d",&row2,&col2);
input(b,row2,col2);
printf("Matrix A is\n");
show(a,row1,col1);
printf("Matrix B is\n");
show(b,row2,col2);
if(col1==row2)
{
mult(a,b,c,row1,col1,col2);
printf("Matrix C=A*B is\n");
show(c,row1,col2);
}
else
#include<stdio.h>
struct complex
{
float a,b;
};
typedef struct complex COMPLEX;
COMPLEX input()
{
COMPLEX x;
printf("Enter the real part");
scanf("%f",&x.a);
printf("Enter the imaginary part");
scanf("%f",&x.b);
return x;
}
void show(COMPLEX x)
{
printf("%g%+gi\n",x.a,x.b);
}
COMPLEX add(COMPLEX x,COMPLEX y)
{
COMPLEX z;
z.a=x.a+y.a;
z.b=x.b+y.b;
return z;
}
COMPLEX sub(COMPLEX x,COMPLEX y)
{
COMPLEX z;
z.a=x.a-y.a;
z.b=x.b-y.b;
return z;
}
COMPLEX mult(COMPLEX x,COMPLEX y)
{
COMPLEX z;
z.a=x.a*y.a-x.b*y.b;
z.b=x.a*y.b+x.b*y.a;
return z;
}
COMPLEX div(COMPLEX x,COMPLEX y)
{
COMPLEX z;
z.a=(x.a*y.a+x.b*y.b)/(y.a*y.a+y.b*y.b);
z.b=(x.b*y.a-x.a*y.b)/(y.a*y.a+y.b*y.b);
return z;
}
COMPLEX power(COMPLEX z,int n)
{
COMPLEX t;
if(n==0)
{
t.a=1;
t.b=0;
return t;
}
if(n==1)
return z;
else
return mult(z,power(z,n-1));
}
int main()
{
COMPLEX x,y,z;
int n;
printf("Complex Numbers");
printf("\nEnter two complex nos.\n");
x=input();
y=input();
printf("Two complex nos. you entered are:\n");
show(x);
show(y);
z=add(x,y);
printf("Sum of the complex nos. is\n");
show(z);
z=sub(x,y);
printf("Difference of the complex nos. is\n");
show(z);
z=mult(x,y);
printf("Product of the complex nos. is\n");
show(z);
z=div(x,y);
printf("Division of the complex nos. is\n");
show(z);
printf("Enter an complex no.\n");
x=input();
printf("Which power of this complex no. do you want?");
scanf("%d",&n);
z=power(x,n);
show(z);
return 0; }
ch[0]=cin.get();
while(ch[i]!='\n'){
i++;
ch[i]=cin.get();
}
k=0;
while(k<=i-1){
val=0;
j=k;
f=j-1;
while(f>=0){
flag=1;
if(ch[j]==ch[f]){
flag=0;
break;
}
f--;
}
if(flag==1){
while(j<i){
if(ch[k]==ch[j]){
val++;
}
j++;
}
cout<<"freq of "<<ch[k]<<" is "<<val;
}
k++;
cout<<'\n';
}
return 0;}
main()
{
int roll1,roll2;
enum standard {FIRST,SECOND,THIRD,FOURTH};
enum standard s1,s2;
printf("\n Enter the roll numbers for two students\n");
scanf("%d%d",&roll1,&roll2);
s1=FIRST;
s2=FOURTH; //assigning the standards
printf("\nThe Roll Number %d is in %d
Standard",roll1,s1+1);
printf("\nThe Roll Number %d is in %d
Standard",roll1,s2+1);
}
#include<stdio.h>
struct product{
char name[100];
float price;
int code;
};
int main(void){
struct product p;
FILE *f,*F;
int code,x=0,ch;
while(ch!=$){
printf("\n1.Add new item");
printf("\n2.Delete an item");
printf("\n3.Find and replace an item");
printf("\nEnter choice: ");
scanf("%d",&ch);
switch(ch){
case 1: f=fopen("inventory.txt","a");
printf("\nEnter product's name: ");
while((getchar())!='\n');
scanf("%s",&p.name);
printf("\nEnter price: ");
while((getchar())!='\n');
scanf("%f",&p.price);
printf("\nEnter products's code: ");
while((getchar())!='\n');
scanf("%d",&p.code);
fwrite(&p,sizeof(struct product),1,f);
fclose(f);
break;
case 2:
F=fopen("temp.txt","w");
f=fopen("inventory.txt","r");
printf("\nEnter code of the product to be deleted: ");
scanf("%d",&code);
while(!feof(f)){
fread(&p,sizeof(struct product),1,f);
if(p.code!=code)
fwrite(&p,sizeof(struct product),1,F);
else
x=1;
}
if(!x)
printf("\nItem not in the inventory");
else{
remove("inventory.txt");
rename("temp.txt","inventory.txt");
printf("\nItem deleted!");
}
fclose(f);
fclose(F);
break;
case 3:
f=fopen("inventory.txt","r+");
printf("\nEnter code of the product to be replaced: ");
scanf("%d",&code);
while(!feof(f)){
fread(&p,sizeof(struct product),1,f);
if(p.code==code){
printf("\nEnter details of the new product: ");
printf("\nName: ");
while((getchar())!='\n');
scanf("%s",&p.name);
printf("\nPrice: ");
while((getchar())!='\n');
scanf("%f",&p.price);
printf("\nProducts's code: ");
while((getchar())!='\n');
scanf("%d",&p.code);
fseek(f,-1*sizeof(struct product),SEEK_CUR);
fwrite(&p,sizeof(struct product),1,f);
x=1;
}
if(x)
break;
}
if(!x)
printf("\nItem not in the inventory.");
else
printf("\nItem found and replaced.");
fclose(f);
break;
default: printf("\nWrong Input!");
}
}
return 0;
}