Record
Record
Record
AND
CERTIFICATE
Certified that this is the bonafide record of the practical work done in C., Univthe Department of Master of Computer Applications, in partial fulfillment of the requirement as a subject under the University of Kannur during the first Semester in the year 2010-2011 .
External Examiner 1. 2.
Faculty in Charge
INDEX
SLNO TITLE LINUX COMMANDS C PROGRAMMS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Simple interest and compound interest Maximum and minimum among three numbers Quadratic Equation Largest and second largest from set of n numbers Series Summation Determine the given number is prime or not Pascal's Triangle String manipulation Function to swap two strings Matrix Multiplication Determinant of 3X3 matrix Linear Search Binary Search Bubble Sort Selection Sort Function to check for tautologies and contradictions Function to find GCD using euclidian algorithm Recursive Function Creating database for telephone numbers and related operations using file concept Creating database for mailing address and related operations using structure concept Creating database for webpage address and related operations using file concept Roots of equations using Bisection Method Roots of equations using Newton Raphson Method Roots of equations using Secant Method Solve algebraic equation using gauss elimination 17/12/10 03/01/11 173 177 10/12/10 14/12/10 164 169 07/12/10 145 30/11/10 126 19/11/10 23/12/10 26/11/10 97 101 108 05/10/10 08/10/10 12/10/10 15/10/10 22/10/10 26/10/10 29/10/10 02/11/10 05/11/10 09/11/10 12/11/10 03/09/10 06/09/10 14/09/10 17 20 26 31 36 48 52 56 60 62 71 74 78 82 86 90 DATE 03/09/10 PAGE NO 5
LINUX COMMANDS
1. INTRODUCTION Linux is one of the most prominent examples of free and open source collaboration; typically all the underlying source code can be used, freely modified, and redistributed, both commercially and non-commercially, by anyone under licenses such as the GNU General Public License. Typically Linux is packaged in a format known as a Linux distribution for desktop and server use. Some popular mainstream Linux distributions include Debian (and its derivatives such as Ubuntu),Fedora and open SUSE. Linux distributions include the Linux kernel and supporting utilities and libraries to fulfill the distribution's intended use. 2. LOGGING IN, LOGGING OUT su username This command is used to login the user eg : su meera Where meera is the username logout This command is used to logs the current user off the system.
3. BASIC LINUX COMMANDS 1. ls This command is used to see a list of files in your current directory. ls can be used with various options ls -l : long listing, including file ownership, date and time, and access permissions
ls -a : Show all files, including hidden files ls -t : Order the list by date and time instead of alphabetically 6
2. man This command is used to shows all information about the command Syntax : man <command> 3. who This command is used to show who is logged on Syntax : who [OPTION] eg. who , who -b , who -q 4. passwd This command is used to update a users authentication tokens(s) Syntax : passwd [OPTION] eg. passwd 5. date This command is used to show date and time Syntax : date 6. echo This command is used to display a line of text Usage : echo [OPTION] [string] ... eg. echo I love India echo $HOME 7. printf This command is used to display the string Syntax : printf string eg : printf hai how r u ? The output is hai how r u ? 7
8. shutdown shutdown arranges for the system to be brought down in a safe way. All logged-in users are notified that the system is going down and, within the last five minutes of TIME, new logins are prevented. Syntax : shutdown 4. LINUX FILE 1. Introduction A file system (often also written as filesystem) is a method of storing and organizing computer files and their data. Essentially, it organizes these files into a database for the storage, organization, manipulation, and retrieval by the computer's operating system. File systems are used on data storage devices such as hard disks or CD-ROMs to maintain the physical location of the files. Beyond this, they might provide access to data on a file server by acting as clients for a network protocol (e.g., NFS, SMB, or 9P clients), or they may be virtual and exist only as an access method for virtual data (e.g., procfs). It is distinguished from a directory service and registry. 2. Diffrent types of files Regular files : A simply definition of a regular file would be that it is a one dimensional assortment of bytes that are stored on a disk or other mass storage devices. Directory files : A simple definition of a directory is that it is a file that provides a mapping mechanism between the names of files and the files (datablocks) themselves. Device files : A device file refers to a device driver and these are important to the kernel. The device drivers are written in C and compiled to make an object file and then placed as part of the kernel. Created a device file using the mknod command. 8
3. UNIX File system Tree In Unix, the operating system consists of files, arranged in a tree structure. Everything is a file: programs, directories, images, texts, services etc. are all files. Sometimes a file with a special attribute, but a file nevertheless.There are 3 major types of files. They are kernel, shell, filesystem The directory tree : The directories on a Unix system are conveniently ordered into a tree structure. The parent directory of the tree is called the "root" directory and is written as a forward slash (/). 4. Handling Directories 1. pwd This command is used to print name of current/working directory Syntax : pwd 2. cd This command is used to changes directories Syntax : cd [DIRECTORY] eg. cd name Change the current working directory to name, 3. mkdir This command is used to make directories Syntax : mkdir [OPTION] DIRECTORY... eg. mkdir name Create name directory 4. rmdir This command is used to remove empty directories Syntax : rmdir directoryname 9
5. Absolute Pathname Vs Relative Pathname Relative path do not begin with /. its specifies locationrelative to your current working directory and it can be used as a shorter way to specify a file name.Absolute path always begin with a / (slash). It is acomplete "road map" file location. 5. Handling Files 1. cat : This command is used concatenate files and print on the standard output Syntax : cat [OPTION] [FILE]... eg. cat file1.txt file2.txt cat -n file1.txt 2. touch This command is used tochange file timestamps Synatx : touch [OPTION]... FILE... 3. cp To copy files use cp command cp myfile myfilecopy The destination can also be the name of the directory. In this case the file is copied to a new file of the same name in that directory. 4. rm This command is used to remove files or directories Syntax : rm [OPTION]... FILE... 5. mv This command is used to to move a file from one location to another use mv command mv myfile mysubdirectory . The mv command is also used to rename files . mv myfile newname 10
6. file This command is used to determine file type 7. cmp This command is used to compare two files byte by byte . Syntax : cmp file1 file2 8. sort This command is used to sort lines of text files Syntax : sort [OPTION]... [FILE]... eg. sort file1.txt sort -r file1.txt 9. cut This command is used to remove sections from each line of files Syntax : cut OPTION... [FILE]... 10. grep This command is used to print lines matching a pattern Usage: grep [OPTION] PATTERN [FILE]... eg. grep -i apple sample.txt 11. head This command is used to show first few lines of a file(s) Syntax : head [OPTION]... [FILE]... 12. tail This command is used to show last few lines of a file Syntax : tail [OPTION]... [FILE]... 13. more This command is used to show text file on display terminal with paging control 11
Syntax : more [-dlfpcsu] [-num] [+/pattern] [+linenum] [file ...] 14. ls -l This command is used to long listing, including file ownership, date and time, and access permissions 5. I/O REDIRECTION & PIPING 1. Standard Input & Output Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your standard input (stdin) device, and the screen or a particular terminal window is the standard output (stdout) device. However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer. 2. Redirecting Input Many commands can accept input from a facility called standard input. By default, standard input gets its contents from the keyboard, but like standard output, it can be redirected. To redirect standard input from a file instead of the keyboard, the "<" character is used like this: sort < file_list.txt In the above example we used the sort command to process the contents of file_list.txt.
12
3. Redirecting Output Most command line programs that display their results do so by sending their results to a facility called standard output. By default, standard output directs its contents to the display. To redirect standard output to a file, the ">" character is used like this: ls > file_list.txt
In this example, the ls command is executed and the results are written in a file named file_list.txt 4. Using Pipes By far, the most useful and powerful thing you can do with I/O redirection is to connect multiple commands together with what are called pipes. With pipes, the standard output of one command is fed into the standard input of another. Here is my absolute favorite: ls -l | less In this example, the output of the ls command is fed into less. By using this "| less" trick, you can make any command have scrolling output. I use this technique all the time. 6. File Permission 1. Introduction Every file or folder in Linux has access permissions. There are three types of permissions (what allowed to do with a file): read access write access execute access 13
Permissions are defined for three types of users: the owner of the file the group that the owner belongs to other users Thus, Linux file permissions are nine bits of information (3 types x 3 type of users), each of them may have just one of two values: allowed or denied. 2. Changing File Permission : chmod This command is used to change file mode bits 3. Changing File Ownership : chown This command is used to change file owner and group 7. The vi Editor 1. Introduction Vim is a text editor that is upwards compatible to Vi. It can be used to edit all kinds of plain text. It is especially useful for editing programs. There are a lot of enhancements above Vi: multi level undo, multi windows and buffers, syntax highlighting, command line editing, filename completion, on-line help, visual selection, etc.. 2. Modes of operation vi has three modes Command mode - where editing commands such as dd to delete the current line
are entered.Pressing the Esc key turns on command mode. Input mod- where text can be entered into any line of text. Input mode is turned on (the only time text entry is permitted) by giving the a, o or i commands in command mode. 14
Status line mode - is used to enter long commands at the bottom of the screen. To enter status mode, press Esc followed by : then type further instructions such as wq to write (save) the current file and quit 8. Rectification Keys 1. Ctrl h : Erase one character. Similar to pressing backspace. 2. Ctrl c : Cancels the currently running command. 3. Ctrl d : Logs out of the current session. 4. Ctrl s : Stops all output on screen (XOFF). 5. Ctrl q : Turns all output stopped on screen back on (XON).
15
C PROGRAMMS
16
ALGORITHM: Step 1 : Start Step 2 : Read P,N,R Step 3 : simple interest=(p*n*r)/100 Step 4 : Compound interset=p*(1+r/100)n Step 5 : Print simple interest and Compound interset Step 6 : Stop FLOWCHART: START
Read P,N,R
STOP 17
PROGRAM #include<stdio.h> #include<math.h> main() { int p,n,r; float i,si,ci; printf("Enter the principal value\t:\t"); scanf("%d",&p); printf("Enter the rate\t:\t"); scanf("%d",&r); printf("Enter the number of year\t:\t"); scanf("%d",&n); si=(p*n*r)/100; i=r/100; ci=p*pow((1+i),n); printf("\n\nSimple Inetrest\t\t:\t%f",si); printf("\nCompound Interest\t:\t%f\n\n",ci); }
18
OUTPUT
19
ALGORITHM: Step 1 : Start Step 2 : Read a,b,c Step 3 : if a>b and a>c , then print a is greater Step 4 : if b>a and b>c , then print b is greater Step 5 : if c>a and c>b , then print c is greater Step 6 : if a<b and a<c , then print a is smaller Step 7: if b<a and b<c , then print b is smaller Step 8 : if c<b and c<a , then print c is smaller Step 6 : Stop
20
FLOWCHART: START
Read a,b,c
Print b is greater
Print c is greater
21
Print a is smaller
Print b is smaller
Print c is smaller
STOP
22
PROGRAM #include<stdio.h> main() { int a,b,c; printf("\nEnter the three numbers \n"); scanf("%d%d%d",&a,&b,&c); if((a>b)&&(a>c)) printf("\nLargest number \t:\t %d\n",a); else if((b>a)&&(b>c)) printf("\nLargest number \t:\t %d\n",b); else if((c>a)&&(c>b)) printf("\nLargest number \t:\t%d\n",c); else printf("\nThe numbers are equal\n"); if((a<b)&&(a<c)) printf("\nSmallest number \t:\t%d\n",a); else if((b<c)&&(b<a)) printf("\nSmallest number \t:\t%d\n",b); else printf("Smallest number\t:\t%d\n",c); }
24
OUTPUT
Enter the three numbers 536 120 345 Largest number Smallest number : : 536 120
25
QUADRATIC EQUATION
ALGORITHM: Step 1 : Start Step 2 : Read a,b,c Step 3 : set d=b2-4ac Step 4 : if d==0 , root=-b/2a Step 5 : if d>0 , root1=(-b+(d)1/2)/2a,root2=(-b-(d)1/2)/2a Step 6 : if d<0, print Roots are imaginery Step 7 : printf roots Step 6 : Stop FLOWCHART: START
Read a,b,c
d=b2-4ac
27
If d=0
r=-b/2a
If d>0
T r1=(-b+d1/2)/2a r1=(-b-d1/2)/2a
Print r If d<0 F real=-b/(2*a); img=(-)d1/2/(2*a); Print r1,r2 T Print Roots are real and distinct
STOP 28
PROGRAM #include<stdio.h> #include<math.h> main() { int a,b,c;float real,img,n; float d,r1,r2; printf("Enter the three coefficients\n"); scanf("%d%d%d",&a,&b,&c); d=(b*b)-(4*a*c); if(d==0) { r1=((-b)+(sqrt(d))/(2*a)); printf("\nRoots are real and equal\nRoot=%f",r1); } else if(d>0) { r1=((-b)+(sqrt(d)))/(2*a); r2=((-b)-(sqrt(d)))/(2*a); printf("\nRoots are real and distinct\nRoot1=%f\nRoot2=%f",r1,r2); } else { printf("\nRoots are imaginery"); real=-b/(2*a); 29
Enter the three coefficients 1 -12 32 Roots are real and distinct Root1=8.000000 Root2=4.000000
Enter the three coefficients 1 2 1 Roots are real and equal Root=-2.000000
30
ALGORITHM: Step 1 : Start Step 2 : Read n,first no: , second no: Step 3 : if first >second ,max=first ,sec=second Otherwise max=second, sec=first Step 4 : set i=0 Step 5 : if i<n-1 ,go to step 6. Otherwise goto step 10 Step 6 : read num Step 7 : if num> max ,sec=max , max=num. Otherwise if num<max and num >sec ,sec=num Step 8 : i=i+1 Step 9 : goto step 5 Step 10 : Printf max,sec Step 11 : Stop
31
FLOWCHART: START
Set i=0
If first>second
max=second sec=first
max=first sec=second
32
sec=max max=no T
sec=no
i=i+1
33
PROGRAM #include<stdio.h> main() { int num,n,i,t=0,s=0,a; printf("\nEnter the limit :"); scanf("%d",&n); printf("\nEnter %d numbers\n",n); scanf("%d%d",&a,&num); s=num; if(s>a) { s=a; t=s; } else t=a; for(i=0;i<n-2;i++) { scanf("%d",&num); if(num>t) { s=t; t=num; } 34
else if((num<t)&&(num>s)) s=num; } printf("Largest value \t:\t %d",t); printf("Second Largest value \t:\t %d",s); }
OUTPUT
: : 4
45 33
35
SERIES SUMMATION
ALGORITHM: Step 1 : Start Step 2 : Print Menu 1.Sine Series 2.Cosine series 3.Exponential Series Step 3 : Read choice Step 4 : if choice=1 ,goto step 5 Step 5 : Read a,n Step 6 : x=a*(3.14/180) Step 7 : k=1,i=1 Step 8 : if i<n, goto step 9. Otherwise goto Step 17 Step 9 : fact=1,j=1 Step 10 : if j<=i , goto step 11. Otherwise goto step 14 Step 11 : fact=fact*j Step 12 : j=j+1 Step 13 : goto step 10 Step 14 : sum=sum+(-1)k+1(xi/fact) Step 15 : i=i+2,k=k+1 Step 16 : goto step 8 Step 17 : print sum 36 otherwise go to step 19
Step 18 :goto step 49 Step 19 : if choice=2 goto step 20 Step 20 : Read a,n Step 21 : x=a*(3.14/180) Step 22 : k=1,i=0 Step 23 : if i<n, goto step 24. Otherwise goto Step 32 Step 24 : fact=1,j=1 Step 25 : if j<=i , goto step 26. Otherwise goto step 29 Step 26 : fact=fact*j Step 27 : j=j+1 Step 28 : goto step 25 Step 29 : sum=sum+(-1)k+1(xi/fact) Step 30 : i=i+2,k=k+1 Step 31 : goto step 23 Step 32 : print sum Step 33 : goto step 49 Step 34 : if choice=3 goto step 35. Otherwise goto step Step 35 : Read a,n Step 36 : x=a*(3.14/180) Step 37 : i=0 Step 38 : if i<n, goto step 39. Otherwise goto Step 47 Step 39 : fact=1,j=1 Step 40 : if j<=i , goto step 41. Otherwise goto step 44 Step 41 : fact=fact*j Step 42 : j=j+1 37 otherwise goto step 34
Step 43 : goto step 40 Step 44 : sum=sum+(xi/fact) Step 45 : i=i+1 Step 46 : goto step 38 Step 47 : print sum Step 48 :goto step 49 Step 49 : Stop
38
FLOWCHART: START
Read choice
If choice =1 F
If choice=2
If choice=3
F D
C D
39
Read a,n
x=3.14*a/180
I=1 k=1,sum=0
Print sum
sum=sum+(-1)k+1(xi/fact)
i=i+2 k=k+1
40
Read a,n
x=3.14*a/180
I=0 k=1,sum=0
Print sum
sum=sum+(-1)k+1(xi /fact)
i=i+2 k=k+1
41
Read a,n
x=3.14*a/180
I=0 sum=0
Print sum
i=i+2
STOP 42
PROGRAM #include<stdio.h> #include<math.h> #include<string.h> void sine(); void cosine(); void exponential(); int menu(); main() { int n; char ch[3]; do { n=menu(); switch(n) { case 1 : sine(); break; case 2 : cosine(); break; case 3 : exponential(); break; default :printf("Thankyou\n"); } 43
printf("Do you want to continue?(Y/N)\t:\t"); scanf("%s",ch); }while((strcmp(ch,"Y")==0)||(strcmp(ch,"y")==0)); } int menu() { int n; printf("MAIN MENU\n..............\n"); printf("A.Sine series\n2.Cosine series\n3.Exponential series\n0.Exit\n"); printf("enter your choice\t:\t"); scanf("%d",&n); return(n); } void sine() { int x,n,i,j,k; float x1,sum=0,fact=1; printf("ENTER THE ANGLE\n"); scanf("%d",&x); printf("ENTER THE VALUE OF n\n"); scanf("%d",&n); x1=3.14*(x/180.0); for(i=1,k=1;i<=n;i=i+2,k++) { for(j=1,fact=1;;j<=i;j++) 44
fact=fact*j; sum=sum+(pow(-1,k+1)*(pow(x1,i)/fact)); } printf("\nSin(%d)=%f\n",x,sum); } void cosine() { int x,n,i,j,k; float x1,sum=0,fact=1; printf("Enter the angle\n"); scanf("%d",&x); printf("Enter the value of n\t:\t"); scanf("%d",&n); x1=3.14*(x/180.0); for(i=0,k=1;i<=n;i=i+2,k++) { for(j=1,fact=1;j<=i;j++) fact=fact*j; sum=sum+(pow(-1,k+1)*(pow(x1,i)/fact)); } printf("\nCos(%d)=%f\n",x,sum); } void exponential() { int x,n,i,j; 45
float x1,sum=0,fact=1; printf("Enter the angle\n"); scanf("%d",&x); printf("Enter the value of n\t:\t"); scanf("%d",&n); for(i=0;i<=n;i++) { for(j=1,fact=1; ;j<=i;j++) fact=fact*j; sum=sum+(pow(x,i)/fact); } printf("\nexp(%d)=%f\n",x,sum); }
OUTPUT
MAIN MENU ...................... 1.Sine series 2.Cosine series 3.Exponential series 0.Exit Enter your choice : ENTER THE ANGLE 1 30
MAIN MENU ...................... 1.Sine series 2.Cosine series 3.Exponential series 0.Exit Enter your choice : Enter the angle 30 Enter the value of n Cos(30)=1.866186 Do you want to continue?(Y/N) : MAIN MENU ....................... 1.Sine series 2.Cosine series 3.Exponential series 0.Exit Enter your choice : Enter the angle 4 Enter the value of n exp(4)=14.000000 Do you want to continue?(Y/N) : n : 2 3 y : 5 2
47
ALGORITHM: Step 1 : Start Step 2 : Read n Step 3 : set flag=0,i=2 Step 4 : if i<2,print the number is not prime. Otherwise goto step 5 Step 5 : if i<n/2, goto step 6. Otherwise goto step 9 Step 6 : if n%i=0 ,flag=1 Step 7 : i=i+1 Step 8 : go to step 5 Step 9 : if flag=0. Print number is prime . Otherwise the number is not prime Step 10 : Stop
48
FLOWCHART: START
Read n
If n<2 T
If flag=0 T
STOP 49
PROGRAM #include<stdio.h> main() { int n,i,flag=1,j; printf("Enter the number"); scanf("%d",&n); if(n<2) printf("%d is not prime number",n); else { for(j=2;j<=n/2;j++) { if((n%j)==0) { flag=0; } } if(flag==1) printf("%d is prime number ",n); else printf("%d is not prime number\n",n); } } 50
OUTPUT
Enter the number11 11 is prime number Enter the number26 26 is not prime number
51
PASCAL'S TRIANGLE
ALGORITHM: Step 1 : Start Step 2 : Read N Step 3 : set k=0,i=0 Step 4 : if i<n , goto step 5. Otherwise goto step 18 Step 5 : set j=0 Step 6 : if j<=n-i, goto step 7. Otherwise goto step 10 Step 7 : print space Step 8 : j=j+1 Step 9 : goto step 6 Step 10 : set j=0 Step 11 : if j<=i, goto step 12. Otherwise goto step 16 Step 12 : if j=0 or j=i, a[i,j]=1 Otherwise a[i,j]=a[i-1,j-1]+a[i-1,j] Step 13 : print a[i,j] Step 14 : j=j+1 Step 15 : goto step 11 Step 16 : i=i+1 Step 17 : go to step 4 Step 18 : Stop
52
FLOWCHART START
Read n
Set i=0
If i <n
F Set j=0
F k=k+1
If j<=i
T Print a[i-1][j-1+a[i-1][j]
j=j+1 53
PROGRAM #include<stdio.h> main() { int a[10][10],n,i,j,k; printf("Enter the limit"); scanf("%d",&n); for(i=0;i<n;i++) { for(k=1;k<=n-i;k++) printf(" "); for(j=0;j<=i;j++) { if(j==0||j==i) a[i][j]=1; else a[i][j]=a[i-1][j-1]+a[i-1][j]; printf("%d ",a[i][j]); } printf("\n"); } }
54
OUTPUT
55
STRING MANIPULATION
ALGORITHM: Step 1 : Start Step 2 : Read string,string1 Step 3 : set i=0,w=0 Step 4 : if string(i)!='\0' , goto step 4.Otherwise goto step 8 Step 5 : if str[i]= , w=w+1 Step 6 : i=i+1 Step 7 : goto step 4 Step 8 : Print w Step 9 : if string1 is substring of string ,goto step 10.Otherwise goto step 11 Step 10 : print string1 is substring of string Step 11 : Print string1 is not a substring of string Step 12 : Stop
56
FLOWCHART:
START
Read string,string1
Set i=0,w=0
If str[i]!='\0'
F F If String1 is substring T
If str[i]!= T w=w+1
i=i+1
STOP 57
PROGRAM #include<stdio.h> #include<string.h> main() { char str[30],str1[10]; int i,n,w=0; printf("Enter the first string\t:\t"); scanf("%[^\n]",str); printf("Enter the substring\t:\t"); scanf("%s",str1); if(strstr(str,str1)!=NULL) printf("\n%s is substring of %s",str1,str); else printf("\n%s is not substring of %s",str1,str); for(i=0;str[i]!='\0';i++) { if(str[i]==' ') w++; } printf("\nNumber of words in the %s \t:\t%d\n\n",str,w+1); }
58
OUTPUT
maya is substring of Chinmaya Institute of Technolology Number of words in the Chinmaya Institute of Technolology : 4
: :
Computer is substring of Master Of Computer Application Number of words in the Master Of Computer Application : 4
59
Pgm No: 9 Date : 15-10-10 FUNCTION TO SWAP TWO STRING ALGORITHM: Step 1 : Start Step 2 : Read string1,string2 Step 3 : Print string1,string2 before swapping Step 4 : Copy string1 to string3 Step 5 :Copy string2 to string1 Step 6 : Copy string3 to string1 Step 5 : Print string1 ,string2 Step 6 : Stop FLOWCHART: START
Read string1,string2
Print string1,string2 Copy string1 to string3 Copy string2 to string1 Copy string3 to string2
Print string1,string2
STOP 60
PROGRAM #include<stdio.h> #include<string.h> void swap(char str1[20],char str2[20]) { char temp[20]; strcpy(temp,str1); strcpy(str1,str2); strcpy(str2,temp); printf("\nAfter swapping\n.................\n"); printf("String 1\t:\t%s\nString2\t:\t%s\n",str1,str2); } main() { int i; char str1[20],str2[20]; printf("\nEnter first strings "); scanf("%s",str1); printf("\nEnter second string "); scanf("%s",str2); printf("\nBefore swapping\n................\n"); printf("String1\t:\t%s\nString2\t:\t%s",str1,str2); swap(str1,str2); } 61
OUTPUT
Enter first strings Computer Enter second string Mathematics Before swapping ........................... String1 String2 : : Computer Mathematics
Enter first strings first Enter second string second Before swapping ........................... String1 String2 : : first second
61
MATRIX MULTIPLICATION
ALGORITHM: Step 1 : Start Step 2 : Read n,m,p,q Step 3 : if p != n , Print Multiplication is not possible. Otherwise goto step 4 Step 4 : set i=0 Step 5 : if i<m goto step 6. otherwise goto step 13 Step 6 : set j=0 Step 7 : if j < n goto step 8. Otherwise goto step 11 Step 8 : read a(i,j) Step 9 : j=j+1 Step 10 : go to step 7 Step 11 : i=i+1 Step 12 : goto step 5 Step 13 : set i=0 Step 14 : if i<p goto step 15. otherwise goto step 22 Step 15 : set j=0 Step 16 : if j < q goto step 17. Otherwise goto step 20 Step 17 : read b(i,j) Step 18 : j=j+1 Step 19 : go to step 16 Step 20 : i=i+1 73
Step 21 : goto step 14 Step 22 : set i=0 Step 23 : if i<m, goto step 24. Otherwise goto step 36 Step 24 : set j=0 Step 25 : if j<q, goto step 26. Otherwise goto step 34 Step 27 : set k=0,c(i,j)=0 Step 28 : if k<n, goto step 29. Otherwise goto step 32 Step 29 : c(i,j)=c(i,j)+a(i,k)+b(k,j) Step 30 : k=k+1 Step 31 : goto step 28 Step 32 : j=j+1 Step 33 : goto step 25 Step 34 : i=i+1 Step 35 : goto step 23 Step 36 : set i=0 Step 37 : if i<m goto step 38. otherwise goto step 45 Step 38 : set j=0 Step 39 : if j < n goto step 40. Otherwise goto step 43 Step 40 : Print c(i,j) Step 41 : j=j+1 Step 42 : go to step 39 Step 43 : i=i+1 Step 44 : goto step 37 Step 45 : Stop
63
FLOWCHART: START
Read m,n,p,q
If n!=p
i=0
If i<m
If i<p
A T j=0
j=j+1
j=j+1
66
i=0
If i<m
i=0
T j=0
If i<m If j<q T j=0 T K=0 c(i,j)=0 T If j<n T Read a(i,j) j=j+1 j=j+1 k=k+1 i=i+1 If k<n F c(i,j)=c(i,j)+a(i,k)*b(k,j) T F i=i+1
STOP 67
PROGRAM #include<stdio.h> #include<stdlib.h> main() { int flag=0,count=0,i,j,p,k,n,m,c[10][10],a[10][10],b[10][10],q; do { printf("Enter the order of matrix1"); scanf("%d%d",&m,&n); printf("Enter the order of matrix2"); scanf("%d%d",&p,&q); if(n!=p) { if(count==2) { printf("You are not enter the correct order\n"); exit(0); } printf("Multiplication is not possible\n"); count++; } else { 68
flag=1; printf("Enter the elements in matrix 1\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter the elements in matrix 2\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++) for(j=0;j<q;j++) for(k=0,c[i][j]=0;k<n;k++) c[i][j]=c[i][j]+(a[i][k]*b[k][j]); printf("The product of two matrix is\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) printf("%d ",c[i][j]); printf("\n"); } } }while((flag==0)&&(count<=2)); }
69
OUTPUT
Enter the order of matrix1 3 2 Enter the order of matrix2 2 4 Enter the elements in matrix 1 1 2 3 4 2 -1 Enter the elements in matrix 2 3 4 5 1 2 0 -1 5 The product of two matrix is 7 4 3 11
17 12 11 23 4 8 11 -3 Enter the order of matrix1 2 2 Enter the order of matrix2 2 2 Enter the elements in matrix 1 1 1 1 1 Enter the elements in matrix 2 1 1 1 1 The product of two matrix is 2 2 2 2 70
ALGORITHM: Step 1 : Start Step 2 : set i=0 Step 3 : if i<3 goto step 4. otherwise goto step 11 Step 4 : set j=0 Step 5 : if j < 3 goto step 6. Otherwise goto step 9 Step 6 : Read a(i,j) Step 7 : j=j+1 Step 8 : go to step 5 Step 9 : i=i+1 Step 10 : goto step 3 Step 11 : d=a[0][0]*((a[1][1]*a[2][2])-(a[2][1]*a[1][2])) s=a[0][1]*((a[1][0]*a[2][2])-(a[2][0]*a[1][2])) n=a[0][2]*((a[1][0]*a[2][1])-(a[1][1]*a[2][0])) Step 12 : n=d-s+n Step 13 : Print n Step 14 : Stop
72
FLOWCHART: START
i=0
If i<m T
j=0
F i=i+1
j=j+1
Print n
STOP
PROGRAM #include<stdio.h> #include<math.h> main() { int i,j,k,a[3][3],d,s,n=0; printf("Enter the matrix\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); d=a[0][0]*((a[1][1]*a[2][2])-(a[2][1]*a[1][2])); s=a[0][1]*((a[1][0]*a[2][2])-(a[2][0]*a[1][2])); n=a[0][2]*((a[1][0]*a[2][1])-(a[1][1]*a[2][0])); n=d-s+n; printf("Determinant=%d",n); } OUTPUT :
LINEAR SEARCH
ALGORITHM: Step 1 : Start Step 2 : Read n Step 3 : set i=0,flag=0 Step 4 : if i<n ,goto step 5. Other wise goto step 8 Step 5 : Read a(i) Step 6 : i=i+1 Step 7 : goto step 4 Step 8 : Read search element Step 9 : set i=0 Step 10 : if i<n, goto step 11. Otheriwse goto step 16 Step 11 : if search element=a(i), goto step 12. Otherwise goto Step 14 Step 12 : flag=1 Step 13 : Print Search element is found Step 14 : i=i+1 Step 15 : goto Step 10 Step 16 : if flag=0, Printf search element is not found Step 17 : Stop
74
FLOWCHART: START
Read n
Set i=0,flag=0
i=i+1
i=0
If i<n
F F
If flag=0 T
STOP i=i+1 75
PROGRAM #include<stdio.h> main() { int i,s,n,a[10],flag=0; printf("Enter the limit\t:\t"); scanf("%d",&n); printf("Enter the %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the element to de searched\t:\t"); scanf("%d",&s); for(i=0;i<n;i++) if(a[i]==s) { printf("%d is found in the %d th position\n",s,i+1); flag=1; } if(flag==0) printf("%d is not found in the list\n\n",s); }
76
OUTPUT
: : 5
14
Enter the 6 elements 123 22 45 67 134 58 Enter the element to de searched 406 is not found in the list : 406
77
Pgm No: 13 Date : 02-11-10 BINARY SEARCH ALGORITHM: Step 1 : Start Step 2 : Read n Step 3 : set i=0,flag=0 Step 4 : if i<n ,goto step 5. Other wise goto step 8 Step 5 : Read a(i) Step 6 : i=i+1 Step 7 : goto step 4 Step 8 : Read search element Step 9 : set low=0,high=n-1 Step 10 : if low<=high, goto step 11. Otheriwse goto step 14 Step 11 : set mid=(low+high)/2 Step 12 : if a(mid) >search, high=mid-1. Otherwise low=mid+1 Step 13 : goto step 10 Step 14 : if search element=a(mid),Print number is found . Otherwise Print Number is not found Step 15 : Stop
78
FLOWCHART: START
Read n
i=0
i=i+1
Low=0 high=n-1
T mid=(low+high)/2
If a(mid)=s T
If s<a(mid)
T low=mid+1
high=mid-1
i=i+1
STOP
PROGRAM #include<stdio.h> main() { int i,mid,hi,lo,n,a[20],s; printf("Enter the limit"); scanf("%d",&n); printf("Enter the %d elements (array should be sorted)\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); lo=0; hi=n-1; mid=(hi+lo)/2; printf("Enter the element to be searched\t:\t\n"); scanf("%d",&s); while((lo<=hi)&&(a[mid]!=s)) { if(s<a[mid]) hi=mid-1; else lo=mid+1; mid=(hi+lo)/2; } if(a[mid]==s) printf("%d is found in the %d position\n",s,mid+1); 80
OUTPUT
Enter the limit5 Enter the 5 elements (array should be sorted) 12 23 33 45 56 Enter the element to be searched 45 is found in the 4 position : 45
Enter the limit6 Enter the 6 elements (array should be sorted) 2 4 6 8 9 10 Enter the element to be searched 13 is not found in the list : 13
81
Pgm No: 14 Date : 05-11-10 BUBBLE SORT ALGORITHM: Step 1 : Start Step 2 : Read n Step 3 : set i=0 Step 4 : if i<n ,goto step 5. Other wise goto step 8 Step 5 : Read a(i) Step 6 : i=i+1 Step 7 : goto step 4 Step 8 : set i=0 Step 9 : if i<n, goto step 10. Otherwise goto step 17 Step 10 : set j=i+1 Step 11 : if j<n, goto step 12. Otherwise goto step 15 Step 12 : if a(j)<a(i), t=a(i),a(i)=a(j),a(j)=t Step 13 : j=j+1 Step 14 : goto step 11 Step 15 : i=i+1 Step 16 : goto step 9 Step 17 : set i=0 Step 18 : if i<n ,goto step 19. Other wise goto step 22 Step 19 : Print a(i) Step 20 : i=i+1 Step 21 : goto step 18 Step 22 : Stop
FLOWCHART: START
Read n
i=0
i=i+1
STOP j=j+1
PROGRAM #include<stdio.h> main() { int j,i,n,temp,a[10]; printf("Enter the limit"); scanf("%d",&n); printf("Enter %d elements\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("Sorted array is\n"); for(i=0;i<n;i++) printf(" %d",a[i]); }
84
OUTPUT
: : 4
Sorted array is 23 34 45 67
Enter the limit : 5 Enter 5 elements 189 56 78 245 34 Sorted array is 34 56 78 189 245
85
SELECTION SORT
ALGORITHM: Step 1 : Start Step 2 : Read n Step 3 : set i=0 Step 4 : if i<n ,goto step 5. Other wise goto step 8 Step 5 : Read a(i) Step 6 : i=i+1 Step 7 : goto step 4 Step 8 : set i=0 Step 9 : if i<n, goto step 10. Otherwise goto step 17 Step 10 : set j=i+1, min=i Step 11 : if j<n, goto step 12. Otherwise goto step 15 Step 12 : if a(j)<a(min), min=j Step 13 : j=j+1 Step 14 : goto step 11 Step 15 : t=a(i),a(i)=a(min),a(min)=t, i=i+1 Step 16 : goto step 9 Step 17 : set i=0 Step 18 : if i<n ,goto step 19. Other wise goto step 22 Step 19 : Print a(i) Step 20 : i=i+1 Step 21 : goto step 19 Step 22 : Stop
FLOWCHART:
START
Read n
i=0
i=0
i=0
If i<n If j<n T T Print a(i) If a(j)<a(min) T min=j j=j+1 t=a(i) a(i)=a(miin) a(min)=t F i=i+1
i=i+1
STOP
PROGRAM #include<stdio.h> main() { int a[10],min,n,i,j,temp; printf("Enter the limit\t:\t"); scanf("%d",&n); printf("Enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { min=i; for(j=i+1;j<n;j++) if(a[j]<a[min]) min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; } printf("Sorted array is\n"); for(i=0;i<n;i++) printf(" %d ",a[i]); printf("\n"); } 88
OUTPUT
: : 4
Enter the limit Enter the elements 345 12 35 22 Sorted array is 12 22 35 345 Enter the limit Enter the elements
1 56 87 22 44 67 Sorted array is 1 22 44 56 67 87
89
Pgm No: 16 Date : 12-11-10 FUNCTION TO CHECK FOR TAUTOLOGIES AND CONTRADICTIONS ALGORITHM: Step 1 : Start Step 2 : Read string Step 3 : if a(0)='~', goto step 4. Otherwise goto step 7 Step 4 : Read value of variable a Step 5 : z=~a Step 6 : goto step 19 Step 7 : if a(1)='&' , goto step 8. Otherwise goto step 11 Step 8 : Read variables a,b Step 9 : z=a&b Step 10 : goto step 19 Step 11 : if a(1)='|' , goto step 12. Otherwise goto step 15 Step 12 : Read variables a,b Step 13 : z=a|b Step 14 : goto step 19 Step 15 : if a(1)='^' , goto step 16. Otherwise goto step 15 Step 16 : Read variables a,b Step 17 : z=a^b Step 18 : goto step 19 Step 19 : if(z==1), Print expression is tautology Otherwise Print expression is contradiction Step 20 : Stop 90
FLOWCHART:
START
Read string
If a(0)='~' F
If a(1)='&' F If a(1)='|' F
T Read a,b
If a(1)='^' F
z=a|b
z=a^b
A 91
N If z=1
Print It is a tautology
Print It is a contradiction
STOP
92
PROGRAM #include<stdio.h> int and(char *a); int xor(char *a); int or(char *a); int not(char *a); main() { char a[3],ch[3]; int i; do { printf("Enter the equations for eg:- a&b ,a^b,a|b,~a\n"); scanf("%s",a); if(a[0]=='~') i=not(a); else { switch(a[1]) { case '&' : i=and(a); break; case '|' : i=or(a); case '^' : i=xor(a); break; break;
if(i==1) printf("%s is tautology\n",a); else printf("\n%s is contradiction\n",a); printf("Do you want to continue?(Y/N)\t:\t"); scanf("%s",ch); }while((strcmp(ch,"Y")==0)||(strcmp(ch,"y")==0)); } int and(char *p) { int z,a,b; printf("Enter the values of %c and %c\t:\t ",*p,*p+1); scanf("%d%d",&a,&b); z=a&b; return(z); } int xor(char *p) { int z,a,b; printf("Enter the values of %c and %c\t:\t",*p,*p+1); scanf("%d%d",&a,&b); z=a^b; return(z); }
94
int or(char *p) { int a,b,z; printf("Enter the values of %c and %c\t:\t",*p,*p+1); scanf("%d%d",&a,&b); z=a|b; return(z); } int not(char *p) { int a,z; printf("enter the value of %c\t:\t",*p); scanf("%d",&a); z=abs(~a;); return(z); }
95
OUTPUT
Enter the equations for eg:- a&b ,a^b,a|b,~a a|b Enter the values of a and b : a|b is tautology Do you want to continue?(Y/N) : y 1 0
Enter the equations for eg:- a&b ,a^b,a|b,~a a&b Enter the values of a and b : a&b is contradiction Do you want to continue?(Y/N) : y 01
Enter the equations for eg:- a&b ,a^b,a|b,~a a^b Enter the values of a and b : a^b is contradiction Do you want to continue?(Y/N) : y 1 1
Enter the equations for eg:- a&b ,a^b,a|b,~a ~a Enter the value of a : ~a is tautology Do you want to continue?(Y/N) : n 96 0
ALGORITHM: Step 1 : Start Step 2 : Read n1,n2 Step 3 : if n1=0 or n2=0, Print GCD is 0. Otherwise goto step 4 Step 4 : if n1<n2, t=n1,n1=n2,n2=t. Step 5 : if n2!=0 ,goto step 6.Otherwise goto step 8 Step 6 : rem=n1%n2,n1=n2,n2=rem Step 7 : goto step 5 Step 5 : Print n1 Step 6 : Stop
97
FLOWCHART:
START
Read n1,n2
Print n1
STOP
98
PROGRAM #include<stdio.h> main() { int j,i=2,a[10],n,n1,dup1,dup2,n2,rem,r,q; printf("Enter two numbers"); scanf("%d%d",&n1,&n2); if((n1==0)||(n2==0)) { printf("\nGCD(%d,%d)=0\nLCM(%d,%d)=0",n1,n2,n1,n2); } else { dup1=n1; dup2=n2; if(n1<n2) { rem=n1; n1=n2; n2=rem } while(n2!=0) { rem=n1%n2; 99
OUTPUT
: : 67 3
12 6
100
RECCURSIVE FUNCTIONS
ALGORITHM: Step 1 : Start Step 2 : Print menu 1.Factorial 2.Fibonacci series 3.Exit Step 3 : read choice Step 4 : if choice=1 ,goto step 5.Otherwise goto step 8 Step 5 : Read n Step 6 : call factorial(n) Step 7 : Print factorial(n) Step 8 : if choice =2, gotostep 9. Otherwise goto step Step 9 : Read n Step 10 : set i=0 Step 11 : if i<n, goto step12.Otherwise goto step12 Step 12 : call fibonacci(i) Step 13 : Print fibonacci(i) Step 14 : i=i+1 Step 15 : goto step 11 Step 16 : if choice =3,exit the program Step 17 : Stop 101
factorial(n) Step 1 : Start Step 2 : read n from main() Step 3 : if n<=1,return 1.Otherwise call n*factorial(n) Step 4 : Stop fibonacci(n) Step 1 : Start Step 2 : Read n from main() Step 3 : if n<=1, return n.Otherwise call (fibonacci(n-1)+fibonacci(n-2)) Step 4 :Stop FLOWCHART:
START
Read choice
102
If choice=1
Read n F If choice=2 T Call factorial (n) Read n Print factorial(n) Set i=0
i=i+1
103
factorial(n)
START
If n<=1
Return 1
Call n*factorial(n-1)
F If n<=1
Return n
Call fibonacci(n-1)+fibonacci(n-2)
STOP
PROGRAM #include<stdio.h> int factorial(int n) { if(n==0) return(1); else return(n*factorial(n-1)); } int fib(int n) { if((n==0)||(n==1)) return(n); else return(fib(n-1)+fib(n-2)); } main() { int i,f,n; char ch[2]; do { printf("MAIN MENU\n1.FACTORIAL\n2.FIBONACCI\n3.EXIT\n"); printf("Enter your choice\t:\t"); 105
scanf("%d",&i); switch(i) { case 1 : printf("Enter the number\t:\t"); scanf("%d",&n); printf("%d !=%d\n",n,factorial(n)); break; case 2 : printf("Enter the limit"); scanf("%d",&n); printf("Fibonacci series is\n"); for(i=0;i<n;i++) printf("%d ",fib(i)); break; case 3 : printf("Thankyou\n"); } printf("\nDo you want to continue?(y/N)\t:\t"); scanf("%s",ch); }while((strcmp(ch,"y")==0) ||strcmp(ch,"y")==0); }
106
OUTPUT
MAIN MENU 1.FACTORIAL 2.FIBONACCI 3.EXIT Enter your choice Enter the number 7 !=5040 Do you want to continue?(y/N) : y : : 1 7
MAIN MENU 1.FACTORIAL 2.FIBONACCI 3.EXIT Enter your choice Enter the limit Fibonacci series is 0112358 Do you want to continue?(y/N) : n : : 7 2
107
CREATING DATABASE FOR TELEPHONE NUMBERS AND RELATED OPERATIONS USING FILE CONCEPTS
ALGORITHM: Step 1 : Start Step 2 : Print 1. ADD 2.DELETE 3.UPDATE 4.DISPLAY 0:EXIT Step 3 : Read choice Step 4 : If choice=1 goto 9 Step 5 : If choice=2 goto 17 Step 6 : If choice=3 goto 30 Step 7 : If choice=4 goto 45 Step 8 : Goto step 53 Step 9 : x=1; Step 10 : Open file tele in append mode Step 11 : Read t.phoneno,t.custname,t.exchange Step 12 : Write t.phoneno,t.custname,t.exchange onto file tele Step 13 : Print 1 FOR MORE ELSE 0 Step 14 : Read x 108
Step 15 :If x=1 goto step 11 Step 16 : Close file tele goto step 2 Step 17 : x=1,flag=0; Step 18 : Open file tele in read binary mode and temp in write binary mode Step 19 : Read pno Step 20 : Read value from file tele and store it in t,if one record is read goto 21 else goto 22 Step 21 : If pno and t.phoneno are same write it to file temp else set flag=1 goto step 20 Step 22 : Close tele and temp Step 23 : Remove tele Step 24 : Rename temp to tele Step 25 : If flag=1 print SUCCESS else NOT FOUND Step 26 : Print 1 FOR MORE DELETION ELSE 0 Step 27 : Read x Step 28 : If x=1 goto 18 Step 29 : Goto step 2 Step 30 : x=1,flag=0; Step 31 : Open file tele in read binary mode and temp in write binary mode Step 32 : Read pno Step 33 : Read value from file tele and store it in t,if one record is read goto 34 else goto 37 Step 34 : If pno and t.phoneno are same write it to file temp goto 36 Step 35 : Read t.phoneno,t.custname,t.exchange and write it to file temp Step 36 : Goto step 33 Step 37 : Close tele and temp Step 38 : Remove tele Step 39 : Rename temp to tele 109
Step 40 : If flag=1 print SUCCESS else NOT FOUND Step 41 : Print 1 FOR MORE UPDATION ELSE 0 Step 42 : Read x Step 43 : If x=1 goto 31 Step 44 : Goto step 2 Step 45 : flag=0 Step 46 : Open file tele in read binary mode Step 47 : Read value from file tele and store it in t,if one record is read goto 48 else goto 50 Step 48 : flag=1 Step 49 : Display t.phoneno,t.custname,t.exchange goto step 47 Step 50 : If flag=0 display EMPTY Step 51 : Close file tele Step 52 : Goto step 2 Step 53 ; Stop
110
FLOWCHART: START
Read n
If n=1 F
ADD T
If n=2 F
DELETE T
If n=3 F T
UPDATE
If n=4 F
DISPLAY
STOP 111
ADD
x=1
If x=1
Read x
112
DELETE
x=1,flag=0 B F
Read phoneno
113
If file is empty T
If phno!=t.phno
Rename temp to tele file T Write Name Phoneno: Exchange name to temp file flag=1
If flag=0
Print The record is successfully deleted Print The record is not successfully deleted
Read x
UPDATE
Read phoneno
115
If file is empty T
If phno!=t.phno
Rename temp to tele file flag=1 Write Name Phoneno: Exchange name to temp file Read Name,Phoneno: Exchange name to temp file
If flag=0
Print The record is successfully updated Print The record is not successfully updated
Read x
B 116
DISPLAY
x=0
117
PROGRAM #include<stdio.h> #include<string.h> void add(); void display(); void update(); void delete(); int menu(); struct telephone { char phoneno[10]; char customer[50]; char exchange[20]; } main() { int n; n=menu(); switch(n) { case 1 : add(); break; case 2 : delete(); break; case 3 : update(); break; case 4 : display(); break; default :printf("Thankyou\n"); 118
} } int menu() { int a; printf("\nMain Menu\n"); printf("\n1.ADDd\n2.DELETE\n3.UPDATE\n4.DISPLAY\n0.EXIT\n"); printf("Enter your choice\t:\t"); scanf("%d",&a); return(a); } void add() { FILE *ft; struct telephone t; int x=1; ft=fopen("tele","a+b"); while(x==1) { printf("Enter the customer name\t;\t"); scanf("%s",t.customer); printf("Enter the phone number\t:\t"); scanf("%s",t.phoneno); printf("Enter the exchange name\t:\t"); scanf("%s",t.exchange); 119
fwrite(&t,sizeof(t),1,ft); printf("\nPress 1 for add another record\nElse Press 0\n"); scanf("%d",&x); } fclose(ft); main(); } void delete() { int x=1,flag=0; struct telephone t; char phno[10]; FILE *ft,*fs; while(x==1) { flag=0; printf("Enter the phone number to be deleted\n"); scanf("%s",phno); ft=fopen("temp","wb+"); fs=fopen("tele","rb+"); while(fread(&t,sizeof(t),1,fs)==1) { if(strcmp(phno,t.phoneno)!=0) fwrite(&t,sizeof(t),1,ft);
120
else flag=1; } fclose(ft); fclose(fs); remove("tele"); rename("temp","tele"); if(flag==1) printf("Record is successfully deleted\n"); else printf("record is not found"); printf("Press 1 for delete another record\nelse Press 0 "); scanf("%d",&x); } main(); } void update() { int x=1; int flag=0; char phno[10]; FILE *ft,*fs; struct telephone t; printf("Enter the phone number to be modified\n"); scanf("%s",phno); 121
while(x==1) { flag=0; ft=fopen("temp","wb+"); fs=fopen("tele","rb+"); while(fread(&t,sizeof(t),1,fs)==1) { if(strcmp(phno,t.phoneno)!=0) fwrite(&t,sizeof(t),1,ft); else { printf("Enter the new customer name\t:\t"); scanf("%s",t.customer); printf("Enter the new phone number\t:\t"); scanf("%s",t.phoneno); printf("Enter the new exchange name\t:\t"); scanf("%s",t.exchange); fwrite(&t,sizeof(t),1,ft); flag=1; } } fclose(ft); fclose(fs); remove("tele"); rename("temp","tele"); 122
if(flag==1) printf("Record is successfully updated\n"); else printf("record is not found"); printf("Press 1 for update another record\nElse 0 \n"); scanf("%d",&x); } main(); } void display() { FILE *f; int flag=0; struct telephone t; printf("the details are.........\n"); f=fopen("tele","rb+"); while(fread(&t,sizeof(t),1,f)==1) { printf("\n..........................\n"); printf("Customer Name\t:\t%s\n",t.customer); printf("Phone number\t:\t%s\n",t.phoneno); printf("Exchange Name\t:\t%s\n",t.exchange); } fclose(f); } 123
Press 1 for add another record else Press 0 Enter the customer name Enter the phone number ; : chinnu
56565656 thrissur
Press 1 for add another record else Press 0 : 1 Enter the customer name Enter the phone number ; : rinku 23950023 calicut : 0
Press 1 for add another record else Press 0 Main Menu 1.ADD 2.DELETE 3.UPDATE
124
Enter the phone number to be deleted 56565656 Record is successfully deleted Press 1 for delete another record else Press 0 Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0. EXIT Enter your choice : The details are ............................................................... Customer Name Phone number Exchange Name : : : Meena 123456789 kannur 4 : 0
......................................................... Customer Name Phone number Exchange Name : : : rinku 23950023 calicut
124
Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0. EXIT Enter your choice : 3
Enter the phone number to be modified 123456789 Enter the new customer name Enter the new phone number Enter the new exchange name Record is successfully updated Press 1 for update another record else 0 Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0. EXIT Enter your choice Thankyou : 0 : 0 : : : bindhu 23879876 kannur
125
CREATING DATABASE FOR MAILING ADDRESS AND RELATED OPERATIONS USING STRUCTURE CONCEPTS
ALGORITHM: Step 1 : Start Step 2 : Print 1. ADD 2.DELETE 3.UPDATE 4.DISPLAY 0:EXIT Step 3 : Read choice Step 4 : If choice=1 goto 9 Step 5 : If choice=2 goto 17 Step 6 : If choice=3 goto 30 Step 7 : If choice=4 goto 45 Step 8 : Goto step 53 Step 9 : x=1; Step 10 : Open file MAIL in append mode Step 11 : Read t.name,t.h_name,t.street,t.post,t.pin Step 12 : Write t.name,t.h_name,t.street,t.post,t.pin onto file MAIL Step 13 : Print 1 FOR MORE ELSE 0 Step 14 : Read x 126
Step 15 :If x=1 goto step 11 Step 16 : Close file MAIL goto step 2 Step 17 : x=1,flag=0; Step 18 : Open file MAIL in read binary mode and temp in write binary mode Step 19 : Read sname Step 20 : Read value from file MAIL and store it in t,if one record is read goto 21 else goto 22 Step 21 : If sname and t.name are same write it to file temp else set flag=1 goto step 20 Step 22 : Close MAIL and temp Step 23 : Remove MAIL Step 24 : Rename temp to MAIL Step 25 : If flag=1 print SUCCESS else NOT FOUND Step 26 : Print 1 FOR MORE DELETION ELSE 0 Step 27 : Read x Step 28 : If x=1 goto 18 Step 29 : Goto step 2 Step 30 : x=1,flag=0; Step 31 : Open file MAIL in read binary mode and temp in write binary mode Step 32 : Read sname Step 33 : Read value from MAIL file and store it in t,if one record is read goto 34 else goto 37 Step 34 : If sname and t.name are same write it to file temp goto 36 Step 35 : Read t.name,t.h_name,t.street,t.post,t.pin from MAILand write it to file temp Step 36 : Goto step 33 Step 37 : Close MAIL and temp Step 38 : Remove MAIL Step 39 : Rename temp to MAIL 127
Step 40 : If flag=1 print SUCCESS else NOT FOUND Step 41 : Print 1 FOR MORE UPDATION ELSE 0 Step 42 : Read x Step 43 : If x=1 goto 31 Step 44 : Goto step 2 Step 45 : flag=0 Step 46 : Open file MAIL in read binary mode Step 47 : Read value from file MAIL and store it in t,if one record is read goto 48 else goto 50 Step 48 : flag=1 Step 49 : Display t.name,t.h_name,t.street,t.post,t.pin goto step 47 Step 50 : If flag=0 display EMPTY Step 51 : Close file MAIL Step 52 : Goto step 2 Step 53 ; Stop
128
FLOWCHART: START
Read n
If n=1
F T
ADD
If n=2 F
DELETE T
If n=3 F
UPDATE T
If n=4
DISPLAY
STOP 129
ADD
x=1
F If x=1
Read x
130
DELETE
x=1,flag=0 B F
Read sname
131
If sname!=t.name
If flag=0
Print The record is not successfully deleted Print The record is successfully deleted
Read x
UPDATE
x=1,flag=0 C F
If x=1 T
Read sname
133
If file is empty T
If phno!=t.phno
Rename temp to MAIL file flag=1 Write t.name, t.h_name, t.street,t.post,t.pin Read name,h_name, street,post,pin to temp file F
If flag=0
Print The record is successfully updated Print The record is not successfully updated
Read x
B 134
DISPLAY
x=0
If x=0
134
PROGRAM #include<stdio.h> #include<string.h> void add(); void display(); void update(); void delete(); int menu(); struct mail { char name[10]; char h_name[50]; char street[20]; char post[20]; char pin[10]; }; main() { int n; n=menu(); switch(n) { case 1 : add(); break; case 2 : delete(); break; case 3 : update(); break; 135
case 4 : display(); break; default :printf("Thankyou\n"); } } int menu() { int a; printf("\nMain Menu\n"); printf("\n1.ADD\n2.DELETE\n3.UPDATE\n4.DISPLAY\n0.EXIT\n"); printf("Enter your choice\t:\t"); scanf("%d",&a); return(a); } void add() { FILE *ft; struct mail t; int x=1; ft=fopen("mail","a+b"); while(x==1) { printf("Enter the customer name\t;\t"); scanf("%s",t.name); printf("Enter the house name\t:\t"); scanf("%s",t.h_name); 136
printf("Enter the street name\t:\t"); scanf("%s",t.street); printf("Enter the post\t:\t"); scanf("%s",t.post); printf("Enter the pin code\t;\t"); scanf("%s",t.pin); fwrite(&t,sizeof(t),1,ft); printf("\nPress 1 for add another record\nElse Press 0\n"); scanf("%d",&x); } fclose(ft); main(); } void delete() { int x=1,flag=0; struct mail t; char name[10]; FILE *ft,*fs; while(x==1) { flag=0; printf("Enter the name to be deleted\n"); scanf("%s",name); ft=fopen("temp","wb+"); 137
fs=fopen("mail","rb+"); while(fread(&t,sizeof(t),1,fs)==1) { if(strcmp(name,t.name)!=0) fwrite(&t,sizeof(t),1,ft); else flag=1; } fclose(ft); fclose(fs); remove("mail"); rename("temp","mail"); if(flag==1) printf("Record is successfully deleted\n"); else printf("record is not found"); printf("Press 1 for delete another record\nelse Press 0 "); scanf("%d",&x); } main(); } void update() { int x=1; int flag=0; 138
char name[10]; FILE *ft,*fs; struct mail t; printf("Enter the name to be modified\n"); scanf("%s",name); while(x==1) { flag=0; ft=fopen("temp","wb+"); fs=fopen("mail","rb+"); while(fread(&t,sizeof(t),1,fs)==1) { if(strcmp(name,t.name)!=0) fwrite(&t,sizeof(t),1,ft); else { printf("Enter the new customer name\t:\t"); scanf("%s",t.name); printf("Enter the new house name\t:\t"); scanf("%s",t.h_name); printf("Enter the new street name\t:\t"); scanf("%s",t.street); printf("Enter the new post\t:\t"); scanf("%s",t.post);
139
printf("Enter the new pin\t;\t",t.pin); scanf("%s",t.pin); fwrite(&t,sizeof(t),1,ft); flag=1; } } fclose(ft); fclose(fs); remove("mail"); rename("temp","mail"); if(flag==1) printf("Record is successfully updated\n"); else printf("record is not found"); printf("Press 1 for update another record\nElse 0 \n"); scanf("%d",&x); } main(); } void display() { FILE *f; int flag=0; struct mail t; printf("the details are.........\n"); 140
f=fopen("mail","rb+"); while(fread(&t,sizeof(t),1,f)==1) { printf("\n..........................\n"); printf("Customer Name\t:\t%s\n",t.name); printf("House Name\t:\t%s\n",t.h_name); printf("street Name\t:\t%s\n",t.street); printf("Post\t:\t%s\n",t.post); printf("Pin\t;\t%s",t.pin); } fclose(f); main(); } OUTPUT :
Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0.EXIT Enter your choice : 1 soorya soorya.house kombara
Enter the customer name ; Enter the house name Enter the street name Enter the post : : :
kombara 141
682828 : 1
Press 1 for add another record else Press 0 Enter the customer name ; Enter the house name Enter the street name Enter the post : : : rekha kaliveed broadway
ernakulam 233445 : 1
Press 1 for add another record else Press 0 Enter the customer name ; Enter the house name Enter the street name Enter the post : : : thevara 4556677 sreekala nest
sooryanagar
Press 1 for add another record else Press 0 : 1 Enter the name to be deleted renukha record is not found Press 1 for delete another record else Press 0 : 0 Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0.EXIT Enter your choice : 4 142
The details are......... .......................... Customer Name House Name street Name : Post Pin ; : : : soorya soorya.house
kombara kombara
682828
.......................... Customer Name House Name street Name Post Pin Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0.EXIT Enter your choice : 3 : ; : : : thevara 4556677 sreekala nest sooryanagar
Enter the name to be modified soorya Enter the new customer name Enter the new house name : : nest 143 surya
Enter the new street name : Enter the new post : Enter the new pin kannur
bjnagar
: 682828
Record is successfully updated Press 1 for update another record else 0 Main Menu 1.ADD 2.DELETE 3.UPDATE 4.DISPLAY 0.EXIT Enter your choice : Thankyou 0 : 0
144
CREATING DATABASE FOR WEBPAGE ADDRESS AND RELATED OPERATIONS USING FILE CONCEPTS
ALGORITHM: Step 1 : Start Step 2 : Print 1. ADD 2.DELETE 3.UPDATE 4.DISPLAY 0:EXIT Step 3 : Read choice Step 4 : If choice=1 goto 9 Step 5 : If choice=2 goto 17 Step 6 : If choice=3 goto 30 Step 7 : If choice=4 goto 45 Step 8 : Goto step 53 Step 9 : x=1; Step 10 : Open file tele in append mode Step 11 : Read t.mailname Step 12 : Write t.mailname onto file mail Step 13 : Print 1 FOR MORE ELSE 0 Step 14 : Read x 145
Step 15 :If x=1 goto step 11 Step 16 : Close file mail goto step 2 Step 17 : x=1,flag=0; Step 18 : Open file mail in read binary mode and temp in write binary mode Step 19 : Read mname Step 20 : Read value from file mail and store it in t,if one record is read goto 21 else goto 22 Step 21 : If mname and t.mailname are same write it to file temp else set flag=1 goto step 20 Step 22 : Close mail and temp Step 23 : Remove mail Step 24 : Rename temp to mail Step 25 : If flag=1 print SUCCESS else NOT FOUND Step 26 : Print 1 FOR MORE DELETION ELSE 0 Step 27 : Read x Step 28 : If x=1 goto 18 Step 29 : Goto step 2 Step 30 : x=1,flag=0; Step 31 : Open file mail in read binary mode and temp in write binary mode Step 32 : Read mname Step 33 : Read value from file mail and store it in t,if one record is read goto 34 else goto 37 Step 34 : If mname and t.mailname are same write it to file temp goto 36 Step 35 : Read t.mailname and write it to file temp Step 36 : Goto step 33 Step 37 : Close mail and temp Step 38 : Remove mail Step 39 : Rename temp to mail 146
Step 40 : If flag=1 print SUCCESS else NOT FOUND Step 41 : Print 1 FOR MORE UPDATION ELSE 0 Step 42 : Read x Step 43 : If x=1 goto 31 Step 44 : Goto step 2 Step 45 : flag=0 Step 46 : Open file mail in read binary mode Step 47 : Read value from file mail and store it in t,if one record is read goto 48 else goto 50 Step 48 : flag=1 Step 49 : Display t.mailname goto step 47 Step 50 : If flag=0 display EMPTY Step 51 : Close file mail Step 52 : Goto step 2 Step 53 ; Stop
147
FLOWCHART: START
Read n
If n=1 F
ADD T
If n=2
F T
DELETE
If n=3 F
UPDATE T
If n=4 F
DISPLAY
STOP 148
ADD
x=1
If x=1
Read mailname
Read x
149
DELETE
Read mname
150
If file is empty T
If mname!= t.mailname T
If flag=0
Print The record is not successfully deleted Print The record is successfully deleted
Read x
UPDATE
x=1,flag=0 C F
If x=1 T
Read mname
152
If file is empty T
If Mname !=t.mailname
Print The record is successfully updated Print The record is not successfully updated
Read x
DISPLAY
x=0
If file is empty T
154
PROGRAM #include<stdio.h> #include<string.h> struct webpage { char mail[50]; }; void add(); void delete(); void update(); void display(); int menu(); main() { int n; n=menu(); switch(n) { case 1 : add(); break; case 2 : delete(); break; case 3 : update(); break; 155
case 4 : display(); break; default : printf("Thankyou\n"); } } int menu() { int n; printf("\nMain Menu\n.............\n"); printf("1.Addition\n2.Delete\n3.Update\n4.Display\n0.Exit\n"); printf("Enter your choice\t:\t"); scanf("%d",&n); return(n); } void add() { FILE *f; struct webpage web; int x=1; f=fopen("webpage","a+b"); while(x==1) { printf("Enter the mail address\t:\t"); scanf("%s",web.mail); fwrite(&web,sizeof(web),1,f); 156
printf("The record successfully adde\n"); printf("Press 1 for add more data else press 0\n"); scanf("%d",&x); } fclose(f); main(); } void delete() { FILE *ft,*fs; char m[20]; int x=1,flag=0; struct webpage web; while(x==1) { ft=fopen("temp","wb+"); fs=fopen("webpage","rb+"); printf("Enter the mail address to be deleted\t:\t"); scanf("%s",m); while(fread(&web,sizeof(web),1,fs)==1) { if(strcmp(web.mail,m)!=0) fwrite(&web,sizeof(web),1,ft); else flag=1; 157
} fclose(ft); fclose(fs); remove("webpage"); rename("temp","webpage"); if(flag==0) printf("The record is not found\n"); else printf("The record successfully deleted\n"); printf("Press 1 for delete another data\nElse Press 0\n"); scanf("%d",&x); } main(); } void update() { int flag=0,x=1; char m[20]; struct webpage web; FILE *ft,*fs; while(x==1) { ft=fopen("temp","wb+"); fs=fopen("webpage","rb+"); printf("Enter the record to be update\t:\t"); 158
scanf("%s",m); while(fread(&web,sizeof(web),1,fs)==1) { if(strcmp(web.mail,m)!=0) fwrite(&web,sizeof(web),1,ft); else { flag=1; printf("Enter the new mail address\t:\t"); scanf("%s",web.mail); fwrite(&web,sizeof(web),1,ft); } } fclose(fs); fclose(ft); remove("webpage"); rename("temp","webpage"); if(flag==0) printf("The record is not found\n"); else printf("The record is successfully updated\n"); printf("Press 1 for update nother data\nElse press 0\n"); scanf("%d",&x); } main(); 159
} void display() { FILE *f; struct webpage web; f=fopen("webpage","rb+"); printf("Details are\n"); while(fread(&web,sizeof(web),1,f)==1) { printf("\nMail address\t:\t%s",web.mail); } }
OUTPUT Main Menu ............. ..... 1.Addition 2.Delete 3.Update 4.Display 0.Exit
The record successfully adde d Press 1 for add more data else press 0 : 1 160
The record successfully adde d Press 1 for add more data else press 0 Enter the mail address : : 1
The record successfully adde d Press 1 for add more data else press 0 : 0
Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit Enter your choice : 2 : [email protected]
Press 1 for delete another data else Press 0 Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit
: 1
161
Press 1 for delete another data else Press 0 Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit Enter your choice : Details are Mail address Mail address Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit Enter your choice : 3 : : : : 4
: 0
[email protected] [email protected]
The record is successfully updated Press 1 for update nother data else press 0 Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit Enter your choice : Details are Mail address Mail address Main Menu .................. 1.Addition 2.Delete 3.Update 4.Display 0.Exit Enter your choice : Thankyou 0 : : [email protected] [email protected] 4 : 0
163
ALGORITHM: Step 1 : Start Step 2 : Read root of equation x3-4x-9 Step 3 : Read n Step 4 : f1=a3-4a-9,f2=b3-4b-9 Step 5 : if f1*f2 >0, Print Invalid. Otherwise goto step 6 Step 6 : set i=0 Step 7 : if i < n, goto step 8. Otherwise goto step 13 Step 8 : x=(a+b )/2 Step 9 : f3=x3-4x-9 Step 10 : if f3 <0, b=x,f2=f3. Otherwise a=x,f1=f3 Step 11 : i=i+1 Step 12 : goto step 7 Step 13 : Print x Step 14 : Stop
165
FLOWCHART:
START
Read n
f1=a3-4a-9 f2=b3-4b-9
If f1*f2>0
T i=0
Print Invalid
T If i<n x=(a+b)/2
Print x
PROGRAM #include<stdio.h> #include<math.h> main() { int i,n; float a,b,x,f1,f2,f3; printf("Enter the roots of x*x*x-4x-9"); scanf("%f%f",&a,&b); f1=a*a*a-4*a-9; f2=b*b*b-4*b-9; printf("Enter the number of iteration\t;\t"); scanf("%d",&n); for(i=0;i<n;i++) { x=(a+b)/2; f3=x*x*x-4*x-9; if(f3<0) { b=x; f2=f3; } else { 167
OUTPUT
: : ; 2 3 4
Enter the roots of x*x*x-4x-9 Enter the number of iteration Root of x*x*x-4x-9 :
2.062500
Enter the roots of x*x*x-4x-9 : Enter the number of iteration Root of x*x*x-4x-9 : ;
2 3 8
2.003906
168
ALGORITHM: Step 1 : Start Step 2 : Read n,initial value of x3-3x-5 Step 3 : set i=0 Step 4 : if i<n goto step 5 .Otherwise goto step 8 Step 5 : f1=x3-3x-5,f2=3x2-3,x1=x1-(f2/f1),x=x1 Step 6 : i=i+1 Step 7 :goto step 4 Step 8 :print x1 Step 6 : Stop
169
FLOWCHART:
START
Set i=0
Print x1
i=i+1
STOP
170
PROGRAM #include<stdio.h> #include<math.h> main() { int x,i,n; float f1,f2,x1; printf("Enter the number of steps\t:\t"); scanf("%d",&n); printf("Enter the initial value of x^3-3x-5\n"); scanf("%d",&x); for(i=0;i<n;i++) { f1=pow(x,3)-3*x-5; f2=3*pow(x,2)-3; x1=x-(f1/f2); x=x1; } printf("Root of x^3-3x-5\t:\t%f",x1); }
171
OUTPUT
: 6 : 3
Enter the number of steps : Enter the initial value of x^3-3x-5 Root of x^3-3x-5 :
2.333333
Enter the number of steps : Enter the initial value of x^3-3x-5 Root of x^3-3x-5 :
8 : 2
2.333333
172
ALGORITHM: Step 1 : Start Step 2 : Read n,initial value of x3-3x-5 Step 3 : set i=0 Step 4 : if i<n goto step 5 .Otherwise goto step 8 Step 5 : f1=x13-2x1-5.f2=x23-2x2-5 , x3=x2-f2((x2-x1)/(f2-f1)), x1=x2 , x2=x3 Step 6 : i=i+1 Step 7 :goto step 4 Step 8 :print x1 Step 6 : Stop
173
FLOWCHART:
START
Set i=0
i=i+1
STOP
174
PROGRAM #include<stdio.h> #include<math.h> main() { float x1,x2,x3,f1,f2,f3; int i,n; printf("Enter the roots of x^3-2x-5"); scanf("%f%f",&x1,&x2); printf("Enter n"); scanf("%d",&n); for(i=0;i<n;i++) { f1=pow(x1,3)-2*x1-5; f2=pow(x2,3)-2*x2-5; x3=x2-f2*((x2-x1)/(f2-f1)); x1=x2; x2=x3; } printf("Root of x^3-2x-5\t:\t%f",x3); }
175
OUTPUT
: : 2 3
Root of x^3-2x-5 :
2.094552
Root of x^3-2x-5 :
2.081264
176
ALGORITHM: Step 1 : Start Step 2 : Enter coefficients Step 3 : i=0 Step 4 : if i<3 goto 4 otherwise goto 10 Step 5 : j=0 Step 6 : if j<3 goto 5 otherwise goto 9 Step 7 : Read a Step 8 : j=j+1,goto 6 Step 9 : i=i+1,goto 4 Step 10 : Print Enter constants Step 11 : i=0 Step 12 : if i<3 goto 13 otherwise goto 15 Step 13 : Read a[i][3] Step 14 : i=i+1,goto 12 Step 15 : if a[0][0] !=0.0 goto 16 otherwise goto 43 Step 16 : p=a[1][0],q=a[0][0],r=a[2][0] Step 17 : j=0 Step 18 :if j<=3 goto 19 otherwise goto 24 Step 19 : b[0][j]=(p/q)*a[0][j] 177
Step 20 : a[1][j]+=b[0][j] Step 21 : c[0][j]=(r/q)*a[0][j] Step 22 : a[2][j]+=c[0][j] Step 23 : j=j+1,goto 18 Step 24 : p=a[2][1],q=a[1][1] Step 25 : j=0 Step 26 : if j<=3 goto 27 otherwise 29 Step 27 : b[1][j]=(p/q)*a[1][j], a[2][j]+=b[1][j] Step 28 : j=j+1,goto 26 Step 29 : print matrix becomes Step 30 : i=0 Step 31 : if i<3 goto 32 otherwise goto Step 32 : j=0 Step 33 : if j<4 goto 34 otherwise goto Step 34 : print a[i][j] Step 35 : j=j+1,goto 33 Step 36 : Print next line Step 37 : i=i+1,goto 31 Step 38 : z=a[2][3]/a[2][2] Step 39 : y=(a[1][3]a[1][2]*z)/a[1][1] Step 40 : x=(a[0][3-a[0][2]*z-a[0][1]*y)/a[0][0] Step 41 : print x,y,z,goto 45 Step 42 : print ENTER AGAIN Step 43 ; goto step 2 Step 44 : Stop 178
FLOWCHART:
START C i=0
If i<3
T j=0
j=0
If j<3 F
If j<3
T Read a[i][j]
j=j+1
j=j+1
179
j=0
If j<=3 T
p=a[2][1],q=a[1][1]
j=0
If j<=3 j=j+1 T
j=j+1
i=0
If i<3 T
j=0
If j<4 T
Print a[i][j]
j=j+1
181
PROGRAM #include<stdio.h> #include<math.h> main() { int i,j; float a[3][4],b[3][4],c[4][4]; float x,y,z,p,q,r; m: printf("Enter the coefficient\t:\t"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%f",&a[i][j]); printf("Enter the constants\t:\t"); for(i=0;i<3;i++) scanf("%f",&a[i][3]); if(a[0][0]!=0.0) { p=a[1][0]; q=a[0][0]; r=a[2][0]; for(j=0;j<=3;j++) { b[0][j]=-(p/q)*a[0][j]; 182
a[1][j]=a[1][j]+b[0][j]; c[0][j]=-(r/q)*a[0][j]; a[2][j]=a[2][j]+c[0][j]; } p=a[2][1]; q=a[1][1]; for(j=0;j<=3;j++) { b[i][j]=-(p/q)*a[1][j]; a[2][j]=a[2][j]+b[1][j]; } printf("\nThe matrix become\n"); for(i=0;i<3;i++) { for(j=0;j<4;j++) printf("%.4f\t",a[i][j]); printf("\n"); } z=a[2][3]/a[2][2]; y=(a[1][3]-a[1][2]*z)/a[1][1]; x=(a[0][3]-a[0][2]*z-a[0][1]*y)/a[0][0]; printf("\nThe solution \n"); printf("\nX=%f\nY=%f\nZ=%f",x,y,z); } else 183
{ printf("\nThe first coefficiemt must not be zero, Enter again\n"); goto m; } } OUTPUT :
184
Pgm No: 26 Date : 07-01-11 SIMULATE RAINDROP VIRUS (TSR) ALGORITHM: Step 1 : Start Step 2 : ticks=0 Step 3 : Make scr point to memory location 0xB8000000L; Step 4 : Store value of getvect(8) function to prev and call our function Step 5 : ticks=ticks+1 Step 6 : if ticks not equal to 91 goto 18 Step 7 : col=random(79) Step 8 : row=random(24) Step 9 : ch=*(scr+row*160+col*2) Step 10 : if row>24 goto 17 Step 11 : v=scr+row*160+col*2 Step 12 : *(v-160)=' ' Step 13 : *v=ch; Step 14 : Delay time by 200 seconds Step 15 : row=row+1 Step 16 : Goto step 10 Step 17 : ticks=0 Step 18 : Call function (*prev)() Step 19 : Store value of our function to memory location outputted by getvect(8) Step 20 : Reserve 500 blocks of memory Step 21 : Stop 185
FLOWCHART:
START
prev=getvect(8)
setvect(8,our) keep(0,500)
STOP
186
our()
ticks=ticks+1
(*prev)()
ch=*(scr+row*160+col*2)
If row<=24
ticks=0 v=scr+row*160+col*2
delay(200)
187
PROGRAM #include dos.h void interrupt our(); void interrupt (*prev)(); char far *scr=(char far*)0xB8000000L; int ticks; main() { prev=getvect(8); setvect(8,our); keep(0,500); } void interrupt our() { int col,row; char far *v,ch; ticks++; if(ticks==91) { col=random(79); row=random(24); ch=*(scr + row*160 + col*2); for(;row<=24;row++) { v = scr + row*160 + col*2; 188
189