├── .vscode ├── settings.json └── tasks.json ├── README.md ├── compiler-design ├── calculator.l ├── calculator.y ├── dfa_parity_checking.l ├── generate_assembly_lan.c ├── intermediate _code_generation.l ├── intermediate _code_generation.y ├── lexical_analyzer.c ├── lexical_analyzer.l ├── lowerToUpper.l ├── remove_whitespaces.l ├── shortened-pgms │ ├── exp1.l │ ├── exp10.l │ ├── exp11.l │ ├── exp11.y │ ├── exp12.c │ ├── exp2.l │ ├── exp3.l │ ├── exp4.c │ ├── exp5.l │ ├── exp6.l │ ├── exp6.y │ ├── exp7.l │ ├── exp7.y │ ├── exp8.l │ ├── exp8.y │ ├── exp9.l │ ├── input.txt │ └── program.txt ├── valid_expression.l ├── valid_expression.y ├── valid_identifier.l └── valid_identifier.y ├── conversions └── infixToPostfix.c ├── data_structures ├── array │ ├── circularQueue.c │ ├── circularQueue.exe │ ├── linearQueue.c │ ├── linearQueue.exe │ ├── priorityQueue.c │ └── stack.c ├── binary_trees │ ├── binarySearchTree.c │ ├── binarySearchTree.exe │ └── binaryTree.c ├── graphs │ └── graphUsingAdjacencyMatrix.c └── linked_lists │ ├── doubly_linked_list.c │ ├── linked_list.c │ └── linked_list.exe ├── misc ├── even_odd.c ├── highestCA.c ├── matInverse.c ├── pascalsTraingle.c ├── pascalsTraingle.exe ├── pointers.c ├── primeRegNo.c ├── productOfPositive.c ├── reverse.c ├── shoppingBill.c ├── smallest_odd.c ├── starPyramid.c └── starPyramid.exe ├── network-lab-programs ├── Dictionary │ ├── client │ ├── client.c │ ├── server │ └── server.c ├── DistanceVectorRouting │ └── dvr.c ├── FTP │ ├── client │ ├── client.c │ ├── file.txt │ ├── new.txt │ ├── server │ └── server.c ├── MultiUserChat │ ├── client │ ├── client.c │ ├── server │ └── server.c ├── SMTP │ ├── client │ ├── client.c │ ├── server │ └── server.c ├── SlidingWindowProtocol │ ├── client │ ├── client.c │ ├── server │ └── server.c ├── TCP │ ├── client │ ├── client.c │ ├── server │ └── server.c └── UDP │ ├── client │ ├── client.c │ ├── server │ └── server.c ├── os-programs ├── .vscode │ └── tasks.json ├── bankersAlgo.c ├── cpu-scheduling-algos │ ├── fcfs.c │ ├── priority.c │ ├── roundRobin.c │ └── sjf.c ├── disk-scheduling-algos │ ├── cscan_disk.c │ ├── fcfs_disk.c │ └── scan_disk.c ├── interprocess-comm-pgm │ ├── reader.c │ └── writer.c ├── mem-allocation-algos │ ├── bestFit.c │ ├── firstFit.c │ └── worstFit.c ├── page-replacement-algos │ ├── fifo.c │ ├── lfu.c │ └── lru.c ├── semaphorePC.c ├── shell-prgms │ ├── Armstrong.sh │ ├── Calculator.sh │ └── largest.sh ├── system-calls │ ├── creat.c │ ├── dir.c │ ├── execv.c │ ├── pid.c │ ├── stats.c │ └── wait.c └── two-pass-assembler │ ├── .vscode │ └── tasks.json │ ├── input.txt │ ├── intermediate.txt │ ├── optab.txt │ ├── output.txt │ ├── pass1.c │ ├── pass2.c │ └── symtab.txt ├── polynomial_arithmetics ├── polyAddUsingLinkedList.c ├── polyMultUsingLinkedList.c ├── polyMultUsingList.c ├── polynomialAddUsingArray.c ├── polynomialAddUsingLinkedList.c ├── polynomialAddUsingStruct.c ├── polynomialMultUsingArray.c └── polynomialMultUsingLinkedList.c ├── searching ├── binary_search.c ├── binary_search.exe └── linear_search.c └── sorting ├── heapSort.c ├── insertionSort.c ├── mergeSort.c ├── quickSort.c └── sorting.c /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "cmath": "c" 4 | } 5 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "type": "cppbuild", 6 | "label": "C/C++: gcc.exe build active file", 7 | "command": "D:\\Programs\\MinGW\\mingw32\\bin\\gcc.exe", 8 | "args": [ 9 | "-fdiagnostics-color=always", 10 | "-g", 11 | "${file}", 12 | "-o", 13 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 14 | ], 15 | "options": { 16 | "cwd": "${fileDirname}" 17 | }, 18 | "problemMatcher": [ 19 | "$gcc" 20 | ], 21 | "group": { 22 | "kind": "build", 23 | "isDefault": true 24 | }, 25 | "detail": "compiler: D:\\Programs\\MinGW\\mingw32\\bin\\gcc.exe" 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Beginner Level C Programs 2 | 3 | Repo created for the purpose of basic programming practice in C language for beginners. 4 | Programming experts can add any kind of simple programs for the learning process of beginners. 5 | 6 | ### FAQ 7 | 8 | #### How to contribute in this repo ? 9 | Fork this repo and create pull requests. Genuine ones would be accepted. 10 | 11 | #### Will all PR's be accepted? 12 | Depends on the quality of PR 13 | 14 | #### CONTACT 15 | 16 | Aswin K - Linkedin - https://www.linkedin.com/in/asterdev/ -------------------------------------------------------------------------------- /compiler-design/calculator.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | extern int yylval; 4 | %} 5 | %% 6 | [0-9]+ { 7 | yylval=atoi(yytext); 8 | return NUMBER; 9 | } 10 | [a-zA-Z]+ {return ID;} 11 | [\t] ; 12 | [\n] {return 0;} 13 | . return yytext[0]; 14 | %% 15 | 16 | 17 | -------------------------------------------------------------------------------- /compiler-design/calculator.y: -------------------------------------------------------------------------------- 1 | 2 | %{ 3 | #include 4 | %} 5 | %token NUMBER ID; 6 | %left '+''-' 7 | %left '*''/' 8 | %% 9 | E:T{ 10 | printf("result=%d\n",$$); 11 | return 0; 12 | } 13 | T: 14 | T'+'T {$$=$1+$3;} 15 | |T'-'T {$$=$1-$3;} 16 | |T'*'T {$$=$1*$3;} 17 | |T'/'T {$$=$1/$3;} 18 | |'-'NUMBER {$$=-$2;} 19 | |'-'ID {$$=-$2;} 20 | |'('T')' {$$=$2;} 21 | |ID {$$=$1;} 22 | |NUMBER {$$=$1;}; 23 | %% 24 | int main() 25 | { 26 | printf("\nEnter the expression:\n"); 27 | yyparse(); 28 | } 29 | int yyerror(char *s) 30 | { 31 | printf("\nIt is not an identifier\n"); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /compiler-design/dfa_parity_checking.l: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | %} 4 | 5 | %s A B C DEAD 6 | 7 | %% 8 | 9 | 1 BEGIN A; 10 | 11 | 0 BEGIN B; 12 | 13 | [^01\n] BEGIN DEAD; 14 | 15 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 16 | 17 | 1 BEGIN INITIAL; 18 | 19 | 0 BEGIN C; 20 | 21 | [^01\n] BEGIN DEAD; 22 | 23 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 24 | 25 | 1 BEGIN C; 26 | 27 | 0 BEGIN INITIAL; 28 | 29 | [^01\n] BEGIN DEAD; 30 | 31 | \n BEGIN INITIAL; {printf("Accepted\n");} 32 | 33 | 1 BEGIN B; 34 | 35 | 0 BEGIN A; 36 | 37 | [^01\n] BEGIN DEAD; 38 | 39 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 40 | 41 | [^\n] BEGIN DEAD; 42 | 43 | \n BEGIN INITIAL; {printf("Invalid\n");} 44 | 45 | %% 46 | 47 | int main() 48 | 49 | { 50 | 51 | printf("Enter String\n"); 52 | 53 | yylex(); 54 | 55 | return 0; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /compiler-design/generate_assembly_lan.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | char icode[10][30],str[20],opr[10]; 7 | int i=0; 8 | //clrscr(); 9 | printf("\n Enter the set of intermediate code (terminated by exit):\n"); 10 | do 11 | { 12 | scanf("%s",icode[i]); 13 | } while(strcmp(icode[i++],"exit")!=0); 14 | printf("\n target code generation"); 15 | printf("\n************************"); 16 | i=0; 17 | do 18 | { 19 | strcpy(str,icode[i]); 20 | switch(str[3]) 21 | { 22 | case '+': 23 | strcpy(opr,"ADD"); 24 | break; 25 | case '-': 26 | strcpy(opr,"SUB"); 27 | break; 28 | case '*': 29 | strcpy(opr,"MUL"); 30 | break; 31 | case '/': 32 | strcpy(opr,"DIV"); 33 | break; 34 | } 35 | printf("\n\tMov %c,R%d",str[2],i); 36 | 37 | printf("\n\t%s%c,R%d",opr,str[4],i); 38 | printf("\n\tMov R%d,%c",i,str[0]); 39 | }while(strcmp(icode[++i],"exit")!=0); 40 | //getch(); 41 | } 42 | -------------------------------------------------------------------------------- /compiler-design/intermediate _code_generation.l: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | #include"y.tab.h" 4 | 5 | %} 6 | 7 | %% 8 | 9 | [0-9]+ {yylval.dval=(*yytext);return NUM;} 10 | 11 | \n return 0; 12 | 13 | . return yytext[0]; 14 | 15 | %% 16 | -------------------------------------------------------------------------------- /compiler-design/intermediate _code_generation.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | char p='A'; 5 | %} 6 | %union {char dval;} 7 | %token NUM sign 8 | %left '+''-' 9 | %left '*''/' 10 | %type S 11 | %type E 12 | %% 13 | 14 | S:E {printf("X=%c\n",$$);} 15 | 16 | E:NUM {}|E'+'E {printf("%c=%c+%c\n",p,$1,$3);$$=p;p++;} 17 | |E'-'E {printf("%c=%c-%c\n",p,$1,$3);$$=p;p++;} 18 | |E'*'E {printf("%c=%c*%c\n",p,$1,$3);$$=p;p++;} 19 | |E'/'E {printf("%c=%c/%c\n",p,$1,$3);$$=p;p++;} 20 | 21 | %% 22 | 23 | int main() 24 | 25 | { 26 | 27 | printf("Enter the expression : "); 28 | 29 | yyparse(); 30 | 31 | printf("Expression is valid\n"); 32 | 33 | return 0; 34 | 35 | } 36 | 37 | int yyerror() 38 | 39 | { 40 | 41 | printf("Expression is invalid \n"); 42 | 43 | exit(0); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /compiler-design/lexical_analyzer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int isKeyword(char buffer[]) 6 | { 7 | char keywords[32][10]={"auto","break","case","char","const","continue","default","do","double","else","enum","extern","float"}; 8 | int i,flag=0; 9 | for(i=0;i<32;i++) 10 | { 11 | if(strcmp(keywords[i],buffer)==0) 12 | { 13 | flag=1; 14 | break; 15 | } 16 | } 17 | return flag; 18 | } 19 | int main() 20 | { 21 | char ch,buffer[15],operators[]="+-*/%="; 22 | FILE*fp; 23 | int i,j=0; 24 | fp=fopen("program.txt","r"); 25 | if(fp==NULL) 26 | { 27 | printf("Error while opening the file\n"); 28 | exit(0); 29 | } 30 | while((ch==fgetc(fp))!=EOF) 31 | { 32 | for(i=0;i<6;++i) 33 | { 34 | if(ch==operators[i]) 35 | printf("%c is operator \n",ch); 36 | } 37 | if(isalnum(ch)) 38 | { 39 | buffer[j++]=ch; 40 | } 41 | else if((ch==' '||ch=='\n')&&(j!=0)) 42 | { 43 | buffer[j]='\O'; 44 | j=0; 45 | if(isKeyword(buffer)==1) 46 | printf("%s is keyword\n",buffer); 47 | else 48 | printf("%s is indentifier \n",buffer); 49 | } 50 | } 51 | fclose(fp); 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /compiler-design/lexical_analyzer.l: -------------------------------------------------------------------------------- 1 | 2 | %{ 3 | int COMMENT = 0; 4 | %} 5 | identifier [a-zA-Z][a-zA-Z0-9]* 6 | %% 7 | #.* {printf("\n%s is a preprocessor directive", yytext);} 8 | int|float|char|double|while|for|struct|typedef|do|if|break|continue|void|switch|return|else|goto {printf("\n%s is a keyword", yytext);} 9 | "/*" {COMMENT=1;} {printf("\n%s is a comment", yytext);} 10 | {identifier}\( {if(!COMMENT) printf("\nFunction %s", yytext);} 11 | \{ {if(!COMMENT) printf("\nBLOCK BEGINS");} 12 | \} {if(!COMMENT) printf("BLOCK ENDS ");} 13 | {identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n%s IDENTIFIER", yytext);} 14 | \".*\" {if(!COMMENT) printf("\n%s is a STRING", yytext);} 15 | [0-9]+ {if(!COMMENT) printf("\n%s is a CONSTANT", yytext);} 16 | \)(\:)? {if(!COMMENT)printf("\n"); ECHO; printf("\n");} 17 | \/ | 18 | \+ | 19 | \- | 20 | \* {if(!COMMENT) printf("\n%s is an ARITHMETIC OPERATOR", yytext);} 21 | \= {if(!COMMENT) printf("\n%s is an ASSIGNMENT OPERATOR",yytext);} 22 | \<= | 23 | \>= |n 24 | \< | 25 | \== | 26 | \> {if(!COMMENT) printf("\n%s is a RELATIONAL OPERATOR",yytext);} 27 | %% 28 | int main(int argc, char **argv){ 29 | FILE *file; 30 | file=fopen("program.c","r"); 31 | if(!file) 32 | { 33 | printf("could not open the file"); 34 | exit(0); 35 | } 36 | yyin=file; 37 | yylex(); 38 | printf("\n"); 39 | return(0); 40 | } 41 | int yywrap() 42 | { 43 | return(1); 44 | } 45 | -------------------------------------------------------------------------------- /compiler-design/lowerToUpper.l: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | FILE *fp; 4 | 5 | FILE *fp1; 6 | 7 | %} 8 | 9 | %% 10 | 11 | [a-z] fprintf(fp1,"%c",toupper(yytext[0])); 12 | 13 | . fprintf(fp1,"%C",yytext[0]); 14 | 15 | %% 16 | 17 | int main(int args,char **argv) 18 | 19 | { 20 | 21 | fp1=fopen("b.txt","w"); 22 | 23 | if(!fp1) 24 | 25 | { 26 | 27 | printf("file not exists\n"); 28 | 29 | return(0); 30 | 31 | } 32 | 33 | fp=fopen("a.txt","r"); 34 | 35 | if(!fp) 36 | 37 | { 38 | 39 | printf("file not exists\n"); 40 | 41 | return(0); 42 | 43 | } 44 | 45 | yyin=fp1; 46 | yyin=fp; 47 | 48 | yylex(); 49 | 50 | return(0); 51 | 52 | } 53 | 54 | -------------------------------------------------------------------------------- /compiler-design/remove_whitespaces.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | %} 4 | %% 5 | [ \n\t] {printf("");} 6 | . {printf("%s",yytext)} 7 | %% 8 | void main(int argc,char*argv[]) 9 | { 10 | yyin=fopen(argv[1],"r") 11 | yylex(); 12 | } 13 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp1.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | int word=0,line=0,ch=0; 4 | %} 5 | 6 | %% 7 | [a-zA-Z0-9] {ch++;} 8 | " " {word++;} 9 | "\n" {line++; word++;} 10 | . {} 11 | %% 12 | int main(){ 13 | yyin=fopen("input.txt","r"); 14 | yylex(); 15 | printf("character count:%d\n",ch); 16 | printf("word count:%d\n",word); 17 | printf("line count:%d\n",line); 18 | } 19 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp10.l: -------------------------------------------------------------------------------- 1 | %{ 2 | %} 3 | %s A B C DEAD 4 | %% 5 | 6 | 1 BEGIN A; 7 | 0 BEGIN B; 8 | [^01\n] BEGIN DEAD; 9 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 10 | 1 BEGIN INITIAL; 11 | 0 BEGIN C; 12 | [^01\n] BEGIN DEAD; 13 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 14 | 1 BEGIN C; 15 | 0 BEGIN INITIAL; 16 | [^01\n] BEGIN DEAD; 17 | \n BEGIN INITIAL; {printf("Accepted\n");} 18 | 1 BEGIN B; 19 | 0 BEGIN A; 20 | [^01\n] BEGIN DEAD; 21 | \n BEGIN INITIAL; {printf("Not Accepted\n");} 22 | [^\n] BEGIN DEAD; 23 | \n BEGIN INITIAL; {printf("Invalid\n");} 24 | %% 25 | 26 | int main() 27 | { 28 | printf("Enter String\n"); 29 | yylex(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp11.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | 5 | %% 6 | 7 | [0-9]+ {yylval=*yytext;return NUM;} 8 | \n return 0; 9 | . return yytext[0]; 10 | %% 11 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp11.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | char p='A'; 4 | %} 5 | 6 | %token NUM 7 | %left '+''-' 8 | %left '*''/' 9 | %% 10 | 11 | S:E {printf("X=%c\n",$$);} 12 | E:NUM {} 13 | |E'+'E {printf("%c=%c+%c\n",p,$1,$3);$$=p;p++;} 14 | |E'-'E {printf("%c=%c-%c\n",p,$1,$3);$$=p;p++;} 15 | |E'*'E {printf("%c=%c*%c\n",p,$1,$3);$$=p;p++;} 16 | |E'/'E {printf("%c=%c/%c\n",p,$1,$3);$$=p;p++;} 17 | 18 | %% 19 | int main() 20 | { 21 | printf("Enter the expression : "); 22 | yyparse(); 23 | } 24 | int yyerror() 25 | { 26 | printf("Expression is invalid \n"); 27 | } 28 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp12.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | char icode[10][30], str[20], opr[10]; 7 | int i = 0; 8 | printf("\n Enter the set of intermediate code (terminated by exit):\n"); 9 | 10 | do 11 | { 12 | scanf("%s", icode[i]); 13 | } while (strcmp(icode[i++], "exit") != 0); 14 | 15 | printf("\n target code generation"); 16 | i = 0; 17 | do 18 | { 19 | strcpy(str, icode[i]); 20 | switch (str[3]) 21 | { 22 | case '+': 23 | strcpy(opr, "ADD"); 24 | break; 25 | case '-': 26 | strcpy(opr, "SUB"); 27 | break; 28 | case '*': 29 | strcpy(opr, "MUL"); 30 | break; 31 | case '/': 32 | strcpy(opr, "DIV"); 33 | break; 34 | } 35 | printf("\n\tMov %c,R%d", str[2], i); 36 | printf("\n\t%s%c,R%d", opr, str[4], i); 37 | printf("\n\tMov R%d,%c", i, str[0]); 38 | 39 | } while (strcmp(icode[++i], "exit") != 0); 40 | } 41 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp2.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | int vowels=0,consonants=0; 4 | %} 5 | 6 | %% 7 | [aeiouAEIOU] {vowels++;} 8 | [a-zA-Z] {consonants++;} 9 | . {} 10 | 11 | %% 12 | 13 | int main(){ 14 | yyin=fopen("input.txt","r"); 15 | yylex(); 16 | printf("vowels=%d\n",vowels); 17 | printf("consonants=%d\n",consonants); 18 | } 19 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp3.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | %} 4 | 5 | %% 6 | [ \n\t] {printf("");} 7 | . {printf("%s",yytext);} 8 | %% 9 | 10 | void main() 11 | { 12 | yyin=fopen("input.txt","r"); 13 | yylex(); 14 | } 15 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int isKeyword(char buffer[]) 7 | { 8 | char keywords[6][10] = {"void", "break", "char", "int", "float", "return"}; // add necessary keywords 9 | 10 | int flag = 0; 11 | for (int i = 0; i < 6; i++) // provide no. of keywords used 12 | { 13 | if (strcmp(keywords[i], buffer) == 0) 14 | { 15 | flag = 1; 16 | break; 17 | } 18 | } 19 | return flag; 20 | } 21 | 22 | int main() 23 | { 24 | char ch, buffer[15], operators[] = "+-*/%="; 25 | FILE *fp; 26 | int i, j = 0; 27 | 28 | fp = fopen("program.txt", "r"); 29 | 30 | while ((ch = fgetc(fp)) != EOF) 31 | { 32 | for (i = 0; i < 6; ++i) 33 | { 34 | if (ch == operators[i]) 35 | printf("%c is operator\n", ch); 36 | } 37 | if (isalnum(ch)) 38 | buffer[j++] = ch; 39 | else if ((ch == ' ' || ch == '\n') && (j != 0)) 40 | { 41 | buffer[j] = '\0'; 42 | 43 | j = 0; 44 | if (isKeyword(buffer) == 1) 45 | printf("%s is keyword\n", buffer); 46 | else 47 | printf("%s is indentifier\n", buffer); 48 | } 49 | } 50 | return 0; 51 | } -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp5.l: -------------------------------------------------------------------------------- 1 | %{ 2 | %} 3 | identifier [a-zA-Z][a-zA-Z0-9]* 4 | %% 5 | 6 | #.* {printf("\n%s is a preprocessor directive", yytext);} 7 | int|float|char|double|while|for|if|break|continue|void|return|else {printf("\n%s is a keyword", yytext);} 8 | 9 | {identifier}\( {printf("\nFunction %s", yytext);} 10 | "{" {printf("BLOCK BEGINS");} 11 | "}" {printf("BLOCK ENDS ");} 12 | 13 | {identifier}(\[[0-9]*\])? {printf("\n%s is an IDENTIFIER", yytext);} 14 | \".*\" {printf("\n%s is a STRING", yytext);} 15 | [0-9]+ {printf("\n%s is a CONSTANT", yytext);} 16 | 17 | "/"|"+"|"-"|"*" {printf("\n%s is an ARITHMETIC OPERATOR", yytext);} 18 | "=" {printf("\n%s is an ASSIGNMENT OPERATOR",yytext);} 19 | "<="|">="|"<"|"=="|">" {printf("\n%s is a RELATIONAL OPERATOR",yytext);} 20 | 21 | %% 22 | 23 | int main(){ 24 | yyin=fopen("program.txt","r"); 25 | yylex(); 26 | } 27 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp6.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | 6 | [a-zA-z_][a-zA-Z_0-9]* return id; 7 | [0-9]+(\.[0-9]*)? return num; 8 | [+/*] return op; 9 | . return yytext[0]; 10 | \n return 0; 11 | %% 12 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp6.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | %} 5 | %token num id op 6 | %% 7 | 8 | start: id'='s';' 9 | s: '-'id x|id x|num x|'-'num x|'('s')'x 10 | x: op s|'-'s| 11 | %% 12 | 13 | int yyerror() 14 | { 15 | printf("\nInvalid expr\n"); 16 | exit(0); 17 | } 18 | int main() 19 | { 20 | printf("Enter expr: "); 21 | yyparse(); 22 | printf("Valid expr\n"); 23 | } 24 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp7.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | [a-zA-Z_] return letter; 6 | [0-9] return digit; 7 | . return yytext[0]; 8 | \n return 0; 9 | %% 10 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp7.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | %} 5 | %token digit letter 6 | %% 7 | 8 | start : letter s 9 | s: letter s|digit s| 10 | %% 11 | 12 | int yyerror() 13 | { 14 | printf("\nIts not an identifier\n"); 15 | exit(0); 16 | } 17 | int main(){ 18 | printf("\nEnter a name to be tested for identifier\n"); 19 | yyparse(); 20 | printf("It is an identifier\n"); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp8.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | [0-9]+ { 6 | yylval=atoi(yytext); 7 | return NUMBER; 8 | } 9 | \n return 0; 10 | . return yytext[0]; 11 | %% 12 | 13 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp8.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | %} 4 | %token NUMBER 5 | %left '+''-' 6 | %left '*''/' 7 | %% 8 | 9 | E: T {printf("result=%d\n",$$);return 0;} 10 | T: T'+'T {$$=$1+$3;} 11 | |T'-'T {$$=$1-$3;} 12 | |T'*'T {$$=$1*$3;} 13 | |T'/'T {$$=$1/$3;} 14 | |'-'NUMBER {$$=-$2;} 15 | |'('T')' {$$=$2;} 16 | |NUMBER {$$=$1;} 17 | %% 18 | int yyerror() 19 | { 20 | printf("\nInput correct format for the calculaor\n"); 21 | } 22 | int main() 23 | { 24 | printf("\nEnter the expression:\n"); 25 | yyparse(); 26 | } 27 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/exp9.l: -------------------------------------------------------------------------------- 1 | %{ 2 | %} 3 | %% 4 | 5 | [a-z] printf("%c",toupper(yytext[0])); 6 | . printf("%c",yytext[0]); 7 | %% 8 | 9 | int main() 10 | { 11 | yyin=fopen("input.txt","r"); 12 | yylex(); 13 | } 14 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/input.txt: -------------------------------------------------------------------------------- 1 | hello 2 | hi 3 | -------------------------------------------------------------------------------- /compiler-design/shortened-pgms/program.txt: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a = 1+2; 5 | char b = "hi"; 6 | return a == b; 7 | } 8 | -------------------------------------------------------------------------------- /compiler-design/valid_expression.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | [a-zA-z_][a-zA-Z_0-9]* return id; 6 | [0-9]+(\.[0-9]*)? return num; 7 | [+/*] return op; 8 | . return yytext[0]; 9 | \n return 0; 10 | %% 11 | int yywrap() 12 | { 13 | return 1; 14 | } 15 | -------------------------------------------------------------------------------- /compiler-design/valid_expression.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | int valid=1; 4 | %} 5 | %token num id op 6 | %% 7 | start: id'='s';' 8 | s: '-' id x|id x|num x|'-'num x|'('s')'x; 9 | x:op s|'-'s| 10 | %% 11 | int yyerror() 12 | { 13 | valid=0; 14 | printf("\nInvalid expr\n"); 15 | return 0; 16 | } 17 | int main() 18 | { 19 | printf("Enter expr: "); 20 | yyparse(); 21 | if(valid) 22 | { 23 | printf("Valid expr\n"); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /compiler-design/valid_identifier.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | %} 4 | %% 5 | [a-zA-Z_] return letter; 6 | [0-9] return digit; 7 | . return yytext[0]; 8 | \n return 0; 9 | %% 10 | int yywrap() 11 | { 12 | return 1; 13 | } 14 | -------------------------------------------------------------------------------- /compiler-design/valid_identifier.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | int valid=1; 4 | %} 5 | %token digit letter 6 | %% 7 | start : letter s 8 | s: letter s| digit s|; 9 | %% 10 | int yyerror() 11 | { 12 | printf("\nIts not an identifier\n"); 13 | valid=0; 14 | return 0; 15 | } 16 | int main(){ 17 | printf("\nEnter a name to be tested for identifier\n"); 18 | yyparse(); 19 | if(valid) 20 | { 21 | printf("It is an identifier\n"); 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /conversions/infixToPostfix.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file infixToPostfix.c 3 | * @brief Expression conversion in C. 4 | * @details To convert an infixed expression to postfixed expression 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | // Globally declared array Stack representing the stack, and 11 | // a variable top pointing to top most index of the array 12 | char Stack[50]; 13 | int top = -1; 14 | 15 | void push(char); 16 | char pop(); 17 | int getPriority(char); 18 | void toPostfix(char[], char[]); 19 | 20 | /** 21 | * @brief Push Operation 22 | * @param x element that is pushed into stack 23 | */ 24 | void push(char x) 25 | { 26 | Stack[++top] = x; 27 | } 28 | 29 | /** 30 | * @brief Pop Operation 31 | * @return char element that is poped from stack 32 | */ 33 | char pop() 34 | { 35 | if (top == -1) 36 | return -1; 37 | else 38 | return Stack[top--]; 39 | } 40 | 41 | /** 42 | * @brief Get the priority of the operators 43 | * @param x operator whose priority is returned 44 | * @return int priority value 45 | */ 46 | int getPriority(char x) 47 | { 48 | if (x == '(') 49 | return 0; 50 | if (x == '+' || x == '-') 51 | return 1; 52 | if (x == '*' || x == '/') 53 | return 2; 54 | if (x == '^') 55 | return 3; 56 | return 0; 57 | } 58 | 59 | /** 60 | * @brief Conversion procedure, infix to postfix 61 | * @param infix expression in infix form 62 | * @param postfix expression in postfix form 63 | */ 64 | void toPostfix(char infix[], char postfix[]) 65 | { 66 | // pos represents top most index value of postfix array 67 | int pos = -1; 68 | for (int i = 0; infix[i] != '\0'; i++) 69 | { 70 | // checks if it is an operand 71 | if (isalnum(infix[i])) 72 | postfix[++pos] = infix[i]; 73 | else if (infix[i] == '(') 74 | push(infix[i]); 75 | else if (infix[i] == ')') 76 | { 77 | char x = pop(); 78 | // pops all the remaing elements in stack untill it reaches '(' 79 | while (x != '(') 80 | postfix[++pos] = x; 81 | } 82 | else 83 | { 84 | // pushes the operator into stack after poping all greater precedence elements in stack 85 | while (getPriority(Stack[top]) >= getPriority(infix[i])) 86 | postfix[++pos] = pop(); 87 | push(infix[i]); 88 | } 89 | } 90 | // pops all the remaining operators in the stack 91 | while (top != -1) 92 | { 93 | postfix[++pos] = pop(); 94 | } 95 | } 96 | 97 | int main() 98 | { 99 | char infix[20], postfix[20]; 100 | printf("Enter the Infix expression: "); 101 | scanf("%s", &infix); 102 | 103 | toPostfix(infix, postfix); 104 | 105 | printf("Postfix expression: %s", postfix); 106 | return 0; 107 | } -------------------------------------------------------------------------------- /data_structures/array/circularQueue.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file circularQueue.c 3 | * @brief Implementation of a Circular Queue using array in C. 4 | * @details The implementation has following functionalities: 5 | * - Enqueue 6 | * - Dequeue 7 | * - Peek, gives the element at front index of queue 8 | */ 9 | 10 | #include 11 | 12 | // Initialization 13 | #define MAXSiZE 4 14 | int queue[MAXSiZE]; 15 | int front = -1; 16 | int rear = -1; 17 | 18 | void enqueue(int); 19 | void dequeue(); 20 | void peek(); 21 | int isEmpty(); 22 | int isFull(); 23 | 24 | /** 25 | * @brief Inserts item to rear index of queue 26 | * @param item the value that is enqueued 27 | */ 28 | void enqueue(int item) 29 | { 30 | if (isFull() == 1) 31 | { 32 | printf("Queue Overflow\n"); 33 | return; 34 | } 35 | if (isEmpty() == 1) 36 | { 37 | front = 0; 38 | queue[++rear] = item; 39 | } 40 | else 41 | { 42 | // if queue is not full and rear + 1 reaches max size of array, then 43 | // circle back to index 0 and store the element here using this formula 44 | rear = (rear + 1) % MAXSiZE; 45 | queue[rear] = item; 46 | } 47 | printf("Enqueued %d\n", queue[rear]); 48 | } 49 | 50 | /** 51 | * @brief Deletes item on front index from queue 52 | */ 53 | void dequeue() 54 | { 55 | if (isEmpty() == 1) 56 | { 57 | printf("Queue Underflow\n"); 58 | return; 59 | } 60 | printf("%d Dequeued\n", queue[front]); 61 | if (front == rear) 62 | { 63 | front = -1; 64 | rear = -1; 65 | } 66 | else 67 | { 68 | // if queue is not empty and front + 1 reaches max size of array, then 69 | // circle back to index 0 and delete the element here using this formula 70 | front = (front + 1) % MAXSiZE; 71 | } 72 | } 73 | 74 | /** 75 | * @brief Displays the element on front index of queue 76 | */ 77 | void peek() 78 | { 79 | if (isEmpty() == 1) 80 | { 81 | printf("Queue Underflow\n"); 82 | return; 83 | } 84 | printf("Front Element: %d\n", queue[front]); 85 | } 86 | 87 | /** 88 | * @brief Checks whether queue is empty or not 89 | * @return int 1 if queue is empty and 0 if not 90 | */ 91 | int isEmpty() 92 | { 93 | return (front == -1 && rear == -1); 94 | } 95 | 96 | /** 97 | * @brief Checks whether queue is full or not 98 | * @return int 1 if queue is full and 0 if not 99 | */ 100 | int isFull() 101 | { 102 | return (front == (rear + 1) % MAXSiZE); 103 | } 104 | 105 | int main() 106 | { 107 | int status = 1, option, key; 108 | while (status == 1) 109 | { 110 | printf("1. Enqueue 2. Dequeue 3. Peek 4. Exit\n"); 111 | printf("Enter option: "); 112 | scanf("%d", &option); 113 | 114 | switch (option) 115 | { 116 | case 1: 117 | printf("Enter value: "); 118 | scanf("%d", &key); 119 | enqueue(key); 120 | break; 121 | case 2: 122 | dequeue(); 123 | break; 124 | case 3: 125 | peek(); 126 | break; 127 | case 4: 128 | status = 0; 129 | break; 130 | default: 131 | printf("Enter appropriate option.\n"); 132 | break; 133 | } 134 | } 135 | return 0; 136 | } -------------------------------------------------------------------------------- /data_structures/array/circularQueue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/data_structures/array/circularQueue.exe -------------------------------------------------------------------------------- /data_structures/array/linearQueue.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file linearQueue.c 3 | * @brief Implementation of a Linear Queue using array in C. 4 | * @details The implementation has following functionalities: 5 | * - Enqueue 6 | * - Dequeue 7 | * - Peek, gives the element at front index of queue 8 | */ 9 | 10 | #include 11 | 12 | // Initialization 13 | #define MAXSiZE 4 14 | int queue[MAXSiZE]; 15 | int front = -1; 16 | int rear = -1; 17 | 18 | void enqueue(int); 19 | void dequeue(); 20 | void peek(); 21 | int isEmpty(); 22 | int isFull(); 23 | 24 | /** 25 | * @brief Inserts item to rear index of queue 26 | * @param item the value that is enqueued 27 | */ 28 | void enqueue(int item) 29 | { 30 | if (isFull() == 1) 31 | { 32 | printf("Queue Overflow\n"); 33 | return; 34 | } 35 | if (isEmpty() == 1) 36 | front = 0; 37 | queue[++rear] = item; 38 | printf("Enqueued %d\n", queue[rear]); 39 | } 40 | 41 | /** 42 | * @brief Deletes item on front index from queue 43 | */ 44 | void dequeue() 45 | { 46 | if (isEmpty() == 1) 47 | { 48 | printf("Queue Underflow\n"); 49 | return; 50 | } 51 | printf("Dequeued %d\n", queue[front]); 52 | if (front == rear) 53 | { 54 | front = -1; 55 | rear = -1; 56 | } 57 | else 58 | front++; 59 | } 60 | 61 | /** 62 | * @brief Displays the element on front index of queue 63 | */ 64 | void peek() 65 | { 66 | if (isEmpty() == 1) 67 | { 68 | printf("Queue Underflow\n"); 69 | return; 70 | } 71 | printf("Front Element: %d\n", queue[front]); 72 | } 73 | 74 | /** 75 | * @brief Checks whether queue is empty or not 76 | * @return int 1 if queue is empty and 0 if not 77 | */ 78 | int isEmpty() 79 | { 80 | return (front == -1 && rear == -1); 81 | } 82 | 83 | /** 84 | * @brief Checks whether queue is full or not 85 | * @return int 1 if queue is full and 0 if not 86 | */ 87 | int isFull() 88 | { 89 | return (rear == MAXSiZE - 1); 90 | } 91 | 92 | int main() 93 | { 94 | int status = 1, option, key; 95 | while (status == 1) 96 | { 97 | printf("1. Enqueue 2. Dequeue 3. Peek 4. Exit\n"); 98 | printf("Enter option: "); 99 | scanf("%d", &option); 100 | 101 | switch (option) 102 | { 103 | case 1: 104 | printf("Enter value: "); 105 | scanf("%d", &key); 106 | enqueue(key); 107 | break; 108 | case 2: 109 | dequeue(); 110 | break; 111 | case 3: 112 | peek(); 113 | break; 114 | case 4: 115 | status = 0; 116 | break; 117 | default: 118 | printf("Enter appropriate option.\n"); 119 | break; 120 | } 121 | } 122 | return 0; 123 | } -------------------------------------------------------------------------------- /data_structures/array/linearQueue.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/data_structures/array/linearQueue.exe -------------------------------------------------------------------------------- /data_structures/array/priorityQueue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define QUEUE_SIZE 5 5 | #define QUEUE_NUM 3 6 | 7 | struct queue 8 | { 9 | int items[10]; 10 | int front; 11 | int rear; 12 | }; 13 | typedef struct queue QUEUE; 14 | 15 | int qempty(QUEUE *q) 16 | { 17 | return (q->front > q->rear) ? 1 : 0; 18 | } 19 | 20 | int qfull(QUEUE *q) 21 | { 22 | return (q->rear == QUEUE_SIZE - 1) ? 1 : 0; 23 | } 24 | 25 | void insert_rear(int item, QUEUE *q) 26 | { 27 | if (qfull(q)) 28 | { 29 | printf("Queue insertion not possible\n"); 30 | return; 31 | } 32 | 33 | q->rear++; 34 | q->items[q->rear] = item; 35 | } 36 | 37 | void delete_front(QUEUE *q) 38 | { 39 | int item_deleted; 40 | 41 | if (qempty(q)) 42 | { 43 | printf("Queue is empty\nDeletion not possible\n"); 44 | return; 45 | } 46 | 47 | item_deleted = q->items[q->front]; 48 | q->front++; 49 | 50 | printf("The element deleted is:\t%d\n", item_deleted); 51 | 52 | if (q->front > q->rear) 53 | { 54 | q->front = 0; 55 | q->rear = -1; 56 | } 57 | } 58 | 59 | void display(QUEUE *q) 60 | { 61 | int i; 62 | 63 | if (qempty(q)) 64 | { 65 | printf("Queue is empty\nNo elements to display\n"); 66 | return; 67 | } 68 | 69 | printf("The contents of queue are:\n"); 70 | 71 | for (i = q->front; i <= q->rear; i++) 72 | { 73 | printf("%d\n", q->items[i]); 74 | } 75 | } 76 | 77 | void main() 78 | { 79 | QUEUE q[QUEUE_NUM]; 80 | int choice, item, i, priority; 81 | 82 | for (i = 0; i < QUEUE_NUM; i++) 83 | { 84 | q[i].front = 0; 85 | q[i].rear = -1; 86 | } 87 | 88 | for (;;) 89 | { 90 | printf("Enter\n"); 91 | printf("1. Insert\n2. Delete\n"); 92 | printf("3. Display\n4. Exit\n"); 93 | scanf("%d", &choice); 94 | 95 | switch (choice) 96 | { 97 | case 1: 98 | printf("Enter priority and item to be inserted\n"); 99 | scanf("%d %d", &priority, &item); 100 | 101 | insert_rear(item, &q[priority - 1]); 102 | 103 | break; 104 | 105 | case 2: 106 | priority = 0; 107 | 108 | if (qempty(&q[0])) 109 | { 110 | printf("Queue 1 is empty\n"); 111 | 112 | if (qempty(&q[1])) 113 | { 114 | printf("Queue 2 is empty\n"); 115 | 116 | if (qempty(&q[2])) 117 | { 118 | printf("Queue 3 is empty\n"); 119 | } 120 | else 121 | priority = 3; 122 | } 123 | else 124 | priority = 2; 125 | } 126 | else 127 | priority = 1; 128 | 129 | if (priority != 0) 130 | { 131 | printf("Deleting from queue %d\n", priority); 132 | delete_front(&q[priority - 1]); 133 | } 134 | 135 | break; 136 | 137 | case 3: 138 | for (i = 0; i < QUEUE_NUM; i++) 139 | { 140 | printf("Queue %d contents\n", i + 1); 141 | display(&q[i]); 142 | } 143 | 144 | break; 145 | 146 | default: 147 | exit(0); 148 | } 149 | } 150 | } -------------------------------------------------------------------------------- /data_structures/array/stack.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file stack.c 3 | * @brief Implementation of a Stack using array in C. 4 | * @details The implementation has following functionalities: 5 | * - Push 6 | * - Pop 7 | * - Peek, gives the element at top index of stack 8 | */ 9 | 10 | #include 11 | 12 | // Initialization 13 | #define MAXSiZE 4 14 | int stack[MAXSiZE]; 15 | int top = -1; 16 | 17 | void push(int); 18 | void pop(); 19 | void peek(); 20 | int isEmpty(); 21 | int isFull(); 22 | 23 | /** 24 | * @brief Inserts item to top index of stack 25 | * @param item the value that is pushed 26 | */ 27 | void push(int item) 28 | { 29 | if (isFull() == 1) 30 | { 31 | printf("Stack Overflow\n"); 32 | return; 33 | } 34 | stack[++top] = item; 35 | printf("Pushed: %d\n", stack[top]); 36 | } 37 | 38 | /** 39 | * @brief Deletes item on top index from stack 40 | */ 41 | void pop() 42 | { 43 | if (isEmpty() == 1) 44 | { 45 | printf("Stack Underflow\n"); 46 | return; 47 | } 48 | printf("Poped: %d\n", stack[top--]); 49 | } 50 | 51 | /** 52 | * @brief Displays the element on top index of stack 53 | */ 54 | void peek() 55 | { 56 | if (isEmpty() == 1) 57 | { 58 | printf("Stack Underflow\n"); 59 | return; 60 | } 61 | printf("Top Element: %d\n", stack[top]); 62 | } 63 | 64 | /** 65 | * @brief Checks whether stack is empty or not 66 | * @return int 1 if stack is empty and 0 if not 67 | */ 68 | int isEmpty() 69 | { 70 | return (top == -1); 71 | } 72 | 73 | /** 74 | * @brief Checks whether stack is full or not 75 | * @return int 1 if stack is full and 0 if not 76 | */ 77 | int isFull() 78 | { 79 | return (top == MAXSiZE - 1); 80 | } 81 | 82 | int main() 83 | { 84 | int status = 1, option, key; 85 | while (status == 1) 86 | { 87 | printf("1. Push 2. Pop 3. Peek 4. Exit\n"); 88 | printf("Enter option: "); 89 | scanf("%d", &option); 90 | 91 | switch (option) 92 | { 93 | case 1: 94 | printf("Enter value: "); 95 | scanf("%d", &key); 96 | push(key); 97 | break; 98 | case 2: 99 | pop(); 100 | break; 101 | case 3: 102 | peek(); 103 | break; 104 | case 4: 105 | status = 0; 106 | break; 107 | default: 108 | printf("Enter appropriate option.\n"); 109 | break; 110 | } 111 | } 112 | return 0; 113 | } -------------------------------------------------------------------------------- /data_structures/binary_trees/binarySearchTree.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file binarySearchTree.c 3 | * @brief Implementation of Binary Search Tree in C. 4 | * @details The implementation has following functionalities: 5 | * - Insertion 6 | * - Deletion 7 | * - Searching using a key value 8 | * - Traversal of nodes in post, pre and in order of values (from left to right) 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | // Basic structure of a node in a tree 15 | typedef struct treeNode 16 | { 17 | int data; // data part of node 18 | struct treeNode *left; // left child 19 | struct treeNode *right; // right child 20 | } node; 21 | 22 | node *newNode(int data); 23 | void purge(node **root); 24 | void insertNode(node **root, int key); 25 | void printInOrder(node *root); 26 | void printPreOrder(node *root); 27 | void printPostOrder(node *root); 28 | node *search(node *root, int key); 29 | node *getSuccessorNode(node *root); 30 | node *deleteNode(node **root, int key); 31 | int countLeafs(node *root); 32 | 33 | /** 34 | * @brief The function to create a node and initialize it's data and link parts 35 | * @param data the data part to be stored in the node 36 | * @returns new node with provided data 37 | * @note the node must be deleted before program terminates to avoid memory leaks 38 | */ 39 | node *newNode(int data) 40 | { 41 | // creates a node 42 | node *new = (node *)malloc(sizeof(node)); 43 | 44 | // initializes the node 45 | new->data = data; 46 | new->left = NULL; 47 | new->right = NULL; 48 | 49 | return new; 50 | } 51 | 52 | /** 53 | * @brief Utilary procedure, to free all the nodes in the tree 54 | * @param root the pointer to the parent node 55 | */ 56 | void purge(node **root) 57 | { 58 | if (*root != NULL) 59 | { 60 | if ((*root)->left != NULL) 61 | purge(&(*root)->right); 62 | if ((*root)->right != NULL) 63 | purge(&(*root)->left); 64 | free(*root); 65 | *root = NULL; 66 | } 67 | } 68 | 69 | /** 70 | * @brief Insertion procedure, which inserts input key into the tree 71 | * @param root the pointer to the parent node 72 | * @param key the input key value, that is to be stored in new node 73 | */ 74 | void insertNode(node **root, int key) 75 | { 76 | // if root of subtree is null, insert key in root, 77 | // if key is lesser than root key, insert in left leaf, and 78 | // if key is greater than root key, insert in right leaf 79 | if ((*root) == NULL) 80 | *root = newNode(key); 81 | else if (key < (*root)->data) 82 | insertNode(&((*root)->left), key); 83 | else 84 | insertNode(&((*root)->right), key); 85 | } 86 | 87 | /** 88 | * @brief Traversal procedure, to list the current keys in the tree in order of value 89 | * @param root the pointer to parent node 90 | */ 91 | void printInOrder(node *root) 92 | { 93 | if (root != NULL) 94 | { 95 | printInOrder(root->left); 96 | printf("%d ", root->data); 97 | printInOrder(root->right); 98 | } 99 | } 100 | 101 | /** 102 | * @brief Traversal procedure, to list the current keys in the tree in pre order of value 103 | * @param root the pointer to parent node 104 | */ 105 | void printPreOrder(node *root) 106 | { 107 | if (root != NULL) 108 | { 109 | printf("%d ", root->data); 110 | printPreOrder(root->left); 111 | printPreOrder(root->right); 112 | } 113 | } 114 | 115 | /** 116 | * @brief Traversal procedure, to list the current keys in the tree in post order of value 117 | * @param root the pointer to parent node 118 | */ 119 | void printPostOrder(node *root) 120 | { 121 | if (root != NULL) 122 | { 123 | printPostOrder(root->left); 124 | printPostOrder(root->right); 125 | printf("%d ", root->data); 126 | } 127 | } 128 | 129 | /** 130 | * @brief Search procedure,to traverse through the tree to find the input key 131 | * @param root the pointer to parent node 132 | * @param key the input key, that is to be searched in the tree 133 | * @return node* if key present or NULL if key not in tree 134 | */ 135 | node *search(node *root, int key) 136 | { 137 | if (root == NULL || root->data == key) 138 | return root; 139 | else if (key < (root->data)) 140 | return search(root->left, key); 141 | else 142 | return search(root->right, key); 143 | } 144 | 145 | /** 146 | * @brief Get the Successor Node object 147 | * @details To find the greatest key in the left subtree by 148 | * traversing to the rightmost node in the left branch 149 | * @param root the pointer to parent node 150 | * @return node* successor node having max value 151 | */ 152 | node *getSuccessorNode(node *root) 153 | { 154 | // if there is no leaf to the right, then this is 155 | // the node having the maximum key value 156 | if (root->right != NULL) 157 | return getSuccessorNode(root->right); 158 | return root; 159 | } 160 | 161 | /** 162 | * @brief Deletion procedure, searches the input key in the tree and then 163 | * removes that node if present 164 | * @param root the pointer to parent node 165 | * @param key the input value that is searched in the tree and removed 166 | * @return node* pointer to parent node 167 | */ 168 | node *deleteNode(node **root, int key) 169 | { 170 | // search for the node having input key in the tree 171 | // if not found, return null 172 | if (*root == NULL) 173 | return *root; 174 | else if (key > (*root)->data) 175 | (*root)->right = deleteNode(&(*root)->right, key); 176 | else if (key < (*root)->data) 177 | (*root)->left = deleteNode(&(*root)->left, key); 178 | else if (key == (*root)->data) 179 | { 180 | // if input key matches the root's value, then 181 | // check for following cases for termination 182 | if ((*root)->left == NULL && (*root)->right == NULL) 183 | { 184 | // case 1: root has no leaves, then remove the root 185 | *root = NULL; 186 | return NULL; 187 | } 188 | else if ((*root)->left == NULL) 189 | { 190 | // case 2: root has one leaf, then make the leaf 191 | // the new root and remove old root 192 | node *temp = *root; 193 | *root = (*root)->right; 194 | free(temp); 195 | return *root; 196 | } 197 | else if ((*root)->right == NULL) 198 | { 199 | node *temp = *root; 200 | *root = (*root)->left; 201 | free(temp); 202 | return *root; 203 | } 204 | else 205 | { 206 | // case 3: root has two leaves, then find its 207 | // successor root and switch them 208 | node *temp = getSuccessorNode((*root)->left); 209 | // set the data of this node equal to data of successor node 210 | (*root)->data = temp->data; 211 | (*root)->left = deleteNode(&(*root)->left, temp->data); 212 | } 213 | } 214 | return *root; 215 | } 216 | 217 | /** 218 | * @brief To count the number of leaves in a tree 219 | * @param root the pointer to parent node 220 | * @return int number of leaves present in the tree 221 | */ 222 | int countLeafs(node *root) 223 | { 224 | if (root == NULL) 225 | return 0; 226 | if (root->left == NULL && root->right == NULL) 227 | return 1; 228 | else 229 | return countLeafs(root->left) + countLeafs(root->right); 230 | } 231 | 232 | int main() 233 | { 234 | node *root = NULL, *ptr; 235 | int status = 1, option, key; 236 | while (status == 1) 237 | { 238 | printf("1. Insert 2. Delete 3. Search 4. Display 5. Leaf Count 6. Exit\n"); 239 | printf("Enter option: "); 240 | scanf("%d", &option); 241 | 242 | switch (option) 243 | { 244 | case 1: 245 | printf("Enter value to be inserted: "); 246 | scanf("%d", &key); 247 | insertNode(&root, key); 248 | break; 249 | case 2: 250 | printf("Enter value to be deleted: "); 251 | scanf("%d", &key); 252 | ptr = deleteNode(&root, key); 253 | if (ptr == NULL) 254 | printf("Element Not Found\n"); 255 | else 256 | printf("%d is Deleted\n", ptr->data); 257 | break; 258 | case 3: 259 | printf("Enter value to be searched: "); 260 | scanf("%d", &key); 261 | ptr = search(root, key); 262 | if (ptr == NULL) 263 | printf("Element Not Found\n"); 264 | else 265 | printf("%d is Found\n", ptr->data); 266 | break; 267 | case 4: 268 | printf("Inorder : "); 269 | printInOrder(root); 270 | printf("\n"); 271 | 272 | printf("Preorder : "); 273 | printPreOrder(root); 274 | printf("\n"); 275 | 276 | printf("Postorder : "); 277 | printPostOrder(root); 278 | printf("\n"); 279 | break; 280 | case 5: 281 | printf("No. of leafs : %d\n", countLeafs(root)); 282 | break; 283 | case 6: 284 | status = 0; 285 | break; 286 | default: 287 | printf("Enter appropriate option.\n"); 288 | break; 289 | } 290 | } 291 | free(ptr); 292 | purge(&root); 293 | return 0; 294 | } -------------------------------------------------------------------------------- /data_structures/binary_trees/binarySearchTree.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/data_structures/binary_trees/binarySearchTree.exe -------------------------------------------------------------------------------- /data_structures/binary_trees/binaryTree.c: -------------------------------------------------------------------------------- 1 | // Tree traversal in C 2 | 3 | #include 4 | #include 5 | 6 | struct node 7 | { 8 | int item; 9 | struct node *left; 10 | struct node *right; 11 | }; 12 | 13 | // Inorder traversal 14 | void inorderTraversal(struct node *root) 15 | { 16 | if (root == NULL) 17 | return; 18 | inorderTraversal(root->left); 19 | printf("%d ->", root->item); 20 | inorderTraversal(root->right); 21 | } 22 | 23 | // Preorder traversal 24 | void preorderTraversal(struct node *root) 25 | { 26 | if (root == NULL) 27 | return; 28 | printf("%d ->", root->item); 29 | preorderTraversal(root->left); 30 | preorderTraversal(root->right); 31 | } 32 | 33 | // Postorder traversal 34 | void postorderTraversal(struct node *root) 35 | { 36 | if (root == NULL) 37 | return; 38 | postorderTraversal(root->left); 39 | postorderTraversal(root->right); 40 | printf("%d ->", root->item); 41 | } 42 | 43 | // Create a new Node 44 | struct node *createNode(value) 45 | { 46 | struct node *newNode = malloc(sizeof(struct node)); 47 | newNode->item = value; 48 | newNode->left = NULL; 49 | newNode->right = NULL; 50 | 51 | return newNode; 52 | } 53 | 54 | // Insert on the left of the node 55 | struct node *insertLeft(struct node *root, int value) 56 | { 57 | root->left = createNode(value); 58 | return root->left; 59 | } 60 | 61 | // Insert on the right of the node 62 | struct node *insertRight(struct node *root, int value) 63 | { 64 | root->right = createNode(value); 65 | return root->right; 66 | } 67 | 68 | int main() 69 | { 70 | struct node *root = createNode(1); 71 | insertLeft(root, 2); 72 | insertRight(root, 3); 73 | insertLeft(root->left, 4); 74 | 75 | printf("Inorder traversal \n"); 76 | inorderTraversal(root); 77 | 78 | printf("\nPreorder traversal \n"); 79 | preorderTraversal(root); 80 | 81 | printf("\nPostorder traversal \n"); 82 | postorderTraversal(root); 83 | } -------------------------------------------------------------------------------- /data_structures/graphs/graphUsingAdjacencyMatrix.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file graphUsingAdjacencyMatrix.c 3 | * @brief To represent a Bi-directional graph using adjacency matrix in C. 4 | */ 5 | 6 | #include 7 | 8 | // Initializing the number of vertices 9 | #define vertices 5 10 | 11 | /** 12 | * @brief To add edge between two vertices 13 | * @param arr the graph representation using matrix 14 | * @param i represents the position of vertex that has edge with j 15 | * @param j represents the position of vertex that has edge with i 16 | */ 17 | void addEdge(int arr[][vertices], int i, int j) 18 | { 19 | if (i < vertices && j < vertices) 20 | { 21 | arr[i][j] = 1; 22 | arr[j][i] = 1; 23 | } 24 | else 25 | printf("Node not found\n"); 26 | } 27 | 28 | int main() 29 | { 30 | int adjMatrix[vertices][vertices]; 31 | for (int i = 0; i < vertices; i++) 32 | for (int j = 0; j < vertices; j++) 33 | adjMatrix[i][j] = 0; 34 | 35 | addEdge(adjMatrix, 0, 1); 36 | addEdge(adjMatrix, 0, 2); 37 | addEdge(adjMatrix, 1, 2); 38 | addEdge(adjMatrix, 2, 0); 39 | addEdge(adjMatrix, 2, 3); 40 | addEdge(adjMatrix, 4, 3); 41 | 42 | printf("Adjacency Matrix representation of graph:\n"); 43 | for (int i = 0; i < vertices; i++) 44 | { 45 | for (int j = 0; j < vertices; j++) 46 | printf("%d ", adjMatrix[i][j]); 47 | printf("\n"); 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /data_structures/linked_lists/doubly_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | /*doubly linked list has additional link in each node compared to 4 | *the linked list we have seen 5 | *a doubly linked list looks like : 6 | * Node1<===>Node2<===>Node3<====>Node4<===>.....Nodek 7 | *whereas a singly linked list looks like: 8 | * Node1--->Node2--->Node3--->Node4....... 9 | *The advantage of doubly linked list is that it permits traversal in both forward 10 | * and backward direction. Given any node you can retrieve the contents of the whole 11 | * list. 12 | */ 13 | 14 | struct dllNode 15 | { 16 | // data part 17 | int key; 18 | 19 | // link part 20 | struct dllNode *next; // this pointer is used to store the address of the next node in the list 21 | struct dllNode *prev; // this pointer is used to store the address of the previous node in the list 22 | }; 23 | 24 | typedef struct dllNode node; 25 | 26 | // this function dynamically creates a node of dllNode type and returns its address 27 | node *newNode() 28 | { 29 | node *newNode = (node *)malloc(sizeof(node)); 30 | newNode->next = NULL; 31 | newNode->prev = NULL; 32 | return newNode; 33 | } 34 | 35 | // this frees the memory alloted to a node and returns the chunk of memory back to the heap 36 | void freeNode(node *remNode) 37 | { 38 | free(remNode); 39 | } 40 | 41 | /* 42 | * Search Function 43 | */ 44 | 45 | node *searchForX(node *header, int x) 46 | { 47 | if (header == NULL) 48 | { 49 | printf("%s\n", "List is empty"); 50 | return NULL; 51 | } 52 | else 53 | { 54 | for (node *ptr = header; ptr != NULL; ptr = ptr->next) 55 | { 56 | if (ptr->key == x) 57 | { 58 | return ptr; 59 | } 60 | } 61 | return NULL; 62 | } 63 | } 64 | 65 | /*Function to insert at the front (at the position of the header) 66 | *The new node is linked using next pointer to the present header. 67 | *Present header's prev pointer is set to the newnode address. 68 | *prev pointer of the newnode is set to NULL 69 | *Finally header is set to newnode. 70 | * 71 | */ 72 | void insertAtFront(node **header, int k) 73 | { 74 | node *newnode; 75 | // check if the header is null that is, the list is empty 76 | if (*header == NULL) 77 | { 78 | *header = newNode(); 79 | (*header)->key = k; 80 | } 81 | else 82 | { 83 | newnode = newNode(); 84 | newnode->key = k; 85 | newnode->next = *header; 86 | (*header)->prev = newnode; 87 | *header = newnode; 88 | } 89 | } 90 | 91 | // *Insert After a given key x 92 | // *Function inserts a new node between the node containing x 93 | // *and its subsequent node. 94 | // * in the function declaration note that x denotes the key after insertion 95 | // * is done and k refers to the key inserted 96 | 97 | void insertAfterX(node *header, int x, int k) 98 | { 99 | node *xNode; 100 | node *kNode; 101 | if (header == NULL) 102 | { 103 | printf(" %s \n", "Insertion failed since list is empty"); 104 | } 105 | else 106 | { 107 | xNode = searchForX(header, x); 108 | if (xNode == NULL) 109 | { 110 | printf("%s\n", "Insertion Failed since x not present"); 111 | } 112 | else 113 | { 114 | kNode = newNode(); 115 | kNode->key = k; 116 | kNode->prev = xNode; 117 | kNode->next = xNode->next; 118 | if (xNode->next != NULL) 119 | { 120 | xNode->next->prev = kNode; 121 | } 122 | xNode->next = kNode; 123 | } 124 | } 125 | } 126 | 127 | /*function to print the doubly linked list 128 | *it starts from the header and traverses forward 129 | *using next pointer in each node to print the key 130 | * 131 | */ 132 | void printList(node *header) 133 | { 134 | if (header == NULL) 135 | { 136 | printf("\n%s\n", "The list is empty!!!"); 137 | } 138 | else 139 | { 140 | printf("\n"); 141 | for (node *ptr = header; ptr != NULL; ptr = ptr->next) 142 | { 143 | printf(" %d ", ptr->key); 144 | } 145 | } 146 | } 147 | 148 | /*Delete function: 149 | *Name of the function is : deleteX 150 | *It searches for the element x and deletes the node from the list. 151 | * 152 | */ 153 | //=========================================deleteX============================ 154 | 155 | void deleteX(node **header, int x) 156 | { 157 | node *xNode; 158 | if (*header == NULL) 159 | { 160 | printf("\nDeletion failed since list is empty"); 161 | return; 162 | } 163 | 164 | xNode = searchForX(*header, x); 165 | if (xNode == NULL) 166 | { 167 | printf("\nDeletion Failed since x not present"); 168 | return; 169 | } 170 | 171 | if (xNode == *header) 172 | *header = xNode->next; 173 | 174 | if (xNode->next != NULL) 175 | xNode->next->prev = xNode->prev; 176 | 177 | if (xNode->prev != NULL) 178 | xNode->prev->next = xNode->next; 179 | 180 | freeNode(xNode); 181 | } 182 | 183 | //=========================================================================== 184 | 185 | int main() 186 | { 187 | 188 | node *header = NULL; 189 | insertAtFront(&header, 10); 190 | insertAtFront(&header, 20); 191 | insertAtFront(&header, 30); 192 | insertAtFront(&header, 40); 193 | printList(header); 194 | insertAfterX(header, 30, 35); 195 | insertAfterX(header, 10, 45); 196 | printList(header); 197 | 198 | deleteX(&header, 40); 199 | printList(header); 200 | return 0; 201 | } -------------------------------------------------------------------------------- /data_structures/linked_lists/linked_list.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file linked_list.c 3 | * @brief Implementation of a Linked List in C 4 | * @details The implementation has following functionalities: 5 | * - Create a new Node 6 | * - Insertion of node at beginning or at specific key in list 7 | * - Deletion of node at beginning or at specific key in list 8 | * - Reversing a linked list 9 | * - Searching for a node having specific key 10 | * - Printing the linked list 11 | */ 12 | 13 | #include 14 | #include 15 | 16 | // Basic structure of a node in a linked list 17 | typedef struct linkedList 18 | { 19 | int data; 20 | struct linkedList *next; 21 | } node; 22 | 23 | node *newNode(int ); 24 | void purge(node **); 25 | void insert(node **, int); 26 | void insertAt(node **, int, int); 27 | void delete (node **); 28 | void deleteAt(node **, int); 29 | void printList(node *); 30 | node *searchElt(node *, int); 31 | void reverse(node **); 32 | 33 | /** 34 | * @brief The function to create a node and initialize it's data and link parts 35 | * 36 | * @param data the data part to be stored in the node 37 | * @return node* new node with provided data 38 | * @note the node must be deleted before program terminates to avoid memory leaks 39 | */ 40 | node *newNode(int data) 41 | { 42 | // creates a node 43 | node *new = (node *)malloc(sizeof(node)); 44 | 45 | // initializes the node 46 | new->data = data; 47 | new->next = NULL; 48 | 49 | return new; 50 | } 51 | 52 | /** 53 | * @brief Utilary procedure, to free all the nodes in the list 54 | * @param header the pointer to the head node 55 | */ 56 | void purge(node **header) 57 | { 58 | if (*header != NULL) 59 | { 60 | if ((*header)->next != NULL) 61 | purge(&(*header)->next); 62 | free(*header); 63 | *header = NULL; 64 | } 65 | } 66 | 67 | /** 68 | * @brief Prints all the data stored in the list 69 | * @param header the pointer to head node 70 | */ 71 | void printList(node *header) 72 | { 73 | if (header == NULL) 74 | { 75 | printf("%s\n", "List is Empty"); 76 | return; 77 | } 78 | for (node *ptr = header; ptr != NULL; ptr = ptr->next) 79 | printf("%d ", ptr->data); 80 | printf("\n"); 81 | } 82 | 83 | /** 84 | * @brief Search procedure,to traverse through the list to find the input key 85 | * @param header the pointer to head node 86 | * @param elt he input key, that is to be searched in the list 87 | * @return node* if key present or NULL if key not in list 88 | */ 89 | node *searchElt(node *header, int elt) 90 | { 91 | if (header == NULL) 92 | { 93 | printf("%s\n", "List is Empty"); 94 | return NULL; 95 | } 96 | for (node *ptr = header; ptr != NULL; ptr = ptr->next) 97 | if (ptr->data == elt) 98 | return ptr; 99 | 100 | printf("%s\n", "Element not present"); 101 | return NULL; 102 | } 103 | 104 | /** 105 | * @brief Insertion procedure, which inserts input key into the list 106 | * @param header the pointer to head node 107 | * @param data the input key value, that is to be stored in the list 108 | */ 109 | void insert(node **header, int data) 110 | { 111 | if (*header == NULL) 112 | { 113 | *header = newNode(data); 114 | } 115 | else 116 | { 117 | node *new = newNode(data); 118 | new->next = *header; 119 | *header = new; 120 | } 121 | } 122 | 123 | /** 124 | * @brief Insertion procedure, searches the target element in the list and then 125 | * inserts input key after that element, if present 126 | * @param header the pointer to head node 127 | * @param elt the target element 128 | * @param data the input key value, that is to be stored in the list 129 | */ 130 | void insertAt(node **header, int elt, int data) 131 | { 132 | node *eltNode = searchElt(*header, elt); 133 | if (eltNode == NULL) 134 | return; 135 | node *new = newNode(data); 136 | new->next = eltNode->next; 137 | eltNode->next = new; 138 | } 139 | 140 | /** 141 | * @brief Deletion procedure, removes the head node in the list 142 | * @param header the pointer to head node 143 | */ 144 | void delete (node **header) 145 | { 146 | if (*header == NULL) 147 | { 148 | printf("%s\n", "List is Empty"); 149 | return; 150 | } 151 | node *ptr = *header; 152 | *header = ptr->next; 153 | free(ptr); 154 | } 155 | 156 | /** 157 | * @brief Deletion procedure, searches the target element in the tree and then 158 | * removes that element, if present 159 | * @param header the pointer to head node 160 | * @param elt the target element 161 | */ 162 | void deleteAt(node **header, int elt) 163 | { 164 | if (*header == NULL) 165 | { 166 | printf("%s\n", "List is Empty"); 167 | return; 168 | } 169 | 170 | node *ptr; 171 | node *prevptr = NULL; 172 | for (ptr = *header; ptr != NULL; ptr = ptr->next) 173 | { 174 | if (ptr->data == elt) 175 | break; 176 | prevptr = ptr; 177 | } 178 | 179 | if (ptr == NULL) 180 | { 181 | printf("%s\n", "Element not present"); 182 | return; 183 | } 184 | 185 | if (ptr == *header) 186 | *header = (*header)->next; 187 | else 188 | prevptr->next = ptr->next; 189 | free(ptr); 190 | } 191 | 192 | /** 193 | * @brief Reverse the elements in the linked list 194 | * @param header the pointer to head node 195 | */ 196 | void reverse(node **header) 197 | { 198 | node *prev = NULL, *next = NULL; 199 | for (node *ptr = (*header); ptr != NULL; ptr = next) 200 | { 201 | next = ptr->next; 202 | ptr->next = prev; 203 | prev = ptr; 204 | } 205 | *header = prev; 206 | } 207 | 208 | int main() 209 | { 210 | node *header = NULL; 211 | insert(&header, 20); 212 | printList(header); 213 | insert(&header, 40); 214 | insert(&header, 10); 215 | insert(&header, 15); 216 | insert(&header, 75); 217 | printList(header); 218 | 219 | delete (&header); 220 | printList(header); 221 | delete (&header); 222 | printList(header); 223 | 224 | insert(&header, 12); 225 | printList(header); 226 | 227 | insertAt(&header, 12, 15); 228 | insertAt(&header, 40, 25); 229 | printList(header); 230 | 231 | deleteAt(&header, 10); 232 | deleteAt(&header, 40); 233 | printList(header); 234 | 235 | reverse(&header); 236 | printList(header); 237 | 238 | purge(&header); 239 | return 0; 240 | } -------------------------------------------------------------------------------- /data_structures/linked_lists/linked_list.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/data_structures/linked_lists/linked_list.exe -------------------------------------------------------------------------------- /misc/even_odd.c: -------------------------------------------------------------------------------- 1 | // To check whether a number is Even or Odd using Bitwise AND 2 | 3 | #include 4 | 5 | void even_odd(int num) { 6 | // num & 1 return 1 if num is odd else 0 if even 7 | if ( !(num & 1) ) 8 | printf("even"); 9 | else 10 | printf("odd"); 11 | } 12 | 13 | void main() { 14 | even_odd(5); 15 | } 16 | -------------------------------------------------------------------------------- /misc/highestCA.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float high(float CA1, float CA2, float CA3) 4 | { 5 | if (CA1 > CA2 && CA1 > CA3) 6 | return CA1; 7 | if (CA2 > CA1 && CA2 > CA3) 8 | return CA2; 9 | return CA3; 10 | } 11 | 12 | int main() 13 | { 14 | float ca1, ca2, ca3, highest; 15 | 16 | printf("Enter the marks got in INT102\n"); 17 | printf("Marks in CA 1: "); 18 | scanf("%f", &ca1); 19 | printf("Marks in CA 2: "); 20 | scanf("%f", &ca2); 21 | printf("Marks in CA 3: "); 22 | scanf("%f", &ca3); 23 | 24 | highest = high(ca1, ca2, ca3); 25 | 26 | printf("Highest Score is : %f", highest); 27 | } -------------------------------------------------------------------------------- /misc/matInverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | float determinant(float [25][25], float); 5 | void cofactor(float [25][25], float, float); 6 | void inverse(float [25][25], float, float); 7 | void minorMat(float[25][25], float[25][25], float, int, int); 8 | 9 | int main() { 10 | float mat[25][25], order, det; 11 | printf("Enter the order of the Matrix : "); 12 | scanf("%f", &order); 13 | printf("Enter the elements of %.0fX%.0f Matrix : \n", order, order); 14 | for (int i = 0;i < order; i++) 15 | for (int j = 0;j < order; j++) 16 | scanf("%f", &mat[i][j]); 17 | det = determinant(mat, order); 18 | if (det == 0) 19 | printf("\nInverse of Entered Matrix is not possible\n"); 20 | else 21 | cofactor(mat, order, det); 22 | } 23 | 24 | // To calculate Determinant of a matrix 25 | float determinant(float mat[25][25], float order) { 26 | float s = 1, det = 0, minor[25][25]; 27 | if (order == 1) 28 | return (mat[0][0]); 29 | else 30 | for (int i = 0; i < order; i++) { 31 | minorMat(mat, *minor, order, 0, i); 32 | det += s * (mat[0][i] * determinant(minor, order - 1)); 33 | s = -1 * s; 34 | } 35 | return det; 36 | } 37 | 38 | // To create a minor of a matrix 39 | void minorMat(float mat[25][25],float minor[25][25], float order, int row, int col) { 40 | int m = 0, n = 0; 41 | for (int i = 0;i < order; i++) 42 | for (int j = 0 ;j < order; j++) { 43 | if (i != row && j != col) { 44 | minor[m][n] = mat[i][j]; 45 | if (n < (order - 2)) 46 | n++; 47 | else { 48 | n = 0; 49 | m++; 50 | } 51 | } 52 | } 53 | } 54 | 55 | // To find the CoFactor of a Matrix 56 | void cofactor(float mat[25][25], float order, float det) { 57 | float minor[25][25], cofac[25][25]; 58 | for (int i = 0;i < order; i++) 59 | for (int j = 0;j < order; j++) { 60 | minorMat(mat, *minor, order, j, i); 61 | cofac[i][j] = pow(-1, i + j) * determinant(minor, order - 1); 62 | } 63 | inverse(cofac, order, det); 64 | } 65 | 66 | // To find the Inverse of Matrix 67 | void inverse(float fac[25][25], float order, float det) 68 | { 69 | float adjoint[25][25], inverse[25][25]; 70 | for (int i = 0;i < order; i++) 71 | for (int j = 0;j < order; j++) 72 | adjoint[i][j] = fac[j][i]; 73 | for (int i = 0;i < order; i++) 74 | for (int j = 0;j < order; j++) 75 | inverse[i][j] = adjoint[i][j] / det; 76 | 77 | printf("The inverse of matrix is :\n"); 78 | for (int i = 0;i < order; i++) { 79 | for (int j = 0;j < order; j++) 80 | printf("%f ", inverse[i][j]); 81 | printf("\n"); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /misc/pascalsTraingle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int rows = 5; 5 | for (int i = 0; i < rows; i++) { 6 | for (int j = 0; j < rows - i; j++) 7 | printf(" "); 8 | for (int k = 0; k <= i; k++) 9 | printf("* "); 10 | printf("\n"); 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /misc/pascalsTraingle.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/misc/pascalsTraingle.exe -------------------------------------------------------------------------------- /misc/pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void fun(int *address,int n) 4 | { 5 | for (int i = 0; i < n/2; i++) 6 | { 7 | int temp = *address; 8 | *address = *(address + n - i - 1); 9 | *(address + n - i - 1) = temp; 10 | } 11 | } 12 | 13 | void main() 14 | { 15 | int A[] = {1,2,3} , num = 3; 16 | fun(&A,num); 17 | for (int i = 0; i < num; i++) 18 | printf("%d",A[i]); 19 | 20 | } -------------------------------------------------------------------------------- /misc/primeRegNo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char regNo[25]; 6 | int i; 7 | 8 | printf("Enter Registration No. : "); 9 | scanf("%s", regNo); 10 | 11 | // check if the charachter is number b/w 2 to 7 12 | // since prime number digits starts at 2 and end at 7 13 | // checking for prime number by excluding the non prime numbers 14 | // which are 4 and 6 b/w 2 to 7 15 | // hence the only prime numbers are 2, 3, 5, 7 16 | 17 | for (i = 0; regNo[i] != '\0'; i++) 18 | if (regNo[i] == '2' || regNo[i] == '3' || regNo[i] == '5' || regNo[i] == '7') 19 | printf("%c\n", regNo[i]); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /misc/productOfPositive.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int status = 1, product = 1; 6 | while (status == 1) 7 | { 8 | int num; 9 | printf("Enter a number : "); 10 | scanf("%d", &num); 11 | if (num < 0) 12 | status = 0; 13 | else 14 | product *= num; 15 | } 16 | printf("Product is : %d", product); 17 | } -------------------------------------------------------------------------------- /misc/reverse.c: -------------------------------------------------------------------------------- 1 | // To display input number in reverse 2 | 3 | #include 4 | 5 | void main() { 6 | int num, rev_num = 0,temp; 7 | 8 | printf("Enter number: "); 9 | scanf("%d",&num); 10 | 11 | temp = num; 12 | 13 | // Storing each digit of num to rev_num in reverse 14 | while (temp != 0) { 15 | rev_num = (rev_num * 10) + (temp % 10); 16 | temp /= 10; 17 | } 18 | 19 | printf("Reversed number: %d",rev_num); 20 | } 21 | -------------------------------------------------------------------------------- /misc/shoppingBill.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // Initialzation 6 | int status = 1, quantity; 7 | float billAmount = 0.0; 8 | char itemNo; 9 | 10 | // Continue untill the purchase is over 11 | while (status == 1) 12 | { 13 | // Displaying the items that can be purchased 14 | printf("\n"); 15 | printf("1. Pen 10/-\n"); 16 | printf("2. Book 45.5/-\n"); 17 | printf("3. A4 Sheets 1.5/-\n"); 18 | printf("4. Graph Paper 2/-\n"); 19 | printf("5. Water Bottle 50/-\n"); 20 | printf("6. Watch 99.99/-\n"); 21 | printf("Enter 'N' to end the purchase\n"); 22 | 23 | // Input the item no 24 | printf("Enter the Item No. : "); 25 | scanf("%s", &itemNo); 26 | 27 | // Checks for the item no 28 | switch (itemNo) 29 | { 30 | case '1': 31 | printf("Enter Quantity : "); 32 | scanf("%d", &quantity); 33 | billAmount += 10 * quantity; // adds the (the price of the item * to its quantity) to the amount 34 | break; 35 | 36 | case '2': 37 | printf("Enter Quantity : "); 38 | scanf("%d", &quantity); 39 | billAmount += 40.5 * quantity; 40 | break; 41 | 42 | case '3': 43 | printf("Enter Quantity : "); 44 | scanf("%d", &quantity); 45 | billAmount += 1.5 * quantity; 46 | break; 47 | 48 | case '4': 49 | printf("Enter Quantity : "); 50 | scanf("%d", &quantity); 51 | billAmount += 2 * quantity; 52 | break; 53 | 54 | case '5': 55 | printf("Enter Quantity : "); 56 | scanf("%d", &quantity); 57 | billAmount += 50 * quantity; 58 | break; 59 | 60 | case '6': 61 | printf("Enter Quantity : "); 62 | scanf("%d", &quantity); 63 | billAmount += 99.9 * quantity; 64 | break; 65 | 66 | case 'N': 67 | status = 0; // changing status to 0 (or false) to stop the purchasing loop 68 | break; 69 | 70 | default: 71 | printf("Invalid Item No. !!!\tPlease enter the correct itemNo\n"); 72 | break; 73 | } 74 | } 75 | 76 | // Displaying the total amount 77 | printf("\nBill : %f", billAmount); 78 | return 0; 79 | } -------------------------------------------------------------------------------- /misc/smallest_odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int a[10],n; 6 | 7 | printf("Enter no of elements: "); 8 | scanf("%d",&n); 9 | 10 | printf("Enter the elements: "); 11 | for (int i = 0; i < n; i++) 12 | scanf("%d",&a[i]); 13 | 14 | for (int i = 0; i < n; i++) 15 | for (int j = 0; j < n; j++) 16 | if (a[j] > a[j+1]) 17 | { 18 | int temp = a[j]; 19 | a[j] = a[j+1]; 20 | a[j+1] = temp; 21 | } 22 | 23 | for (int i = 0; i < n; i++) 24 | if (a[i]%2 != 0) 25 | { 26 | printf("%d is smallest odd number and ",a[i]); 27 | break; 28 | } 29 | 30 | for (int i = 0; i < n; i++) 31 | if (a[n-i-1]%2 == 0) 32 | { 33 | printf("%d is largest even number",a[n-i-1]); 34 | break; 35 | } 36 | } -------------------------------------------------------------------------------- /misc/starPyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int rows = 5; 5 | for (int i = 0; i < rows; i++) { 6 | for (int j = 0; j < rows - i; j++) 7 | printf(" "); 8 | for (int k = 0; k != (2*i) + 1; k++) 9 | printf("* "); 10 | printf("\n"); 11 | } 12 | return 0; 13 | } -------------------------------------------------------------------------------- /misc/starPyramid.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/misc/starPyramid.exe -------------------------------------------------------------------------------- /network-lab-programs/Dictionary/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/Dictionary/client -------------------------------------------------------------------------------- /network-lab-programs/Dictionary/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 27 | printf("Connected\n"); 28 | 29 | while (1) 30 | { 31 | printf("Enter Word: "); 32 | scanf("%s", buffer); 33 | send(clientSocket, buffer, sizeof(buffer), 0); 34 | 35 | recv(clientSocket, buffer, sizeof(buffer), 0); 36 | printf("Meaning: %s \n", buffer); 37 | recv(clientSocket, buffer, sizeof(buffer), 0); 38 | printf("Antonym: %s \n", buffer); 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /network-lab-programs/Dictionary/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/Dictionary/server -------------------------------------------------------------------------------- /network-lab-programs/Dictionary/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket, clientSocket; 12 | int found = 0, position = -1; 13 | char buffer[1000]; 14 | char dict[4][3][1024] = { 15 | {"Term1", "Meaning1", "Antonym1"}, 16 | {"Term2", "Meaning2", "Antonym2"}, 17 | {"Term3", "Meaning3", "Antonym3"}, 18 | {"Term4", "Meaning4", "Antonym4"}, 19 | }; 20 | 21 | struct sockaddr_in serverAddr; 22 | socklen_t addrSize; 23 | 24 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 25 | serverAddr.sin_family = AF_INET; 26 | serverAddr.sin_port = htons(7891); 27 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 28 | 29 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 30 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 31 | addrSize = sizeof(serverAddr); 32 | 33 | if (listen(serverSocket, 5) == 0) 34 | { 35 | printf("listening\n"); 36 | } 37 | else 38 | { 39 | printf("error\n"); 40 | } 41 | 42 | clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize); 43 | printf("Connected\n"); 44 | 45 | while (1) 46 | { 47 | recv(clientSocket, buffer, sizeof(buffer), 0); 48 | printf("Word: %s \n", buffer); 49 | for (int i = 0; i < 4; i++) 50 | { 51 | if (strcmp(buffer, dict[i][0]) == 0) 52 | { 53 | found = 1; 54 | position = i; 55 | break; 56 | } 57 | } 58 | if (found == 1) 59 | { 60 | strcpy(buffer, dict[position][1]); 61 | send(clientSocket, buffer, sizeof(buffer), 0); 62 | strcpy(buffer, dict[position][2]); 63 | send(clientSocket, buffer, sizeof(buffer), 0); 64 | found = 0; 65 | } 66 | else 67 | { 68 | strcpy(buffer, "Not Found"); 69 | send(clientSocket, buffer, sizeof(buffer), 0); 70 | send(clientSocket, buffer, sizeof(buffer), 0); 71 | } 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /network-lab-programs/DistanceVectorRouting/dvr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int costMatrix[20][20], n; 4 | int i, j, k; 5 | struct router 6 | { 7 | int distance[20]; 8 | int nextHop[20]; 9 | } node[20]; 10 | 11 | void readCostMatrix() 12 | { 13 | printf("Enter cost matrix:\n"); 14 | for (i = 0; i < n; i++) 15 | { 16 | for (j = 0; j < n; j++) 17 | { 18 | scanf("%d", &costMatrix[i][j]); 19 | 20 | if (i == j) 21 | { 22 | costMatrix[i][j] = 0; 23 | } 24 | node[i].distance[j] = costMatrix[i][j]; 25 | node[i].nextHop[j] = j; 26 | } 27 | } 28 | } 29 | 30 | void calcRoutingTable() 31 | { 32 | for (int l = 0; l < n; l++) 33 | { 34 | for (i = 0; i < n; i++) 35 | { 36 | for (j = 0; j < n; j++) 37 | { 38 | for (k = 0; k < n; k++) 39 | { 40 | if (node[i].distance[k] + node[k].distance[j] < node[i].distance[j]) 41 | { 42 | node[i].distance[j] = node[i].distance[k] + node[k].distance[j]; 43 | node[i].nextHop[j] = k; 44 | } 45 | } 46 | } 47 | } 48 | } 49 | } 50 | 51 | void displayRoutes() 52 | { 53 | for (i = 0; i < n; i++) 54 | { 55 | printf("\n Router %d\n", i + 1); 56 | 57 | for (j = 0; j < n; j++) 58 | { 59 | printf("Node %d via %d | distance %d", j + 1, node[i].nextHop[j] + 1, node[i].distance[j]); 60 | printf("\n"); 61 | } 62 | } 63 | } 64 | 65 | int main() 66 | { 67 | printf("Enter no of nodes: "); 68 | scanf("%d", &n); 69 | 70 | readCostMatrix(); 71 | calcRoutingTable(); 72 | displayRoutes(); 73 | 74 | return 0; 75 | } -------------------------------------------------------------------------------- /network-lab-programs/FTP/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/FTP/client -------------------------------------------------------------------------------- /network-lab-programs/FTP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000], fname[200]; 13 | FILE *fp; 14 | 15 | struct sockaddr_in serverAddr; 16 | socklen_t addrSize; 17 | 18 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 19 | 20 | serverAddr.sin_family = AF_INET; 21 | serverAddr.sin_port = htons(7891); 22 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 23 | 24 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 25 | addrSize = sizeof(serverAddr); 26 | 27 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 28 | printf("Connected\n"); 29 | 30 | printf("Enter existing file name: "); 31 | scanf("%s", buffer); 32 | send(clientSocket, buffer, sizeof(buffer), 0); 33 | 34 | printf("Enter new file name: "); 35 | scanf("%s", fname); 36 | 37 | fp = fopen(fname, "w"); 38 | 39 | while (1) 40 | { 41 | recv(clientSocket, buffer, sizeof(buffer), 0); 42 | if (strcmp(buffer, "completed") == 0) 43 | { 44 | printf("\nFile Transferred \n"); 45 | break; 46 | } 47 | fprintf(fp, "%s", buffer); 48 | printf("%s", buffer); 49 | } 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /network-lab-programs/FTP/file.txt: -------------------------------------------------------------------------------- 1 | Hello 2 | Hi Bye -------------------------------------------------------------------------------- /network-lab-programs/FTP/new.txt: -------------------------------------------------------------------------------- 1 | Hello 2 | Hi Bye -------------------------------------------------------------------------------- /network-lab-programs/FTP/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/FTP/server -------------------------------------------------------------------------------- /network-lab-programs/FTP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket, clientSocket; 12 | char buffer[1000]; 13 | FILE *fp; 14 | 15 | struct sockaddr_in serverAddr; 16 | socklen_t addrSize; 17 | 18 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 25 | addrSize = sizeof(serverAddr); 26 | 27 | if (listen(serverSocket, 5) == 0) 28 | { 29 | printf("listening\n"); 30 | } 31 | else 32 | { 33 | printf("error\n"); 34 | } 35 | 36 | clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize); 37 | printf("Connected\n"); 38 | 39 | recv(clientSocket, buffer, sizeof(buffer), 0); 40 | 41 | fp = fopen(buffer, "r"); 42 | while (fgets(buffer, sizeof(buffer), fp)) 43 | { 44 | send(clientSocket, buffer, sizeof(buffer), 0); 45 | } 46 | strcpy(buffer, "completed"); 47 | send(clientSocket, buffer, sizeof(buffer), 0); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /network-lab-programs/MultiUserChat/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/MultiUserChat/client -------------------------------------------------------------------------------- /network-lab-programs/MultiUserChat/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 27 | printf("Connected\n"); 28 | 29 | while (1) 30 | { 31 | printf("Enter message: "); 32 | scanf("%s", buffer); 33 | send(clientSocket, buffer, sizeof(buffer), 0); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /network-lab-programs/MultiUserChat/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/MultiUserChat/server -------------------------------------------------------------------------------- /network-lab-programs/MultiUserChat/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket, clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | serverAddr.sin_family = AF_INET; 19 | serverAddr.sin_port = htons(7891); 20 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | 22 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 23 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | if (listen(serverSocket, 5) == 0) 27 | { 28 | printf("listening\n"); 29 | } 30 | else 31 | { 32 | printf("error\n"); 33 | } 34 | 35 | while (clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize)) 36 | { 37 | int pid; 38 | if ((pid = fork()) == 0) 39 | { 40 | while (recv(clientSocket, buffer, sizeof(buffer), 0) > 0) 41 | { 42 | printf("Message Recieved: %s \n", buffer); 43 | } 44 | } 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /network-lab-programs/SMTP/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/SMTP/client -------------------------------------------------------------------------------- /network-lab-programs/SMTP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 27 | printf("Connected\n"); 28 | 29 | strcpy(buffer, "Client Greeting Hello"); 30 | send(clientSocket, buffer, sizeof(buffer), 0); 31 | recv(clientSocket, buffer, sizeof(buffer), 0); 32 | printf("Received: %s \n", buffer); 33 | 34 | printf("Enter FROM address: "); 35 | scanf("%s", buffer); 36 | send(clientSocket, buffer, sizeof(buffer), 0); 37 | 38 | printf("Enter TO address: "); 39 | scanf("%s", buffer); 40 | send(clientSocket, buffer, sizeof(buffer), 0); 41 | 42 | printf("Enter mail body:\n"); 43 | int i = 0; 44 | char ch; 45 | while ((ch = getchar()) != '#') // stops when input is # 46 | { 47 | if (ch == '\0') 48 | continue; 49 | buffer[i++] = ch; 50 | } 51 | buffer[i] = '\0'; 52 | 53 | printf("%s", buffer); 54 | send(clientSocket, buffer, sizeof(buffer), 0); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /network-lab-programs/SMTP/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/SMTP/server -------------------------------------------------------------------------------- /network-lab-programs/SMTP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket, clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | serverAddr.sin_family = AF_INET; 19 | serverAddr.sin_port = htons(7891); 20 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | 22 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 23 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | if (listen(serverSocket, 5) == 0) 27 | { 28 | printf("listening\n"); 29 | } 30 | else 31 | { 32 | printf("error\n"); 33 | } 34 | 35 | clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize); 36 | printf("Connected\n"); 37 | 38 | recv(clientSocket, buffer, sizeof(buffer), 0); 39 | printf("Recieved: %s \n", buffer); 40 | strcpy(buffer, "Server Greeting Hello"); 41 | send(clientSocket, buffer, sizeof(buffer), 0); 42 | 43 | recv(clientSocket, buffer, sizeof(buffer), 0); 44 | printf("MAIL FROM: %s \n", buffer); 45 | 46 | recv(clientSocket, buffer, sizeof(buffer), 0); 47 | printf("RECPT TO: %s \n", buffer); 48 | 49 | recv(clientSocket, buffer, sizeof(buffer), 0); 50 | printf("Mail Received: %s \n", buffer); 51 | 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /network-lab-programs/SlidingWindowProtocol/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/SlidingWindowProtocol/client -------------------------------------------------------------------------------- /network-lab-programs/SlidingWindowProtocol/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | int choice; 14 | 15 | struct sockaddr_in serverAddr; 16 | socklen_t addrSize; 17 | 18 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 19 | 20 | serverAddr.sin_family = AF_INET; 21 | serverAddr.sin_port = htons(7891); 22 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 23 | 24 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 25 | addrSize = sizeof(serverAddr); 26 | 27 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 28 | printf("Connected\n"); 29 | 30 | while (1) 31 | { 32 | recv(clientSocket, buffer, sizeof(buffer), 0); 33 | if (strcmp(buffer, "exit") == 0) 34 | { 35 | printf("Exiting\n"); 36 | break; 37 | } 38 | printf("Data Received: %s \n", buffer); 39 | 40 | printf("Do you want to report an error? (1-Yes, 0-No): "); 41 | scanf("%d", &choice); 42 | if (choice == 0) 43 | { 44 | strcpy(buffer, "-1"); 45 | send(clientSocket, buffer, sizeof(buffer), 0); 46 | } 47 | else 48 | { 49 | printf("Enter sequence no. of frame where error is found:"); 50 | scanf("%s", buffer); 51 | send(clientSocket, buffer, sizeof(buffer), 0); 52 | } 53 | } 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /network-lab-programs/SlidingWindowProtocol/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/SlidingWindowProtocol/server -------------------------------------------------------------------------------- /network-lab-programs/SlidingWindowProtocol/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define SIZE 4 10 | 11 | int main() 12 | { 13 | int serverSocket, clientSocket; 14 | char buffer[1000], str[1000], temp[1000]; 15 | 16 | struct sockaddr_in serverAddr; 17 | socklen_t addrSize; 18 | 19 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 20 | serverAddr.sin_family = AF_INET; 21 | serverAddr.sin_port = htons(7891); 22 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 23 | 24 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 25 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 26 | addrSize = sizeof(serverAddr); 27 | 28 | if (listen(serverSocket, 5) == 0) 29 | { 30 | printf("listening\n"); 31 | } 32 | else 33 | { 34 | printf("error\n"); 35 | } 36 | 37 | clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize); 38 | printf("Connected\n"); 39 | 40 | printf("Enter text to send: "); 41 | scanf("%s", str); 42 | 43 | int i = 0; 44 | while (i < strlen(str)) 45 | { 46 | memset(buffer, 0, sizeof(buffer)); 47 | strncpy(buffer, str + i, SIZE); 48 | int len = strlen(buffer); 49 | for (int j = 0; j < len; j++) 50 | { 51 | sprintf(temp, "%d", i + j); 52 | strcat(buffer, temp); 53 | } 54 | printf("Frame Transmitted: %s \n", buffer); 55 | send(clientSocket, buffer, sizeof(buffer), 0); 56 | 57 | recv(clientSocket, buffer, sizeof(buffer), 0); 58 | int status = atoi(buffer); 59 | if (status == -1) 60 | { 61 | printf("Transmission is successfull \n"); 62 | i += SIZE; 63 | } 64 | else 65 | { 66 | printf("Recieved an error in %d \n", status); 67 | i = status; 68 | } 69 | } 70 | strcpy(buffer, "exit"); 71 | send(clientSocket, buffer, sizeof(buffer), 0); 72 | printf("Exiting\n"); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /network-lab-programs/TCP/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/TCP/client -------------------------------------------------------------------------------- /network-lab-programs/TCP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | clientSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | connect(clientSocket, (struct sockaddr *)&serverAddr, addrSize); 27 | printf("Connected\n"); 28 | 29 | recv(clientSocket, buffer, sizeof(buffer), 0); 30 | printf("Data Received: %s \n", buffer); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /network-lab-programs/TCP/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/TCP/server -------------------------------------------------------------------------------- /network-lab-programs/TCP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket, clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | serverSocket = socket(PF_INET, SOCK_STREAM, 0); 18 | serverAddr.sin_family = AF_INET; 19 | serverAddr.sin_port = htons(7891); 20 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | 22 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 23 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | if (listen(serverSocket, 5) == 0) 27 | { 28 | printf("listening\n"); 29 | } 30 | else 31 | { 32 | printf("error\n"); 33 | } 34 | 35 | clientSocket = accept(serverSocket, (struct sockaddr *)&serverAddr, &addrSize); 36 | printf("Connected\n"); 37 | 38 | strcpy(buffer, "Hello"); 39 | send(clientSocket, buffer, sizeof(buffer), 0); 40 | printf("Data Send: %s \n", buffer); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /network-lab-programs/UDP/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/UDP/client -------------------------------------------------------------------------------- /network-lab-programs/UDP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int clientSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | clientSocket = socket(AF_INET, SOCK_DGRAM, 0); 18 | 19 | serverAddr.sin_family = AF_INET; 20 | serverAddr.sin_port = htons(7891); 21 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 22 | 23 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | strcpy(buffer, "Hello"); 27 | sendto(clientSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, addrSize); 28 | printf("Data Send: %s \n", buffer); 29 | 30 | recvfrom(clientSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, &addrSize); 31 | printf("Data Received: %s \n", buffer); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /network-lab-programs/UDP/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/network-lab-programs/UDP/server -------------------------------------------------------------------------------- /network-lab-programs/UDP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | int serverSocket; 12 | char buffer[1000]; 13 | 14 | struct sockaddr_in serverAddr; 15 | socklen_t addrSize; 16 | 17 | serverSocket = socket(AF_INET, SOCK_DGRAM, 0); 18 | serverAddr.sin_family = AF_INET; 19 | serverAddr.sin_port = htons(7891); 20 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | 22 | memset(serverAddr.sin_zero, '\0', sizeof(serverAddr.sin_zero)); 23 | bind(serverSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); 24 | addrSize = sizeof(serverAddr); 25 | 26 | if (listen(serverSocket, 5) < 0) 27 | { 28 | printf("listening\n"); 29 | } 30 | else 31 | { 32 | printf("error\n"); 33 | } 34 | 35 | recvfrom(serverSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, &addrSize); 36 | printf("Data Received: %s \n", buffer); 37 | 38 | sleep(3); 39 | 40 | strcpy(buffer, "Bye"); 41 | sendto(serverSocket, buffer, sizeof(buffer), 0, (struct sockaddr *)&serverAddr, addrSize); 42 | printf("Data Send: %s \n", buffer); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /os-programs/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc.exe build active file", 6 | "command": "D:\\Programs\\MinGW\\mingw32\\bin\\gcc.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /os-programs/bankersAlgo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int nop, nor, alloc[20][20], avail[20], max[20][20], need[20][20], finish[20], seq[20]; 6 | 7 | printf("Enter no. of processes: "); 8 | scanf("%d", &nop); 9 | 10 | printf("Enter no. of resource types: "); 11 | scanf("%d", &nor); 12 | 13 | printf("Enter values of allocated instances for each resource:\n"); 14 | for(int i = 0; i < nop; i++) 15 | for(int j = 0; j < nor; j++) 16 | scanf("%d", &alloc[i][j]); 17 | 18 | printf("Enter values of max allocated instances for each resource:\n"); 19 | for(int i = 0; i < nop; i++) 20 | for(int j = 0; j < nor; j++) 21 | scanf("%d", &max[i][j]); 22 | 23 | printf("Enter values of available instances for each resource: "); 24 | for(int i = 0; i < nor; i++) 25 | scanf("%d", &avail[i]); 26 | 27 | for(int i = 0; i < nop; i++) 28 | for(int j = 0; j < nor; j++) 29 | need[i][j] = max[i][j] - alloc[i][j]; 30 | 31 | printf("Need matrx:\n"); 32 | for(int i = 0; i < nop; i++) { 33 | for(int j = 0; j < nor; j++) 34 | printf("%d ", need[i][j]); 35 | printf("\n"); 36 | } 37 | 38 | for(int i = 0; i < nop; i++) 39 | finish[i] = 0; 40 | 41 | int k = 0, flag = 1, count; 42 | while(flag == 1) { 43 | flag = 0; 44 | for(int i = 0; i < nop; i++) { 45 | count = 0; 46 | if( finish[i] == 0 ) { 47 | for(int j = 0; j < nor; j++) 48 | if(need[i][j] <= avail[j] ) 49 | count++; 50 | if(count == nor) { 51 | for(int j = 0; j < nor; j++) 52 | avail[j] += alloc[i][j]; 53 | finish[i] = 1; 54 | flag = 1; 55 | seq[k++] = i + 1; 56 | } 57 | } 58 | } 59 | } 60 | 61 | 62 | for(int i = 0; i < nop; i++) 63 | if(finish[i] != 1) { 64 | printf("\nSystem not in safe state"); 65 | exit(1); 66 | } 67 | 68 | printf("Sequence : "); 69 | for(int i = 0; i < nop; i++) 70 | printf("P%d->", seq[i]); 71 | 72 | } 73 | -------------------------------------------------------------------------------- /os-programs/cpu-scheduling-algos/fcfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int nop, p[10], bt[10], tat[10], wt[10]; 4 | float avgtat = 0.0, avgwt = 0.0; 5 | printf("Enter the no. of processes: "); 6 | scanf("%d", &nop); 7 | printf("Enter Burst time of each process: "); 8 | for (int i = 0; i < nop; i++) { 9 | scanf("%d", &bt[i]); 10 | p[i] = i + 1; 11 | if (i == 0) 12 | tat[i] = bt[i]; 13 | else 14 | tat[i] = tat[i - 1] + bt[i]; 15 | wt[i] = tat[i] - bt[i]; 16 | avgtat += tat[i]; 17 | avgwt += wt[i]; 18 | } 19 | avgtat = (float)(avgtat / nop); 20 | avgwt = (float)(avgwt / nop); 21 | printf("Process\tBT\tTAT\tWT\n"); 22 | for (int i = 0; i < nop; i++) 23 | printf("%d\t%d\t%d\t%d\n", p[i], bt[i], tat[i], wt[i]); 24 | printf("\nAvg TAT: %f", avgtat); 25 | printf("\nAvg WT: %f\n", avgwt); 26 | } 27 | -------------------------------------------------------------------------------- /os-programs/cpu-scheduling-algos/priority.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *a, int *b) { 3 | int temp = *a; 4 | *a = *b; 5 | *b = temp; 6 | } 7 | int main() { 8 | int nop, p[10], bt[10], tat[10], wt[10], priority[10]; 9 | float avgtat = 0.0, avgwt = 0.0; 10 | printf("Enter the no. of processes: "); 11 | scanf("%d", &nop); 12 | printf("Enter Priority and Burst time of each process:\n"); 13 | for (int i = 0; i < nop; i++) { 14 | scanf("%d", &priority[i]); 15 | scanf("%d", &bt[i]); 16 | p[i] = i + 1; 17 | } 18 | for (int i = 0; i < nop; i++) 19 | for (int j = 0; j < nop - i - 1; j++) 20 | if (priority[j] > priority[j + 1]) { 21 | swap(&priority[j], &priority[j + 1]); 22 | swap(&bt[j], &bt[j + 1]); 23 | swap(&p[j], &p[j + 1]); 24 | } 25 | for (int i = 0; i < nop; i++) { 26 | if (i == 0) 27 | tat[i] = bt[i]; 28 | else 29 | tat[i] = tat[i - 1] + bt[i]; 30 | wt[i] = tat[i] - bt[i]; 31 | avgtat += tat[i]; 32 | avgwt += wt[i]; 33 | } 34 | avgtat = (float)(avgtat / nop); 35 | avgwt = (float)(avgwt / nop); 36 | printf("Process\tPriority BT\tTAT\tWT\n"); 37 | for (int i = 0; i < nop; i++) 38 | printf("%d\t%d\t %d\t%d\t%d\n", p[i], priority[i], bt[i], tat[i], wt[i]); 39 | printf("\nAvg TAT: %f", avgtat); 40 | printf("\nAvg WT: %f\n", avgwt); 41 | } 42 | -------------------------------------------------------------------------------- /os-programs/cpu-scheduling-algos/roundRobin.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int nop, time = 0, p[10], quant, wt[10], tat[10], bt[10], rbt[10]; 4 | float avgwt = 0.0, avgtat = 0.0; 5 | printf("Enter no. of processes: "); 6 | scanf("%d", &nop); 7 | int rnop = nop; 8 | printf("Enter the Time Quantum: "); 9 | scanf("%d", &quant); 10 | printf("Enter Burst Time of each process: "); 11 | for (int i = 0; i < nop; i++) { 12 | scanf("%d", &bt[i]); 13 | p[i] = i + 1; 14 | rbt[i] = bt[i]; 15 | } 16 | for (int i = 0; rnop != 0; i = (i + 1) % nop) 17 | if (rbt[i] <= quant && rbt[i] > 0) { 18 | time += rbt[i]; 19 | rbt[i] = 0; 20 | rnop--; 21 | tat[i] = time; 22 | wt[i] = tat[i] - bt[i]; 23 | avgtat += tat[i]; 24 | avgwt += wt[i]; 25 | } 26 | else if (rbt[i] > 0) { 27 | rbt[i] -= quant; 28 | time += quant; 29 | } 30 | avgtat = (float)(avgtat / nop); 31 | avgwt = (float)(avgwt / nop); 32 | printf("Process\tBT\tTAT\tWT\n"); 33 | for (int i = 0; i < nop; i++) 34 | printf("%d\t%d\t%d\t%d\n", p[i], bt[i], tat[i], wt[i]); 35 | printf("\nAvg TAT: %f", avgtat); 36 | printf("\nAvg WT: %f\n", avgwt); 37 | } -------------------------------------------------------------------------------- /os-programs/cpu-scheduling-algos/sjf.c: -------------------------------------------------------------------------------- 1 | #include 2 | void swap(int *a, int *b) { 3 | int temp = *a; 4 | *a = *b; 5 | *b = temp; 6 | } 7 | int main() { 8 | int nop, p[10], bt[10], tat[10], wt[10]; 9 | float avgtat = 0.0, avgwt = 0.0; 10 | printf("Enter the no. of processes: "); 11 | scanf("%d", &nop); 12 | printf("Enter Burst time of each process: "); 13 | for (int i = 0; i < nop; i++) { 14 | scanf("%d", &bt[i]); 15 | p[i] = i + 1; 16 | } 17 | for (int i = 0; i < nop; i++) 18 | for (int j = 0; j < nop - i - 1; j++) 19 | if (bt[j] > bt[j + 1]) { 20 | swap(&bt[j], &bt[j + 1]); 21 | swap(&p[j], &p[j + 1]); 22 | } 23 | for (int i = 0; i < nop; i++) { 24 | if (i == 0) 25 | tat[i] = bt[i]; 26 | else 27 | tat[i] = tat[i - 1] + bt[i]; 28 | wt[i] = tat[i] - bt[i]; 29 | avgtat += tat[i]; 30 | avgwt += wt[i]; 31 | } 32 | avgtat = (float)(avgtat / nop); 33 | avgwt = (float)(avgwt / nop); 34 | printf("Process\tBT\tTAT\tWT\n"); 35 | for (int i = 0; i < nop; i++) 36 | printf("%d\t%d\t%d\t%d\n", p[i], bt[i], tat[i], wt[i]); 37 | printf("\nAvg TAT: %f", avgtat); 38 | printf("\nAvg WT: %f\n", avgwt); 39 | } 40 | -------------------------------------------------------------------------------- /os-programs/disk-scheduling-algos/cscan_disk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int reqSeq[100], noOfReq, totHeadMoment = 0, headPos, maxRange; 6 | int leftSeq[50], rightSeq[50], leftSize = 0, rightSize = 0; 7 | printf("Enter initial head position: "); 8 | scanf("%d", &headPos); 9 | printf("Enter the max range of disk: "); 10 | scanf("%d", &maxRange); 11 | printf("Enter the number of Requests: "); 12 | scanf("%d", &noOfReq); 13 | printf("Enter the Requests sequence: "); 14 | for (int i = 0; i < noOfReq; i++) { 15 | int temp; 16 | scanf("%d", &temp); 17 | if(temp > headPos) 18 | rightSeq[rightSize++] = temp; 19 | else 20 | leftSeq[leftSize++] = temp; 21 | } 22 | 23 | for(int i = 0 ; i < rightSize - 1; i++) 24 | for(int j = i + 1; j < rightSize; j++) 25 | if(rightSeq[i] > rightSeq[j]) { 26 | int temp = rightSeq[i]; 27 | rightSeq[i] = rightSeq[j]; 28 | rightSeq[j] = temp; 29 | } 30 | 31 | for(int i = 0 ; i < leftSize - 1; i++) 32 | for(int j = i + 1; j < leftSize; j++) 33 | if(leftSeq[i] > leftSeq[j]) { 34 | int temp = leftSeq[i]; 35 | leftSeq[i] = leftSeq[j]; 36 | leftSeq[j] = temp; 37 | } 38 | 39 | for (int i = 0; i < rightSize; i++) 40 | reqSeq[i] = rightSeq[i]; 41 | 42 | reqSeq[rightSize] = maxRange; 43 | reqSeq[rightSize + 1] = 0; 44 | 45 | for (int i = rightSize + 2, j = 0; j < leftSize; i++, j++) 46 | reqSeq[i] = leftSeq[j]; 47 | 48 | for (int i = 0; i < noOfReq + 2; i++) { 49 | totHeadMoment = totHeadMoment + abs(reqSeq[i] - headPos); 50 | headPos = reqSeq[i]; 51 | } 52 | 53 | printf("Seek sequence: "); 54 | for (int i = 0; i < noOfReq + 2; i++) 55 | printf("%d ", reqSeq[i]); 56 | printf("\nTotal head moment is %d", totHeadMoment); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /os-programs/disk-scheduling-algos/fcfs_disk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int reqSeq[100], noOfReq, totHeadMoment = 0, headPos; 6 | printf("Enter the number of Requests: "); 7 | scanf("%d", &noOfReq); 8 | printf("Enter the Requests sequence: "); 9 | for (int i = 0; i < noOfReq; i++) 10 | scanf("%d", &reqSeq[i]); 11 | printf("Enter initial head position: "); 12 | scanf("%d", &headPos); 13 | 14 | for (int i = 0; i < noOfReq; i++) 15 | { 16 | totHeadMoment = totHeadMoment + abs(reqSeq[i] - headPos); 17 | headPos = reqSeq[i]; 18 | } 19 | 20 | printf("Total head moment is %d", totHeadMoment); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /os-programs/disk-scheduling-algos/scan_disk.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int reqSeq[100], noOfReq, totHeadMoment = 0, headPos; 6 | int leftSeq[50], rightSeq[50], leftSize = 0, rightSize = 0; 7 | printf("Enter initial head position: "); 8 | scanf("%d", &headPos); 9 | printf("Enter the number of Requests: "); 10 | scanf("%d", &noOfReq); 11 | printf("Enter the Requests sequence: "); 12 | for (int i = 0; i < noOfReq; i++) { 13 | int temp; 14 | scanf("%d", &temp); 15 | if(temp > headPos) 16 | rightSeq[rightSize++] = temp; 17 | else 18 | leftSeq[leftSize++] = temp; 19 | } 20 | 21 | for(int i = 0 ; i < rightSize - 1; i++) 22 | for(int j = i + 1; j < rightSize; j++) 23 | if(rightSeq[i] > rightSeq[j]) { 24 | int temp = rightSeq[i]; 25 | rightSeq[i] = rightSeq[j]; 26 | rightSeq[j] = temp; 27 | } 28 | 29 | for(int i = 0 ; i < leftSize - 1; i++) 30 | for(int j = i + 1; j < leftSize; j++) 31 | if(leftSeq[i] < leftSeq[j]) { 32 | int temp = leftSeq[i]; 33 | leftSeq[i] = leftSeq[j]; 34 | leftSeq[j] = temp; 35 | } 36 | 37 | for (int i = 0; i < leftSize; i++) 38 | reqSeq[i] = leftSeq[i]; 39 | 40 | reqSeq[leftSize] = 0; 41 | 42 | for (int i = leftSize + 1, j = 0; j < rightSize; i++, j++) 43 | reqSeq[i] = rightSeq[j]; 44 | 45 | for (int i = 0; i < noOfReq + 1; i++) { 46 | totHeadMoment = totHeadMoment + abs(reqSeq[i] - headPos); 47 | headPos = reqSeq[i]; 48 | } 49 | 50 | printf("Seek sequence: "); 51 | for (int i = 0; i < noOfReq + 1; i++) 52 | printf("%d ", reqSeq[i]); 53 | printf("\nTotal head moment is %d", totHeadMoment); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /os-programs/interprocess-comm-pgm/reader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | key_t key = 15678; 7 | int shmid = shmget(key,1024,0666|IPC_CREAT); 8 | char *str = (char*) shmat(shmid,0,0); 9 | 10 | printf("Data read from memory: %s\n",str); 11 | 12 | shmdt(str); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /os-programs/interprocess-comm-pgm/writer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | key_t key = 15678; 7 | int shmid = shmget(key,1024,0666|IPC_CREAT); 8 | char *str = (char*) shmat(shmid,0,0); 9 | 10 | printf("Write Data : "); 11 | fgets(str, 1024, stdin); 12 | 13 | printf("Data written in memory: %s\n",str); 14 | 15 | shmdt(str); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /os-programs/mem-allocation-algos/bestFit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int blockSize[20],processSize[20],nob, nop, allocation[20]; 5 | printf("Enter no. of processes: "); 6 | scanf("%d", &nop); 7 | 8 | printf("Enter no. of blocks: "); 9 | scanf("%d", &nob); 10 | 11 | printf("Enter process sizes: "); 12 | for(int i = 0; i < nop; i++) 13 | scanf("%d", &processSize[i]); 14 | 15 | printf("Enter block sizes: "); 16 | for(int i = 0; i < nob; i++) 17 | scanf("%d", &blockSize[i]); 18 | 19 | for (int i=0; i= processSize[i]) { 26 | if (bestIdx == -1) 27 | bestIdx = j; 28 | else if (blockSize[bestIdx] > blockSize[j]) 29 | bestIdx = j; 30 | } 31 | } 32 | 33 | if (bestIdx != -1) { 34 | allocation[i] = bestIdx; 35 | blockSize[bestIdx] -= processSize[i]; 36 | } 37 | } 38 | 39 | printf("Process No.\tProcess Size\tBlock no.\n"); 40 | for (int i = 0; i < nop; i++) { 41 | printf(" %d \t\t %d \t\t",i+1,processSize[i]); 42 | if (allocation[i] != -1) 43 | printf("%d",allocation[i] + 1); 44 | else 45 | printf("Not Allocated"); 46 | printf("\n"); 47 | } 48 | 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /os-programs/mem-allocation-algos/firstFit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int blockSize[20],processSize[20],nob, nop, allocation[20]; 5 | printf("Enter no. of processes: "); 6 | scanf("%d", &nop); 7 | 8 | printf("Enter no. of blocks: "); 9 | scanf("%d", &nob); 10 | 11 | printf("Enter process sizes: "); 12 | for(int i = 0; i < nop; i++) 13 | scanf("%d", &processSize[i]); 14 | 15 | printf("Enter block sizes: "); 16 | for(int i = 0; i < nob; i++) 17 | scanf("%d", &blockSize[i]); 18 | 19 | for (int i=0; i= processSize[i]) { 28 | bestIdx = j; 29 | break; 30 | } 31 | } 32 | 33 | if (bestIdx != -1) { 34 | allocation[i] = bestIdx; 35 | blockSize[bestIdx] -= processSize[i]; 36 | } 37 | } 38 | 39 | printf("Process No.\tProcess Size\tBlock no.\n"); 40 | for (int i = 0; i < nop; i++) { 41 | printf(" %d \t\t %d \t\t",i+1,processSize[i]); 42 | if (allocation[i] != -1) 43 | printf("%d",allocation[i] + 1); 44 | else 45 | printf("Not Allocated"); 46 | printf("\n"); 47 | } 48 | 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /os-programs/mem-allocation-algos/worstFit.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int blockSize[20],processSize[20],nob, nop, allocation[20]; 5 | printf("Enter no. of processes: "); 6 | scanf("%d", &nop); 7 | 8 | printf("Enter no. of blocks: "); 9 | scanf("%d", &nob); 10 | 11 | printf("Enter process sizes: "); 12 | for(int i = 0; i < nop; i++) 13 | scanf("%d", &processSize[i]); 14 | 15 | printf("Enter block sizes: "); 16 | for(int i = 0; i < nob; i++) 17 | scanf("%d", &blockSize[i]); 18 | 19 | for (int i=0; i= processSize[i]) { 25 | if (bestIdx == -1) 26 | bestIdx = j; 27 | else if (blockSize[bestIdx] < blockSize[j]) 28 | bestIdx = j; 29 | } 30 | } 31 | 32 | if (bestIdx != -1) { 33 | allocation[i] = bestIdx; 34 | blockSize[bestIdx] -= processSize[i]; 35 | } 36 | } 37 | 38 | printf("Process No.\tProcess Size\tBlock no.\n"); 39 | for (int i = 0; i < nop; i++) { 40 | printf(" %d \t\t %d \t\t",i+1,processSize[i]); 41 | if (allocation[i] != -1) 42 | printf("%d",allocation[i] + 1); 43 | else 44 | printf("Not Allocated"); 45 | printf("\n"); 46 | } 47 | 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /os-programs/page-replacement-algos/fifo.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int noOfPages, pages[50], noOfFrames, frames[50], pageFaults = 0, frameIndex = 0; 5 | printf("Enter the number of pages: "); 6 | scanf("%d", &noOfPages); 7 | printf("Enter the page number: "); 8 | for (int i = 0; i < noOfPages; i++) 9 | scanf("%d", &pages[i]); 10 | printf("Enter the number of frames: "); 11 | scanf("%d", &noOfFrames); 12 | for (int i = 0; i < noOfFrames; i++) 13 | frames[i] = -1; 14 | 15 | printf("ref string\t page frames\n"); 16 | for (int i = 0; i < noOfPages; i++) 17 | { 18 | printf("%d\t\t", pages[i]); 19 | int flag = 0; 20 | for (int j = 0; j < noOfFrames; j++) 21 | if (frames[j] == pages[i]) 22 | { 23 | flag = 1; 24 | printf("No replacement"); 25 | } 26 | if (flag == 0) 27 | { 28 | frames[frameIndex] = pages[i]; 29 | frameIndex = (frameIndex + 1) % noOfFrames; 30 | pageFaults++; 31 | for (int j = 0; j < noOfFrames; j++) 32 | printf("%d\t", frames[j]); 33 | } 34 | printf("\n"); 35 | } 36 | printf("Page fault is %d", pageFaults); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /os-programs/page-replacement-algos/lfu.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int np, pages[15], nf, frames[10], pagesFaults = 0, freq[10][2] = {}, nfreq = 0, pageIndx[10] = {}; 6 | printf("Enter the number of pages: "); 7 | scanf("%d", &np); 8 | printf("Enter the pages reference numbers: "); 9 | for (int i = 0; i < np; i++) 10 | { 11 | scanf("%d", &pages[i]); 12 | if (nfreq == 0) 13 | freq[nfreq++][0] = pages[i]; 14 | else 15 | { 16 | int flag = 0; 17 | for (int j = 0; j < nfreq; j++) 18 | if (pages[i] == freq[j][0]) 19 | { 20 | flag = 1; 21 | break; 22 | } 23 | if (flag == 0) 24 | freq[nfreq++][0] = pages[i]; 25 | } 26 | } 27 | 28 | printf("Enter the number of frames: "); 29 | scanf("%d", &nf); 30 | for (int i = 0; i < nf; i++) 31 | frames[i] = -1; 32 | 33 | printf("ref string\t pages frames\n"); 34 | for (int i = 0; i < np; i++) 35 | { 36 | printf("%d\t\t", pages[i]); 37 | int flag1 = 0; 38 | 39 | // pages Hit 40 | for (int j = 0; j < nf; j++) 41 | if (frames[j] == pages[i]) 42 | { 43 | flag1 = 1; 44 | for (int k = 0; k < nfreq; k++) 45 | if (freq[k][0] == pages[i]) 46 | freq[k][1]++; 47 | printf("No replacement"); 48 | break; 49 | } 50 | if (flag1 == 0) 51 | { 52 | pagesFaults++; 53 | // pages Fault with Free frames 54 | int flag2 = 0; 55 | for (int j = 0; j < nf; j++) 56 | if (frames[j] == -1) 57 | { 58 | frames[j] = pages[i]; 59 | flag2 = 1; 60 | for (int k = 0; k < nfreq; k++) 61 | if (freq[k][0] == pages[i]) 62 | freq[k][1]++; 63 | 64 | pageIndx[j] = i; 65 | break; 66 | } 67 | // pages Fault But No Free frames 68 | if (flag2 == 0) 69 | { 70 | int minfreq, frqIndx, fpos, minIndx; 71 | // Finding the frame index to be changed with page[i] 72 | for (int j = 0; j < nf; j++) 73 | { 74 | for (int k = 0; k < nfreq; k++) 75 | { 76 | if (frames[j] == freq[k][0]) 77 | { 78 | if ((j == 0) || (freq[k][1] < minfreq) || ((freq[k][1] == minfreq) && (pageIndx[j] < minIndx))) 79 | { 80 | minfreq = freq[k][1]; 81 | minIndx = pageIndx[j]; 82 | fpos = j; 83 | frqIndx = k; 84 | } 85 | break; 86 | } 87 | } 88 | } 89 | 90 | frames[fpos] = pages[i]; 91 | for (int j = 0; j < nfreq; j++) 92 | if (freq[j][0] == pages[i]) 93 | freq[j][1]++; 94 | freq[frqIndx][1] = 0; 95 | pageIndx[fpos] = i; 96 | } 97 | for (int j = 0; j < nf; j++) 98 | printf("%d\t", frames[j]); 99 | } 100 | printf("\n"); 101 | } 102 | printf("pages fault is %d", pagesFaults); 103 | 104 | return 0; 105 | } -------------------------------------------------------------------------------- /os-programs/page-replacement-algos/lru.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int noOfPages, pages[50], noOfFrames, frames[50], pageFaults = 0, count = 0, time[50]; 6 | printf("Enter the number of pages: "); 7 | scanf("%d", &noOfPages); 8 | printf("Enter the page number: "); 9 | for (int i = 0; i < noOfPages; i++) 10 | scanf("%d", &pages[i]); 11 | printf("Enter the number of frames: "); 12 | scanf("%d", &noOfFrames); 13 | for (int i = 0; i < noOfFrames; i++) 14 | frames[i] = -1; 15 | 16 | printf("ref string\t page frames\n"); 17 | for (int i = 0; i < noOfPages; i++) 18 | { 19 | printf("%d\t\t", pages[i]); 20 | 21 | int flag1 = 0; 22 | // Page Hit 23 | for (int j = 0; j < noOfFrames; j++) 24 | if (frames[j] == pages[i]) 25 | { 26 | flag1 = 1; 27 | count++; 28 | time[j] = count; 29 | printf("No Replacements"); 30 | break; 31 | } 32 | if (flag1 == 0) 33 | { 34 | pageFaults++; 35 | // Page Fault with Free Frames 36 | int flag2 = 0; 37 | for (int j = 0; j < noOfFrames; j++) 38 | if (frames[j] == -1) 39 | { 40 | frames[j] = pages[i]; 41 | flag2 = 1; 42 | count++; 43 | time[j] = count; 44 | break; 45 | } 46 | // Page Fault But No Free Frames 47 | if (flag2 == 0) 48 | { 49 | int min = time[0], pos = 0; 50 | for (int i = 1; i < noOfFrames; i++) 51 | if (time[i] < min) 52 | { 53 | min = time[i]; 54 | pos = i; 55 | } 56 | frames[pos] = pages[i]; 57 | count++; 58 | time[pos] = count; 59 | } 60 | for (int j = 0; j < noOfFrames; j++) 61 | printf("%d\t", frames[j]); 62 | } 63 | printf("\n"); 64 | } 65 | printf("Page fault is %d", pageFaults); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /os-programs/semaphorePC.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX_SIZE 3 5 | 6 | int mutex = 1, full = 0, empty = MAX_SIZE, buff[50], item = 1, front = -1, rear = -1; 7 | 8 | int wait(int s) { 9 | while( s <= 0); 10 | return --s; 11 | } 12 | 13 | int signal(int s) { 14 | return ++s; 15 | } 16 | 17 | void producer() { 18 | mutex = wait(mutex); 19 | empty = wait(empty); 20 | 21 | if(front == -1 && rear == -1) { 22 | front = 0; 23 | buff[++rear] = item; 24 | } else { 25 | rear = (rear + 1) % MAX_SIZE; 26 | buff[rear] = item; 27 | } 28 | printf("%d added to Buffer", buff[rear]); 29 | item++; 30 | 31 | full = signal(full); 32 | mutex = signal(mutex); 33 | } 34 | 35 | void consumer() { 36 | mutex = wait(mutex); 37 | full = wait(full); 38 | 39 | printf("%d removed to Buffer", buff[front]); 40 | if (front == rear) { 41 | front = -1; 42 | rear = -1; 43 | } else { 44 | front = (front + 1) % MAX_SIZE; 45 | } 46 | 47 | empty = signal(empty); 48 | mutex = signal(mutex); 49 | } 50 | 51 | int main() { 52 | while(1) { 53 | printf("\n1.Producer 2. Consumer 3.Exit"); 54 | printf("\nYour choice: "); 55 | int choice; 56 | scanf("%d", &choice); 57 | 58 | switch(choice) { 59 | case 1: 60 | if(mutex == 1 && empty != 0) 61 | producer(); 62 | else printf("Buffer Full"); 63 | break; 64 | case 2: 65 | if(mutex == 1 && full != 0) 66 | consumer(); 67 | else printf("Buffer Empty"); 68 | break; 69 | case 3: 70 | exit(0); 71 | 72 | } 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /os-programs/shell-prgms/Armstrong.sh: -------------------------------------------------------------------------------- 1 | echo "Enter a number" 2 | read num 3 | temp=$num 4 | sum=0 5 | 6 | while [ $temp != 0 ] 7 | do 8 | digit=$(( temp%10 )) 9 | sum=$(( sum + ( digit*digit*digit ))) 10 | temp=$(( temp/10 )) 11 | done 12 | 13 | if [ $sum == $num ] 14 | then echo "Armstrong" 15 | else echo "Not Armstrong" 16 | fi 17 | -------------------------------------------------------------------------------- /os-programs/shell-prgms/Calculator.sh: -------------------------------------------------------------------------------- 1 | status=1 2 | 3 | while [ $status == 1 ] 4 | do 5 | echo "1.Add 2.Subtract 3.Multiply 4.Divide 5.Exit" 6 | echo "Enter option" 7 | read option 8 | 9 | case $option in 10 | 1) echo "Enter 2 no." 11 | read num1 12 | read num2 13 | result=$(( num1 + num2 )) 14 | echo "Result is $result";; 15 | 2) echo "Enter 2 no." 16 | read num1 17 | read num2 18 | result=$(( num1 - num2 )) 19 | echo "Result is $result";; 20 | 3) echo "Enter 2 no." 21 | read num1 22 | read num2 23 | result=$(( num1 * num2 )) 24 | echo "Result is $result";; 25 | 4) echo "Enter 2 no." 26 | read num1 27 | read num2 28 | result=$(( num1 / num2 )) 29 | echo "Result is $result";; 30 | 5) status=0;; 31 | *) echo "Enter appropriate option" 32 | esac 33 | 34 | done 35 | -------------------------------------------------------------------------------- /os-programs/shell-prgms/largest.sh: -------------------------------------------------------------------------------- 1 | echo "Enter a number: " 2 | read num1 3 | echo "Enter a number: " 4 | read num2 5 | echo "Enter a number: " 6 | read num3 7 | if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ] 8 | then echo "$num1" 9 | elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ] 10 | then echo "$num2" 11 | else echo "$num3" 12 | fi 13 | -------------------------------------------------------------------------------- /os-programs/system-calls/creat.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | int fid = creat("file.dat", S_IREAD/S_IWRITE); 10 | 11 | if(fid == -1) 12 | printf("File not found"); 13 | printf("File read\n"); 14 | close(fid); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /os-programs/system-calls/dir.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() { 6 | struct dirent *dptr; 7 | DIR *dir; 8 | char buff[50]; 9 | 10 | printf("Enter directory: "); 11 | scanf("%s", buff); 12 | 13 | dir = opendir(buff); 14 | while(dptr = readdir(dir)) 15 | printf("%s\n", dptr->d_name); 16 | closedir(dir); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /os-programs/system-calls/execv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | int pid, status; 10 | char *args[] = {"ls",NULL}; 11 | 12 | pid = fork(); 13 | 14 | if (pid == 0) { 15 | printf("Child Process\n"); 16 | execv("/bin/ls",args); 17 | } else { 18 | printf("Parent Process\n"); 19 | wait(&status); 20 | printf("Parent Process Exiting\n"); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /os-programs/system-calls/pid.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | for(int i = 0; i < 2; i++) { 7 | printf("Hello %d \n", fork()); 8 | } 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /os-programs/system-calls/stats.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | char path[10]; 8 | struct stat *nfile; 9 | 10 | nfile = (struct stat *) malloc(sizeof(struct stat)); 11 | printf("Enter file name: "); 12 | scanf("%s", path); 13 | 14 | stat(path, nfile); 15 | 16 | printf("User Id: %d \n", nfile->st_uid); 17 | printf("Group Id: %d \n", nfile->st_gid); 18 | printf("Size: %ld \n", nfile->st_size); 19 | printf("Block Size: %ld \n", nfile->st_blksize); 20 | printf("Mode: %d \n", nfile->st_mode); 21 | printf("No. of links: %ld \n", nfile->st_nlink); 22 | printf("Last access time: %ld \n", nfile->st_atime); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /os-programs/system-calls/wait.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int pid, status; 9 | 10 | pid = fork(); 11 | 12 | if (pid == 0) { 13 | printf("Child Process\n"); 14 | exit(0); 15 | } else { 16 | printf("Parent Process\n"); 17 | wait(&status); 18 | printf("Parent Process Exiting\n"); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: gcc.exe build active file", 6 | "command": "D:\\Programs\\MinGW\\mingw32\\bin\\gcc.exe", 7 | "args": [ 8 | "-fdiagnostics-color=always", 9 | "-g", 10 | "${file}", 11 | "-o", 12 | "${fileDirname}\\${fileBasenameNoExtension}.exe" 13 | ], 14 | "options": { 15 | "cwd": "${fileDirname}" 16 | }, 17 | "problemMatcher": [ 18 | "$gcc" 19 | ], 20 | "group": { 21 | "kind": "build", 22 | "isDefault": true 23 | }, 24 | "detail": "Task generated by Debugger." 25 | } 26 | ], 27 | "version": "2.0.0" 28 | } -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/input.txt: -------------------------------------------------------------------------------- 1 | - START 1000 2 | - LDA ALPHA 3 | - ADD ONE 4 | - SUB TWO 5 | - STA BETA 6 | ALPHA BYTE C'KLNCE' 7 | ONE RESB 2 8 | TWO WORD 5 9 | BETA RESW 1 10 | - END - 11 | -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/intermediate.txt: -------------------------------------------------------------------------------- 1 | - START 1000 2 | 1000 - LDA ALPHA 3 | 1003 - ADD ONE 4 | 1006 - SUB TWO 5 | 1009 - STA BETA 6 | 1012 ALPHA BYTE C'KLNCE' 7 | 1017 ONE RESB 2 8 | 1019 TWO WORD 5 9 | 1022 BETA RESW 1 10 | 1025 - END - 11 | -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/optab.txt: -------------------------------------------------------------------------------- 1 | LDA 03 2 | STA 0F 3 | ADD 01 4 | SUB 05 5 | LDCH 53 6 | STCH 57 7 | -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/output.txt: -------------------------------------------------------------------------------- 1 | - START 1000 2 | - LDA ALPHA 1000 031012 3 | - ADD ONE 1003 011017 4 | - SUB TWO 1006 051019 5 | - STA BETA 1009 0F1022 6 | ALPHA BYTE 1012 4b4c4e4345 7 | ONE RESB 2 1017 8 | TWO WORD 5 1019 5 9 | BETA RESW 1 1022 10 | - END - 1025 11 | -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/pass1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | FILE *f1, *f2, *f3, *f4; 8 | int locctr = 0, startaddr = 0; 9 | char label[10], mnemonic[10], operand[10], opcode[10], machlan[10]; 10 | 11 | f1 = fopen("input.txt", "r"); 12 | f2 = fopen("symtab.txt", "w"); 13 | f3 = fopen("intermediate.txt", "w"); 14 | 15 | fscanf(f1, "%s%s%s", label, mnemonic, operand); 16 | 17 | if (strcmp(mnemonic, "START") == 0) 18 | { 19 | startaddr = atoi(operand); 20 | locctr = startaddr; 21 | fprintf(f3, "\t\t%s\t%s\t%s\n", label, mnemonic, operand); 22 | fscanf(f1, "%s%s%s", label, mnemonic, operand); 23 | } 24 | 25 | while (!feof(f1)) 26 | { 27 | fprintf(f3, "%d\t%s\t%s\t%s\n", locctr, label, mnemonic, operand); 28 | 29 | if (strcmp(label, "-") != 0) 30 | fprintf(f2, "%s\t%d\n", label, locctr); 31 | 32 | f4 = fopen("optab.txt", "r"); 33 | fscanf(f4, "%s%s", opcode, machlan); 34 | 35 | while (!feof(f4)) 36 | { 37 | if (strcmp(mnemonic, opcode) == 0) 38 | { 39 | locctr += 3; 40 | break; 41 | } 42 | fscanf(f4, "%s%s", opcode, machlan); 43 | } 44 | fclose(f4); 45 | 46 | if (strcmp(mnemonic, "RESW") == 0) 47 | locctr += 3 * atoi(operand); 48 | else if (strcmp(mnemonic, "RESB") == 0) 49 | locctr += atoi(operand); 50 | else if (strcmp(mnemonic, "WORD") == 0) 51 | locctr += 3; 52 | else if (strcmp(mnemonic, "BYTE") == 0) 53 | { 54 | if (operand[0] == 'X') 55 | locctr += 1; 56 | else 57 | locctr += strlen(operand) - 3; 58 | } 59 | 60 | fscanf(f1, "%s%s%s", label, mnemonic, operand); 61 | } 62 | 63 | if (strcmp(mnemonic, "END") == 0) 64 | printf("Program Length: %d", locctr - startaddr); 65 | 66 | fclose(f1); 67 | fclose(f2); 68 | fclose(f3); 69 | 70 | return 0; 71 | } -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/pass2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | FILE *f1, *f2, *f3, *f4; 8 | int locctr, symloc, flag1, flag2; 9 | char label[10], mnemonic[10], operand[10], objcode[10], opcode[10], machlan[10], symname[10], str[10]; 10 | 11 | f1 = fopen("intermediate.txt", "r"); 12 | f2 = fopen("symtab.txt", "r"); 13 | f3 = fopen("optab.txt", "r"); 14 | f4 = fopen("output.txt", "w"); 15 | 16 | fscanf(f1, "%s%s%s", label, mnemonic, operand); 17 | 18 | if (strcmp(mnemonic, "START") == 0) 19 | { 20 | fprintf(f4, "%s\t%s\t%s\n", label, mnemonic, operand); 21 | fscanf(f1, "%d%s%s%s", &locctr, label, mnemonic, operand); 22 | } 23 | 24 | while (strcmp(mnemonic, "END") != 0) 25 | { 26 | rewind(f3); 27 | flag1 = 0; 28 | 29 | fscanf(f3, "%s%s", opcode, machlan); 30 | while (!feof(f3)) 31 | { 32 | if (strcmp(mnemonic, opcode) == 0) 33 | { 34 | flag1 = 1; 35 | break; 36 | } 37 | fscanf(f3, "%s%s", opcode, machlan); 38 | } 39 | 40 | if (flag1 == 1) 41 | { 42 | flag2 = 0; 43 | rewind(f2); 44 | fscanf(f2, "%s%d", symname, &symloc); 45 | while (!feof(f2)) 46 | { 47 | 48 | if (strcmp(symname, operand) == 0) 49 | { 50 | flag2 = 1; 51 | break; 52 | } 53 | fscanf(f2, "%s%d", symname, &symloc); 54 | } 55 | 56 | if (flag2 == 1) 57 | { 58 | sprintf(str, "%d", symloc); 59 | strcpy(objcode, strcat(machlan, str)); 60 | } 61 | } 62 | else if (strcmp(mnemonic, "WORD") == 0) 63 | strcpy(objcode, operand); 64 | else if (strcmp(mnemonic, "BYTE") == 0) 65 | { 66 | strcpy(str, "\0"); 67 | char str2[10]; 68 | for (int i = 2; i < strlen(operand) - 1; i++) 69 | { 70 | sprintf(str2, "%x", operand[i]); 71 | strcpy(str, strcat(str, str2)); 72 | } 73 | strcpy(objcode, str); 74 | } 75 | else 76 | strcpy(objcode, "\0"); 77 | 78 | fprintf(f4, "%s\t%s\t%s\t%d\t%s\n", label, mnemonic, operand, locctr, objcode); 79 | fscanf(f1, "%d%s%s%s", &locctr, label, mnemonic, operand); 80 | } 81 | fprintf(f4, "%s\t%s\t%s\t%d\n", label, mnemonic, operand, locctr); 82 | 83 | fclose(f1); 84 | fclose(f2); 85 | fclose(f3); 86 | 87 | return 0; 88 | } -------------------------------------------------------------------------------- /os-programs/two-pass-assembler/symtab.txt: -------------------------------------------------------------------------------- 1 | ALPHA 1012 2 | ONE 1017 3 | TWO 1019 4 | BETA 1022 5 | -------------------------------------------------------------------------------- /polynomial_arithmetics/polyAddUsingLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int coeff, expo; 7 | struct node *next; 8 | }; 9 | struct node *Phead, *Qhead, *Rhead; 10 | 11 | struct node *ReadPoly() 12 | { 13 | struct node *new, *ptr, *head = NULL; 14 | int n, i; 15 | printf("enter total number of terms in polynomial:"); 16 | scanf("%d", &n); 17 | printf("enter coefficients and exponents of terms in polynomial:"); 18 | for (i = 1; i <= n; i++) 19 | { 20 | printf("enter (coeff%d,expo%d)", i, i); 21 | new = (struct node *)malloc(sizeof(struct node)); 22 | scanf("%d", &new->coeff); 23 | scanf("%d", &new->expo); 24 | new->next = NULL; 25 | if (head == NULL) 26 | { 27 | head = new; 28 | ptr = head; 29 | } 30 | else 31 | { 32 | ptr->next = new; 33 | ptr = new; 34 | } 35 | } 36 | return (head); 37 | } 38 | 39 | void DisplayPoly(struct node *head) 40 | { 41 | struct node *ptr; 42 | if (head == NULL) 43 | printf("polynomial is Empty"); 44 | else 45 | { 46 | ptr = head; 47 | while (ptr->next != NULL) 48 | { 49 | printf("%dx^%d+", ptr->coeff, ptr->expo); 50 | ptr = ptr->next; 51 | } 52 | printf("%dx^%d\t+", ptr->coeff, ptr->expo); 53 | } 54 | } 55 | struct node *AddPoly() 56 | { 57 | struct node *new, *P, *Q, *R, *head = NULL; 58 | P = Phead; 59 | Q = Qhead; 60 | while (P != NULL && Q != NULL) 61 | { 62 | if (P->expo == Q->expo) 63 | { 64 | new = (struct node *)malloc(sizeof(struct node)); 65 | new->coeff = P->coeff + Q->coeff; 66 | new->expo = P->expo; 67 | new->next = NULL; 68 | P = P->next; 69 | Q = Q->next; 70 | } 71 | 72 | else if (P->expo > Q->expo) 73 | { 74 | new->coeff = P->coeff; 75 | new->expo = P->expo; 76 | new->next = NULL; 77 | P = P->next; 78 | } 79 | else 80 | { 81 | new->coeff = Q->coeff; 82 | new->expo = Q->expo; 83 | new->next = NULL; 84 | Q = Q->next; 85 | } 86 | 87 | if (head == NULL) 88 | { 89 | head = R = new; 90 | } 91 | else 92 | { 93 | R->next = new; 94 | R = new; 95 | } 96 | } 97 | 98 | while (P != NULL) 99 | { 100 | new = (struct node *)malloc(sizeof(struct node)); 101 | new->coeff = P->coeff; 102 | new->expo = P->expo; 103 | new->next = NULL; 104 | 105 | if (head == NULL) 106 | { 107 | head = R = new; 108 | } 109 | else 110 | { 111 | R->next = new; 112 | R = new; 113 | P = P->next; 114 | } 115 | } 116 | while (Q != NULL) 117 | { 118 | new = (struct node *)malloc(sizeof(struct node)); 119 | new->coeff = Q->coeff; 120 | new->expo = Q->expo; 121 | new->next = NULL; 122 | 123 | if (head == NULL) 124 | { 125 | head = R = new; 126 | } 127 | else 128 | { 129 | R->next = new; 130 | R = new; 131 | Q = Q->next; 132 | } 133 | } 134 | 135 | return (head); 136 | } 137 | 138 | int main() 139 | { 140 | printf("enter the details of first polynomial:"); 141 | Phead = ReadPoly(); 142 | 143 | printf("enter the details of second polynomial:"); 144 | Qhead = ReadPoly(); 145 | 146 | printf("first polynomial:"); 147 | DisplayPoly(Phead); 148 | 149 | printf("second polynomial:"); 150 | DisplayPoly(Qhead); 151 | 152 | Rhead = AddPoly(); 153 | printf("resultant polynomial:"); 154 | DisplayPoly(Rhead); 155 | } 156 | -------------------------------------------------------------------------------- /polynomial_arithmetics/polyMultUsingLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct term 5 | { 6 | int coeff, expo; 7 | struct term *next; 8 | }; 9 | 10 | typedef struct term node; 11 | 12 | void ReadPoly(node **poly) 13 | { 14 | int n; 15 | printf("enter total number of terms in polynomial:"); 16 | scanf("%d", &n); 17 | printf("enter coefficients and exponents of terms in polynomial\n"); 18 | for (int i = 1; i <= n; i++) 19 | { 20 | printf("enter (coeff%d,expo%d)", i, i); 21 | node *new = (node *)malloc(sizeof(node)); 22 | scanf("%d", &new->coeff); 23 | scanf("%d", &new->expo); 24 | new->next = *poly; 25 | *poly = new; 26 | } 27 | } 28 | 29 | void DisplayPoly(node *rp) 30 | { 31 | node *ptr; 32 | if (rp == NULL) 33 | printf("polynomial is Empty"); 34 | else 35 | { 36 | ptr = rp; 37 | while (ptr->next != NULL) 38 | { 39 | printf("%dx^%d + ", ptr->coeff, ptr->expo); 40 | ptr = ptr->next; 41 | } 42 | printf("%dx^%d\t + ", ptr->coeff, ptr->expo); 43 | } 44 | } 45 | 46 | void MultPoly(node *p1, node *p2, node *rp) 47 | { 48 | node *ptr1 = p1; 49 | node *ptr1 = p2; 50 | 51 | while (p1 != NULL) 52 | { 53 | while (p2 != NULL) 54 | { 55 | node *new = (node *)malloc(sizeof(node)); 56 | new->coeff = p1->coeff * p2->coeff; 57 | new->expo = p1->expo + p2->coeff; 58 | new->next = NULL; 59 | if (rp == NULL) 60 | { 61 | rp = new; 62 | rp = rp; 63 | } 64 | 65 | else 66 | { 67 | rp->next = new; 68 | rp = new; 69 | } 70 | p2 = p2->next; 71 | } 72 | p1 = p1->next; 73 | p2 = p2; 74 | } 75 | 76 | p1 = rp; 77 | 78 | while (p1 != NULL) 79 | { 80 | prev = p1; 81 | p2 = p1->next; 82 | while (p2 != NULL) 83 | { 84 | if (p1->expo == p2->expo) 85 | { 86 | p1->coeff = p1->coeff + p2->coeff; 87 | prev->next = p2->next; 88 | free(p2); 89 | p2 = prev->next; 90 | } 91 | else 92 | { 93 | prev = p2; 94 | p2 = p2->next; 95 | } 96 | } 97 | p1 = p1->next; 98 | } 99 | } 100 | 101 | int main() 102 | { 103 | node *poly1 = NULL; 104 | node *poly2 = NULL; 105 | node *rPoly = NULL; 106 | 107 | printf("enter the details of first polynomial\n"); 108 | ReadPoly(&poly1); 109 | 110 | printf("enter the details of second polynomial\n"); 111 | ReadPoly(&poly2); 112 | 113 | printf("first polynomial:"); 114 | DisplayPoly(poly1); 115 | 116 | printf("second polynomial:"); 117 | DisplayPoly(poly2); 118 | 119 | MultPoly(poly1, poly2, rPoly); 120 | printf("resultant polynomial:"); 121 | DisplayPoly(rPoly); 122 | } -------------------------------------------------------------------------------- /polynomial_arithmetics/polyMultUsingList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int coeff, expo; 7 | struct node *next; 8 | }; 9 | struct node *Phead, *Qhead, *Rhead; 10 | 11 | struct node *ReadPoly() 12 | { 13 | struct node *new, *ptr, *head = NULL; 14 | int n, i; 15 | printf("enter total number of terms in polynomial:"); 16 | scanf("%d", &n); 17 | printf("enter coefficients and exponents of terms in polynomial\n"); 18 | for (i = 1; i <= n; i++) 19 | { 20 | printf("enter (coeff%d,expo%d)", i, i); 21 | new = (struct node *)malloc(sizeof(struct node)); 22 | scanf("%d", &new->coeff); 23 | scanf("%d", &new->expo); 24 | new->next = NULL; 25 | if (head == NULL) 26 | { 27 | head = new; 28 | ptr = head; 29 | } 30 | else 31 | { 32 | ptr->next = new; 33 | ptr = new; 34 | } 35 | } 36 | return (head); 37 | } 38 | void DisplayPoly(struct node *head) 39 | { 40 | struct node *ptr; 41 | if (head == NULL) 42 | printf("polynomial is Empty"); 43 | else 44 | { 45 | ptr = head; 46 | while (ptr->next != NULL) 47 | { 48 | printf("%dx^%d + ", ptr->coeff, ptr->expo); 49 | ptr = ptr->next; 50 | } 51 | printf("%dx^%d\t + ", ptr->coeff, ptr->expo); 52 | } 53 | } 54 | 55 | struct node *MultPoly() 56 | { 57 | struct node *new, *P, *Q, *R, *prev, *head = NULL; 58 | P = Phead; 59 | Q = Qhead; 60 | while (P != NULL) 61 | { 62 | while (Q != NULL) 63 | { 64 | new = (struct node *)malloc(sizeof(struct node)); 65 | new->coeff = P->coeff * Q->coeff; 66 | new->expo = P->expo + Q->coeff; 67 | new->next = NULL; 68 | if (head == NULL) 69 | { 70 | head = new; 71 | R = head; 72 | } 73 | 74 | else 75 | { 76 | R->next = new; 77 | R = new; 78 | } 79 | Q = Q->next; 80 | } 81 | P = P->next; 82 | Q = Qhead; 83 | } 84 | 85 | P = head; 86 | 87 | while (P != NULL) 88 | { 89 | prev = P; 90 | Q = P->next; 91 | while (Q != NULL) 92 | { 93 | if (P->expo == Q->expo) 94 | { 95 | P->coeff = P->coeff + Q->coeff; 96 | prev->next = Q->next; 97 | free(Q); 98 | Q = prev->next; 99 | } 100 | else 101 | { 102 | prev = Q; 103 | Q = Q->next; 104 | } 105 | } 106 | P = P->next; 107 | } 108 | return (head); 109 | } 110 | 111 | int main() 112 | { 113 | printf("enter the details of first polynomial\n"); 114 | Phead = ReadPoly(); 115 | 116 | printf("enter the details of second polynomial\n"); 117 | Qhead = ReadPoly(); 118 | 119 | printf("first polynomial:"); 120 | DisplayPoly(Phead); 121 | 122 | printf("second polynomial:"); 123 | DisplayPoly(Qhead); 124 | 125 | Rhead = MultPoly(); 126 | printf("resultant polynomial:"); 127 | DisplayPoly(Rhead); 128 | } -------------------------------------------------------------------------------- /polynomial_arithmetics/polynomialAddUsingArray.c: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | int main() { 4 | int poly1[20], poly2[20], sum[20], deg1, deg2; 5 | 6 | // To read value for max degree and the operands of the first polynomial 7 | printf("Deg 1: "); 8 | scanf("%d", °1); 9 | printf("Polynomial 1:\n"); 10 | for (int i = deg1; i >= 0; i-- ) { 11 | printf("Coefficient of x^%d: ",i); 12 | scanf("%d", &poly1[i]); 13 | } 14 | 15 | // To read value for max degree and the operands of the second polynomial 16 | printf("Deg 2: "); 17 | scanf("%d", °2); 18 | printf("Polnomial 2:\n"); 19 | for (int i = deg2; i >= 0; i-- ) { 20 | printf("Coefficient of x^%d: ",i); 21 | scanf("%d", &poly2[i]); 22 | } 23 | 24 | // to find the highest and lowest possible degree among the plynomials 25 | int largestPolyDeg = deg1 > deg2 ? deg1 : deg2; 26 | int smallestPolyDeg = deg1 < deg2 ? deg1 : deg2; 27 | 28 | // stores values of the sum of the operands 29 | // with same coefficients upto the lowest degree 30 | for (int i = 0; i <= smallestPolyDeg; i++) 31 | sum[i] = poly1[i] + poly2[i]; 32 | 33 | // stores values of remaining operands to sum 34 | if(deg1 > deg2) 35 | for (int i = smallestPolyDeg + 1; i <= largestPolyDeg; i++) 36 | sum[i] = poly1[i]; 37 | if(deg2 > deg1) 38 | for (int i = smallestPolyDeg + 1; i <= largestPolyDeg; i++) 39 | sum[i] = poly2[i]; 40 | 41 | // Display sum of the polynomials 42 | printf("Polnomial Sum: "); 43 | for (int i = largestPolyDeg; i >= 0; i--) 44 | printf("%dx^%d + ",sum[i], i); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /polynomial_arithmetics/polynomialAddUsingLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct term 5 | { 6 | int coeff, expo; 7 | struct term *next; 8 | }; 9 | 10 | typedef struct term node; 11 | 12 | void displayPoly(node *header) 13 | { 14 | if (header == NULL) 15 | { 16 | printf("%s\n", "List is Empty"); 17 | return; 18 | } 19 | node *ptr = header; 20 | while (ptr->next != NULL) 21 | { 22 | printf("%dx^%d + ", ptr->coeff, ptr->expo); 23 | ptr = ptr->next; 24 | } 25 | printf("%dx^%d\n", ptr->coeff, ptr->expo); 26 | } 27 | 28 | void readPoly(node **header, int coeff, int expo) 29 | { 30 | node *newNode = (node *)malloc(sizeof(node)); 31 | newNode->coeff = coeff; 32 | newNode->expo = expo; 33 | 34 | if (*header == NULL) 35 | { 36 | newNode->next = NULL; 37 | *header = newNode; 38 | } 39 | else if (expo >= (*header)->expo) 40 | { 41 | newNode->next = (*header); 42 | *header = newNode; 43 | } 44 | else 45 | { 46 | node *prev = (*header); 47 | for (node *ptr = (*header)->next; ptr != NULL; ptr = ptr->next) 48 | { 49 | if (expo >= ptr->expo) 50 | break; 51 | prev = ptr; 52 | } 53 | newNode->next = prev->next; 54 | prev->next = newNode; 55 | } 56 | } 57 | 58 | void polynomialAdd(node *poly1, node *poly2, node **poly3) 59 | { 60 | node *ptr1 = poly1, *ptr2 = poly2, *ptr3; 61 | 62 | while (ptr1 != NULL && ptr2 != NULL) 63 | { 64 | node *new = (node *)malloc(sizeof(node)); 65 | if (ptr1->expo == ptr2->expo) 66 | { 67 | new->coeff = ptr1->coeff + ptr2->coeff; 68 | new->expo = ptr1->expo; 69 | new->next = NULL; 70 | 71 | ptr1 = ptr1->next; 72 | ptr2 = ptr2->next; 73 | } 74 | else if (ptr1->expo > ptr2->expo) 75 | { 76 | new->coeff = ptr1->coeff; 77 | new->expo = ptr1->expo; 78 | new->next = NULL; 79 | 80 | ptr1 = ptr1->next; 81 | } 82 | else 83 | { 84 | new->coeff = ptr2->coeff; 85 | new->expo = ptr2->expo; 86 | new->next = NULL; 87 | 88 | ptr2 = ptr2->next; 89 | } 90 | 91 | if ((*poly3) == NULL) 92 | { 93 | (*poly3) = new; 94 | ptr3 = new; 95 | } 96 | else 97 | { 98 | ptr3->next = new; 99 | ptr3 = new; 100 | } 101 | } 102 | 103 | while (ptr1 != NULL) 104 | { 105 | node *new = (node *)malloc(sizeof(node)); 106 | new->coeff = ptr1->coeff; 107 | new->expo = ptr1->expo; 108 | 109 | if ((*poly3) == NULL) 110 | { 111 | (*poly3) = new; 112 | ptr3 = new; 113 | } 114 | else 115 | { 116 | ptr3->next = new; 117 | ptr3 = new; 118 | ptr1 = ptr1->next; 119 | } 120 | } 121 | while (ptr2 != NULL) 122 | { 123 | node *new = (node *)malloc(sizeof(node)); 124 | new->coeff = ptr2->coeff; 125 | new->expo = ptr2->expo; 126 | 127 | if ((*poly3) == NULL) 128 | { 129 | (*poly3) = new; 130 | ptr3 = new; 131 | } 132 | else 133 | { 134 | ptr3->next = new; 135 | ptr3 = new; 136 | ptr2 = ptr2->next; 137 | } 138 | } 139 | } 140 | 141 | void main() 142 | { 143 | node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL; 144 | readPoly(&poly1, 20, 3); 145 | readPoly(&poly1, 5, 4); 146 | readPoly(&poly1, 10, 1); 147 | readPoly(&poly1, 17, 2); 148 | displayPoly(poly1); 149 | 150 | readPoly(&poly2, 10, 5); 151 | readPoly(&poly2, 15, 3); 152 | readPoly(&poly2, 7, 4); 153 | readPoly(&poly2, 9, 1); 154 | displayPoly(poly2); 155 | 156 | polynomialAdd(poly1, poly2, &poly3); 157 | displayPoly(poly3); 158 | } -------------------------------------------------------------------------------- /polynomial_arithmetics/polynomialAddUsingStruct.c: -------------------------------------------------------------------------------- 1 | # include 2 | 3 | struct polynomial{ 4 | int coeff; 5 | int expo; 6 | } p1[20], p2[20], sum[20]; 7 | 8 | int main() { 9 | int terms1, terms2, terms3=0; 10 | 11 | // To read value for no of terms and the terms of the first polynomial 12 | printf("Terms 1: "); 13 | scanf("%d", &terms1); 14 | printf("Polnomial 1: "); 15 | for (int i = 0; i < terms1; i++) { 16 | scanf("%d", &p1[i].coeff); 17 | scanf("%d", &p1[i].expo); 18 | } 19 | 20 | // To read value for no of terms and the terms of the second polynomial 21 | printf("Terms 2: "); 22 | scanf("%d", &terms2); 23 | printf("Polnomial 2: "); 24 | for (int i = 0; i < terms2; i++) { 25 | scanf("%d", &p2[i].coeff); 26 | scanf("%d", &p2[i].expo); 27 | } 28 | 29 | // stores values of the sum of the coefficients 30 | // with same exponent upto the lowest degree 31 | int i=0, j=0; 32 | while (i p2[j].expo) 42 | { 43 | sum[terms3].coeff = p1[i].coeff; 44 | sum[terms3].expo = p1[i].expo; 45 | i++; 46 | terms3++; 47 | } 48 | else { 49 | sum[terms3].coeff = p2[j].coeff; 50 | sum[terms3].expo = p2[j].expo; 51 | j++; 52 | terms3++; 53 | } 54 | 55 | // stores values of remaining terms to sum 56 | while (i < terms1) 57 | { 58 | sum[terms3].coeff = p1[i].coeff; 59 | sum[terms3].expo = p1[i].expo; 60 | i++; 61 | terms3++; 62 | } 63 | while (i < terms2) 64 | { 65 | sum[terms3].coeff = p2[j].coeff; 66 | sum[terms3].expo = p2[j].expo; 67 | j++; 68 | terms3++; 69 | } 70 | 71 | // Display sum of the polynomials 72 | printf("Polnomial Sum: "); 73 | for (int i = 0; i < terms3; i++) 74 | printf("%d x^%d + ", sum[i].coeff, sum[i].expo); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /polynomial_arithmetics/polynomialMultUsingArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int poly1[20], poly2[20], mult[20], deg1, deg2; 6 | 7 | // To read value for max degree and the operands of the first polynomial 8 | printf("Deg 1: "); 9 | scanf("%d", °1); 10 | printf("Polynomial 1:\n"); 11 | for (int i = deg1; i >= 0; i--) 12 | { 13 | printf("Coefficient of x^%d: ", i); 14 | scanf("%d", &poly1[i]); 15 | } 16 | 17 | // To read value for max degree and the operands of the second polynomial 18 | printf("Deg 2: "); 19 | scanf("%d", °2); 20 | printf("Polnomial 2:\n"); 21 | for (int i = deg2; i >= 0; i--) 22 | { 23 | printf("Coefficient of x^%d: ", i); 24 | scanf("%d", &poly2[i]); 25 | } 26 | 27 | // to find the highest and lowest possible degree among the polynomials 28 | int highest_deg = deg1 < deg2 ? deg1 : deg2; 29 | int lowest_deg = deg1 > deg2 ? deg1 : deg2; 30 | 31 | for (int coeff = 0; coeff <= deg1 + deg2; coeff++) 32 | mult[coeff] = 0; 33 | 34 | // Multiplying current term with of largest polynomial with 35 | // every other terms of smallest polnomial 36 | for (int i = highest_deg; i >= 0; i--) 37 | for (int j = lowest_deg; j >= 0; j--) 38 | { 39 | if (deg1 < deg2) 40 | mult[i + j] += poly1[i] * poly2[j]; 41 | if (deg2 < deg1) 42 | mult[i + j] += poly2[i] * poly1[j]; 43 | } 44 | 45 | // Display sum of the polynomials 46 | printf("Polnomial Multiplicative: "); 47 | for (int i = deg1 + deg2; i >= 0; i--) 48 | printf("%dx^%d + ", mult[i], i); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /polynomial_arithmetics/polynomialMultUsingLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct term 5 | { 6 | int coeff, expo; 7 | struct term *next; 8 | }; 9 | 10 | typedef struct term node; 11 | 12 | void displayPoly(node *header) 13 | { 14 | if (header == NULL) 15 | { 16 | printf("%s\n", "List is Empty"); 17 | return; 18 | } 19 | node *ptr = header; 20 | while (ptr->next != NULL) 21 | { 22 | printf("%dx^%d + ", ptr->coeff, ptr->expo); 23 | ptr = ptr->next; 24 | } 25 | printf("%dx^%d\n", ptr->coeff, ptr->expo); 26 | } 27 | 28 | void readPoly(node **header, int coeff, int expo) 29 | { 30 | node *newNode = (node *)malloc(sizeof(node)); 31 | newNode->coeff = coeff; 32 | newNode->expo = expo; 33 | 34 | if (*header == NULL) 35 | { 36 | newNode->next = NULL; 37 | *header = newNode; 38 | } 39 | else if (expo >= (*header)->expo) 40 | { 41 | newNode->next = (*header); 42 | *header = newNode; 43 | } 44 | else 45 | { 46 | node *prev = (*header); 47 | for (node *ptr = (*header)->next; ptr != NULL; ptr = ptr->next) 48 | { 49 | if (expo >= ptr->expo) 50 | break; 51 | prev = ptr; 52 | } 53 | newNode->next = prev->next; 54 | prev->next = newNode; 55 | } 56 | } 57 | 58 | void polynomialMult(node *poly1, node *poly2, node **poly3) 59 | { 60 | node *ptr1 = poly1, *ptr2 = poly2, *ptr3; 61 | 62 | while (ptr1 != NULL) 63 | { 64 | while (ptr2 != NULL) 65 | { 66 | node *new = (node *)malloc(sizeof(node)); 67 | new->coeff = ptr1->coeff + ptr2->coeff; 68 | new->expo = ptr1->expo; 69 | new->next = NULL; 70 | 71 | if ((*poly3) == NULL) 72 | { 73 | (*poly3) = new; 74 | ptr3 = new; 75 | } 76 | else 77 | { 78 | ptr3->next = new; 79 | ptr3 = new; 80 | } 81 | ptr2 = ptr2->next; 82 | } 83 | ptr1 = ptr1->next; 84 | } 85 | 86 | ptr1 = (*poly3); 87 | while (ptr1 != NULL) 88 | { 89 | 90 | } 91 | 92 | if (ptr1->expo == ptr2->expo) 93 | { 94 | 95 | 96 | ptr1 = ptr1->next; 97 | ptr2 = ptr2->next; 98 | } 99 | else if (ptr1->expo > ptr2->expo) 100 | { 101 | new->coeff = ptr1->coeff; 102 | new->expo = ptr1->expo; 103 | new->next = NULL; 104 | 105 | ptr1 = ptr1->next; 106 | } 107 | else 108 | { 109 | new->coeff = ptr2->coeff; 110 | new->expo = ptr2->expo; 111 | new->next = NULL; 112 | 113 | ptr2 = ptr2->next; 114 | } 115 | 116 | if ((*poly3) == NULL) 117 | { 118 | (*poly3) = new; 119 | ptr3 = new; 120 | } 121 | else 122 | { 123 | ptr3->next = new; 124 | ptr3 = new; 125 | } 126 | } 127 | 128 | while (ptr1 != NULL) 129 | { 130 | node *new = (node *)malloc(sizeof(node)); 131 | new->coeff = ptr1->coeff; 132 | new->expo = ptr1->expo; 133 | 134 | if ((*poly3) == NULL) 135 | { 136 | (*poly3) = new; 137 | ptr3 = new; 138 | } 139 | else 140 | { 141 | ptr3->next = new; 142 | ptr3 = new; 143 | ptr1 = ptr1->next; 144 | } 145 | } 146 | while (ptr2 != NULL) 147 | { 148 | node *new = (node *)malloc(sizeof(node)); 149 | new->coeff = ptr2->coeff; 150 | new->expo = ptr2->expo; 151 | 152 | if ((*poly3) == NULL) 153 | { 154 | (*poly3) = new; 155 | ptr3 = new; 156 | } 157 | else 158 | { 159 | ptr3->next = new; 160 | ptr3 = new; 161 | ptr2 = ptr2->next; 162 | } 163 | } 164 | } 165 | 166 | void main() 167 | { 168 | node *poly1 = NULL, *poly2 = NULL, *poly3 = NULL; 169 | readPoly(&poly1, 20, 3); 170 | readPoly(&poly1, 5, 4); 171 | readPoly(&poly1, 10, 1); 172 | readPoly(&poly1, 17, 2); 173 | displayPoly(poly1); 174 | 175 | readPoly(&poly2, 10, 5); 176 | readPoly(&poly2, 15, 3); 177 | readPoly(&poly2, 7, 4); 178 | readPoly(&poly2, 9, 1); 179 | displayPoly(poly2); 180 | 181 | polynomialMult(poly1, poly2, &poly3); 182 | displayPoly(poly3); 183 | } -------------------------------------------------------------------------------- /searching/binary_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sort_array(int arr[]) 4 | { 5 | for (int i = 1; arr[i] != '\0'; i++) 6 | { 7 | int j, temp = arr[i]; 8 | for (j = i - 1; temp < arr[j] && j >= 0; j--) 9 | arr[j + 1] = arr[j]; 10 | arr[j + 1] = temp; 11 | } 12 | } 13 | 14 | int binary_search(int arr[], int start, int end, int key) 15 | { 16 | if (end >= start) 17 | { 18 | int mid = (start + end) / 2; 19 | if (arr[mid] == key) 20 | return mid; 21 | if (arr[mid] > key) 22 | return binary_search(arr, start, mid - 1, key); 23 | return binary_search(arr, mid + 1, end, key); 24 | ; 25 | } 26 | return -1; 27 | } 28 | 29 | int main() 30 | { 31 | int arr[50], size, key; 32 | 33 | printf("Enter size of the array: "); 34 | scanf("%d", &size); 35 | 36 | printf("Enter elements of the array: "); 37 | for (int i = 0; i < size; i++) 38 | scanf("%d", &arr[i]); 39 | 40 | printf("Enter key to be searched in the array: "); 41 | scanf("%d", &key); 42 | 43 | sort_array(arr); 44 | int index = binary_search(arr, 0, size, key); 45 | 46 | if (index == -1) 47 | printf("Key not found"); 48 | else 49 | printf("Key found at index %d", index); 50 | 51 | return 0; 52 | } -------------------------------------------------------------------------------- /searching/binary_search.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Asterdev-03/C/9c074cc1f81f2c205cfb43c1cbed38df3cef4ad3/searching/binary_search.exe -------------------------------------------------------------------------------- /searching/linear_search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int linear_search(int arr[], int key) 4 | { 5 | for (int i = 0; arr[i] != '\0'; i++) 6 | if (key == arr[i]) 7 | return i; 8 | return -1; 9 | } 10 | 11 | int main() 12 | { 13 | int arr[50], size, key; 14 | 15 | printf("Enter size of the array: "); 16 | scanf("%d", &size); 17 | 18 | printf("Enter elements of the array: "); 19 | for (int i = 0; i < size; i++) 20 | scanf("%d", &arr[i]); 21 | 22 | printf("Enter key to be searched in the array: "); 23 | scanf("%d", &key); 24 | 25 | int index = linear_search(arr, key); 26 | if (index == -1) 27 | printf("Key not found"); 28 | else 29 | printf("Key found at index %d", index); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /sorting/heapSort.c: -------------------------------------------------------------------------------- 1 | // Heap Sort in C 2 | #include 3 | 4 | void swap(int *a, int *b) 5 | { 6 | int temp = *a; 7 | *a = *b; 8 | *b = temp; 9 | } 10 | 11 | void heapify(int arr[], int size, int i) 12 | { 13 | int largest = i; 14 | int left = 2 * i + 1; 15 | int right = 2 * i + 2; 16 | 17 | if (left < size && arr[left] > arr[largest]) 18 | largest = left; 19 | if (right < size && arr[right] > arr[largest]) 20 | largest = right; 21 | if (largest != i) 22 | { 23 | swap(&arr[i], &arr[largest]); 24 | heapify(arr, size, largest); 25 | } 26 | } 27 | 28 | void heapSort(int arr[], int size) 29 | { 30 | // to build heap 31 | for (int i = size / 2 - 1; i >= 0; i--) 32 | heapify(arr, size, i); 33 | // to sort the heap 34 | for (int i = size - 1; i >= 0; i--) 35 | { 36 | swap(&arr[0], &arr[i]); 37 | heapify(arr, i, 0); 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | int arr[] = {6, 5, 12, 10, 9, 1}; 44 | int size = sizeof(arr) / sizeof(arr[0]); 45 | heapSort(arr, size); 46 | printf("Sorted array : "); 47 | for (int i = 0; i < size; i++) 48 | printf("%d ", arr[i]); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /sorting/insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void insertionsort(int arr[], int size) 4 | { 5 | for (int i = 1; i < size; i++) 6 | { 7 | int j, temp = arr[i]; 8 | for (j = i - 1; temp < arr[j] && j >= 0; j--) 9 | arr[j + 1] = arr[j]; 10 | arr[j + 1] = temp; 11 | } 12 | } 13 | 14 | int main() 15 | { 16 | int arr[] = {6, 5, 12, 10, 9, 1}; 17 | int size = sizeof(arr) / sizeof(arr[0]); 18 | insertionsort(arr, size); 19 | printf("Sorted array : "); 20 | for (int i = 0; i < size; i++) 21 | printf("%d ", arr[i]); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /sorting/mergeSort.c: -------------------------------------------------------------------------------- 1 | // Merge sort in C 2 | #include 3 | 4 | void merge(int arr[], int beg, int mid, int end) 5 | { 6 | int L[mid - beg + 1], R[end - mid]; 7 | 8 | for (int i = 0; i < mid - beg + 1; i++) 9 | L[i] = arr[beg + i]; 10 | for (int i = 0; i < end - mid; i++) 11 | R[i] = arr[mid + 1 + i]; 12 | 13 | int i = 0, j = 0, k = beg; 14 | 15 | while (i < mid - beg + 1 && j < end - mid) 16 | if (L[i] <= R[j]) 17 | arr[k++] = L[i++]; 18 | else 19 | arr[k++] = R[j++]; 20 | while (i < mid - beg + 1) 21 | arr[k++] = L[i++]; 22 | while (j < end - mid) 23 | arr[k++] = R[j++]; 24 | } 25 | 26 | void mergeSort(int arr[], int beg, int end) 27 | { 28 | if (beg < end) 29 | { 30 | int mid = (beg + end) / 2; 31 | mergeSort(arr, beg, mid); 32 | mergeSort(arr, mid + 1, end); 33 | merge(arr, beg, mid, end); 34 | } 35 | } 36 | 37 | int main() 38 | { 39 | int arr[] = {6, 5, 12, 10, 9, 1}; 40 | int size = sizeof(arr) / sizeof(arr[0]); 41 | mergeSort(arr, 0, size - 1); 42 | printf("Sorted array : "); 43 | for (int i = 0; i < size; i++) 44 | printf("%d ", arr[i]); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /sorting/quickSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a, int *b) 4 | { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | void quicksort(int arr[], int first, int last) 11 | { 12 | if (first < last) 13 | { 14 | int pivot = first; 15 | int i = first; 16 | int j = last; 17 | while (i < j) 18 | { 19 | while (arr[i] <= arr[pivot] && i < last) 20 | i++; 21 | while (arr[j] > arr[pivot]) 22 | j--; 23 | if (i < j) 24 | swap(&arr[i], &arr[j]); 25 | } 26 | swap(&arr[pivot], &arr[j]); 27 | quicksort(arr, first, j - 1); 28 | quicksort(arr, j + 1, last); 29 | } 30 | } 31 | 32 | int main() 33 | { 34 | int arr[] = {6, 5, 12, 10, 9, 1}; 35 | int size = sizeof(arr) / sizeof(arr[0]); 36 | quicksort(arr, 0, size - 1); 37 | printf("Sorted array : "); 38 | for (int i = 0; i < size; i++) 39 | printf("%d ", arr[i]); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /sorting/sorting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int bubble_sort(int A[10],int n) //BUBBLE SORT METHOD 4 | { 5 | int num; 6 | printf("\nEnter no to be searched: "); 7 | scanf("%d",&num); 8 | 9 | for (int i = 0; i < n; i++) 10 | if(A[i] == num) 11 | { 12 | printf("Found at %d",i+1); 13 | return 0; 14 | } 15 | printf("Not Found"); 16 | return 0; 17 | } 18 | 19 | int linear_search(int A[10],int n) //LINEAR SEARCH METHOD 20 | { 21 | for (int i = 0; i < n; i++) 22 | for (int j = 0; j < n-1; j++) 23 | if (A[j] > A[j+1]) 24 | { 25 | int temp = A[j]; 26 | A[j] = A[j+1]; 27 | A[j+1] = temp; 28 | } 29 | 30 | printf("\nSorted array: "); 31 | for (int i = 0; i < n; i++) 32 | printf("%d ",A[i]); 33 | 34 | return 0; 35 | } 36 | 37 | void main() 38 | { 39 | int A[] = {1,5,2,9,4},n = 5; 40 | 41 | printf("Given Array: "); 42 | for (int i = 0; i < n; i++) 43 | printf("%d ",A[i]); 44 | 45 | bubble_sort(A,n); 46 | linear_search(A,n); 47 | } --------------------------------------------------------------------------------