c lab manual-1
TRANSCRIPT
-
7/28/2019 C Lab Manual-1
1/54
UNIVERSAL ENGINEERING
COLLEGE
Vallivattom(P.O), Thrissur-680123.
EN109(P)Computer Programming in C
Lab Manual
-
7/28/2019 C Lab Manual-1
2/54
EX. No. : 1(A) STUDY OF MS-DOS COMMANDS
AIM:
To study the simple OS interface MS DOS commands.
DIRECTORY RELATED COMMANDS:
1. DIR:
This command is used to display the files and directories of the current drive and
directory.
Syntax: DIR [drive:][path][filename]
DIR/P: To display files and directories page wise.
DIR/W: To display files and directories width wise ( horizontally ).
DIR/AD: To display directories only.
DIR/AH: To display hidden files only.
DIR/AR: To display read only files.
DIR/ON: To display files in sorted order name wise.
DIR/OS: To display files in sorted order size wise.
EXAMPLE:
C:\> DIR
2. MD [MAKE DIRECTORY]:
This command is used to create a new folder or directory.
Syntax: MD [drive:] [path]
Where drive is the floppy \ hard disk drive on which you want to create a new
directory.
EXAMPLE:
C:\> MD DRSJS
C:\>
3. CD [CHANGE DIRECTORY]:
This command is used to change current directory to another directory.
Syntax: CD [drive:][path]
2
-
7/28/2019 C Lab Manual-1
3/54
CD.. : This command is used to go to previous directory.
CD\ : This command is used to root directory.
EXAMPLE:
C:\> CD DRSJS
C:\DRSJS>
C:\> CD..
C:\>
4. RD [REMOVE DIRECTORY]:
This command is used to remove the directory.
SYNTAX: RD [directory]
Where directory is the name of the directory which we want to remove.
EXAMPLE:
C:\> RD DRSJS
C:\>
FILE RELATED COMMANDS:
1. COPY CON :
This command is used to create a new file.
SYNTAX : COPY CON [File Name]
EXAMPLE:-
C:\> copy con college
Sri Manakula Vinayagar Engineering College.
(Approved by AICTE New Delhi & Affiliated to Pondicherry University)
Ctrl Z or F6 to save the file.
2. TYPE:
This command is used to display the contents of the file.
SYNTAX: TYPE [File Name]
Example: C:\> type address
3
-
7/28/2019 C Lab Manual-1
4/54
3. REN:
This command is used to change the file name.
SYNTAX: REN [Old File Name] [New Name]
Example:
C:\>REN circle area
4. DEL:
This command is used to delete the file.
SYNTAX: DEL [File Name]
Example:-
C:\>DEL area
RESULT:
Thus the MS DOS directory related and file related commands have been studied
and executed.
4
-
7/28/2019 C Lab Manual-1
5/54
EX. No. : 1(B) STUDY OF TURBO C
AIM: To study the TURBO C IDE, compile and execute a simple C program.
DESCRIPTION:
The TURBO C development environment offers the following main menu
commands.
File
Edit
Search
Run
Compile
Debug
Options
Windows
Help
At the main menu, either press an arrow key to move to the desired option and then press
enter key or press the highlighted capital letters of the option. You can also press alt key
and the first letter of the option, which you desire to open.
FILE (ALT-F):
This menu offers choices for loading existing files, creating new files and saving
files. This menu contains New, Open, Save, Save as, Save all, Change dir, Print, Dos
Shell and Quit.
File ->New: This option is used to create a new program.
File ->Open (F3): This option is used to open an existing files.
File ->Save (F2): This option is used to save the program.
5
-
7/28/2019 C Lab Manual-1
6/54
File ->Save as: This option is used to save the program in another name.
File ->Change dir: This option is used to change the directory of existing folder
to another directory.
File ->Print: This option is used to print the existing programs.
File ->Dos shell: This option is used to come out from the c editor to dos shell.
To return to Turbo C, type exit and press enter and the DOS prompt.
File ->Quit: This option is used to exit from C editor.
EDIT: (ALT E): This command takes you to the edit window. In this menu, it is used
to modify the programs. In this menu we can copy, cut and paste the programs.
SEARCH (ALT S): This menu lets you to find particular part of the program, replace
the particular part of program and move to particular line number.
RUN (ALT-R): This menu lets you to compile, link and run our program and start and
debugging sessions.
RUN->RUN (CTRL+F9): This option is used to compile, link and run our program. If
we compile and link with the option debug source debugging on, your program will run
in debug mode. This command will stop execution at the first break point or it will run
the program all the way through. After compilation, to view the result press Alt-F5.
COMPILE (ALT-F9): This menu allows to invoke compile to obj, Make Exe file, Link
Exe file, etc., Alt-F9 is used to compile and create an object file. F9 command is used to
build a execution file.
DEBUG: This menus options let you look at and change variable values, go to the
declaration of any function in the source code and look at the call stack. Break/Watch
option is used to insert, remove and go breakpoint in your program and set delete and edit
watch expressions.
PROJECT (ALT-P): This menu is used to define and manage project.
6
-
7/28/2019 C Lab Manual-1
7/54
OPTIONS (ALT-O): This menu allows configuring the Turbo C Editor.
WINDOWS (ALT-W): This menu is used to Cascade all files, move programs and close
all programs.
STEPS FOR PROGRAM WRITING:
1. Invoke Turbo C editor using StartRunc:tc\bin\tc
2. Type the source program.
3. Save the program using F2 command. (filename.c)
4. Compile and run the program using Alt F9.
5. To view the result use Alt F5.
SAMPLE 1: Write a program to print simple interest.
/* program to find simple interest */
#include
void main()
{
float p,n,r,si;
clrscr();
printf(Enter principle, Number of years and Rate of Interest :\n);
scanf(%f%f%f,&p,&n,&r);
si=(p*n*r/100);
printf(The Simple Interest is : %2.2f: ,si);
getch();
}
OUTPUT:-
Enter principle, Number of years and Rate of Interest:
5000
5
10
The Simple Interest is: 2500.00
7
-
7/28/2019 C Lab Manual-1
8/54
SAMPLE 2: Write a program to generate Sales slip.
/************** SALES SLIP ***************/
#include int main()
{
float purchase, tax_amt, total, rate=0.125;clrscr();
printf("\n Amount of purchase: ");
scanf("%f",&purchase);
tax_amt = purchase * rate;total = purchase + tax_amt;
printf("\n Purchase is : %0.2f ",purchase);
printf("\n Tax : %0.2f",tax_amt);
printf("\n Total : %0.2f",total);getch();
}
OUTPUT 1:
Amount of purchase: 500
Purchase is: 500.00
Tax : 62.50Total : 562.50
OUTPUT 2:
Amount of purchase: 1773
Purchase is: 1773.00
Tax : 221.62
Total : 1994.62
RESULT:
8
-
7/28/2019 C Lab Manual-1
9/54
Thus the options in the TURBO C IDE have been studied and a simple C program
is compiled and executed.
EX. No. : 2 MS-WORD MAIL MERGE
AIM:
To create Student Progress Report as the main document and create 45 reports
for 45 students. Use mail merge to create reports for 42 students among the 45.
PROCEDURE:
1. Open your letter in Word and then bring up the Mail Merge Sidebar Wizard by
going to Tools > Letters and Mailings > Mail Merge.
2. The sidebar will appear on the right side of the window, next to your document.
3. Check the letter option from the document type and click Next below the
wizard.
4. Leave the Use the current document checked and click on Next: Select
recipients to move on.
5. This step is used to select the data source for the recipient list. Check the Type a
new list option to create a new data source list or leave the checked option Use
existing list and then click Next. A Select Data Source dialog box will
appear. Choose the data source from the existing list and then clickopen.
6. Mail merge recipient dialog box will appear choose the recipients, you want to
send letter from the list and then clickok.
7. The next part is inserting recipient information from the data source into the Word
document.
8. To do this, first select the place in the document where you want to insert a field
from the data source and click on More items. A dialog box will pop-up, giving
you a list of all of the fields that are possible to insert.
9. Select the field that you want to insert and click Insert. It will replace the
selected section of text with field_name.
9
-
7/28/2019 C Lab Manual-1
10/54
10. Once you have entered all of your merged fields into the correct places and
checked the formatting around them, click on Next: Preview your letters.
11. You can preview your letters to make sure that everything will look correct once
the records from the data source are merged. You can check different recipients
letters by using the arrows, look for a specific record by searching the merge
fields, or even exclude one from the merge.
12. Once your merge is set up correctly you can complete it by clicking next
complete the merge.
13. Next you can choose to eitherprint the letters orview the merge as individual
letters. Either option will bring up the dialog box. You can choose to merge all
of the records in the data source, just the Current record that you are
displaying in the preview, or a certain section of continuous records.
14. Selecting Edit individual letters will open up a new Word document (usually
called Letters 1). In this document, each page will contain the form letter with
one of the records merged into it.
15. You can then edit individual letters if you want to. For example if you want to add
a message to a specific recipient you can do it in this step.
16. Thus the document is merged successfully with the selected recipient.
10
-
7/28/2019 C Lab Manual-1
11/54
EX. No. : 3 EXCEL CHARTS
AIM:
To try with different types of graphs (bar, XY, Pie) for the following data,
Test results of a student in semester I
Test T1 T2 PT1 T3 T4 PT2
Marks 100 100 54 95 88 98
PROCEDURE:
1. Enter the data in the worksheet as shown in question
2. Choose Chart option from the Insert menu, or choose the Chart Wizard button
from the standard toolbar
3. Shows the chart wizard Step1 of 4 Chart Type Dialog Box.
4. Choose the appropriate chart type from the chart type list box then click next.
5. Shows the chart wizard step 2 of 4 Chart Source Data Dialog Box then click
Next.
6. Shows the chart wizard step 3 of 4 Chart Options Dialog Box, here you can give
the Chart Titles i.e., X and Y-Axis Titles etc, and then clickNext.
7. Shows the chart wizard step 4 of 4 Chart Location Dialog Box then clickFinish.
8. Now the chart is displayed in your active worksheet, as shown below.
11
-
7/28/2019 C Lab Manual-1
12/54
SNAPSHOTS
Figure 1: XY chart
Figure 2: Bar chart
12
-
7/28/2019 C Lab Manual-1
13/54
Figure 3: Pie chart
13
-
7/28/2019 C Lab Manual-1
14/54
EX. No. : 4 GREATEST OF THREE NUMBERS
AIM:
To write a C program to find the largest of three numbers using conditional and IF
statement.
ALGORITHM
1. Start
2. Read three numbers A, B, C
3. Compare whether A is greater than B and C using conditional operator
4. If it is equal then print A is greater
5. If step 3 is false check whether B is greater than A and C using conditional
operator.
6. If it is true print B is greater
7. If step 5 is false print C is greater
8. Stop
PROGRAM
/***** GREATEST OF THREE NUMBERS USING
IF AND CONDITIONAL OPERATOR *****/
#include
#include
void main()
{
clrscr();
int a,b,c;
printf("enter 3 numbers");
scanf("%d %d %d",&a,&b,&c);
printf("\n Using conditional");
printf("\n");
14
-
7/28/2019 C Lab Manual-1
15/54
(a>b&&a>c)?printf("A is larger"):(b>a&&b>c)?printf("B is larger"):printf("C is
larger");
printf("\n Using IF \n");
if(a>b && a>c)
printf("A is largest");
else
if(b>a&&b>c)
printf("B is largest");
else
printf("C is largest");
getch();
}
OUTPUT
Enter 3 numbers 2 5 6
Using conditional
C is larger
Using IF
C is largest
RESULT
Thus the largest of three numbers using conditional and if statement was
successfully executed.
15
-
7/28/2019 C Lab Manual-1
16/54
EX. No. : 5 SWAPPING OF TWO NUMBERS
AIM
To write a C program to swap two numbers using temporary variable and without
using temporary variable.
ALGORITHM
1. Start
2. Read two numbers A, B
3. Copy A to TEMP
4. Copy B to A
5. Copy TEMP to B
6. Print A, B
7. Calculate A=A+B, B=A-B, A=A-B
8. Display A, B
9. Stop
PROGRAM
/***************** SWAPPING OF TWO NUMBERS *****************/
#include
#include
void main()
{
clrscr();
int a,b,temp;
printf("Enter Two numbers");
scanf("%d %d",&a,&b);
printf("\nSwapping Using Temporary Variable\n");
printf("\n Before Swap A=%d B=%d",a,b);
temp=a;
a=b;
16
-
7/28/2019 C Lab Manual-1
17/54
b=temp;
printf("\nAfter Swap A=%d B=%d",a,b);
printf("\nSwapping Without Using Temporary Variable\n");
printf("\n Before Swap A=%d B=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Swap A=%d B=%d",a,b);
getch();
}
OUTPUT
Enter Two numbers 3 2
Swapping Using Temporary Variable
Before Swap A=3 B=2
After Swap A=2 B=3
Swapping Without Using Temporary Variable
Before Swap A=2 B=3
After Swap A=3 B=2
RESULT
Thus swapping of two numbers using temporary variable and without using
temporary variable was done and successfully executed.
17
-
7/28/2019 C Lab Manual-1
18/54
EX. No. : 6 QUADRATIC EQUATION
AIM:
To write a c program to solve Quadratic Equation for various possible inputs.
ALGORITHM:
1. Start the program.
2. Get the values of A,B,C for a quadratic equation of the form ax2+bx+c=0.
3. Compute discriminate value d as D=B2-4AC.
4. Check for D is D=0 then go to step 5, otherwise go to step 8.
5. Compute the value of the roots (i.e) E = -B/2A.
6. Display that the roots are Real and Unequal.
7. Check for D, if D>0 then go to step 9, otherwise go to 1.
8. Display that The roots are real and Unequal.
9. Compute the value of the roots ( i.e ) E=-B+sqrt(D)/2A and F=-B-sqrt(D)/2A.
10. Display the value of the roots and move to 5.
11. Check for D, if D
-
7/28/2019 C Lab Manual-1
19/54
float r1,r2,e,f;
clrscr();
printf("\nEnter the co-efficient of x^2:\t");
scanf("%d",&a);
printf("\nEnter the co-efficient of x:\t");
scanf("%d",&b);
printf("\nEnter the constant value:\t");
scanf("%d",&c);
printf("\n\nSOLUTION:\n***********");
d=(b*b)-(4*a*c);
if(d==0)
{
r1=-((float)b/(float)(2*a));
printf("\n The roots are real and equal");
printf("\n The roots are:\n\t root1=root2=%f",r1);
}
else
{
if(d>0)
{
r1=(-b+sqrt(abs(d)))/(2*a);
r2=(-b-sqrt(abs(d)))/(2*a);
printf("\nThe roots are real and unequal");
printf("\nThe roots are:\n\t root1=%f\t root2=%f",r1,r2);
}
else
{
e=(float)-b/(float)(2*a);
f=(sqrt(abs(d)))/(2*a);
printf("\nThe roots are imaginary");
printf("\n\nThe roots are:\n root1=%.2f+i%.2f",e,f);
19
-
7/28/2019 C Lab Manual-1
20/54
printf("\troot2=%7.2f-i%.2f",e,f);
}
}
getch();
}
OUTPUT 1:
Enter the co-efficient of x^2 : 2
Enter the co-efficient of x : 4
Enter the constant value : 2
SOLUTION:
The roots are real and equal
The roots are
root1 = root2 = -1.00
OUTPUT 2:
Enter the co-efficient of x^2 : 2
Enter the co-efficient of x : 1
Enter the constant value : 4
SOLUTION:
The roots are imaginary
The roots are
root1 = -0.25 + i1.39 root2 = -0.25 + i1.39
OUTPUT 3:
Enter the co-efficient of x^2 : 1
Enter the co-efficient of x : 4
Enter the constant value : 2
20
-
7/28/2019 C Lab Manual-1
21/54
SOLUTION:
The roots are real and unequal
The roots are
root1 = -0.59
root2 = -3.41
RESULT:
Thus a C program to solve the quadratic equation was compiled and executed
successfully.
21
-
7/28/2019 C Lab Manual-1
22/54
EX. No. : 7 COUNTING NUMBER OF VOWELS AND DIGITS IN A STRING
AIM
To write a C program to count the number of vowels and digits in a given string
using switch case statement.
ALGORITHM
1. Start
2. Read a string CH
3. Loop until CH is equal to new line
4. Check whether the entered character is vowel or a digit using switch case
5. If it is vowel increment VOWEL
6. If it is digit increment DIGIT
7. End loop
8. Print VOWEL, DIGIT
9. Stop
PROGRAM
/**** COUNTING NUMBER OF VOWELS AND DIGITS
IN A STRING USING SWITCH CASE ****/
#include
#include
void main()
{
clrscr();
char ch;
int digits=0,vowels=0;
printf("Enter a string with digits\n");
while((ch=getchar())!='\n')
{
22
-
7/28/2019 C Lab Manual-1
23/54
switch(ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digits++;
break;
case 'a':case 'A':
case 'e':case 'E':
case 'i':case 'I':
case 'o':case 'O':
case 'u':case 'U':
vowels++;
break;
}
}
printf("\n No. of vowels in the given string %d",vowels);
printf("\n No. of digits in the given string %d",digits);
getch();
}
23
-
7/28/2019 C Lab Manual-1
24/54
OUTPUT
Enter a string with digits
COMPTER PROGRAMMING 123
No. of vowels in the given string 5
No. of digits in the given string 3
RESULT
Thus a C program to count the number of vowels and digits using switch case was
compiled and executed successfully.
24
-
7/28/2019 C Lab Manual-1
25/54
EX. No. : 8(A) GENERATION OF PRIME NUMBERS
AIM:
To write a C program to generate all prime numbers in a given range.
ALGORITHM:
1: Start the program.
2: Get the range of prime numbers N
3: Check if N= =1 if equal go to step 4
4: Print number is not valid to generate prime.
5: By using for loop check (i
-
7/28/2019 C Lab Manual-1
26/54
for(j=1;j
-
7/28/2019 C Lab Manual-1
27/54
EX. No. : 8 (B) FIBONACCI SERIES
AIM:
To write a c program to generate the Fibonacci series.
ALGORITHM:
1. Start
2. Initialize FIB1 = 0, FIB2 = 1, FIB3, NUMBERS = 2, COUNTER = 2
3. Read NUMBERS
4. If NUMBERS
-
7/28/2019 C Lab Manual-1
28/54
int fib1 = 0,fib2 = 1,fib3,numbers = 2,counter = 2;
printf("How many Fibonacci number you need ? : ");
scanf("%d",&numbers);
if (numbers < 3)
exit(0);
printf("%d ",fib2);
do
{
counter++;
fib3 = fib1 + fib2;
printf("\n%d",fib3);
fib1 = fib2;
fib2 = fib3;
} while (counter
-
7/28/2019 C Lab Manual-1
29/54
EX. No. : 9 COSINE SERIES USING LOOP
AIM
To write a C program to find the cosine series for a given range and an angle.
ALGORITHM
1. Start
2. Read ANGLE, N
3. Calculate X=ANGLE*3.14/180
4. Initialize TERM=1, SUM=1, Y=1
5. Loop i=1 to n
6. Y= Y*(2*i*(2*i-1));7. TERM=(-TERM*X*X)/Y)
8. Increment SUM to TERM
9. End loop
10. Print ANGLE, X, SUM
11. Stop
PROGRAM
/************** COSINE SERIES USING FOR LOOP *************/
#include
#include
void main()
{int i,n,y=1;
float x,angle,term,sum;
clrscr();printf("Enter the angle and the number of terms\n");
scanf("%f %d",&angle,&n);
x=angle*3.141592/180;term=1;
sum=1;
for(i=1;i
-
7/28/2019 C Lab Manual-1
30/54
y=y*(2*i*(2*i-1));
term=(-term*x*x)/y;
sum+=term;}
printf("Entered angle = %.4f\n",angle);
printf("Its radian value =%.3f\n",x);printf("Value of Cosine series= %.3f\n",sum);
getch();
}
OUTPUT
Enter the angle and the number of terms
45 5
Entered angle = 45.00
Its radian value = 0.785
Value of Cosine series = 0.707
RESULT
Thus the cosine series for the given range and angle was compiled and executed
successfully.
30
-
7/28/2019 C Lab Manual-1
31/54
EX. No. : 10 MATRIX OPERATIONS
AIM:
To write a C program to perform the matrix operation (Addition, Transpose and
Multiplication).
ALGORITHM
1. Start
2. Read the order of matrix M,N
3. Read the value of two matrices A,B
4. Set loop i=0 to m and j=0 to N5. Calculate c[i][j]=a[i][j]+b[i][j]
6. End loop
7. Print c[i][j]
8. Initialize c[i][j]=0
9. Set loop i=0 to M j=0 to N and k=0 to N
10. Calculate c[i][j]=c[i][j]+a[i][k]*b[k][j]
11. End loop
12. Print c[i][j]
13. Set loop i=0 to M and j=0 to N
14. Assign t[i][j]=a[j][i]
15. End loop
16. Print t[i][j]
17. Stop
31
-
7/28/2019 C Lab Manual-1
32/54
PROGRAM
/*** MATRIX OPERATIONS - ADDITION, TRANSPOSE & MULTIPLICATION ***/
#include#include
void main()
{int n,i,j,a[3][3],b[3][3],c[3][3],m,k;
clrscr();
printf("Enter the order of two Matrices");scanf("%d %d",&m,&n);
printf("\n Enter the Matrix A");
for(i=0;i
-
7/28/2019 C Lab Manual-1
33/54
for(j=0;j
-
7/28/2019 C Lab Manual-1
34/54
OUTPUT
Enter the order of two Matrices 3 3
Enter the Matrix A
4 1 44 4 4
4 1 4
Enter the Matrix B
2 2 2
2 2 2
2 2 2
Matrix addition
6 3 6
6 6 6
6 3 6
Matrix Multiplication
18 18 18
24 24 24
18 18 18
Transpose of matrix A
4 4 4
1 4 1
4 4 4
RESULT
Thus the C program to perform the matrix operations was compiled and
successfully executed.
34
-
7/28/2019 C Lab Manual-1
35/54
EX. No. : 11 SINE SERIES USING RECURSIVE FUNCTION
AIM
To write a C program to perform the sine series using recursive function.
ALGORITHM
1. Start
2. Read X,N
3. Calculate X=3.14/180
4. Assign SUM=X
5. Cal and print function sine(N,X)
6. Stop
Function sine(n,x)
1. Initialize i=1, Y=1 as static
2. If N==1 return sum
3. If N>0
4. Y= Y*(2*i)*(2*i+1);
5. Calculate TERM=-TERM*X*X/Y
6. Increment I to 1
7. SUM=SUM+TERM
8. TERM=sine(--n,x)
9. return SUM
35
-
7/28/2019 C Lab Manual-1
36/54
PROGRAM
/************** SINE SERIES USING RECURSIVE FUNCTION *************/
#include
#include#include
#define PI 3.14285
float sum;void main()
{
int n;
float x;double sine(int,float);
clrscr();
printf("\n Enter the angle: ");
scanf("%f",&x);x*=(PI/180.0);
printf("Enter the no. of terms to be summed:");scanf("%d",&n);
sum=x;
printf("Sum of the series :%.4f\n",sine(n,x));getch();
}
double sine (int n, float x){
static float term;
static float i=1;static int y=1;
if(n==1)
return sum;if(i==1)
term=x;
if(n>0)
{y=y*(2*i)*(2*i+1);
term=-term*x*x/y;
i++;sum+=term;
term=sine (--n,x);
}if(n
-
7/28/2019 C Lab Manual-1
37/54
}
return sum;
}
OUTPUT
Enter the angle: 30
Enter the no. of terms to be summed: 5
Sum of the series: 0.4999
RESULT
Thus the C program to perform the sine series using recursive function was
compiled and executed successfully.
37
-
7/28/2019 C Lab Manual-1
38/54
EX. No. : 12 REMOVING DUPLICATE STRING FROM A GIVEN SENTENCE
AIM:
To write a C program to remove the duplicate string from the given sentence.
ALGORITHM
1. Start
2. Read the sentence in bin variable from the user
3. Separate the words and store it in temp variable which is two dimension array
4. Compare the consecutive words in temp upto the word count
5. If it is not equal print the word else do not print that word
6. Repeat 4 and 5 until all the words in temp are compared
7. Stop
PROGRAM
/**** REMOVE DUPLICATE STRING FROM A GIVEN SENTENCE ****/
#include
#include #include
#include
void main(void)
{
char temp[10][20];char bin[100];
int index=0;
int word=0,len=0;
int loop,chain;clrscr();
printf("\nEnter the sentence: ");
gets(bin);
while(bin[index]!=NULL)
{if(isalpha(bin[index]) || isdigit(bin[index]))
{
temp[word][len]=bin[index];
len++;
38
-
7/28/2019 C Lab Manual-1
39/54
}
else if(isalpha(bin[index-1]) || isdigit(bin[index-1]))
{temp[word][len]=NULL;
word++;len=0;
}index++;
}
temp[word][len]=bin[index];word++;
printf("\nThe output data is: ");
for(index=0;index
-
7/28/2019 C Lab Manual-1
40/54
EX. No. : 13 ARRAY OF STRUCTURES
AIM
To write a C program to create an array of structures for a list of items with the
following details
Item_Code Item_Name
102 Paste-Colgate
102 Paste-Pepsodent
102 Paste-Close-up
101 Soap-Cinthol
101 Soap-Lux
101 Soap-Hammam
101 Soap-Dove
Arrange the set of items in ascending order of its Item_Code and descending
order of its Item_Name as given below
Item_Code Item_Name
101 Soap-Lux
101 Soap-Hamam
101 Soap-Dove
101 Soap-Cinthol
102 Paste-Pepsodent
102 Paste-Colgate
102 Paste-Close-up
40
-
7/28/2019 C Lab Manual-1
41/54
ALGORITHM
1. Start
2. Define structure item with the members
Item_code : integerItem_name : character
end item
3. Read the given details in item_code and item_name
4. Set loop i=0 to 6 and j=1 to 6
5. Check if a[i].item_code
-
7/28/2019 C Lab Manual-1
42/54
PROGRAM
/******************* ARRAY OF STRUCTURES ******************/
#include#include
#include
void main(){
clrscr();
int i,j,temp;
char temp1[30];struct item
{
int item_code;
char item_name[30];}a[7];
printf("Enter the item code and item name");for(i=0;i
-
7/28/2019 C Lab Manual-1
43/54
}
printf("Item_Code \t\t\t Item_Name");
for(i=0;i
-
7/28/2019 C Lab Manual-1
44/54
EX. No. : 14 USER-DEFINED DATA TYPE USING STRUCTURE
AIM
To write a C program to create a user defined data type using structure to accept a
list of N numbers of students with their names and roll numbers and arrange them in analphabetical order and write the details in the file.
ALGORITHM
1. Start
2. Create a user-defined structure tag with the members
name: character
rno: integer
3. Open the file details .txt in writing mode
4. Read no for the number of student details
5. Read all the student details in name, rno
6. Before sorting print the student details and same time write it in the file details.txt
7. Using function sort(), sort the names upto no count
8. After sorting print the student details and same time write it in the file
9. Stop
PROGRAM
/********* USER-DEFINED DATA TYPE USING STRUCTURE ********/
#include
#include
#includestruct tag
{
char name[10];
int rno;};
typedef struct tag node;
node s[10],t[10];void main()
{
FILE *fp;int no,i;
44
-
7/28/2019 C Lab Manual-1
45/54
fp=fopen("details.txt","w");
clrscr();
fflush(stdin);printf("\n Enter the Number of students:");
scanf("%d",&no);
for(i=0;i
-
7/28/2019 C Lab Manual-1
46/54
{
temp=s[i];
s[i]=s[j];s[j]=temp;
}
}}
}
OUTPUT
Enter the Number of students: 2
Enter the name of student 1=Sundar
Enter the roll number=30
Enter the name of student 3=Reshma
Enter the roll number=25
Before Sorting...
30 Sundar
25 Reshma
After Sorting...
25 Reshma
30 Sundar
The data are written successfully to the file
RESULT
Thus the C program to accept a list of N numbers of students with their names and
roll numbers are arranged in an alphabetical order and successfully written to the file
using user defined data type using structure.
46
-
7/28/2019 C Lab Manual-1
47/54
EX. No. : 15 SORTING OF NAMES USING ARRAY OF POINTERS
AIM
To write a C program to sort the given names using array of pointers.
ALGORITHM
1. Start
2. Read n for number of names to be sorted
3. Read the names
4. Set loop i=0 to n and j=1 to n
5. Compare the first name with second name if is greater than 0
6. Interchange the first name and the second name using third variable
7. Repeat until all the names are sorted8. Print the sorted names
9. Stop
PROGRAM
/****** SORTING OF NAMES USING ARRAY OF POINTERS ******/
#include
#include#include#include
#define MAX 10
void main(){
clrscr();
char *names[MAX],temp[15],*temp1;int n,i,j,len;
printf("Enter the no. of names to be sorted");
scanf("%d",&n);
*names=NULL;fflush(stdin);
printf("enter the names one by one\n");
for(i=0;i
-
7/28/2019 C Lab Manual-1
48/54
strcpy(*(names+i),temp);
}
for(i=0;i
-
7/28/2019 C Lab Manual-1
49/54
EX. No. : 16 FUNCTIONS WITH STATIC DATA TYPE
AIM
To write a C program using functions with static data type.
ALGORITHM
1. Start
2. Call function f1()
3. Print after first call
4. Call function f1()
5. Print after second call
6. Call function f1()
7. Print after third call8. Stop
Function f1()
1. Initialize k= 0 as static and j=10
2. Print the value of k and j
3. Calculate k=k+10
PROGRAM
/********* FUNCTIONS WITH STATIC DATA TYPE*********/
#include int g = 10;
main()
{void f1();
clrscr();
f1();
printf(" after first call \n");f1();
printf(" after second call \n");
f1();printf(" after third call \n");
getch();
}
49
-
7/28/2019 C Lab Manual-1
50/54
void f1()
{
static int k=0;int j = 10;
printf("\nValue of k = %d and j = %d",k,j);
k=k+10;}
OUTPUT
Value of k = 0 and j = 10 after first call
Value of k = 10 and j = 10 after second call
Value of k = 20 and j = 10 after third call
RESULT
Thus the C program using functions with static data type was compiled and
executed successfully.
50
-
7/28/2019 C Lab Manual-1
51/54
EX. No. : 17(A) IMPLEMENTING DEL COMMANDS USING FILE
AIM
To write a C program to implement the DEL command using file.
ALGORITHM
1. Start
2. Pass the command line argument to the parameter argc and argv in main function
3. If argc is not equal to 2
4. Print parameter missing
5. Remove the entered file using remove function
6. If it is removed correctly print removed filename
7. Else print error
8. Stop
PROGRAM
/********************* DEL COMMAND USING FILE *******************/
#include
#include
int main(int argc,char *argv[])
{
if(argc!=2)printf("Parameter missing");
if (remove(argv[1]) == 0)
printf("Removed %s \n",argv[1]);else
perror(" ERROR");
return 0;
}
51
-
7/28/2019 C Lab Manual-1
52/54
OUTPUT
C:\TURBOC2> DELFILE details.txt
Removed details.txt
RESULT
Thus the C program to implement the DEL command using file was compiled and
executed successfully.
52
-
7/28/2019 C Lab Manual-1
53/54
EX. No. : 17(B) COPY COMMAND USING FILE
AIM
To write a C program to implement the COPY command using file.
ALGORITHM
1. Start
2. Pass the command line argument to the parameter argc and argv in main function
3. Check if argc not equal to 3
4. If true print parameter missing
5. Open the first file in reading mode and second file in append mode
6. Set loop to read the characters from the first file
7. Copy the character to second file
8. Repeat until EOF is reached
9. Print file is successfully copied
10. Stop
PROGRAM
/***************** COPY COMMAND USING FILE *****************/
#include#include
void main(int argc,char *argv[])
{FILE *fp1,*fp2;
int i,ch;
clrscr();if(argc!=3)
{
printf("Program parameter missing");exit(0);
}
fp1=fopen(argv[1],"r");
fp2=fopen(argv[2],"a");
53
-
7/28/2019 C Lab Manual-1
54/54
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
printf("file %s is successfully copied",argv[1]);getch();
}
OUTPUT
C:\TURBOC2> CREATE result.txt update.txt
file result.txt is successfully copied
RESULT