Programming in C Report File: Submitted By: Manish Kumar Gahalout 66/EC/09 Semester-5th, ECE-2
Programming in C Report File: Submitted By: Manish Kumar Gahalout 66/EC/09 Semester-5th, ECE-2
PROGRAMS
SNO
1 2 3 4 5 6
PROGRAMS LIST
WAP to perform Arithmetic operations on complex numbers. WAP to find out the eigen value of a matrix. WAP to find out the inverse of a matrix. WAP to delete a specified character from a string WAP to find out the largest element in a given array of numbers. WAP to search a given number in a given array of numbers using linear search WAP to search a given number in a given array of numbers using binary search. WAP to sort the numbers in an array using merge sort. WAP to sort the string using pointers. WAP to sort the numbers in a given array using quick sort. WAP to implement a stack using arrays and linked list. WAP to implement queue using arrays and linked list. WAP to implement Bank Customer record using structure. Perform various operations like withdrawal, deposits, balance enquiry.
DATE
SIGN
8 9 10 11 12 13
PROGRAM-1
//WAP to perform Arithmetic operations on complex numbers. #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> struct complex { float real; float img; }; int main() { int choice; float x, y, temp1, temp2, temp3; struct complex a, b, c; while(1) { printf("Press 1 to add two complex numbers.\n"); printf("Press 2 to subtract two complex numbers.\n"); printf("Press 3 to multiply two complex numbers.\n"); printf("Press 4 to divide two complex numbers.\n"); printf("Press 5 to exit.\n"); printf("Enter your choice "); scanf("%d",&choice); if( choice == 5) exit(0); if(choice >= 1 && choice <= 4) { printf("Enter a and b where a + ib is the first complex number."); printf("\na = "); scanf("%f", &a.real); printf("b = "); scanf("%f", &a.img); printf("Enter c and d where c + id is the second complex number."); printf("\nc = "); scanf("%f", &b.real); printf("d = "); scanf("%f", &b.img); } if ( choice == 1 ) { c.real=a.real+b.real; c.img=a.img+b.img; printf("Sum of two complex numbers = %3f + %3fi",c.real,c.img); } else if ( choice == 2 )
{ c.real=a.real-b.real; c.img=a.img-b.img; printf("Difference of two complex numbers = %3f + %3fi",c.real,c.img); } else if ( choice == 3 ) { c.real=a.real*b.real-a.img*b.img; c.img=a.img*b.real+a.real*b.img; printf("Multiplication of two complex numbers = %3f + %3fi",c.real,c.img); } else if ( choice == 4 ) { if ( b.real == 0 && b.img == 0 ) printf("Division by 0 + 0i is not allowed."); else { temp1=a.real*b.real+a.img*b.img; temp2=a.img*b.real-a.real*b.img; temp3=b.real*b.real+b.img*b.img; x=temp1/temp3; y=temp2/temp3; printf("Division of two complex numbers = %3f + %3fi",x,y); } } else printf("Invalid choice."); printf("\nPress any key to enter choice again..."); } return 1; getch(); }
OUTPUT:
PROGRAM-2
//WAP to find out the eigen value of a matrix. #include<stdio.h> #include<math.h> #include<conio.h> void eigena(float[2][2]); int main() { float a[2][2]; int i,j; printf("THE ORDER OF THE MATRIX is 2\n"); printf("ENTER THE ELEMENTS OF THE MATRIX:\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { scanf("%f",&a[i][j]); } } eigena(a); getch(); return 0; } /***********FUNCTION FOR EIGEN VALUES*********/ void eigena(float a[2][2]) { float eig[2],x,y,y1,y2,y3,y4; int i; x=(a[0][0]+a[1][1])/2; y1=((a[0][0]+a[1][1])*(a[0][0]+a[1][1]))/4; y3=a[0][1]*a[1][0]; y4=a[0][0]*a[1][1]; y2=y3-y4; y=sqrt(y1+y2); eig[0]=x+y; eig[1]=x-y; for(i=0;i<2;i++) { printf("THE Eigen value is %f ",eig[i]); } } OUTPUT:
PROGRAM-3
//WAP to find out the inverse of a matrix. #include<stdio.h> #include<math.h> #include<conio.h> float detrm(float[25][25],float); void cofact(float[25][25],float); void trans(float[25][25],float[25][25],float); int main() { float a[25][25],k,d; int i,j; printf("ENTER THE ORDER OF THE MATRIX:\n"); scanf("%f",&k); printf("ENTER THE ELEMENTS OF THE MATRIX:\n"); for(i=0;i<k;i++) { for(j=0;j<k;j++) { scanf("%f",&a[i][j]); } } d=detrm(a,k); printf("THE DETERMINANT IS=%f",d); if(d==0) { printf("\nMATRIX IS NOT INVERSIBLE\n");} else { cofact(a,k);} getch(); return 0; } /******************FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/ float detrm(float a[25][25],float k) { float s=1,det=0,b[25][25]; int i,j,m,n,c; if(k==1) { return(a[0][0]); } else { det=0; for(c=0;c<k;c++) { m=0; n=0; for(i=0;i<k;i++)
{ for(j=0;j<k;j++) { b[i][j]=0; if(i!=0&&j!=c) { b[m][n]=a[i][j]; if(n<(k-2)) n++; else { n=0; m++; } } } } det=det+s*(a[0][c]*detrm(b,k-1)); s=-1*s; } } return(det); } /*******************FUNCTION TO FIND COFACTOR*********************************/ void cofact(float num[25][25],float f) { float b[25][25],fac[25][25]; int p,q,m,n,i,j; for(q=0;q<f;q++) { for(p=0;p<f;p++) { m=0; n=0; for(i=0;i<f;i++) { for(j=0;j<f;j++) { b[i][j]=0; if(i!=q&&j!=p) { b[m][n]=num[i][j]; if(n<(f-2)) n++; else { n=0; m++; } } } } fac[q][p]=pow(-1,q+p)*detrm(b,f-1); } }
trans(num,fac,f); } /*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/ void trans(float num[25][25],float fac[25][25],float r) { int i,j; float b[25][25],inv[25][25],d; for(i=0;i<r;i++) { for(j=0;j<r;j++) { b[i][j]=fac[j][i]; } } d=detrm(num,r); inv[i][j]=0; for(i=0;i<r;i++) { for(j=0;j<r;j++) { inv[i][j]=b[i][j]/d; } } printf("\nTHE INVERSE OF THE MATRIX:\n"); for(i=0;i<r;i++) { for(j=0;j<r;j++) { printf("\t%f",inv[i][j]); } printf("\n"); } } OUTPUT:
PROGRAM-4
//WAP to delete a specified character from a string. #include<stdio.h> #include<string.h> #include<conio.h> void removeChar( char * string, char letter ); int main() { char myString[] = "Cats are black! Oh, God!"; char x; printf( "Unmodified string: %s\n", myString ); printf( "Enter the character to be removed ="); x=getchar(); removeChar(myString,x); printf( "Modified string: %s\n", myString ); getch(); return 0; } void removeChar(char *string,char letter ) { for(unsigned int i=0;i<strlen(string);i++) if(string[i]==letter ) strcpy(string+i,string+i+1); } OUTPUT:
PROGRAM-5
//C program to find the largest element in an array #include<stdio.h> #include<conio.h> int main() { int a[50],size,i,big; printf("\nEnter the size of the array:"); scanf("%d",&size); printf("\nEnter the elements \n"); for(i=0;i<size;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<size;i++) { if(big<a[i]) big=a[i]; } printf("\nBiggest: %d",big); getch(); return 0; } OUTPUT:
PROGRAM-6
// Write a simple code for linear search in c programming language // Wap a c program to search an element in an array using linear search
#include<stdio.h> #include<conio.h> int main() { int a[10],i,x,n,m,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++) { scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++) { if(a[i]==m) { c=1; x=i+1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found at position:%d",x); getch(); return 0; } OUTPUT:
PROGRAM-7
//Write a simple code for binary search in c programming language //Wap a c program to search an element in an array using binary search
#include<stdio.h> #include<conio.h> int main() { int a[10],i,n,m,x,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; while(l<=u) { mid=(l+u)/2; if(m==a[mid]) { c=1; x=mid+1; break; } else if(m<a[mid]) { u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found at position:%d",x); getch(); return 0; } OUTPUT:
PROGRAM-8
//Source code of simple merge sort implementation using array in ascending order in c programming language #include<stdio.h> #include<conio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() { int merge[MAX],i,n; printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++) { scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++) { printf("%d ",merge[i]); } getch(); return 0; } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); } } void mergeSort(int arr[],int low,int mid,int high) { int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)) { if(arr[l]<=arr[m]) { temp[i]=arr[l];
l++; } else { temp[i]=arr[m]; m++; } i++; } if(l>mid){ for(k=m;k<=high;k++) { temp[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++) { temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=temp[k]; } } OUTPUT:
PROGRAM-9
//Source code of simple quick sort implementation using array ascending order in c programming language #include<stdio.h> #include<conio.h> void quicksort(int [10],int,int); int main() { int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); getch(); return 0; } void quicksort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first;j=last; while(i<j) { while(x[i]<=x[pivot]&&i<last)i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); } } OUTPUT:
quicksort(x,j+1,last);
PROGRAM-10
/*WAP to Ask user, how many strings he would like to enter, read strings from user, sort them in alphabetical order.*/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include<conio.h> int readLine(char **pStr) { char buf[256]; int len; printf("Input string: "); fgets(buf, sizeof(buf), stdin); len = strlen(buf); // remove new line character if (buf[len - 1] == '\n') buf[len - 1] = 0; *pStr = (char *)calloc(len, sizeof(char)); strcpy(*pStr, buf); return len; } int readLines(char ***pStrings) { int n, i; printf("How many strings to input: "); scanf("%d", &n); // skip newline getc(stdin); *pStrings = (char **)calloc(n, sizeof(char *)); for (i = 0; i < n; i++) readLine(&(*pStrings)[i]); return n; } int compareStrings(const void *pStr1, const void *pStr2) { return strcmp(*(char **)pStr1, *(char **)pStr2); } int main() { char **strings; int n, i; n = readLines(&strings); qsort(strings, n, sizeof(char *), compareStrings); printf("Sorted strings: \n"); for (i = 0; i < n; i++) printf("%s\n", strings[i]); // free memory for (i = 0; i < n; i++) free(strings[i]); free(strings); system("PAUSE"); getch();
return 0; } OUTPUT:
PROGRAM-11(a)
//WAP for implementation of stack using array #include<stdio.h> #include<conio.h> void push(int); int pop(void); void display(void); int main() { int choice=0,val=0; do { printf("\n\t1.Push 2.Pop 3.Display 4.Exit\n\tSelect Your Choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\tElement to be Pushed : "); scanf("%d",&val); push(val); break; case 2: val=pop(); if(val!=-1) printf("\tPopped Element : %d\n",val); break; case 3: display(); break; case 4: break; default: printf("\tWrong Choice"); break; } }while(choice!=4); getch(); return 0; } #define maxsize 100 int stack[maxsize]; int stacktop=0; void push(int val) { if(stacktop<maxsize) stack[stacktop++]=val;
else printf("Stack Overflow"); } int pop() { int a; if(stacktop>0) { stacktop--; a=stack[stacktop]; return a; } else { printf("Stack is Empty"); return -1; } } void display() { int i=0; if(stacktop>0) { printf("\tElements are:"); while(i<stacktop) { printf("\t%d",stack[i++]); } printf("\n"); } else printf("\tStack is Empty\n"); } OUTPUT:
PROGRAM-11(b)
//WAP for implementation of stack using linked list #include<stdio.h> #include<conio.h> #include<stdlib.h> struct stack{ int data; struct stack* next; };
typedef struct stack* node; void push(node); int pop(); node top=NULL; void push(node temp) { if(top==NULL)//if inserting first elsement { top=temp;//|value||null(top)| top->next=NULL; } else//general case { temp->next=top; top=temp; //|newvalue(TOP)||->|value||NULL| /*new element becomes new top*/ } } int pop() { node temp; int x; if(top==NULL)//if top is null { printf("Stack Empty\n"); } else { temp=top; x=temp->data; top=top->next; delete temp; } return x; } void display() { node temp; if(top==NULL) { printf("empty stack\n"); } else { temp=top; printf("Stack is:-\n"); while(temp!=NULL) { printf("%d\n",temp->data); temp=temp->next; } } } int main()
{ int ch; printf("1.Push\n2.Pop\n3.Display\n4.Exit\n"); printf("Enter your choice:"); scanf("%d",&ch); node temp; int x; while(ch!=4) { switch(ch) { case 1: temp=(struct stack*)malloc(sizeof(struct stack)); printf("Enter data:"); scanf("%d",&(*temp).data); push(temp); break; case 2: x=pop(); printf("Removed %d\n",x); break; case 3: display(); break; default:printf("WRONG CHOICE ENTER AGAIN\n"); } printf("1.Push\n2.Pop\n3.Display\n4.Exit\n"); printf("Enter your choice:"); scanf("%d",&ch); } getch(); return 0; } OUTPUT:
PROGRAM-12(a)
// WAP a C program to implement a Queue Implementation using array #include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 50 void insert(); void del(); void display(); int queue_arr[MAX]; int rear = - 1; int front = - 1; int main() { int choice; while (1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: del(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); } /*End of switch*/ } /*End of while*/ getch(); return 0; } /*End of main()*/ void insert() { int added_item; if (rear == MAX - 1) printf("Queue Overflow\n"); else { if (front == - 1) /*If queue is initially empty */ front = 0; printf("Input the element for adding in queue : "); scanf("%d", &added_item); rear = rear + 1; queue_arr[rear] = added_item; } } /*End of insert()*/
void del() { if (front == - 1 || front > rear) { printf("Queue Underflow\n"); } else { printf("Element deleted from queue is : %d\n", queue_arr[front]); front = front + 1; } } /*End of del() */ void display() { int i; if (front == - 1) printf("Queue is empty\n"); else { printf("Queue is :\n"); for (i = front; i <= rear; i++) printf("%d ", queue_arr[i]); printf("\n"); } } /*End of display() */ OUTPUT:
PROGRAM-12(b)
// WAP a C program to implement a Queue Implementation using Linked List #include<stdio.h> #include<malloc.h> #include<conio.h> #include<stdlib.h> struct node { int info; struct node *next; } *front, *rear; void enqueue(int elt); int dequeue(); void display(); int main() { int ch, elt; rear = NULL; front = NULL; while (1) { printf("************ Menu ***************"); printf("\nEnter:\n1->Insert\n2->Delete\n3->Display\n4->Exit\n"); printf("Enter your choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter The Element Value :"); scanf("%d", &elt); enqueue(elt); break; case 2: elt = dequeue(); printf("The deleted element = %d\n", elt); break; case 3: display(); break; case 4: printf("~~~Exit~~~"); exit(0); break; default: printf("Wrong choice\n"); } } getch(); return 0; } void enqueue(int elt) { struct node *p;
p = (struct node*)malloc(sizeof(struct node)); p->info = elt; p->next = NULL; if (rear == NULL || front == NULL) front = p; else rear->next = p; rear = p; } int dequeue() { struct node *p; int elt; if (front == NULL || rear == NULL) { printf("\nUnder Flow"); getch(); exit(0); } else { p = front; elt = p->info; front = front->next; free(p); } return (elt); } void display() { struct node *t; t = front; while (front == NULL || rear == NULL) { printf("\nQueue is empty"); getch(); exit(0); } while (t != NULL) { printf("->%d", t->info); t = t->next; } } OUTPUT:
PROGRAM-13
/*12. WAP to implement Bank Customer record using structure. Perform various operations like withdrawal, deposits, balance enquiry*/ #include<stdio.h> #include<conio.h> #include <stdlib.h> void creation(); void deposit(); void withdraw(); void bal(); int a=0,i = 101; struct bank { int no; char name[20]; float bal; float dep; }s[20]; int main() { int ch; while(1) { printf("\n*********************"); printf("\n BANKING"); printf("\n*********************"); printf("\n1-Creation"); printf("\n2-Deposit"); printf("\n3-Withdraw"); printf("\n4-Balance Enquiry"); printf("\n5-Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: creation(); break; case 2: deposit(); break; case 3: withdraw(); break; case 4: bal(); break; case 5: exit(0); default: printf("Enter 1-5 only"); } } getch(); return 0; } void creation() { printf("\n*************************************"); printf("\n ACCOUNT CREATION "); printf("\n*************************************"); printf("\nYour Account Number is :%d",i); s[a].no = i; printf("\nEnter your Name:"); scanf("%s",s[a].name);
printf("\nYour Deposit is Minimum Rs.500"); s[a].dep=500; a++; i++; } void deposit() { int no,b=0,m=0; float aa; printf("\n*************************************"); printf("\n DEPOSIT "); printf("\n*************************************"); printf("\nEnter your Account Number"); scanf("%d",&no); for(b=0;b<=i;b++) { if(s[b].no == no) m = b; } if(s[m].no == no) { printf("\n Account Number : %d",s[m].no); printf("\n Name : %s",s[m].name); printf("\n Deposit : %f",s[m].dep); printf("\n How Much Deposited Now:"); scanf("%f",&aa); s[m].dep+=aa; printf("\nDeposit Amount is :%f",s[m].dep); getch(); } else { printf("\nACCOUNT NUMBER IS INVALID");} } void withdraw() { int no,b=0,m=0; float aa; printf("\n*************************************"); printf("\n WITHDRAW "); printf("\n*************************************"); printf("\nEnter your Account Number"); scanf("%d",&no); for(b=0;b<=i;b++) { if(s[b].no == no) m = b; } if(s[m].no == no) { printf("\n Account Number : %d",s[m].no); printf("\n Name : %s",s[m].name); printf("\n Deposit : %f",s[m].dep); printf("\n How Much Withdraw Now:"); scanf("%f",&aa); if(s[m].dep<=500) printf("\nCANNOT WITHDRAW YOUR ACCOUNT HAS MINIMUM BALANCE"); else { s[m].dep-=aa; printf("\nThe Balance Amount is:%f",s[m].dep);
} } else printf("INVALID"); } void bal() { int no,b=0,m=0; float aa; printf("\n*************************************"); printf("\n BALANCE ENQUIRY "); printf("\n*************************************"); printf("\nEnter your Account Number"); scanf("%d",&no); for(b=0;b<=i;b++) { if(s[b].no == no) m = b; } if(s[m].no==no) { printf("\n Account Number : %d",s[m].no); printf("\n Name : %s",s[m].name); printf("\n Deposit : %f",s[m].dep); } else printf("INVALID"); } OUTPUT: