LABORATORY MANUAL (CSE-103F) FCPC Lab Department of Computer Science & Engineering BRCM College of Engineering & Technology Bahal, Haryana
Aim: Main aim of this course is to understand and solve logical & mathematical problems through C language and Flowchart. Strengthen knowledge of a procedural programming language. Design and develop solutions to intermediate level problems using the C language. Further develop your skills in software development using a procedural language. Objectives: The C language is a simple computer language designed to enable sophisticated object-oriented programming. C is defined as a small but powerful set of extensions to the standard ANSI C language. Its additions to C are mostly based on Smalltalk, one of the first object-oriented programming languages. C is designed to give C full object-oriented programming capabilities, and to do so in a simple and straightforward way. Most object-oriented development environments consist of several parts: An object-oriented programming language A library of objects A suite of development tools A runtime environment
DESCRIPTION OF SCHEDULE: S.No List of Experiments 1. Introduction of Turbo C IDE and Programming Environment. 2. Write a program to find the largest of three numbers. (if-then-else) 3. Write a program to find the largest of ten numbers (for-statement) 4. Write a program to find the average mail height & average female heights in the class (input is in the form of sex code, height). 5. Write a program to find roots of a quadratic equation using functions and switch statements. 6. Write a program using arrays to find the largest and second largest numbers out of given 50 numbers. 7. Write a program to multiply two matrices. 8. Write a program to read a string and write it in reverse order. 9. Write a program to concatenate two strings of different lengths. 10 Write a program to check that the input string is a palindrome or not. 11. Programs on file handling. Lab No. 01
OBJECTIVE: Introduction of Turbo C IDE and Programming Environment THEORY The Development Environment - Integrated Development Environment (IDE): The Turbo C compiler has its own built-in text editor. The files you create with text editor are called source files, and for C++ they typically are named with the extension.cpp,.cp, or.c. The C Developing Environment, also called as Programmer s Platform, is a screen display with windows and pull-down menus. The program listing, error messages and other information are displayed in separate windows. The menus may be used to invoke all the operations necessary to develop the program, including editing, compiling, linking, and debugging and program execution. Invoking the IDE To invoke the IDE from the windows you need to double click the TC icon in the directory c:\tc\bin. The alternate approach is that we can make a shortcut of tc.exe on the desktop. This makes you enter the IDE interface, which initially displays only a menu bar at the top of the screen and a status line below will appear. The menu bar displays the menu names and the status line tells what various function keys will do. Default Directory The default directory of Turbo C compiler is c:\tc\bin.
Using Menus If the menu bar is inactive, it may be invoked by pressing the [F10] function key. To select different menu, move the highlight left or right with cursor (arrow) keys. You can also revoke the selection by pressing the key combination for the specific menu. Opening New Window To type a program, you need to open an Edit Window. For this, open file menu and click new. A window will appear on the screen where the program may be typed.
Writing a Program When the Edit window is active, the program may be typed. Use the certain key combinations to perform specific edit functions. Saving a Program To save the program, select save command from the file menu. This function can also be performed by pressing the [F2] button. A dialog box will appear asking for the path and name of the file. Provide an appropriate and unique file name. You can save the program after compiling too but saving it before compilation is more appropriate. Making an Executable File The source file is required to be turned into an executable file. This is called Making of the.exe file. The steps required to create an executable file are: 1. Create a source file, with a.c extension. 2. Compile the source code into a file with the.obj extension. 3. Link your.obj file with any needed libraries to produce an executable program. All the above steps can be done by using Run option from the menu bar or using key combination Ctrl+F9 (By this linking & compiling is done in one step).
Compiling the Source Code Although the source code in your file is somewhat cryptic, and anyone who doesn't know C will struggle to understand what it is for, it is still in what we call humanreadable form. But, for the computer to understand this source code, it must be converted into machine-readable form. This is done by using a compiler. Hence, compiling is the process in which source code is translated into machine understandable language. It can be done by selecting Compile option from menu bar or using key combination Alt+F9. Creating an Executable File with the Linker After your source code is compiled, an object file is produced. This file is often named with the extension.obj. This is still not an executable program, however. To turn this into an executable program, you must run your linker. C programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with your compiler. Compiling and linking in the IDE In the Turbo C IDE, compiling and linking can be performed together in one step. There are two ways to do this: you can select Make EXE from the compile menu, or you can press the [F9] key. Executing a Program If the program is compiled and linked without errors, the program is executed by selecting Run from the Run Menu or by pressing the [Ctrl+F9] key combination. The Development Cycle If every program worked the first time you tried it that would be the complete development cycle: Write the program, compile the source code, link the program, and run it. Unfortunately, almost every program, no matter how trivial, can and will have errors, or bugs, in the program. Some bugs will cause the compile to fail, some will cause the link to fail, and some will only show up when you run the program.
Whatever type of bug you find, you must fix it, and that involves editing your source code, recompiling and relinking, and then rerunning the program. Correcting Errors If the compiler recognizes some error, it will let you know through the Compiler window. You ll see that the number of errors is not listed as 0, and the word Error appears instead of the word Success at the bottom of the window. The errors are to be removed by returning to the edit window. Usually these errors are a result of a typing mistake. The compiler will not only tell you what you did wrong; they ll point you to the exact place in your code where you made the mistake. Exiting IDE An Edit window may be closed in a number of different ways. You can click on the small square in the upper left corner, you can select close from the window menu, or you can press the [Alt][F3] combination. To exit from the IDE, select [Alt][X] Combination. EXERCISE Exit from the File Menu or press 1. Type the following program in C Editor and execute it. Mention the Error. void main(void) printf( This is my first program in C );
Lab No. 02 OBJECTIVE: Write a program to find the largest of three numbers. (if-then-else) #include<stdio.h> #include<conio.h> void main( ) float a,b,c; printf( Enter any three numbers: ); scanf( %f%f%f,&a,&b,&c); if((a>b)&&(a>c)) printf( Largest of three numbers: %f,a); else if((b>a)&&(b>c)) printf( Largest of three numbers: %f,b); else printf( Largest of three numbers: %f,c); getch( );
Lab No. 03 OBJECTIVE: Write a program to find the largest of ten numbers (for-statement) #include<stdio.h> #include<conio.h> void main() int a[10],i,s=0,j,t; clrscr(); printf( Enter ten numbers: ); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) for(j=i+1;j<10;j++) if(a[i]>a[j]) t=1; else t=0; break; if(t==1) printf("\nlargest no.:%d",a[i]); break; getch();
Lab No. 04 OBJECTIVE: Write a program to find the average mail height & average female heights in the class (input is in the form of sex code, height). #include<stdio.h> #include<conio.h> void main() int mh[5],fh[5],i; float mavg,favg,msum=0,fsum=0; clrscr(); printf( enter the height of male\n ); for(i=0;i<5;i++) scanf( %d,&mh[i]); msum=msum+mh[i]; mavg=msum/5; printf( enter the height of female\n ); for(i=0;i<5;i++) scanf( %d,&fh[i]); fsum=fsum+fh[i]; favg=fsum/5; printf( the average of male height is %f \n,mavg); printf( the average of female height is %f \n,favg); getch();
Lab No. 05 OBJECTIVE: Write a program to find roots of a quadratic equation using switch statements. #include<stdio.h> #include<conio.h> #include<math.h> main() float a,b,c,d,x1,x2; int ch; clrscr(); printf("enter a,b,c values"); scanf("%f%f%f",&a,&b,&c); d=pow(b,2)-4*a*c; if(d==0) ch=1; else if(d>0) ch=2; else ch=3; printf("a=%6.2f\nb=%6.2f\nc=%6.2f\n",a,b,c); printf("discriminate D=%6.2f\n",d); switch(ch) case 1: printf("equal Roots"); x1=x2=-b/(2*a); printf("x1=x2=%6.2f\n",x1); break; case 2: printf("roots are Real and inequal"); x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); printf("x1=%6.2f\n",x1); printf("x2=%6.2f\n",x2);
break; case 3: printf("imaginary roots \n"); printf("x1=%6.2f+i%6.2f\n",-b/(2*a), sqrt(-d)/(2*a)); printf("x2=%6.2f-i%6.2f\n",-b/(2*a), sqrt(-d)/(2*a)); break; getch();
Lab No. 6 OBJECTIVE: Write a program using arrays to find the largest and second largest numbers out of given 50 numbers. #include<stdio.h> #include<conio.h> void main() int i,a[55],n,l=0,sl; clrscr(); printf("how many no. will you enter: "); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); l=a[1]; for(i=0;i<n;i++) if(a[i]>l) l=a[i]; printf("largest element is:%d",l); sl=a[1]; for(i=0;i<n;i++) if(a[i]>sl) sl=a[i]; printf("second Largest element is:%d",sl); getch();
Lab No. 7 OBJECTIVE: Write a program to multiply two matrices. #include<stdio.h> #include<conio.h> #include<process.h> void main() int a[10][10],b[10][10],c[10][10]; int i,j,m,n,p,q,s; clrscr(); printf("input row and column of matrix-a\n"); scanf("%d%d",&m,&n); printf("input row and column of matrix-b\n"); scanf("%d%d",&p,&q); if(n!=p) printf("matrix cannot be multiplied\n"); getch(); exit(0); printf("input Matrix-A\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("input Matrix-B\n"); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf("%d",&b[i][j]);
c[i][j]=0; for(s=0;s<n;s++) c[i][j]=c[i][j]+a[i][s]*b[s][j]; printf("\nproduct of matrix A and matrix B is:\n"); for(i=0;i<m;i++) for(j=0;j<q;j++) printf("\t%d",c[i][j]); printf("\n"); getch();
Lab No. 8 OBJECTIVE: Write a program to read a string and write it in reverse order. #include<stdio.h> #include<conio.h> void main() char a[20],i, b[20],g=0,j; clrscr(); printf("enter any string: "); scanf("%s",a); for(i=0;a[i]!='\0';i++) g++; j=g-1; for(i=0;i<g;i++) b[j]=a[i]; j--; printf("reverse order of string: %s",b); getch();
Lab No. 9 OBJECTIVE: Write a program to concatenate two strings of different lengths. #include<stdio.h> #include<conio.h> void main() char a[25],b[25],c[25]; int i.j; clrscr(); printf( Enter first string: ); scanf( %s,a); printf( Enter second string: ); scanf( %s,b); for(i=0;a[i]!= \0 ;i++) c[i]=a[i]; for(j=0; b[j]!= \0 ; j++) c[i+j]=b[j]; c[i+j]= \0 ; printf( The concatenated string is:\n%s,c ); getch();
Lab No. 10 OBJECTIVE: Write a program to check that the input string is a palindrome or not. #include<stdio.h> #include<conio.h> #include<process.h> void main() char a[ 20],i,b[20],g=0,j,m; clrscr(); printf("enter any string: "); scanf("%s",a); for(i=0;a[i]!='\0';i++) g++; j=g-1; for(i=0;i<g;i++) b[j]=a[i]; j--; for(i=0;i<g;i++) if(b[i]==a[i]) m=0; else
m=1; break; if(m==0) printf("string is a Palindrome"); else if(m==1) printf("string is not a Palindrome");
Lab No. 11 OBJECTIVE: Programs on file handling, which copies one file to another Program: #include<stdio.h> #include<string.h> #include<dos.h> #include<conio.h> #define L 80 main(int argc, char *argv[]) FILE *sfpt,*dfpt; char c; char sfname[l],dfname[l]; clrscr(); if(argc!=3) printf("invalid No of parameters."); exit(1); strcpy(sfname,argv[1]); strcpy(dfname,argv[2]); sfpt=fopen(sfname,"r"); if(sfpt==null) printf("file can't be opend %s",sfname); else dfpt=fopen(dfname,"w"); if(dfpt==null) printf("file can't be found: %s",sfname); else while(!feof(sfpt))
fclose(sfpt); fclose(dfpt); getch(); c=getc(sfpt); putc(c,dfpt);