Sie sind auf Seite 1von 31

PROGRAMMING

PROJECT FILE

AMAR MALIK
217/COE/14
COE- I

Programs List:
1

2
3

4
5
6
7
8

Write a programe to perfom the following matrix operations:


Determine the Determinant value
Inverse of matrix
Rank of matrix
Power of matrix
Write a program using the pointer-to-function to print the tables of cos and tan
trigonometric functions.
Write a program to create structure for the following records:
Name of student (string variable), roll number (string variable),date (date data
type to be defined by the user),attendance(can have three values A for absent ,P
for present and M for medical).
Using the attendance rules of NSIT, implement the program for any subject.You
must store the attendances in a file through the program on day-to-day basis and
then at the end of month, list the students having short attendances.
Using pointers make a program to perform multiplication of matrices.
Using structures write a program to handle the operations on the complex
numbers.
Write a program to count the frequencies of individual words in a file of text.
Write a program to exhibit the use of enumerated data types.
Using files and structures implement the program for inventory control with
following provisions.
Addition of an item.
Deletion of an item.
Find and replace an item.

TO DETERMINE THE DETERMINAMT OF A MATRIX


#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
double d = 0;
double det(int n, double mat[10][10])
{
int c, subi, i, j, subj;
double submat[10][10];
if (n == 2)
{
return( (mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1]));
}
else
{
for(c = 0; c < n; c++)
{
subi = 0;
for(i = 1; i < n; i++)
{
subj = 0;
for(j = 0; j < n; j++)
{
if (j == c)
{
continue;
}
submat[subi][subj] = mat[i][j];
subj++;
}
subi++;
}
d = d + (pow(-1 ,c) * mat[0][c] * det(n - 1 ,submat));
}
}
return d;
}
int main()
{
int n;

cout<<"enter the order of matrix" ;


cin>>n;
double mat[10][10];
int i, j;
cout<<"enter the elements"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>mat[i][j];
}
}
cout<<"\ndeterminant"<<det(n,mat);
getch();
}

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;

printf("Inverse of matrix is\n");


show(b,n);
}
return 0;
}

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;

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


b[i][j]=b[i][j]+a[i][k]*c[k][j];
}}
for(i=0;i < r;i++)
{
for(j=0;j < r;j++)
c[i][j]=b[i][j];
}
}

PRINT TABLES OF COS AND TAN


#include<iostream>
#include<math.h>

using namespace std;


int fact(int x){
int val=1;
while(x!=0){
val=val*x;
x--;
}
return val;}
float cos(int x){
float val=0;
val=1-(pow(x,2)/fact(2))+(pow(x,4)/fact(4))-(pow(x,6)/fact(6))+(pow(x,8)/fact(8));
return val;}
float tan(int x){
float val=0;
val=x+(pow(x,3)/3)+2*(pow(x,5)/15)+17*(pow(x,7)/315);
return val;}
int main(){
float low=0,upp=90,interval=15;
cout<<"ANGLE"<<" "<<"COS"<<" "<<"TAN"<<endl;
for(low=0;low<=upp;low=low+interval){
if(low==0){
cout<<low<<"
"<<cos((low/180)*3.14)<<"
"<<tan((low/180)*3.14)<<endl;
}
else
cout<<low<<" "<<cos((low/180)*3.14)<<" "<<tan((low/180)*3.14)<<endl;
}
return 0;}

MANAGING ATTENDENCE USING STRUCTURES

#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<string.h>
<math.h>

const char *FILENAME = "ATTENDANCE";


enum cases{A, P, M, N};
typedef struct
{
char name[255];
char roll[32];
enum cases attendance[12][32];
} student;
inline float IsSafe(student stu)
{
int Medical = 0, Absent = 0, Present = 0, i, j;
for (i = 0; i < 12; i++)
{
for (j = 0; j < 32; j++)
{
switch(stu.attendance[i][j])
{
case A: Absent++; break;
case P: Present++; break;
case M: Medical++; break;
default:break;
}
}
}
float factor = Present + Medical;
factor /= Present + Absent + Medical;
return factor*100;
}
int main()
{
student tmp;
FILE *fp;
int choice;
while(1)

{
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);

fwrite(&tmp, sizeof(tmp), 1, fp);


}
fclose(fp);
break;
case 3:
fp = fopen(FILENAME, "r");
if (fp == NULL){ printf("Something went wrong\n"); break;}
rewind(fp);
printf("Name\tRoll No\tPercentage\n");
float temp;
while(fread(&tmp, sizeof(tmp), 1, fp) == 1)
if ((temp = IsSafe(tmp)) < 75)
printf("%s\t%s\t%.2f\n", tmp.name, tmp.roll, temp);
fclose(fp);
break;
case 4: return 0;
default:
break;
}
}
return 0;
}

MULTIPLICATION OF MATRICE USING POINTERS

#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

printf("Multiplication of matrices cannot be done.");


return 0;
}

PROGRAM ON COMPLEX NUMBERS

#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; }

FREQUENCY OF INDIVIDUAL LETTER


#include<iostream>
using namespace std;
int main(){
int i=0,j,val,flag=1,f,k;
char ch[80];

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;}

USE OF ENUMERATED DATATYPE


#include<stdio.h>

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);
}

PERFORM ADDITION ,DELETION,REVERSE OPERATION IN A


DATA FILE

#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;
}

Das könnte Ihnen auch gefallen