Sei sulla pagina 1di 16

Programming in C PL

(MCA - 193) MCA 1st Sem. 1st Year

Q1. Using call by reference concept write a function to swap 2 variables without using additional variable.
Program: PRG01.c

#include<stdio.h> #include<conio.h> void swap(int *,int *); void main() { int a,b; clrscr(); printf(" nter !alue of "st !ariable #"); scanf("$d",%a); printf(" nter !alue of &nd !ariable #"); scanf("$d",%b); printf("'n(((!alues )efore *wappin+((("); printf("'n!alue ,f "st !ariable# $d",a); printf("'n!alue ,f &nd !ariable# $d",b); swap(%a,%b); printf("'n'n(((!alues -fter *wappin+((("); printf("'n!alue ,f "st !ariable# $d",a); printf("'n!alue ,f &nd !ariable# $d",b); +etch(); . void swap(int *p,int */) { *p0*p1*/; */0*p(*/; *p0*p(*/; .

Assignment No. VI

Page - 1

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: nter !alue of "st !ariable #2 nter !alue of &nd !ariable #3 (((!alues )efore *wappin+((( !alue ,f "st !ariable# 2 !alue ,f &nd !ariable# 3 (((!alues -fter *wappin+((( !alue ,f "st !ariable# 3 !alue ,f &nd !ariable# 2

Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. 8ointers provide a powerful and fle7ible method for manipulatin+ data in pro+rams. 5o understand how to use pointers, a pro+rammer must have a basic 6nowled+e of how a computer stores information in memor4. 8ointers closel4 related to memor4 manipulation. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the pro+ram. *ome pro+ram routines can be more efficientl4 e7ecuted with pointers than without them, and some routines can be performed onl4 with pointers. )4 usin+ pointers, the use of this fi7ed, limited and shared resources should be optimi9ed. 5he unloaded resources from memor4 will release it for other applications utili9ation.

Assignment No. VI

Page - $

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q2. !rite a progra" to perfor" the bubble sort using pointer.


Program: PRG02.c

#include<conio.h> #include<stdio.h> void swap(int *,int *); void main() { int a:";;<; int i,n,=; clrscr(); printf(" nter the number of elements in the arra4 # "); scanf("$d",%n); printf("'n nter the values in the arra4 # 'n"); for(i0;;i<n;i11) { scanf("$d",%a:i<); . printf("'n)efore sortin+ 'n"); for(i0;;i<n;i11) printf("$d ",a:i<); for(i0;;i<n;i11) { for(=0;;=<n(i(";=11) { if(*(a1=)>*(a1=1")) swap(a1=,a1=1"); . . printf("'n'n-fter sortin+ 'n"); for(i0;;i<n;i11) printf("$d ",a:i<); +etch(); . void swap(int *p,int */) { int temp; temp0*p; *p0*/; */0temp; .

Assignment No. VI

Page - 3

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: nter the number of elements in the arra4 # 2 nter the values in the arra4 # 2 & 3 > )efore sortin+ 2&3> -fter sortin+ &>23 Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. 8ointers provide a powerful and fle7ible method for manipulatin+ data in pro+rams. 5o understand how to use pointers, a pro+rammer must have a basic 6nowled+e of how a computer stores information in memor4. 8ointers closel4 related to memor4 manipulation. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the pro+ram. *ome pro+ram routines can be more efficientl4 e7ecuted with pointers than without them, and some routines can be performed onl4 with pointers. )4 usin+ pointers, the use of this fi7ed, limited and shared resources should be optimi9ed. 5he unloaded resources from memor4 will release it for other applications utili9ation.

Assignment No. VI

Page - 4

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q#. !rite a progra" to print the ele"ents of a 2D array using pointer to array and array of pointer.
Program: PRG03.c

#include<stdio.h> #include<conio.h> void main() { int i,=,60;; int a:><:2<; int (*p):2<; int */:"&<; clrscr(); printf(" nter 5he elements of &? -rra4#'n"); for(i0;;i<>;i11) { for(=0;;=<2;=11) { scanf("$d",%a:i<:=<); /:6<0%a:i<:=<; 611; . . p0a; printf("(((8rintin+ @sin+ 8ointer to an -rra4((('n"); for(i0;;i<>;i11) AApointer to an arra4 { for(=0;;=<2;=11) printf("$d, ",*(*(p1i)1=)); printf("'n"); . 60;; printf("'n(((8rintin+ @sin+ -rra4 of pointer((('n"); for(i0;;i<>;i11) AA-rra4 ,f 8ointer { for(=0;;=<2;=11) { printf("$d, ",*/:6<); 611; . printf("'n"); . +etch(); .
Assignment No. VI Page - % Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: nter 5he elements of &? -rra4# & > 2 3 B C 2 > & C D E (((8rintin+ @sin+ 8ointer to an -rra4((( &, >, 2, 3, B, C, 2, >, &, C, D, E, (((8rintin+ @sin+ -rra4 of pointer((( &, >, 2, 3, B, C, 2, >, &, C, D, E, Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. 8ointers provide a powerful and fle7ible method for manipulatin+ data in pro+rams. 5o understand how to use pointers, a pro+rammer must have a basic 6nowled+e of how a computer stores information in memor4. 8ointers closel4 related to memor4 manipulation.

onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the pro+ram. *ome pro+ram routines can be more efficientl4 e7ecuted with pointers than without them, and some routines can be performed onl4 with pointers. )4 usin+ pointers, the use of this fi7ed, limited and shared resources should be optimi9ed. 5he unloaded resources from memor4 will release it for other applications utili9ation.

Assignment No. VI

Page -

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q$. !rite a progra" which passes a string %&his is a test' in co""and line and print those argu"ents one by one and also print the nu"ber of argu"ents.
Program: PRG04.c

#include<stdio.h> #include<conio.h> void main(int ar+c,char *ar+v:<) { int i; printf("'n5he number of ar+uments # $d'n",ar+c("); printf("'n5he -r+uments -re #'n"); for(i0";i<ar+c;i11) printf("'n$s",ar+v:i<); +etch(); . Output: F#'5F')GH>tcc pr+;2.c 5urbo F11 !ersion >.;; Fop4ri+ht (c) "EE& )orland Gnternational 8r+;2.c# 5urbo Iin6 !ersion 3.; Fop4ri+ht (c) "EE& )orland Gnternational -vailable memor4 2"2&EB; F#'5F')GH>pr+;2.e7e this is a test 5he number of ar+uments # 2 5he -r+uments -re # this is a test

Assignment No. VI

Page - &

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. 5he inte+er, ar+c is the argument count. Gt is the number of ar+uments passed into the pro+ram from the command line, includin+ the name of the pro+ram. 5he arra4 of character pointers is the listin+ of all the ar+uments. ar+v:;< is the name of the pro+ram, or an empt4 strin+ if the name is not available. -fter that, ever4 element number less than ar+c is a command line ar+ument. Jou can use each ar+v element =ust li6e a strin+, or use ar+v as a two dimensional arra4. ar+v:ar+c< is a null pointer. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the 8ro+ram.

Assignment No. VI

Page - '

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q(. )rint this pattern and the nu"ber of line pass as a co""and line argu"ent. 1 *1 *1* 1*1* 1*1*1
Program: PRG05.c

#include<stdio.h> #include<conio.h> void main(int ar+c,char *ar+v:<) { int i,=,6,c,p0"; if(ar+c00&) { c0atoi(ar+v:"<); for(i0";i<0c;i11) { for(60c(i;6>0";6(() printf(" "); for(=0";=<0i;=11) { printf("$d ",p$&); p11; . printf("'n"); . . +etch(); .

Assignment No. VI

Page - (

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: F#'?ocuments and *ettin+s'-dministrator>cd' F#'>cd tc F#'5F>cd bin F#'5F')GH>tcc pr+;3.c 5urbo F11 !ersion >.;; Fop4ri+ht (c) "EE& )orland Gnternational pr+;3.c# 5urbo Iin6 !ersion 3.; Fop4ri+ht (c) "EE& )orland Gnternational -vailable memor4 2"2"BD; F#'5F')GH>pr+;3.e7e 3 " ;" ;"; ";"; ";";" Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. 5he inte+er, ar+c is the argument count. Gt is the number of ar+uments passed into the pro+ram from the command line, includin+ the name of the pro+ram. 5he arra4 of character pointers is the listin+ of all the ar+uments. ar+v:;< is the name of the pro+ram, or an empt4 strin+ if the name is not available. -fter that, ever4 element number less than ar+c is a command line ar+ument. Jou can use each ar+v element =ust li6e a strin+, or use ar+v as a two dimensional arra4. ar+v:ar+c< is a null pointer. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the pro+ram

Assignment No. VI

Page - 1)

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q+. reate a file in write "ode, write so"ething into it and then copy its content into another file and -i"ultaneously display it.
Program: PRG0 .c

#include<stdio.h> #include<conio.h> void main() { KGI *fs,*ft; char ch,c; clrscr(); fs0fopen("file".t7t","w"); fputs("i am a student",fs); fclose(fs); fs0fopen("file".t7t","r"); ft0fopen("file&.t7t","w"); printf("'n5he Fontent ,f file"#'n"); while((c0f+etc(fs))L0 ,K) { fprintf(ft,"$c",(char) c); printf("$c",(char)c); . fclose(fs); fclose(ft); ft0fopen("file&.t7t","r"); printf("'n5he Fontent ,f file&#'n"); while((ch0f+etc(fs))L0 ,K) { printf("$c",(char)ch); . fclose(ft); +etch(); .

Assignment No. VI

Page - 11

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: 5he Fontent ,f file"# i am a student 5he Fontent ,f file&# i am a student Result & Discussion -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. ,K is a character which indicates the end of a file. Gt is returned b4 read commands of the +etc() and scanf() families when the4 tr4 to read be4ond the end of a file. H@II is returned b4 read commands of the +ets famil4 when the4 tr4 to read be4ond the end of an input file. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the 8ro+ram. 5he variet4 of different t4pes of input and output, usin+ standard input or output, files or character strin+s ma6e F a ver4 powerful lan+ua+e. 5he addition of character input and output ma6e it hi+hl4 suitable for applications where the format of data must be controlled ver4 precisel4.

Assignment No. VI

Page - 1$

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q.. reate a structure e"p/char na"e02*1,int sal2.create the database of so"e e"ployee and display it using fprintf34,fscanf34 functions.
Program: PRG0!.c

#include<stdio.h> #include<conio.h> t4pedef struct emp { char name:&;<; lon+ int sal; .emp; void main() { emp e:&;<; int i,n; char c; KGI *fp; clrscr(); printf("enter the number of records#"); scanf("$d",%n); for(i0;;i<n;i11) { printf("enter record no $d# ",i1"); printf("'n*************'n"); printf("H-M # "); scanf("$s",e:i<.name); printf("*-I-NJ#"); scanf("$d",%e:i<.sal); . printf("'nNecords are#'n"); fp0fopen("ft".t7t","w"); for(i0;;i<n;i11) { fprintf(fp,"name##$s",e:i<.name); fprintf(fp,"'nsalar4#$d'n",e:i<.sal); . fclose(fp); fp0fopen("ft".t7t","r"); while((fscanf(fp,"$c",%c))L0 ,K) printf("$c",c); fclose(fp); +etch(); .

Assignment No. VI

Page - 13

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: enter the number of records#> enter record no "# ************* H-M # =adu *-I-NJ#&3;;; enter record no &# ************* H-M # sham *-I-NJ#>;;;; enter record no ># ************* H-M # ram *-I-NJ#"3;;; Necords are# name##=adu salar4#&3;;; name##sham salar4#>;;;; name##ram salar4#"3;;; Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. ,K is a character which indicates the end of a file. Gt is returned b4 read commands of the +etc() and scanf() families when the4 tr4 to read be4ond the end of a file. H@II is returned b4 read commands of the +ets famil4 when the4 tr4 to read be4ond the end of an input file. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the 8ro+ram. 5he variet4 of different t4pes of input and output, usin+ standard input or output, files or character strin+s ma6e F a ver4 powerful lan+ua+e. 5he addition of character input and output ma6e it hi+hl4 suitable for applications where the format of data must be controlled ver4 precisel4.

Assignment No. VI

Page - 14

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Q5. !rite a progra" to copy the content of a file into another file. &he na"es of the two file should be sent as co""and line argu"ent.
Program: PRG0".c

#include<stdio.h> #include<conio.h> void main(int ar+c,char *ar+v:<) { KGI *ft,*fs; char ch:";;<,c; if(ar+c00>) { fs0fopen(ar+v:"<,"w"); printf("'n nter the content of $s file #'n",ar+v:"<); fputs(+ets(ch),fs); fclose(fs); fs0fopen(ar+v:"<,"r"); ft0fopen(ar+v:&<,"w"); do{ c0f+etc(fs); fprintf(ft,"$c",(char)c); .while(cL0 ,K); fclose(ft); fclose(fs); printf("'n5he content of file $s is copied to file $s'n",ar+v:"<,ar+v:&<); ft0fopen(ar+v:&<,"r"); printf("'n5he content of $s file #'n",ar+v:&<); do{ c0f+etc(ft); printf("$c",(char)c); .while(cL0 ,K); fclose(ft); . else printf("8lease nter *ource and ?estination file name onl4"); +etch(); .

Assignment No. VI

Page - 1%

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Programming in C PL
(MCA - 193) MCA 1st Sem. 1st Year

Output: F#'5F')GH>tcc pr+;D.c 5urbo F11 !ersion >.;; Fop4ri+ht (c) "EE& )orland Gnternational pr+;D.c# 5urbo Iin6 !ersion 3.; Fop4ri+ht (c) "EE& )orland Gnternational -vailable memor4 2">E&EB F#'5F')GH>pr+;D.e7e source.t7t destination.t7t nter the content of source.t7t file # G am student of fiem 5he content of file source.t7t is copied to file destination.t7t 5he content of destination.t7t file # G am student of fiem Result & Discussion: -t first, necessar4 header files are added. -ll the necessar4 variables are declared under the function main().5he lo+ics are also 6ept under the function main() with the re/uired operators that has been re/uired for the pro+ram, 6eepin+ all the s4nta7 accuratel4, as we 6now. ,K is a character which indicates the end of a file. Gt is returned b4 read commands of the +etc() and scanf() families when the4 tr4 to read be4ond the end of a file. H@II is returned b4 read commands of the +ets famil4 when the4 tr4 to read be4ond the end of an input file. 5he inte+er, ar+c is the argument count. Gt is the number of ar+uments passed into the pro+ram from the command line, includin+ the name of the pro+ram. 5he arra4 of character pointers is the listin+ of all the ar+uments. ar+v:;< is the name of the pro+ram, or an empt4 strin+ if the name is not available. -fter that, ever4 element number less than ar+c is a command line ar+ument. Jou can use each ar+v element =ust li6e a strin+, or use ar+v as a two dimensional arra4. ar+v:ar+c< is a null pointer. onclusion: ,n usin+ of correct lo+ic with proper s4nta7 in the proper place we +ot the re/uired output for the 8ro+ram. 5he variet4 of different t4pes of input and output, usin+ standard input or output, files or character strin+s ma6e F a ver4 powerful lan+ua+e. 5he addition of character input and output ma6e it hi+hl4 suitable for applications where the format of data must be controlled ver4 precisel4.

Assignment No. VI

Page - 1

Subhasish Dey, 13/MCA/4 , MCA De!t, "I#M

Potrebbero piacerti anche