CS11001/CS11002 Programming and Data Structures (PDS...
Transcript of CS11001/CS11002 Programming and Data Structures (PDS...
![Page 1: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/1.jpg)
CS11001/CS11002ProgrammingandDataStructures
(PDS)(Theory:3-0-0)
Teacher:SourangshuBha@[email protected]
h@p://cse.iitkgp.ac.in/~sourangshu/
DepartmentofComputerScienceandEngineeringIndianInsJtuteofTechnologyKharagpur
![Page 2: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/2.jpg)
AcompleteCprogram
voidPRINT(){
prin1(“HelloEverybody!!!\n”);}
voidPRINT_I(intb){
prin1(“%d\n”,b);}
voidPRINT_F(floatp,floatq){
prin1(“%2.1f%2.2f\n”,p,q);}
/*prog.c*/#include<stdio.h>voidPRINT();voidPRINT_I(int);voidPRINT_F(float,float);voidmain(){
inta=10;floatx,y;scanf(“%d%d”,&x,&y);PRINT();PRINT_I(a);PRINT_F(x,y);
}
![Page 3: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/3.jpg)
CompilaJonandExecuJon
$cc–Wallprog.c$$./a.out2.343.45HelloEverybody!!!102.33.45$
![Page 4: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/4.jpg)
AcompleteCprogram/*prog.c*/#include<stdio.h>voidPRINT();voidPRINT_I(int);voidPRINT_F(float,float);voidmain(????){
inta=10;floatx,y;scanf(“%d%d”,&x,&y);PRINT();PRINT_I(a);PRINT_F(x,y);
}
voidPRINT(){
prin1(“HelloEverybody!!!\n”);}
voidPRINT_I(intb){
prin1(“%d\n”,b);}
voidPRINT_F(floatp,floatq){
prin1(“%2.1f%2.2f\n”,p,q);}
![Page 5: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/5.jpg)
CommandLineArguments
• Commandlineargumentsmaybepassedbyspecifyingthemundermain().
intmain(intargc,char*argv[]);
Argument Count Array of Strings
as command line arguments including the command itself.
![Page 6: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/6.jpg)
Passingparameterstomain()
#include<stdio.h>intmain(intargc,char*argv[]){inti;for(i=0;i<argc;i++){prin1("%d:%s\n",i,argv[i]);}return0;}
$cc-Wallwk13_commandline.c$./a.out0:./a.out$./a.outHI0:./a.out1:HI$./a.outIndianInshtuteofTechnology0:./a.out1:Indian2:Inshtute3:of4:Technology
![Page 7: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/7.jpg)
Passingparameterstomain()
#include<stdio.h>intmain(intargc,char*argv[]){inti;for(i=0;i<argc;i++){prin1("%d:%s\n",i,argv[i]);}return0;}
$Jme./a.outIndianInsJtuteofTechnology0:./a.out1:Indian2:Inshtute3:of4:Technologyreal0m0.002suser0m0.001ssys0m0.001s
![Page 8: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/8.jpg)
Passingparameterstomain()#include<stdio.h>intmain(intargc,char*argv[]){inti;for(i=0;i<argc;i++){prin1("%d:%s\n",i,argv[i]);}return0;}
$./a.outAnirban218.30:./a.out1:Anirban2:213:8.3
./a.out Anirban 21 8.3
argc=4
./a.out Anirban
21 argv
8.3
![Page 9: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/9.jpg)
LibraryfuncJonsscanf()
• Headerfile:– #include<stdio.h>
• Funchonprototype:– intsscanf(constchar*str,constchar*format,...);
• Conversioncharacterissameasscanf().
![Page 10: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/10.jpg)
Passingparameterstomain()
#include<stdio.h>intmain(intargc,char*argv[]){inti;for(i=0;i<argc;i++){prin1("%d:%s\n",i,argv[i]);}return0;}
#include<stdio.h>intmain(intargc,char*argv[]){charname[20];intage;floatcgpa;sscanf(argv[1],"%s",name);sscanf(argv[2],"%d",&age);sscanf(argv[3],"%f",&cgpa);prin1("%s%d%f\n",
name,age,cgpa);return0;}
$./a.outAnirban218.30:./a.out1:Anirban2:213:8.3$
$./a.outAnirban218.3Anirban218.300000$
![Page 11: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/11.jpg)
FuncJonstoconvertstringstonumbers
• Oncewe'vegotastringwithanumberinit(eitherfromafileorfromtheusertyping)wecanuseatoioratoftoconvertittoanumber
• Thefunchonsarepartofheaderfilestdlib.hchar numberstring[]= "3.14"; int i; double pi; pi= atof (numberstring); i= atoi ("12"); Bothofthesefunchonsreturn0iftheyhaveaproblem
![Page 12: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/12.jpg)
Example:AveragefromCommandLine
#include<stdio.h>#include<stdlib.h>intmain(intargc,char*argv[]){floatsum=0;inti,num;num=argc-1;for(i=1;i<=num;i++) sum+=atof(argv[i]);prin1("Average=%f\n",sum/(float)num);return0;}
$ ./a.out 45 239 123 Average=135.666667 $
![Page 13: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/13.jpg)
Passingparameterstomain()• Twoparameterswillbepassedtofunchonmain()through
commandline–argcandargv.
• Nameoftheparametersarefixed.
• argcisofintegertypeanditstoresthenumberofparameters(delimitedbyspace)inthecommandline.
• argvisa2Darrayofcharactersanditstoresallthewordsinthecommandline.
• Bydefaultalltheparametersaretakenasarrayofcharacters(strings)thatcanbeconvertedtootherdatatypes.
![Page 14: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/14.jpg)
FileHandling
![Page 15: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/15.jpg)
AdvantagesofFilehandling
• Athmessizeoftheprograminputisverylarge.
• Duringtheteshngphaseprovidinginputsintheinterachvewayistedious.
• Thesizeoftheprogramoutputmaybelargeenoughandwillnotfitinasinglescreen.
• Youmaywishtostoretheoutputforfutureanalysis.
![Page 16: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/16.jpg)
FilehandlinginC
• Afileneedstobeopenedfirstforanyinput/outputoperahonsonthefile.– Itmaybeopenedforreading/wrihng/appending.
• Thefilemustbeclosedoncetheuse/handlingofthefileisover.
• Inbetweentheaddressofthefilewillbestoredinapointerdatatypeviz.,FILE*.
![Page 17: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/17.jpg)
FilehandlinginC
• InCweuseFILE*torepresentapointertoafile.• fopenisusedtoopenafile.ItreturnsapointertothefileifsuccessfullyopenedthefileelseitreturnsNULL.
FILE *fptr; char filename[]= "file2.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf (“ERROR IN FILE CREATION”); /* DO SOMETHING */ }
![Page 18: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/18.jpg)
exit()funcJon• Somehmeserrorcheckingmeanswewantan"emergencyexit"fromaprogram.Wewantittostopdead.
• Inmainwecanuse"return"tostop.• Infunchonswecanuseexittodothis.• Libraryfilestdlib.histheheaderfileforexit()funchon.
FILE*fptr;charfilename[]="file2.dat";fptr=fopen(filename,"w");if(fptr==NULL){prin1(“ERRORINFILECREATION”);exit(-1);}
![Page 19: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/19.jpg)
Modesforopeningfiles
• Thesecondargumentoffopenisthemodeinwhichweopenthefile.
• "r"opensafileforreading.
• "w"createsafileforwrihng-andwritesoverallpreviouscontents(deletesthefilesobecareful!).
• "a"opensafileforappending-wrihngontheendofthefile.
![Page 20: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/20.jpg)
Closingafile
• Wecancloseafilesimplyusingfclose()andthefilepointer.
FILE *fptr; char filename[]= "myfile.dat"; fptr= fopen (filename,"w"); if (fptr == NULL) { printf ("Cannot open file to write!\n"); exit(-1); } fprintf (fptr,"Hello World of filing!\n"); fclose (fptr);
Opening
Access
closing
![Page 21: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/21.jpg)
WriJngtoafileusingfprin\()
• fprin1()worksjustlikeprin1andsprin1exceptthatitsfirstargumentisafilepointer.
FILE *fptr; fptr= fopen ("file.dat","w"); /* Check it's open */ fprintf (fptr,"Hello World!\n");
![Page 22: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/22.jpg)
ReadingDataUsingfscanf()
FILE*fptr;fptr=fopen(“input.dat”,“r”);/*Checkit'sopen*/if(fptr==NULL){prin1(“Errorinopeningfile\n”);}fscanf(fptr,“%d%d”,&x,&y);
20 30
input.dat
x=20 y=30
![Page 23: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/23.jpg)
ReadinglinesfromafileusingWecanreadastringusingfgets().
FILE *fptr; char line [1000]; /* Open file and check it is open */ while (fgets(line,1000,fptr) != NULL) { printf ("Read line %s\n",line); }
fgets() takes 3 arguments, a string, a maximum number of characters to read and a file pointer. It returns NULL if there is an error (such as EOF).
![Page 24: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/24.jpg)
Checkwhetheramatrixissymmetricornot–V1
#include<stdio.h>intReadMat(intmat[1000][1000]){inti,j,size;prin1(“Matrixsize?:\n");scanf("%d",&size);for(i=0;i<size;i++)for(j=0;j<size;j++)scanf("%d",&mat[i][j]);returnsize;}
intmain(){charsymm='y';inti,j,size,mat[1000][1000];size=ReadMat(mat);for(i=0;i<size;i++){for(j=i+1;j<size;j++){if(mat[i][j]!=mat[j][i])symm='n';break;}if(symm=='n')break;}if(symm=='y')prin1("SymmetrixMatrix\n");elseprin1("NotSymmetric");return0;}
![Page 25: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/25.jpg)
Checkwhetheramatrixissymmetricornot–V2
#include<stdio.h>intReadMat(intmat[1000][1000]){inti,j,size;FILE*fp;fp=fopen("Matrix.txt","r");//prin1(“Matrixsize:\n");fscanf(fp,"%d",&size);for(i=0;i<size;i++)for(j=0;j<size;j++)fscanf(fp,"%d",&mat[i][j]);fclose(fp);returnsize;}
intmain(){charsymm='y';inti,j,size,mat[1000][1000];size=ReadMat(mat);for(i=0;i<size;i++){for(j=i+1;j<size;j++){if(mat[i][j]!=mat[j][i])symm='n';break;}if(symm=='n')break;}if(symm=='y')prin1("SymmetrixMatrix\n");elseprin1("NotSymmetric");return0;}
![Page 26: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/26.jpg)
Checkwhetheramatrixissymmetricornot–V3
#include<stdio.h>intReadMat(intmat[1000][1000]){charfilename[100];inti,j,size;FILE*fp;scanf(“%s”,filename);fp=fopen(filename,"r");//prin1(“Matrixsize:\n");fscanf(fp,"%d",&size);for(i=0;i<size;i++)for(j=0;j<size;j++)fscanf(fp,"%d",&mat[i][j]);fclose(fp);returnsize;}
intmain(){charsymm='y';inti,j,size,mat[1000][1000];size=ReadMat(mat);for(i=0;i<size;i++){for(j=i+1;j<size;j++){if(mat[i][j]!=mat[j][i])symm='n';break;}if(symm=='n')break;}if(symm=='y')prin1("SymmetrixMatrix\n");elseprin1("NotSymmetric");return0;}
![Page 27: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/27.jpg)
Checkwhetheramatrixissymmetricornot–V4
#include<stdio.h>intReadMat(charfilename,intmat[1000][1000]){inti,j,size;FILE*fp;fp=fopen(filename,"r");//prin1(“Matrixsize:\n");fscanf(fp,"%d",&size);for(i=0;i<size;i++)for(j=0;j<size;j++)fscanf(fp,"%d",&mat[i][j]);fclose(fp);returnsize;}
intmain(intargc,char*argv[]){charsymm='y';inti,j,size,mat[1000][1000];size=ReadMat(argv[1],mat);for(i=0;i<size;i++){for(j=i+1;j<size;j++){if(mat[i][j]!=mat[j][i])symm='n';break;}if(symm=='n')break;}if(symm=='y')prin1("SymmetrixMatrix\n");elseprin1("NotSymmetric");return0;}
![Page 28: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/28.jpg)
BalancedSymbolChecking
Inprocessingprogramsandworkingwithcomputerlanguagestherearemanyinstanceswhensymbolsmustbebalanced{},[],()
WriteaprogramthatwilltakeaCprogramfileascommandlineinputandprintswhetheralltheparenthesis,curlyandsquarebracketsthatareopenedhasclosedornot.
![Page 29: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/29.jpg)
BalancedSymbolCheckingwhile(line[i]!='\0'){switch(line[i]){case'(':pbracket++;break;case')':pbracket--;break;case'{':cbracket++;break;case'}':cbracket--;break;case'[':sbracket++;break;case']':sbracket--;break;}i++;}}fclose(fp);if(pbracket==0)prin1("ParentesisOpen-Close.\n");elseprin1("ParentesisMismatches.\n");if(cbracket==0)prin1("CurlyOpen-Close.\n");elseprin1("CurlyMismatches.\n");if(sbracket==0)prin1("SquareOpen-Close.\n");elseprin1("SquareMismatches.\n");return0;}
#include<stdio.h>#include<stdlib.h>intmain(intargc,char*argv[]){FILE*fp;charline[500];inti,pbracket,cbracket,sbracket;fp=fopen(argv[1],"r");if(fp==NULL){prin1("Fileopeningerror...exihng");exit(0);}pbracket=cbracket=sbracket=0;while(!feof(fp)){fgets(line,100,fp);i=0;
Didfileopenproperly?
Fileendisnotreached.
![Page 30: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/30.jpg)
BalancedSymbolChecking
Inprocessingprogramsandworkingwithcomputerlanguagestherearemanyinstanceswhensymbolsmustbebalanced{},[],()
FollowingCsyntax: - {}[]()….isallowed - {[}]()…isnotallowed.
![Page 31: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/31.jpg)
Algorithm:BalancedSymbolChecking
• Makeanemptystack
• Readcharactersunhlendoffile– Ifasymbolisanopeningsymbolpushitontothestack– Ifasymbolisaclosingsymbolpopthestack
• ifthestackisemptyreportanerror• ifthepoppedsymboldoesnotmatchtheclosingsymbolreportanerror
• Ifthestackisemptyreportsymbolsarebalanced.
• Elsereportanerror
HomeworkWriteaCprogramthatwilltakeanyCprogramascommandlineinputandwillreportanysyntaxerrorduetobalancinginbracketsymbols.
![Page 32: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/32.jpg)
ThreespecialI/Ostreams
• Threespecialfilestreamsaredefinedinthe<stdio.h>header
• stdinreadsinputfromthekeyboard
• stdoutsendoutputtothescreen
• stderrprintserrorstoanerrordevice(usuallyalsothescreen)
• Whatmightthisdo?
fprintf (stdout,"Hello World!\n");
![Page 33: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/33.jpg)
Anexampleprogram#include<stdio.h>intmain(){ inti; fprin1(stdout,"Givevalueofi\n"); fscanf(stdin,"%d",&i); fprin1(stdout,"Valueofi=%d\n",i); fprin1(stderr,"Noerror:Butanexampletoshowerrormessage.\n");
return0;}$./a.outGivevalueofi15Valueofi=15Noerror:Butanexampletoshowerrormessage.$
Display on the screen
![Page 34: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/34.jpg)
InputFile&OutputFileredirecJon
• Onemayredirecttheinputandoutputfilestootherfiles(otherthanstdinandstdout).
• Usage:Supposetheexecutablefileisa.out$ ./a.out <in.dat >out.dat
15
in.dat
Give value of i Value of i=15
out.dat
No error: But an example to show error message.
Display screen
![Page 35: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/35.jpg)
Example1:Reverseastackusingrecursion
Youarenotallowedtouseloopconstructslikewhile,for..etc
Sampleinput:1234Sampleoutput:4321
4 3 2 1
top
1 2 3 4
top
![Page 36: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/36.jpg)
Example2:Sortastackusingrecursion
Youarenotallowedtouseloopconstructslikewhile,for..etc
Sampleinput:1418-530Sampleoutput: 301814-5
30 -5 18 14
top
-5 14 18 30
top
![Page 37: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/37.jpg)
Example3:ImplementaJonofMergingoperaJoninMergeSortusing
Stacks
-9 -3 56 90
top
-5 14 18 30
top
18 30 56 90
top
-9 -5 -3 14
![Page 38: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/38.jpg)
Example4:Givenastack,printtheNextGreaterElement(NGE)foreveryelement.
• Foranystackthebo}ommostelementalwayshasnextgreaterelementas-1
• Forastackwhichissortedindecreasingorder,allelementshavenextgreaterelementas-1.
• Fortheinputstack[4,5,2,25],thenextgreaterelementsforeachelementareasfollows.
• Element NGE4 55 252 2525 -1
![Page 39: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/39.jpg)
Example5:ImplementaStackusingaLinkedList
-9 -3 56 90
top
90 56 -3 -9
head
![Page 40: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/40.jpg)
Example6:PrintfirstNFibonacciNumbersusingaQueue
Thequeueinihallycontains0and1
1 0
frontrear
![Page 41: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/41.jpg)
Example7:UseaStacktoreverseaQueue
30 -5 18 14
frontrear
14 18 -5 30
top
14 18 -5 30
frontrear
![Page 42: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/42.jpg)
Example 8: Create a newQueuewith givenelementsappendedattheendoftheQueueinareverseorder
*Hint-Youcanuseastackinordertoachievetheoutcome
30 -5 18 14
frontrear
14 18 -5 30
frontrear
30 -5 18 14
![Page 43: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/43.jpg)
Example9:ImplementaStackusingaQueuedatastructure
ForagivenstackcreateasamesizearraywhichyouaregoingtouseasaQueue.
Pushandpopoperahonofstack’sshouldbeemulatedwiththeEnqueueandDequeueoperahon.
YoucanuseanintermediateQueuefortheaboveimplementahon.
![Page 44: CS11001/CS11002 Programming and Data Structures (PDS ...pds/semester/2017s/slides/Presentation12.pdfPassing parameters to main() • Two parameters will be passed to funchon main()](https://reader033.fdocuments.net/reader033/viewer/2022050418/5f8e00b1f2a38111bc5fdede/html5/thumbnails/44.jpg)
Example10:ImplementaQueueusingaLinkedList
30 -5 18 14
head 30 -5 18 14
frontrear