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]
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. 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.
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
Project
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.
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 menu’s 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.
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 Start→Run→c: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
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.50
Total : 562.50
OUTPUT 2:
Amount of purchase: 1773
Purchase is: 1773.00
Tax : 221.62
Total : 1994.62
RESULT:
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:
Open your letter in Word and then bring up the Mail Merge Sidebar Wizard by going to Tools > Letters and Mailings > Mail Merge.
The sidebar will appear on the right side of the window, next to your document.
Check the ‘letter’ option from the document type and click Next below the wizard.
Leave the ‘Use the current document’ checked and click on ‘Next: Select recipients’ to move on.
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 click open.
Mail merge recipient dialog box will appear choose the recipients, you want to send letter from the list and then click ok.
The next part is inserting recipient information from the data source into the Word document.
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.
Select the field that you want to insert and click ‘Insert’. It will replace the selected section of text with “«field_name».”
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’.
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.
Once your merge is set up correctly you can complete it by clicking next ‘complete the merge’.
Next you can choose to either print the letters or view 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.
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.
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.
Thus the document is merged successfully with the selected recipient.
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:
Enter the data in the worksheet as shown in question
Choose Chart option from the Insert menu, or choose the Chart Wizard button from the standard toolbar
Shows the chart wizard Step1 of 4 Chart Type Dialog Box.
Choose the appropriate chart type from the chart type list box then click next.
Shows the chart wizard step 2 of 4 Chart Source Data Dialog Box then click Next.
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 click Next.
Shows the chart wizard step 4 of 4 Chart Location Dialog Box then click Finish.
Now the chart is displayed in your active worksheet, as shown below.
SNAPSHOTS
Figure 1: XY chart
Figure 2: Bar chart
Figure 3: Pie chart
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
Start
Read three numbers A, B, C
Compare whether A is greater than B and C using conditional operator
If it is equal then print “A is greater”
If step 3 is false check whether B is greater than A and C using conditional operator.
If it is true print “B is greater”
If step 5 is false print “C is greater”
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");
(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.
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
Start
Read two numbers A, B
Copy A to TEMP
Copy B to A
Copy TEMP to B
Print A, B
Calculate A=A+B, B=A-B, A=A-B
Display A, B
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;
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.
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<0 then go to 2, otherwise go to 5.
12. Compute the value of the roots (i.e) E=-B/2A and F=-B-sqrt(D)/2A.
13. Display the values of the roots and go to 5.
14. Stop.
PROGRAM:
/* * * PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION * * * /
#include
#include
#include
void main()
{
int d;
int a,b,c;
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);
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
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.
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
Start
Read a string CH
Loop until CH is equal to new line
Check whether the entered character is vowel or a digit using switch case
If it is vowel increment VOWEL
If it is digit increment DIGIT
End loop
Print VOWEL, DIGIT
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')
{
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();
}
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.
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<=N).
6: Assign c=0 check the process j<=i using for loop.
5: Check for the condition (i%j==0) then c=c+1.
6: Check for the condition whether c=2, then display the value.
7: Stop
PROGRAM:
/* * * * * * * * * PRIME NUMBERS * * * * * * * * * * */
#include
#include
void main()
{
clrscr();
int n,p,i,j,c;
printf("Enter the limit");
scanf("%d",&n);
printf("\n The prime numbers are");
for (i=1; i<=n; i++)
{
c=0;
for(j=1;j<=n;j++)
{
if((i%j)==0)
{
c=c+1;
}
}
if(c= =2)
{
printf("\n%d",i);
}
}
getch();
}
OUTPUT :
Enter the limit: 10
The prime numbers are:
2
3
5
7
RESULT:
Thus the C program to generate prime numbers in the given range was compiled and executed successfully.
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<3 stop the program
5. Print FIB2
6. Loop until COUNTER<=NUMBERS
7. Increment COUNTER by 1
8. FIB3=FIB1+FIB2
9. Print FIB3
10. Copy FIB2 to FIB1
11. Copy FIB3 to FIB2
12. End loop
13. Stop
PROGRAM:
/*************FIBONACCI SERIES ***************/
#include
#include
#include
void main()
{
clrscr();
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 <= numbers);
getch();
}
OUTPUT:
How many Fibonacci number you need? : 5
1
1
2
3
5
RESULT
Thus the Fibonacci series for a given range was compiled and executed successfully.
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
Start
Read ANGLE, N
Calculate X=ANGLE*3.14/180
Initialize TERM=1, SUM=1, Y=1
Loop i=1 to n
Y= Y*(2*i*(2*i-1));
TERM=(-TERM*X*X)/Y)
Increment SUM to TERM
End loop
Print ANGLE, X, SUM
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
#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;i0
Y= Y*(2*i)*(2*i+1);
Calculate TERM=-TERM*X*X/Y
Increment I to 1
SUM=SUM+TERM
TERM=sine(--n,x)
return SUM
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<0)
{
printf("ERROR:Give positive value for n\n");
exit(0);
}
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.
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
Start
Read the sentence in bin variable from the user
Separate the words and store it in temp variable which is two dimension array
Compare the consecutive words in temp upto the word count
If it is not equal print the word else do not print that word
Repeat 4 and 5 until all the words in temp are compared
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++;
}
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
#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;i++)
{
scanf("%d %s",&a[i].item_code,&a[i].item_name);
}
for(i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(a[i].item_code>a[j].item_code)
{
temp=a[i].item_code;
a[i].item_code=a[j].item_code;
a[j].item_code=temp;
}
}
}
for(i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(strcmp(a[i].item_name,a[j].item_name)<0)
{
strcpy(temp1,a[i].item_name);
strcpy(a[i].item_name,a[j].item_name);
strcpy(a[j].item_name,temp1);
}
}
}
printf("Item_Code \t\t\t Item_Name");
for(i=0;i<7;i++)
{
printf("\n");
printf("%d \t\t %s",a[i].item_code,a[i].item_name);
}
getch();
}
OUTPUT
Enter the item code and item name
102 Paste-Colgate
102 Paste-Pepsodent
102 Paste-Close-up
101 Soap-Cinthol
101 Soap-Lux
101 Soap-Hamam
101 Soap-Dove
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
RESULT
Thus the list of items using array of structures was successfully compiled and executed.
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 an alphabetical order and write the details in the file.
ALGORITHM
Start
Create a user-defined structure tag with the members
name: character
rno: integer
Open the file details .txt in writing mode
Read no for the number of student details
Read all the student details in name, rno
Before sorting print the student details and same time write it in the file details.txt
Using function sort(), sort the names upto no count
After sorting print the student details and same time write it in the file
Stop
PROGRAM
/********* USER-DEFINED DATA TYPE USING STRUCTURE ********/
#include
#include
#include
struct tag
{
char name[10];
int rno;
};
typedef struct tag node;
node s[10],t[10];
void main()
{
FILE *fp;
int no,i;
fp=fopen("details.txt","w");
clrscr();
fflush(stdin);
printf("\n Enter the Number of students:");
scanf("%d",&no);
for(i=0;i 0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
OUTPUTEnter 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.
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
Start
Read n for number of names to be sorted
Read the names
Set loop i=0 to n and j=1 to n
Compare the first name with second name if is greater than 0
Interchange the first name and the second name using third variable
Repeat until all the names are sorted
Print the sorted names
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;i0)
{
temp1=*(names+i);
*(names+i)=*(names+j);
*(names+j)=temp1;
}
}
printf("\n the sorted list of names:\n");
for(i=0;i
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();
}
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.
EX. No. : 17(A) IMPLEMENTING DEL COMMANDS USING FILE
AIM
To write a C program to implement the DEL command using file.
ALGORITHM
Start
Pass the command line argument to the parameter argc and argv in main function
If argc is not equal to 2
Print “parameter missing”
Remove the entered file using remove function
If it is removed correctly print “removed filename”
Else print error
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;
}
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.
EX. No. : 17(B) COPY COMMAND USING FILE
AIM
To write a C program to implement the COPY command using file.
ALGORITHM
Start
Pass the command line argument to the parameter argc and argv in main function
Check if argc not equal to 3
If true print “parameter missing”
Open the first file in reading mode and second file in append mode
Set loop to read the characters from the first file
Copy the character to second file
Repeat until EOF is reached
Print “file is successfully copied”
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");
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
Thus the C program to implement the COPY command using file was compiled and executed successfully.
19