The document describes a C program to multiply two matrices. It accepts the rows and columns of the two matrices as input and checks if multiplication is possible. It then accepts the elements of both matrices and calculates the product matrix using a nested for loop.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
77 views
C Lab Programs 2
The document describes a C program to multiply two matrices. It accepts the rows and columns of the two matrices as input and checks if multiplication is possible. It then accepts the elements of both matrices and calculates the product matrix using a nested for loop.
int i; cinEEi; ret&rn 0; 3 print sum of two complex numbers 56 The fo%%o!in9 code e*amp%e i" taken from the book 6 "The $22 /tandard Aibrar> D # T&toria% and eference" 6 b> ;ico%ai ): Lo"&tti", #ddi"onDMe"%e>, 1NNN 6 6 ($) $op>ri9ht ;ico%ai ): Lo"&tti" 1NNN: 6 7ermi""ion to cop>, &"e, modif>, "e%% and di"trib&te thi" "oft!ar e 6 i" 9ranted provided thi" cop>ri9ht notice appear" in a%% copie": 6 Thi" "oft!are i" provided "a" i"" !itho&t e*pre"" or imp%ied 6 !arrant>, and !ith no c%aim a" to it" "&itabi%it> for an> p&rpo"e: 65 <inc%&de 1io"treamE <inc%&de 1comp%e*E using name"pace "td; int main() { 56 comp%e* n&mber !ith rea% and ima9inar> part" 6 D rea% part+ K:0 6 D ima9inar> part+ J:0 65 comp%e*1%oubleE c1(K:0,J:0); co&t 11 "c1+ " 11 c1 11 end%; 56 create comp%e* n&mber from po%ar coordinate" 6 D ma9nit&de+ H:0 6 D pha"e an9%e+ 0:OH 65 comp%e*1floatE c@(po%ar(H:0,0:OH)); 56 print "&m of c1 and c@+ 6 D note+ different t>pe" 65 co&t 11 "c1 2 c@+ " 11 c1 2 comp%e*1%oubleE(c@:rea%(),c@:ima9()) 11 end%; 3 c1+ (K,J) c1 2 c@+ (O:PHQKK,P:K0Q1N) Write a program in c++ that %ivi%es two complex numbersQ class Komplex * privateH %ouble real+ imag, publicH Komplex () * real = #.#, imag = #.#, 4 Komplex (%ouble r+ %ouble i) * real = r, imag = i, 4 Komplex A%% (Komplex1 a+ Komplex1 b) * return Komplex (a.real + b.real+ a.imag + b.imag), 4 %ouble abs () * return sRrt (this2'real 3 this2'real + this2'imag 3 this2'imag), 4 Komplex ?ivision( Komplex1 a+ Komplex 1b) * write the fowrmula here 4 voi% show() * cout && real && - + - && imag && -i- &&en%l, 4 4, voi% main() * Komplex >um1(2+E), Komplex >um2(E+), Komplex 7esult, >um1.show(), >um2.show(), 7esult.A%%(>um1+>um2), 7esult.show(), 4 Write a K program to reverse the string without using strrev() functionQ <inc%&de 1"tdio:hE <inc%&de 1conio:hE <inc%&de 1"trin9:hE void main() { char "tr[10],temp; int i,%en; printf("Enter /trin9 + "); "canf(","","tr); %en0"tr%en("tr)D1; for(i00;i1"tr%en("tr)5@;i22) { temp0"tr[i]; "tr[i]0"tr[%en]; "tr[%enDD]0temp; 3 printf(","","tr); 9etch(); 3 K co%e programming to reverse a stringQ Bo& have to read a "trin9 ">mbo% b> ">mbo% in an arra> and &"in9 the arra> and on of the %oop operator" (for, !hi%e, do:::!hi%e and "o on) p&t the arra> in o&tp&t in rever"e order: <inc%&de 1"tdio:hE int main () { int i,j; char a[10]; char temp; 55c%r"cr (); 55 on%> !ork" on !indo!" 9et"(a); for (i00;a[i]80G.0G;i22); iDD; for (j00;j 10 i5@ ;j22) { temp 0 a[j]; a[j] 0 a[iDj]; a[iDj] 0 temp; 3 printf(","",a); ret&rn(0); 3 56$22 pro9ram to divide t!o comp%e* n&mber" &"in9 operator over%oadin965 <inc%&de1io"tream:hE <inc%&de1conio:hE c%a"" comp%e* { f%oat *,>; p&b%ic+ comp%e*() {3 comp%e*(f%oat rea%,f%oat im9) { *0rea%; >0im9; 3 void di"p%a>() { co&t11*11" 2 "11>11"i"11end%; 3 void operator5(comp%e*); 3; void comp%e*++operator5(comp%e* c) { comp%e* temp; 55;&merator temp:*0*6c:*2>6(Dc:>)6(D1); 55ea% n&mber of the n&merator temp:>0*6(Dc:>)2>6c:*; 554ma9inar> n&mber of the n&merator f%oat deno; 55?enominator deno0c:*6c:*Dc:>6c:>6(D1); 55/imi%ar to (a2b)(aDb)0a @ Db @ co&t11temp:*11" 2 "11temp:>11"i 5 "11deno; 3 void main() { c%r"cr(); comp%e* c1(H,K),c@(J,@); c1:di"p%a>(); c@:di"p%a>(); co&t11"?ividin9 the comp%e* n&mber""11end%; c15c@; 9etch(); 3 '=T7=T+ H 2 Ki J 2 @i ?ividin9 the comp%e* n&mber" @J 2 @i 5 1J write program for palin%rome <inc%&de1"tdio:hE <inc%&de1conio:hE void main() { int n,"00,m; c%r"cr(); printf("enter an> no"); "canf(",d",-n); m0n; !hi%e(nE0) { r0n,10; "0"6102r; n0n510; 3 if(m00n) printf("the no i" pa%indrome"); e%"e printf("no i" not pa%indrome"); 9etch(); 3 K program to chec. whether a Btring is 6alin%rome or notQ <inc%&de1"tdio:hE <inc%&de1"trin9:hE <define "iRe @P void main() { char "tr"rc["iRe]; char "trtmp["iRe]; c%r"cr(); printf(".n Enter /trin9+0 "); 9et"("tr"rc); "trcp>("trtmp,"tr"rc); "trrev("trtmp); if("trcmp("tr"rc,"trtmp)000) printf(".n Entered "trin9 .","." i"pa%indrome","tr"rc); e%"e printf(".n Entered "trin9 .","." i" not pa%indrome","tr"rc); 9etch(); 3 Write a K program to chec. whether the string is palin%rome or notQ #include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) { int strLen=,ind!" i#(argv[$]==%&LL){ return()" ' (hile((argv[$][strLen]))= *+*) { strLen,," ' #or(ind! = "ind! < strLen-."ind!,,) { i#(argv[$][ind!] )= argv[$][strLen/$/ind!]) { 0rint#(12s %ot 0alindrome+n1,argv[$])" return()" ' ' 0rint#(12s 3alindrome1,argv[$])" return " ' 4r #include<stdio.h> #include<string.h> #de#ine si5e .6 void main() { char strsrc[si5e]" char strtm0[si5e]" clrscr()" 0rint#(1+n 7nter 8tring9= 1)" gets(strsrc)" strc0:(strtm0,stru0r(strsrc))" strrev(strtm0)" i#(strcm0(strsrc,strtm0)==) 0rint#(1+n 7ntered string 12s1 is 0alindrome1,strsrc)" else 0rint#(1+n 7ntered string 12s1 is not 0alindrome1,strsrc)" getch()" ' 6rogram to Kalculate the 6ascal triangle $inclu%e&st%io.h' $inclu%e&conio.h' voi% main() * int aL1#ML1#M, int i+N+c+n, clrscr(), printf(-Anter how manP lines %o Pou want-), scanf(-0%-+1n), aL1ML1M=1, printf(-05%-+aL1ML1M), aL2ML1M=1,aL2ML2M=2,aL2MLEM=1, printf(-0% 0% 0%-+aL2ML1M+aL2ML2M+aL2MLEM), for(i=E,i&=n,i++) * aLiML1M=1, printf(-0%-+aLiML1M), N=2,c=E, while(N&=i) * aLiMLNM=aLi21MLc21M+aLi21MLc22M, printf(-05%-+aLiMLNM), c=c+1, N=N+1, 4 aLiMLNM=1, printf(-0%-+aLiMLNM), 4 4 K program to print the 6ascalWs triangle $inclu%e voi% main() * int aL1#ML1#M, int i+N+c+n, printf(-Anter how manP lines %o Pou want-), scanf(-0%-+1n), aL1ML1M=1, printf(-05%-+aL1ML1M), aL2ML1M=1,aL2ML2M=2,aL2MLEM=1, printf(-0% 0% 0%-+aL2ML1M+aL2ML2M+aL2MLEM), for(i=E,i&=n,i++) * aLiML1M=1, printf(-0%-+aLiML1M), N=2,c=E, while(N&=i) * aLiMLNM=aLi21MLc21M+aLi21MLc22M, printf(-05%-+aLiMLNM), c=c+1, N=N+1, 4 aLiMLNM=1, printf(-0%-+aLiMLNM), 4 4 @ow to write c co%e to generate pascal triangleQ @i the waP u explaine% the logic an% the program You have %one is+ perfect.Jut Nus two things You nee% to concentrate for printing the pPrami% in a right shape.= have inclu%e% changes ..@ope it helps You. $inclu%e&st%io.h' $inclu%e&conio.h' /3program to generate pascal triangle+ no. of lines r to be entere% bP the user3/ voi% main() * int a+b+c+i+N+n+r+s+ncr+factr+factn+factnr+line, printf(-please enter the number of lines of the pascal triangle/n-), scanf(-0%-+1line), //inputs the no. of lines we want clrscr(), for(i=#,i&=line21,i++)//outer loop for the value of n * factn=1,// reinitialiVing the factorial values to avoi% garbage values... factr=1, factnr=1, for(s=line21,s'=i,s22) 55 L&"t 4nc%&ded a (or %oop T' have %eadin9 /pace" * printf(- -), 4 for(N=#,N&=i,N++) //outer loop for the values of r * n=i, //here we have assigne% i to n an% N to r for our nKr calculation an% convinience r=N, factn=1, // again we have re initialise% factorial values to avoi% garbage values factr=1, factnr=1, if(n==#) //we .now factorial of # =1 factn=1, else * for(a=n,a'=1,a22) //factorial calculation of n is n!=# factn=factn3a, 4 if(r==#) factr=1, else * for(b=r,b'=1,b22) //factorial calculation of r if r!=# factr=factr3b, 4 if((n2r)==#) factnr=1, else * for(c=(n2r),c'=1,c22) //factorial calculation of n2r if (n2r)!=# factnr=factnr3c, 4 ncr=factn/(factr3factnr), //ncr calculation printf(-02%-+ncr), 55emove that tab and S4ven @d to have "pace" bet!een each character" and print" the va%&e of ncr 4 printf(-/n-), //cursor get to the next line when the control exits the inner loop 4 4 K language program to print 6ascal ;riangleQ <inc%&de 1io"tream:hE do&b%e fact(do&b%e n) { ret&rn (n E 1) T n 6 fact(n D 1) + 1; 3 do&b%e ncr(int n, int r) { ret&rn fact(n) 5 (fact(r) 6 fact(n D r)); 3 void main() { for (int i 0 0; i 1 1H; i22) { for (int j 0 0; j 10 i; j22) co&t 11ncr(i, j) 11 G G; co&t 11 end%; 3 3 K program of matrix a%%ition+substraction+an% multiplication /3 6rogram of a%%ition+substraction+an% multiplication of matrix 3/ $inclu%e $inclu%e voi% rea%Tmat(int m+int n+int aL5ML5M) * int i+N, for(i=#,i * for(N=#,N * printf(-/n Anter element H-), scanf(-0%-+1aLiMLNM), 4 4 voi% printTmat(int m+int n+int aL5ML5M) * int i+N, for(i=#,i * for(N=#,N printf(- 0%-+aLiMLNM), printf(-/n-), 4 4 voi% a%%Tmat(int m+int n+int aL5ML5M+int bL5ML5M+int sL5ML5M) * int i+N, for(i=#,i * for(N=#,N * sLiMLNM=aLiMLNM+bLiMLNM, 4 4 4 voi% subTmat(int m+int n+int aL5ML5M+int bL5ML5M+int s%L5ML5M) * int i+N, for(i=#,i * for(N=#,N * s%LiMLNM=aLiMLNM2bLiMLNM, 4 4 4 voi% mulTmat(int m+int n+int R+int aL5ML5M+int bL5ML5M+int mtL5ML5M) * int i+N+., for(i=#,i * for(N=#,N * mtLiMLNM=#, for(.=#,. * mtLiMLNM=mtLiMLNM+aLiML.M3bL.MLNM, 4 4 4 4 voi% main() * int m+n+p+R+i+N+aL5ML5M+bL5ML5M+sL5ML5M+s%L5ML5M+mtL5ML5M, printf(-/n Anter or%er of first matrix H-), scanf(-0%0%-+1m+1n), printf(-/n 8r%er of secon% matrix H-), scanf(-0%0%-+1p+1R), if((m!=p)11(n!=R)) printf(-/n :atrices are imcompatable for a%%otion an% substraction-), else * printf(-/n 7ea% the element of first matrix -), rea%Tmat(m+n+a), printf(-/n 7ea% the element of secon% matrix -), rea%Tmat(p+R+b), printf(-/n <=7B; :A;7=D =B ....../n-), printTmat(m+n+a), printf(-/n BAK8>? :A;7=D =B ..../n-), printTmat(p+R+b), a%%Tmat(m+n+a+b+s), printf(-/n A<;A7 A??=;=8> :A;7=D =B ..../n-), printTmat(m+n+s), subTmat(m+n+a+b+s%), printf(-/n A<;A7 BCJB;7AK;=8> :A;7=D =B ..../n-), printTmat(m+n+s%), 4 if(p==R) * mulTmat(m+n+R+a+b+mt), printf(-/n A<;A7 :CG;=6G=KA;=8> :A;7=D =B ..../n-), printTmat(m+R+mt), 4 else printf(-/n :atrices are incompatible for multiplication -), getch(), 4 8r a11 a1@ a1J #11 #1@ #1J a11*#112a1@*#@12a1J*#J1 a11*#1@2a1@*#@@2a1J*#J@ a@1 a@@ a@J * #@1 #@@ #@J 0 a@1*#112a@@*#@12a@J*#J1 a@1*#1@2a@@*#@@2a@J*#J@ aJ1 aJ@ aJJ #J1 #J@ #JJ aJ1*#112aJ@*#@12aJJ*#J1 :ore specific %etails are bePon% the scope of this article but Pou can refer :atrix multiplication in wi.ipe%ia. The $ode Ko%eH K void main() { int m1[10][10],i,j,k,m@[10][10],add[10][10],m&%t[10][10],r1,c1,r@,c@; printf("Enter n&mber of ro!" and co%&mn" of fir"t matri* )#C 10/n"); "canf(",d,d",-r1,-c1); printf("Enter n&mber of ro!" and co%&mn" of "econd matri* )#C 10/n"); "canf(",d,d",-r@,-c@); if(r@00c1) { printf("Enter ro!" and co%&mn" of (ir"t matri* /n"); printf("o! !i"e/n"); for(i00;i1r1;i22) { for(j00;j1c1;j22) "canf(",d",-m1[i][j]); 3 printf("Bo& have entered the fir"t matri* a" fo%%o!"+/n"); for(i00;i1r1;i22) { for(j00;j1c1;j22) printf(",d/t",m1[i][j]); printf("/n"); 3 printf("Enter ro!" and co%&mn" of /econd matri* /n"); printf("#9ain ro! !i"e/n"); for(i00;i1r@;i22) { for(j00;j1c@;j22) "canf(",d",-m@[i][j]); 3 printf("Bo& have entered the "econd matri* a" fo%%o!"+/n"); for(i00;i1r@;i22) { for(j00;j1c@;j22) printf(",d/t",m@[i][j]); printf("/n"); 3 if(r100r@--c100c@) { printf(";o! !e add both the above matri* /n"); printf("The re"&%t of the addition i" a" fo%%o!";/n"); for(i00;i1r1;i22) { for(j00;j1c1;j22) { add[i][j]0m1[i][j]2m@[i][j]; printf(",d/t",add[i][j]); 3 printf("/n"); 3 3 e%"e { printf("#ddition cannot be done a" ro!" or co%&mn" are not eq&a%/n"); 3 printf(";o! !e m&%tip%> both the above matri* /n"); printf("The re"&%t of the m&%tip%ication i" a" fo%%o!"+/n"); -*a$$!;$$,a$.!;.$,a$<!;<$ a$$!;$.,a$.!;..,a$<!;<. a$$!;$<,a$.!;.<,a$<!;<<*- for(i00;i1r1;i22) { for(j00;j1c@;j22) { m&%t[i][j]00; for(k00;k1r1;k22) { m&%t[i][j]20m1[i][k]6m@[k][j]; -*mult[][]=m$[][]*m.[][],m$[][$]*m.[$][],m$[][.]*m.[.] []"*- 3 printf(",d/t",m&%t[i][j]); 3 printf("/n"); 3 9etch(); 3 e%"e { printf(")atri* m&%tip%ication cannot be done"); 3 3 K program for an Armstrong number voi% main() * int n+b=#+t, clrscr(), printf(XAnter the noY), scanf(X0%Y+1n), t=n, while(n'#) * a=n01#, b=b+a3a3a, n=n/1#, 4 if(b==t) * printf(XArmstrong noY), 4 else * printf(X>ot an armstrong noY), 4 getch(), 4 write a program to fin% a given number is arms... main() { int amm 0 temp 0 n *; printf( enter thea%&e of n ); "canf( d -n); * n; for(;nE0;) { temp n 10; amm amm2(temp6temp6temp); n n510; 3 if(* n) { printf( TUE S4VE; ;=)IE 4/ #)/T';S ;=)IE ); 3 e%"e printf( ;'T #)/T';S ;=)IE ); 3 Write a program to print all Armstrong number between 1 an% 5## =f sum of cubes of each %igit of the number it self then the number is calle% an Armstrong numberQ 567ro9ram to find arm"tron9 n&mber bet!een 1 to H0065 int main() { int i,a,b,c,*,>; c%r"cr(); for (i00;i10H00;i22) { c0i,10; *0i510; b0*,10; *0*510; a0*,10; d0(a6a6a)2(b6b6b)2(c6c6c); if (i00d) printf(",d.n",i); 3 9etch(); 3 Armstrong number program in c languageQ <inc%&de1"tdio:hE <inc%&de1conio:hE void main() { int n,m,"&00,b; c%r"cr(); printf("enter the n&mber to find am"tron9+"); "canf(",d",-n); b0n; !hi%e(nE0) { m0n,10; "&0"&2(m6m6m); n0n510; 55printf(",d","); 3 if(b00"&) printf(" am"tron9 no i" ,d",b); e%"e printf("not am"tron9"); 9etch(); 3 Write a program in c to sort an unsorte% arraP using bubble sort metho%Q <inc%&de1"tdio:hE <inc%&de1conio:hE main() { int arr[H],temp,i,j; c%r"cr(); printf(".n.tEnter The Va%&e" into ##B "); for(i00;i1H;i22) { printf(".n.n Enter E%ement no ,d+ ",i21); "canf(",d",-arr[i]); 3 for(i00;i1H;i22) { for(j01;j10K;j22) { if(arr[j] Ea[j21]) { temp0arr[j]; arr[j]0arr[j21]; arr[j21]0temp; 3 3 3 printf(".n.nDD /orted /erie" DD"); for(i00;i10K;i22) { printf(".n .n .t ,d",arr[i]); 3 9etch(); 3 6rogram to =mplement Jubble Bort ;echniRue. $inclu%e&st%io.h' $inclu%e&conio.h' voi% bubble(int aLM+int n) * int i+N+t, for(i=n22,i'=#,i22) * for(N=#,N&=i,N++) * if(aLNM'aLN+1M) * t=aLNM, aLNM=aLN+1M, aLN+1M=t, 4 4
4//en% for 1. 4//en% function. voi% main() * int aL1##M+n+i, clrscr(), printf(-/n/n Anter integer value for total no.s of elements to be sorte%H -), scanf(-0%-+1n), for( i=#,i&=n21,i++) * printf(-/n/n Anter integer value for element no.0% H -+i+1), scanf(-0%-+1aLiM), 4 bubble(a+n), printf(-/n/n <inallP sorte% arraP isH -), for( i=#,i&=n21,i++) printf(-0E%-+aLiM), 4 //en% program. /3 22222222BA:6GA 8C;6C;2222222222222222222222 Anter integer value for total no.s of elements to be sorte%H ! Anter integer value for element no.1 H IO Anter integer value for element no.2 H 2 Anter integer value for element no.E H 2!F Anter integer value for element no. H 5 Anter integer value for element no.5 H FI Anter integer value for element no.! H 11 <inallP sorte% arraP isH 2!F 2 5 11 FI IO 222222222222222222222222222222222222222222 3/ ;he simplest sorting algorithm is bubble sort. ;he bubble sort wor.s bP iterating %own an arraP to be sorte% from the first element to the last+ comparing each pair of elements an% switching their positions if necessarP. ;his process is repeate% as manP times as necessarP+ until the arraP is sorte%. Bince the worst case scenario is that the arraP is in reverse or%er+ an% that the first element in sorte% arraP is the last element in the starting arraP+ the most exchanges that will be necessarP is eRual to the length of the arraP. @ere is a simple exampleH 9iven an arraP 2E15 a bubble sort woul% lea% to the following seRuence of partiallP sorte% arraPsH 21E5+ 21E5+ 12E5. <irst the 1 an% E woul% be compare% an% switche%+ then the an% 5. 8n the next pass+ the 1 an% 2 woul% switch+ an% the arraP woul% be in or%er. binarP search program in c language $inclu%e $inclu%e voi% main() * int arraPL1#M, int i+ N+ >+ temp+ .ePnum, int low+mi%+high, clrscr(), printf(-Anter the value of >/n-), scanf(-0%-+1>), printf(-Anter the elements one bP one/n-), for(i=#, i * scanf(-0%-+1arraPLiM), 4 printf(-=nput arraP elements/n-), for(i=#, i * printf(-0%/n-+arraPLiM), 4 /3 Jubble sorting begins 3/ for(i=#, i& > , i++) * for(N=#, N& (>2i21) , N++) * if(arraPLNM ' arraPLN+1M) * temp = arraPLNM, arraPLNM = arraPLN+1M, arraPLN+1M = temp, 4 4 4 printf(-Borte% arraP is.../n-), for(i=#, i * printf(-0%/n-+arraPLiM), 4 printf(-Anter the element to be searche%/n-), scanf(-0%-+ 1.ePnum), /3 JinarP searching begins 3/ low=1, high=>, %o * mi%= (low + high) / 2, if ( .ePnum & arraPLmi%M ) high = mi% 2 1, else if ( .ePnum ' arraPLmi%M) low = mi% + 1, 4 while( .ePnum!=arraPLmi%M 11 low &= high), /3 An% of %o2 while 3/ if( .ePnum == arraPLmi%M ) * printf(-BCKKABB<CG BAA7K@/n-), 4 else * printf(-Bearch is <A=GA?/n-), 4 4 /3 An% of main3/ J=>A7Y BAA7K@ CB=>9 K 67897A: voi% main() * int aL1#M+i+n+m+c+l+u, clrscr(), printf(-Anter the siVe of an arraP2'-), scanf(-0%-+1n), printf(-/nAnter the elements of the arraP2'-), for(i=#,i-), for(i=#,i-), scanf(-0%-+1m), l=#+u=n21, c=binarP(a+n+m+l+u), if(c==#) printf(-/n;he number is not in the list-), else printf(-/n;he number is foun%-), getch(), 4 int binarP(int aLM+int n+int m+int l+int u) * int mi%+c=#, if(l&=u) * mi%=(l+u)/2, if(m==aLmi%M) * c=1, 4 else if(m * return binarP(a+n+m+l+mi%21), 4 else return binarP(a+n+m+mi%+1+u), 4 else return c, 4 C PROGRAM FOR BINARY SEARCH void main() { int a[10],i,n,m,c,%,&; c%r"cr(); printf("Enter the "iRe of an arra>DE"); "canf(",d",-n); printf(".nEnter the e%ement" of the arra>DE"); for(i00;i { "canf(",d",-a[i]); 3 printf(".nThe e%ement" of an arra> areDE"); for(i00;i { printf(" ,d",a[i]); 3 printf(".nEnter the n&mber to be "earchDE"); "canf(",d",-m); %00,&0nD1; c0binar>(a,n,m,%,&); if(c000) printf(".nThe n&mber i" not in the %i"t"); e%"e printf(".nThe n&mber i" fo&nd"); 9etch(); 3 int binar>(int a[],int n,int m,int %,int &) { int mid,c00; if(%10&) { mid0(%2&)5@; if(m00a[mid]) { c01; 3 e%"e if(m { ret&rn binar>(a,n,m,%,midD1); 3 e%"e ret&rn binar>(a,n,m,mid21,&); 3 e%"e ret&rn c; 3 Iinar> /earch $ 7ro9ram $inclu%e &st%io.h' $%efine ;7CA # $%efine <AGBA 1 int main(voi%) * int arraPL1#M = *1+ 2+ E+ + 5+ !+ F+ I+ O+ 1#4, int left = #, int right = 1#, int mi%%le = #, int number = #, int bsearch = <AGBA, int i = #, printf(-A77AYH -), for(i = 1, i &= 1#, i++) printf(-L0%M -+ i), printf(-/nBearch for >umberH -), scanf(-0%-+ 1number), while(bsearch == <AGBA 11 left &= right) * mi%%le = (left + right) / 2, if(number == arraPLmi%%leM) * bsearch = ;7CA, printf(-33 >umber <oun% 33/n-), 4 else * if(number & arraPLmi%%leM) right = mi%%le 2 1, if(number ' arraPLmi%%leM) left = mi%%le + 1, 4 4 if(bsearch == <AGBA) printf(-22 >umber >ot foun% 22/n-), return #, 4 K program that implement :erge sort to sort a given list of integers in ascen%ing or%erH $inclu%e $inclu%e $%efine :ADTA7Y 1# voi% mergeTsort(int xLM+ int en%+ int start), int main(voi%) * int arPL:ADTA7YM, int N = #, printf(-/n/nAnter the elements to be sorte%H /n-), for(N=#,N&:ADTA7Y,Z++) scanf(-0%-+1arPLNM), /3 arraP before mergesort 3/ printf(-Jefore H-), for(N = #, N & :ADTA7Y, N++) printf(- 0%-+ arPLNM), printf(-/n-), mergeTsort(arP+ #+ :ADTA7Y 2 1), /3 arraP after mergesort 3/ printf(-After :erge Bort H-), for(N = #, N & :ADTA7Y, N++) printf(- 0%-+ arPLNM), printf(-/n-), getch(), 4 /3 :etho% to implement :erge Bort3/ voi% mergeTsort(int xLM+ int en%+ int start) * int N = #, const int siVe = start 2 en% + 1, int mi% = #, int mrg1 = #, int mrg2 = #, int executingL:ADTA7YM, if(en% == start) return, mi% = (en% + start) / 2, mergeTsort(x+ en%+ mi%), mergeTsort(x+ mi% + 1+ start), for(N = #, N & siVe, N++) executingLNM = xLen% + NM, mrg1 = #, mrg2 = mi% 2 en% + 1, for(N = #, N & siVe, N++) * if(mrg2 &= start 2 en%) if(mrg1 &= mi% 2 en%) if(executingLmrg1M ' executingLmrg2M) xLN + en%M = executingLmrg2++M, else xLN + en%M = executingLmrg1++M, else xLN + en%M = executingLmrg2++M, else xLN + en%M = executingLmrg1++M, 4 4 )er9e /ort $ 7ro9ram $inclu%e &st%io.h' $inclu%e &st%lib.h' struct no%e * int number, struct no%e 3next, 4, /3 a%% a no%e to the lin.e% list 3/ struct no%e 3a%%no%e(int number+ struct no%e 3next), /3 preform merge sort on the lin.e% list 3/ struct no%e 3mergesort(struct no%e 3hea%), /3 merge the lists.. 3/ struct no%e 3merge(struct no%e 3hea%Tone+ struct no%e 3hea%Ttwo), int main(voi%) * struct no%e 3hea%, struct no%e 3current, struct no%e 3next, int testLM = *I+ E+ 2+ !+ 1+ 5+ + F+ O+ #4, int i, hea% = >CGG, /3 insert some numbers into the lin.e% list 3/ for(i = #, i & 1#, i++) hea% = a%%no%e(testLiM+ hea%), /3 sort the list 3/ hea% = mergesort(hea%), /3 print the list 3/ printf(- before after/n-)+ i = #, for(current = hea%, current != >CGG, current = current2'next) printf(-0%/t0%/n-+ testLi++M+ current2'number), /3 free the list 3/ for(current = hea%, current != >CGG, current = next) next = current2'next+ free(current), /3 %one... 3/ return #, 4 /3 a%% a no%e to the lin.e% list 3/ struct no%e 3a%%no%e(int number+ struct no%e 3next) * struct no%e 3tno%e, tno%e = (struct no%e3)malloc(siVeof(3tno%e)), if(tno%e != >CGG) * tno%e2'number = number, tno%e2'next = next, 4 return tno%e, 4 /3 preform merge sort on the lin.e% list 3/ struct no%e 3mergesort(struct no%e 3hea%) * struct no%e 3hea%Tone, struct no%e 3hea%Ttwo, if((hea% == >CGG) [[ (hea%2'next == >CGG)) return hea%, hea%Tone = hea%, hea%Ttwo = hea%2'next, while((hea%Ttwo != >CGG) 11 (hea%Ttwo2'next != >CGG)) * hea% = hea%2'next, hea%Ttwo = hea%2'next2'next, 4 hea%Ttwo = hea%2'next, hea%2'next = >CGG, return merge(mergesort(hea%Tone)+ mergesort(hea%Ttwo)), 4 /3 merge the lists.. 3/ struct no%e 3merge(struct no%e 3hea%Tone+ struct no%e 3hea%Ttwo) * struct no%e 3hea%Tthree, if(hea%Tone == >CGG) return hea%Ttwo, if(hea%Ttwo == >CGG) return hea%Tone, if(hea%Tone2'number & hea%Ttwo2'number) * hea%Tthree = hea%Tone, hea%Tthree2'next = merge(hea%Tone2'next+ hea%Ttwo), 4 else * hea%Tthree = hea%Ttwo, hea%Tthree2'next = merge(hea%Tone+ hea%Ttwo2'next), 4 return hea%Tthree, 4 Write a K program to convert %ecimal number into binarP numberQ have here co%e to convert %ecimal to binarP number+ octal an% hexa in K..he re is the co%eH $inclu%e &st%io.h' voi% main() * voi% welcome(), voi% co%e(), int %, int i=#+n+N+bL1##M, printf(-/n/n/t/tKhooseH/n-), printf(-/n/n/t/tL1M ?ecimal to JinarP/n-), printf(-/t/tL2M ?ecimal to 8ctal/n-), printf(-/t/tLEM ?ecimal to @exa%ecimal/n-), printf(-/t/tLM Axit/n-), printf(-/n/n/t/tAnter Pour choiceH -), scanf(-0%-+ 1%), switch(%) * case 1H printf(-/n/t/tAnter %ecimal numberH -), scanf(-0%-+ 1n), while (n'#) * bLiM=n02, n=n/2, i++, 4 printf(-/n/t/tJinarP isH -), N=i21, for (i=N,N'=#,N22) * printf(-0%-+ bLNM), 4 brea., case 2H printf(-/n/t/tAnter %ecimal numberH -), scanf(-0%-+ 1n), while (n'#) * bLiM=n0I, n=n/I, i++, 4 printf(-/n/t/t8ctal isH-), N=i21, for (i=N,N'=#,N22) * printf(-0%-+ bLNM), 4 brea., case EH printf(-/n/t/tAnter %ecimal numberH -), scanf(-0%-+ 1n), while (n'#) * bLiM=n01!, n=n/1!, i++, 4 printf(-/n/t/t@exa%ecimal isH-), N=i21, for (i=N,N'=#,N22) * printf(-0%-+ bLNM), if(bLNM&1#) * printf(-0%-+ bLNM), 4 else * switch(bLNM) * case 1#H printf(-A-), brea., case 11H printf(-J-), brea., case 12H printf(-K-), brea., case 1EH printf(-?-), brea., case 1H printf(-A-), brea., case 15H printf(-<-), brea., 4 4 4 4 4 1: 55 $onvert a decima% inte9er do a binar> "trin9 @: 55 added a te"t printf() >o& can remove %ater J: 55 T&rbo $ modified for 7e%%e" $ ve9a"eat 1Nnov@00K K: H: <inc%&de 1"tdio:hE P: F. voi% dec@bin(long decima%, char 6binar>); Q: O. int main() 1#. * 11. long decima%; 12. char binar>LQ0M; 1J: 1. printf("/n/n Enter an inte9er va%&e + "); 15. "canf(",%d",-decima%); 1!. dec@bin(decima%,binar>); 1F. printf("/n The binar> va%&e of ,%d i" ," /n",decima%,binar>); 1Q: 1O. 9etchar(); 55 trap enter 2#. 9etchar(); 55 !ait 21. return 0; 22. 4 @J: @K: 55 @H: 55 accept" a decima% inte9er and ret&rn" a binar> coded "trin9 @P: 55 2F. voi% dec@bin(long decima%, char 6binar>) 2I. * 2O. int k 0 0, n 0 0; E#. int ne9Ff%a9 0 0; E1. int remain; E2. int o%dFdecima%; 55 for te"t EE. char tempLQ0M; JK: JH: 55 take care of ne9ative inp&t E!. if (decima% 1 0) EF. * JQ: decima% 0 Ddecima%; JN: ne9Ff%a9 0 1; #. 4 1. %o 2. * KJ: o%dFdecima% 0 decima%; 55 for te"t KK: remain 0 decima% , @; KH: 55 !hitt%e do!n the decima% n&mber KP: decima% 0 decima% 5 @; KO: 55 thi" i" a te"t to "ho! the action I. printf(",d5@ 0 ,d remainder 0 ,d/n", o%dFdecima%, decima%, remain); KN: 55 convert" di9it 0 or 1 to character G0G or G1G 5#. tempLk22M 0 remain 2 G0G; 51. 4 while (decima% E 0); H@: 5E. if (ne9Ff%a9) 5. tempLk22M 0 GDG; 55 add D "i9n 55. else 5!. tempLk22M 0 G G; 55 "pace HO: HQ: 55 rever"e the "pe%%in9 5O. while (k E0 0) !#. binar>Ln22M 0 tempLDDkM; P1: !2. binar>LnD1M 0 0; 55 end !ith ;=AA !E. 4 Write a program to convert the binarP to %ecimal conversion bP using while statementQ $inclu%e&st%io.h' $inclu%e&conio.h' $inclu%e&math.h' voi% main() * long int aL2#M+i+n+count=#+bL2#M+cL2#M+sum=#, printf(-A>ter the number in binarP form=/t-), scanf(-0l%-+1n), // 9et a binarP number from the user for (i=#,n'=1,i++) * aLiM=n01#, n=n/1#, // Goop ;o reverse the number An% put all reverse% numbers in arrP aLiM count=count + 1, // count to count the number of times this loop runs 4 for (i=#,i&=count21,i++) // count 21 con%ition is use% to run the loop till the previous loop run * bLiM=pow(2+i), // ;his is to raise the power of 2 to no of times previous loop runne%. 4 for (i=#,i&=count21,i++) * cLiM=aLiM 3 bLiM, // :ultiplP aLiM or reverese% binarP no with bLiM or increasing pow of 2 to count21 sum=sum +cLiM, // it is to a%% the cLiM elements with each other n put into sum variable. 4 printf(-?ecimal form =0l%-+sum), // printing the sum to get the %ecimal form getch(), 4 K programming co%e for newton raphson metho% to solve Rua%ratic eRuationQ >e" Thi" i" the code" for ;e!tonDaph"on method for "o%vin9 W&adratic eq&ation" <inc%&de1"tdio:hE <inc%&de1conio:hE <inc%&de1math:hE <define ((*)(*6*6*)D(K6*)N <define (?(*)(J6*6*)DK <define )#C4T @0 void main() { int co&nt; f%oat *0,*1,f*,fd*; c%r"cr(); printf(";EMT';D#7U/'; )ETU'?.n"); printf("DDDDDDDDDDDDDDDDDDDDD.n"); printf("initia% va%&e+"); "canf(",f",*0); co&nt01; be9in+ f*0((*0); fd*0(?(*0); *10(*0D(f*5fd*)); if(fab"((*1D*0)5*1)10:00001) { printf("The root i"+,f.n",*1); printf("4teration i"+,d.n",co&nt); 3 e%"e { *00*1; co&nt0co&nt21; if((co&nt1)#C4T)); 9oto be9in; 3 9etch(); 3 3 6rogram to fin% the roots of an ARuation f(x)=# using >ewton 2 7aphson :etho% 3/ $inclu%e $inclu%e $inclu%e float f(float), float %f(float), voi% main() * float x#+x1+%+eps, int i+n, clrscr(), printf(-/n=nput the intial values H -), scanf(-0f-+1x#), printf(-/nAnter the number of =terations -), scanf(-0%-+1n), printf(-/n=nput the error of tolerance/n-), scanf(-0f-+1eps), printf(-=teration/t x#/t/t x1-), for(i=#,i&>,=++) * x1=x#2f(x#)/%f(x#), printf(-/n/t 0%/t0f/t0f/n-+i+x#+x1), %=x12x#, x#=x1, if(%==eps) * brea., 4 4 printf(-/n7oots of given eRuation = 0f-+x#), getch(), 4 float f(float x) * float f, f=cos(x)2x3exp(x), return(f), 4 float %f(float x) * float %f, %f=sin(x)2x3exp(x)2exp(x), return(%f), 4 What is the c programming co%e for newton raphson eRuationQ 56pro9ramm for modified ne!ton raph"on method65 #include<stdio.h> #include<math.h> #define epsilon 1e-6 main() { /*This is a programm ased on modified ne!ton raphson method to find out root of the e"uation #$%-&*#$'()*#-%*+*/ doule g,g1,-,-1,-',#,d#. int con-erged*+,i. printf(/pl0 enter the guess -alue1n/). scanf(/2f/,3g1). i*1. !hile (con-erged**+) { printf(/1n iteration no*2d1n/,i). -*g1*g1*g1-&*g1*g1()*g1-%. printf(/-*2lf1n/,-). -1*%*g1*g1-1+*g1(). printf(/-1*2lf1n/,-1). -'*6*g1-1+. printf(/-'*2lf1n/,-'). #*(-*-1)/(-1*-1--*-'). printf(/-alue*2lf1n/,#). g*g1-((-*-1)/(-1*-1--*-')). printf(/ne! guess is*2lf1n/,g). d#*((g-g1)/g). printf(/error*2lf1n/,d#). g1*g. if (fas(d#)4less than4epsilon) {con-erged*1.5 printf(/the root of e"uation is*2lf i*2d1n/,g,i). i*i(1. 5 5 6rg. to convert upper case to lower case or lower case to upper case %epen%ing on the name it is invo.e% with as foun% in argument. <inc%&de 1"tdio:hE <inc%&de 1conio:hE void %o!erFtoF&pper(); void &pperFtoF%o!er(); void main() { int n; c%r"cr(); printf(" 7%ea"e enter >o&r choice:"); printf(" (1) for &pper to %o!er conver"ion:"); printf(" (@) for %o!er to &pper conver"ion:"); printf(" $U'4$E+D "); "canf(",d",-n); "!itch (n) { ca"e 1+ { printf("7%ea"e enter a "trin9 in &pper ca"e:"); printf(" /trin9 !i%% be terminated if >o& pre"" $tr%DX:"); printf(" /T4;S+D "); &pperFtoF%o!er(); break; 3 ca"e @+ { printf("7%ea"e enter a "trin9 in %o!er ca"e:"); printf(" /trin9 !i%% be terminated if >o& pre"" $tr%DX:"); printf(" /T4;S+D "); %o!erFtoF&pper(); break; 3 defa&%t+ printf("E'"); 3 printf(" U#VE # ;4$E ?#B8 IBE:"); 9etch(); 3 void &pperFtoF%o!er() { int i,j; char cK[Q0],cJ; for (i00;(cJ09etchar())80E'(;i22) cK[i]0(cJE0G#G -- cJ10GXG)T(GaG 2 cJ DG#G)+cJ; printf(" The %o!er ca"e eq&iva%ent i" "); for (j00;j1i;j22) p&tchar(cK[j]); ret&rn; 3 void %o!erFtoF&pper() { int i,j; char c@[Q0],c1; for (i00;(c109etchar())80E'(;i22) c@[i]0(c1E0GaG -- c110GRG)T(G#G 2 c1 DGaG)+c1; printf(" The &pper ca"e eq&iva%ent i" "); for (j00;j1i;j22) p&tchar(c@[j]); ret&rn; 3 Konvert string to upper case an% lower case $inclu%e &ctPpe.h' $inclu%e &st%io.h' int main(voi%) * char strLI#M, int i, printf(-Anter a stringH -), gets(str), for( i = #, strL i M, i++) strL i M = toupper( strL i M ), printf(-0s/n-+ str), /3 uppercase string 3/ for(i = #, strL i M, i++) strLiM = tolower(strL i M), printf(-0s/n-+ str), /3 lowercase string 3/ return #, } @ow %o Pou write a K program to input a string of lowercase alphabets an% convert it to uppercaseQ Uere i" pro9ram code !ritten in p%ain $+ <inc%&de 1"tdio:hE <inc%&de 1ct>pe:hE void &p/trin9(char 6"tr); int main() { char "tr[100]; printf("Enter "trin9+ "); 9et"("tr); &p/trin9("tr); printf("=pper$a"e ver"ion+ ,".n", "tr); ret&rn 0; 3 void &p/trin9(char 6"tr) { re9i"ter int ind 0 0; !hi%e ("tr[ind]) { "tr[ind] 0 to&pper("tr[ind]); ind22; 3 3 K <unction2Konvert ;o CpperKase 2 K 6rogramming
566666666666666666666666666666666 6 $onvert the to =pper$a"e 6 6666666666666666666666666666666665 void $onvertTo=pper$a"e(char 6 "tr) { int ch, i; for(i00;i 1 "tr%en("tr); i22) { ch 0 to&pper("tr[i]); "tr[i] 0 ch; 3 3 Write a c program to convert upper case letter to lower case letter without using librarP functionQ <inc%&de1"tdio:hE <inc%&de1conio:hE void main() { char ch; c%r"cr(); printf("Enter a $haracter"); "canf(",c",-ch); if(chE0PH -- ch10N0) ch0ch2J@; printf("=pper $a"e 0,c",ch); 9etch(); 3 K pgm to convert lower case to upper case an% upper case ... $inclu%e &st%io.h' $inclu%e &st%lib.h' /3 3 >otice that 3/ int Ttolower ( int ch ) * unsigne% char Tch = (unsigne% char)ch '' , /3 =f K@ is alrea%P in lower case+ return it 3/ if ( Tch == #x! [[ Tch == #xF ) return ch, /3 convert it to lower case 3/ if ( Tch == #x ) return (int)( ( (unsigne% char)ch 1 #x#f ) [ #x!# ), else if ( Tch == #x5 ) return (int)( ( (unsigne% char)ch 1 #x#f ) [ #xF# ), else return ch, 4 int Ttoupper ( int ch ) * unsigne% char Tch = (unsigne% char)ch '' , /3 =f K@ is alrea%P in upper case+ return it 3/ if ( Tch == #x [[ Tch == #x5 ) return ch, /3 convert it to upper case 3/ if ( Tch == #x! ) return (int)( ( (unsigne% char)ch 1 #x#f ) [ #x# ), else if ( Tch == #xF ) return (int)( ( (unsigne% char)ch 1 #x#f ) [ #x5# ), else return ch, 4 int main ( voi% ) * printf ( -0c/n-+ Ttoupper ( U+U ) ), printf ( -0c/n-+ Ttoupper ( UrU ) ), printf ( -0c/n-+ Ttoupper ( UlU ) ), printf ( -0c/n-+ Ttoupper ( U6U ) ), printf ( -0c/n-+ Ttolower ( U\U ) ), printf ( -0c/n-+ Ttolower ( U7U ) ), printf ( -0c/n-+ Ttolower ( U8U ) ), printf ( -0c/n-+ Ttolower ( UOU ) ), return #, 4 K program to implement the linear regression algorithm $inclu%e $inclu%e $inclu%e $inclu%e float mean(float 3a+ int n), voi% %eviation(float 3a+ float mean+ int n+ float 3%+ float 3B), voi% main() * float aL2#M+bL2#M+%xL2#M+%PL2#M, float sP=#+sx=#+meanTx=#+meanTP=#+sumTxP=#, float corrTcoff=#+regTcoffTxP=#+ regTcoffTPx=#, char tPpeTcoffLFM, int n=#+i=#, clrscr(), printf(-Anter the value of nH -), scanf(-0%-+1n), printf(-Anter the values of x an% PH/n-), for(i=#,i&>,=++) scanf(-0f0f-+1aLiM+1bLiM), meanTx=mean(a+n), meanTP=mean(b+n), %eviation(a+meanTx+n+%x+1sx), %eviation(b+meanTP+n+%P+1sP), for(i=#,i&>,=++) sumTxP=sumTxP+%xLiM3%PLiM, corrTcoff=sumTxP/(n3sx3sP), printf(-Anter the tPpe of regression coefficient as Ux on PU or UP on xUH -), fflush(st%in), gets(tPpeTcoff), if(strcmp(tPpeTcoff+-x on P-)==1) * regTcoffTxP=corrTcoff3(sx/sP), printf(-/n;he value of linear regression coefficient is 0f-+regTcoffTxP), 4 else if(strcmp(tPpeTcoff+-P on x-)==1) * regTcoffTPx=corrTcoff3(sP/sx), printf(-/n;he value of linear regression coefficient is 0f-+regTcoffTPx), 4 else printf(-/nAnter the correct tPpe of regression coefficient.-), getch(), 4 float mean(float 3a+ int n) * float sum=#+ i=#, for(i=#,i&>,=++) sum=sum+aLiM, sum=sum/n, return (sum), 4 voi% %eviation(float 3a+ float mean+ int n+ float 3%+ float 3s) * float sum=#+t=#, int i=#, for(i=#,i&>,=++) * %LiM=aLiM2mean, t=%LiM3%LiM, sum=sum+t, 4 sum=sum/n, 3s=sRrt(sum), 4 K program that implement =nsertion sort to sort a given list of integers in ascen%ing or%er $inclu%e $inclu%e voi% instTsort(int LM), voi% main() * int numL5M+count, clrscr(), printf(-/nAnter the <ive Alements to sortH/n-), for (count=#,count&5,count++) scanf(-0%-+1numLcountM), instTsort(num), printf(-/n/nAlements after sortingH /n-), for(count=#,count&5,count++) printf(-0%/n-+numLcountM), getch(), 4 // <unction for =nsertion Borting voi% instTsort(int numLM) * int i+N+., for(N=1,N&5,N++) * .=numLNM, for(i=N21,i'=# 11 .&>C:L=M,=22) numLi+1M=numLiM, numLi+1M=., 4 4 Sa&""DLordan E%imination (&nction for performin9 Sa&""DLordan e%imination to obtain the "o%&tion to a ">"tem of %inear eq&ation", inc%&din9 matri* inver"ion: (or do&b%e arra>" on%> 56666666666666666666666666666666666666666666666666666666666666666666666666666665 56 7erform Sa&""DLordan e%imination !ith ro!Dpivotin9 to obtain the "o%&tion to 6 the ">"tem of %inear eq&ation" 6 # C 0 I 6 6 #r9&ment"+ 6 %h" D %eftDhand "ide of the eq&ation, matri* # 6 rh" D ri9htDhand "ide of the eq&ation, matri* I 6 nro!" D n&mber of ro!" in the arra>" %h" and rh" 6 nco%"rh"D n&mber of co%&mn" in the arra> rh" 6 6 The f&nction &"e" Sa&""DLordan e%imination !ith pivotin9: The "o%&tion C to 6 the %inear ">"tem !ind" &p "tored in the arra> rh"; create a cop> to pa"" to 6 the f&nction if >o& !i"h to retain the ori9ina% U/ arra>: 6 6 7a""in9 the identit> matri* a" the rh" ar9&ment re"&%t" in the inver"e of 6 matri* #, if it e*i"t": 6 6 ;o %ibrar> or header dependencie", b&t req&ire" the f&nction "!apro!", !hich 6 i" inc%&ded here: 65 55 "!apro!" D e*chan9e" the content" of ro!0 and ro!1 in a @d arra> void "!apro!"(do&b%e66 arr, %on9 ro!0, %on9 ro!1) { do&b%e6 temp; temp0arr[ro!0]; arr[ro!0]0arr[ro!1]; arr[ro!1]0temp; 3 55 9je%im void 9je%im(do&b%e66 %h", do&b%e66 rh", %on9 nro!", %on9 nco%"rh") { 55 a&9ment %h" arra> !ith rh" arra> and "tore in arr@ do&b%e66 arr@0ne! do&b%e6[nro!"]; for (%on9 ro!00; ro!1nro!"; 22ro!) arr@[ro!]0ne! do&b%e[nro!"2nco%"rh"]; for (%on9 ro!00; ro!1nro!"; 22ro!) { for (%on9 co%00; co%1nro!"; 22co%) { arr@[ro!][co%]0%h"[ro!][co%]; 3 for (%on9 co%0nro!"; co%1nro!"2nco%"rh"; 22co%) { arr@[ro!][co%]0rh"[ro!][co%Dnro!"]; 3 3 55 perform for!ard e%imination to 9et arr@ in ro!Deche%on form for (%on9 dinde*00; dinde*1nro!"; 22dinde*) { 55 r&n a%on9 dia9ona%, "!appin9 ro!" to move Rero" in !orkin9 po"ition 55 (a%on9 the dia9ona%) do!n!ard" if ( (dinde*00(nro!"D1)) -- (arr@[dinde*][dinde*]000)) { ret&rn; 55 no "o%&tion 3 e%"e if (arr@[dinde*][dinde*]000) { "!apro!"(arr@, dinde*, dinde*21); 3 55 divide !orkin9 ro! b> va%&e of !orkin9 po"ition to 9et a 1 on the 55 dia9ona% if (arr@[dinde*][dinde*] 00 0:0) { ret&rn; 3 e%"e { do&b%e tempva%0arr@[dinde*][dinde*]; for (%on9 co%00; co%1nro!"2nco%"rh"; 22co%) { arr@[dinde*][co%]50tempva%; 3 3 55 e%iminate va%&e be%o! !orkin9 po"ition b> "&btractin9 a m&%tip%e of 55 the c&rrent ro! for (%on9 ro!0dinde*21; ro!1nro!"; 22ro!) { do&b%e !va%0arr@[ro!][dinde*]; for (%on9 co%00; co%1nro!"2nco%"rh"; 22co%) { arr@[ro!][co%]D0!va%6arr@[dinde*][co%]; 3 3 3 55 back!ard "&b"tit&tion "tep" for (%on9 dinde*0nro!"D1; dinde*E00; DDdinde*) { 55 e%iminate va%&e above !orkin9 po"ition b> "&btractin9 a m&%tip%e of 55 the c&rrent ro! for (%on9 ro!0dinde*D1; ro!E00; DDro!) { do&b%e !va%0arr@[ro!][dinde*]; for (%on9 co%00; co%1nro!"2nco%"rh"; 22co%) { arr@[ro!][co%]D0!va%6arr@[dinde*][co%]; 3 3 3 55 a""i9n re"&%t to rep%ace rh" for (%on9 ro!00; ro!1nro!"; 22ro!) { for (%on9 co%00; co%1nco%"rh"; 22co%) { rh"[ro!][co%]0arr@[ro!][co%2nro!"]; 3 3 for (%on9 ro!00; ro!1nro!"; 22ro!) de%ete[] arr@[ro!]; de%ete[] arr@; 3 56666666666666666666666666666666666666666666666666666666666666666666666666666 6 Sa&""F/eide%@:cpp 6 6 6 6 /o%ve" a ">"tem of %inear eq&ation" &"in9 Sa&""D/eide% iteration 6 6 6 6 6 "amp%e te"t that i &"e i" matri* # 0 O D@ 1 @ I 0 J 6 6 @ Q J 1 D@ 6 6 D1 0 H @ H 6 6 0 @ D1 K K 6 6 6 6 the "o%&tion for * "ho&%d be print %ike thi" + D0:@ D0:H 0:K 1:K 6 66666666666666666666666666666666666666666666666666666666666666666666666666665 <inc%&de 1"tdio:hE <inc%&de 1"td%ib:hE <inc%&de "mpi:h" <inc%&de 1math:hE <define /!ap(*,>) {f%oat6 temp; temp 0 *; * 0 >; > 0 temp;3 <define )#C 1@ 56 <define to%erance 0:0001 65 f%oat ?i"tance(f%oat *[], f%oat >[], int n) { int i; f%oat "&m 0 0:0; for (i 0 0; i 1 n; i22) { "&m 0 "&m 2 (*[i] D >[i])6(*[i] D >[i]); 3 ret&rn (f%oat)"qrt("&m); 3 55 Thi" !i%% read the matri* void eadFmatri*(char6 prompt, f%oat #[)#C][)#C], int n, int m>Frank, int proc) { int i, j; f%oat temp[)#C][)#C]; int nFbar;
nFbar 0 n5proc; 55 (i%% d&mm> entrie" in temp !ith Reroe" for (i 0 0; i 1 n; i22) for (j 0 n; j 1 )#C; j22) temp[i][j] 0 0:0; if (m>Frank 00 0) { printf(",".n", prompt); for (i 0 0; i 1 n; i22) for (j 0 0; j 1 n; j22) "canf(",f",-temp[i][j]); 3 55/catter temp to # from proce"" 0 )74F/catter(temp, nFbar6)#C, )74F(A'#T, #, nFbar6)#C, )74F(A'#T, 0, )74F$'))FM'A?); 3 55Thi" !i%% read the vector * void eadFvector(char6 prompt, f%oat *[], int n, int m>Frank, int proc) { int i; f%oat temp[)#C]; int nFbar;
nFbar 0 n5proc; if (m>Frank 00 0) { printf(",".n", prompt); for (i 0 0; i 1 n; i22) "canf(",f", -temp[i]); 3 55/catter temp to * from proce"" 0 )74F/catter(temp, nFbar, )74F(A'#T, *, nFbar, )74F(A'#T, 0, )74F$'))FM'A?); 3 void main(int ar9c, char6 ar9v[]) { 55)ain 7roce"" int proc; int m>Frank; f%oat #[)#C][)#C]; f%oat *[)#C]; f%oat I[)#C]; int n; 55(%oat to%erance; int ma*Fiteration; 55Sa&""D/eide% 4teration (&nction int iteration 0 0; int i, j; f%oat %a"tFiteration[)#C]; f%oat "i9ma1, "i9ma@; f%oat prece"ion; int k; 55?i"p%a>in9 arra>" int i; f%oat temp[)#C]; )74F4nit(-ar9c, -ar9v); )74F$ommF"iRe()74F$'))FM'A?, -proc); )74F$ommFrank()74F$'))FM'A?, -m>Frank); if (m>Frank 00 0) { ">"tem("c%""); printf("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD.n"); printf("7ro9ram !i%% perform Sa&""D/eide% 4teration )ethod to "o%ve Ainear Eq&ation .n"); printf("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD.n"); printf("Enter variab%e n and ma* n&mber of iteration"+ .n.n"); "canf(",d ,d", -n, -ma*Fiteration); 3 55Iroadca"t n - ma*im&m iteration to a%% other proce"" )74FIca"t(-n, 1, )74F4;T, 0, )74F$'))FM'A?); )74FIca"t(-ma*Fiteration, 1, )74F4;T, 0, )74F$'))FM'A?); nFbar 0 n5proc; 55(i%% the matri* and the vector eadFmatri*(".n4n"ert the variab%e for the matri* # .n", #, n, m>Frank, proc); eadFvector(".nEnter the ri9htDhand "ide of I .n", I, n, m>Frank, proc); 55 4nitia%iRe *
for (i00;i1;;i22) t1[i]0>[i]20:H6(k1[i]0"tep6f(*, >, i)); for (i00;i1;;i22) t@[i]0>[i]20:H6(k@[i]0"tep6f(*2h, t1, i)); for (i00;i1;;i22) tJ[i]0>[i]2 (kJ[i]0"tep6f(*2h, t@, i)); for (i00;i1;;i22) kK[i]0 "tep6f(*2"tep, tJ, i); for (i00;i1;;i22) >[i]20(k1[i]2@6k@[i]2@6kJ[i]2kK[i])5P:0; 3 %ouble f(%ouble x+ %ouble PLM+ int i) { if (i000) ret&rn(>[1]); 56 derivative of fir"t eq&ation 65 if (i001) ret&rn(D0:@6>[1]D>[0]); 56 derivative of "econd eq&ation 65 3 ;he K co%e to implement Simpo!" Ru#e. ;he number of steps is ta.en to be 2#. <inc%&de 1math:hE <inc%&de 1"tdio:hE $%efine > 2# $%efine a #.# $%efine b 2.# %ouble f(%ouble x); main() { int c, k01; 56 $o&nter" in the a%9orithm 65 do&b%e *, h, /=); 56 (4AE 6fp; ?ec%are" a pointer to a fi%e 65 56 fp0fopen(""imp"on:o&t","!"); 'pen" the fi%e for o&tp&t of data 65 printf(".n/imp"onG" &%e for n&merica% inte9ration of f(*).n"); printf(".n (rom a0,%f to b0,%f", a,b); printf(".n.n4nterva%".t.t/tep /iRe.t.t/imp"on /&m.n"); /=)0f(a); 56 4nitia% f&nction va%&e 65 c0@; h0(bDa)5;; 56 /tep "iRe h0(bDa)5; 65 !hi%e (k 10 ;D1) 56 /tep" thro&9h the iteration 65 { c0PDc; 56 9ive" the K,@,K,@,::: in the a%9orithm 65 /=) 0 /=) 2 c6f(a2k6h); 56 #dd" on the ne*t area 65 k22; 56 4ncrea"e" k, the no: of "tep" taken, b> 1 65 3 /=) 0 ( /=) 2 f(b) )6h5J ; 56 add the va%&e f(b) to the "&m and m&%tip%>65 56 b> overa%% factor, h5J 65 printf(",d .t.t,%f .t.t,%f.n", ;, h, /=) ); 56 print the an"!er 65
56 the G.tG here i" j&"t to format the o&tp&t: 4t corre"pond" to hittin9 the T#I ke> on the ke>board ie: "kip" a n&mber of "pace" before printin9 the ne*t o&tp&t: 65 3 %ouble f(%ouble x) { do&b%e f&nction; f&nction 0 *6*6*6*; ret&rn(f&nction); 3