Professional Documents
Culture Documents
I B-TECH
D KOTESWARARAO, 2
D KOTESWARARAO, 3
INDEX
S.No. 1. 33 2. 34 3. 4. 5. 40 6. 43 7. 44 8. 45 9. 46 10. 47 NAME OF THE PROGRAM Sum of Digits of Given Number Fibonacci Series Prime Numers between 1 to N Evaluate the expression Roots of Quadratic equations Factorial of Given Number using Non-Recursive Factorial of Given Number using Recursive GCD of Two numbers using Non-Recursive GCD of Two numbers using Non-Recursive Towers of Hanoi Problem 35 39 PAGE No.
D KOTESWARARAO, 4
Pyrmid of Numbers Geometric Regression To Find 2s Complement of Given Number To Convert Roman To decimal Number Complex Numbers
D KOTESWARARAO, 5
D KOTESWARARAO, 6
45. To implement the Newton-Gregory Forward Interpolation186 46. 191 47. 195 48. 201 To implement the Linear Regression To implement Simpson Method To implement Tr apezoidal Method
D KOTESWARARAO, 7
D KOTESWARARAO, 8
DOS COMMANDS
D KOTESWARARAO, 9
D KOTESWARARAO, 10
D KOTESWARARAO, 11
3. MD (Make Directory): Definition: It is used to create a new Directory. Syntax: Example: C:\md <directory name> C:\md XYZ
4. CD (Change Directory): Definition: It is used to change the new Directory. Syntax: Example: C:\cd <directory name> C:\cd XYZ
5. RD (Remove Directory): Definition: It is used to remove the existing directory. Syntax: Example: C:\rd <directory name> C:\rd XYZ
D KOTESWARARAO, 12
7. Date: Definition: It is used to move or modify the current system date. Syntax: Example: C:\ date C:\date Current date is: Fri 10/23/2009 Enter the new date: (mm-dd-yy) 8. Time: Definition: It is used to view or modify the current system time. Syntax: Example: C:\time C:\time Current time is: 17:08:08.39 Enter the new time: (hh: mm: ss.ms) 9. Del (Delete): Definition: It is used to delete a file from the storage device. Syntax: Example:
D KOTESWARARAO, 13
Definition: It is used to create a new file. Syntax: Example: C:\copy con <file name> C:\copy con ABC F6 (or) Ctrl +Z are used to save file. 11. Edit : Definition: It is used to edit the existing file and modify the existing file. Syntax: Example: 12. Copy : Definition: It is used to copy the data from existing file to a new file. Syntax: Example: 13. Move : Definition: It is used to existing file data will be removed to move to a new file. Syntax: Example: C:\move <existing file name> <new file name> C:\move ABC CBA C:\copy <existing file name> <new file name> C:\copy ABC DEF C:\edit <file name> C:\edit ABC
******************
D KOTESWARARAO, 15
D KOTESWARARAO, 16
INTRODUCTION
TO
C - LANGUAGE
D KOTESWARARAO, 17
D KOTESWARARAO, 18
Introduction: C is a general purpose programming language C Language was developed by Dennis Ritchie. at AT & T Bell Laboratories of USA in 1972. C language was developed based on BCPL (Basic Combined Programming language) called B language Applications of C: C is used for writing
Operating systems Language Translators( Compiles, Interpreters and Assemblers) Editors Worksheets Database Management Systems games
Advantages of C:
1. C is robust language.
That means C language contains rich set of operators and built-in functions and several data types.
2.
It is a middle-level language.
Associate Professor, CSE Dept.
D KOTESWARARAO, 19
C is a portable language.
Portable means Programs written in C in one machine can be easily run on some other machine without any modifications.
4.
Fast execution of programs when compared to other languages. For example compare to BASIC if you want to increment I value 1 to
20000.It takes nearly 60sec but our C language takes only 1sec 5. It has the ability to deal efficiently with bits, bytes, addresses, etc. 6. it is easy to extends itself That means easily add the new features and develop the other programming languages 7. C language have only 32 keywords In C language have only 32 keywords but other languages have above 200 keywords like COBOL so easily remember the keywords to develop the programs
8.
It is a structured language. Structured programs means large programs divide into sub programs
that is called modules or functions, easy to understand, easy to debug, maintenance is very easy, parallel executions Loading, editing, saving and compiling a C-program: Now before entering the program into the computer, C-compiler should be loaded into the computers main memory.
D KOTESWARARAO, 20 Associate Professor, CSE Dept.
How to enter C editor: The procedure can be done as described below:(Note: if your C stored in tc then type cd tc instead of cd turboc2)
D KOTESWARARAO, 21
Whenever you type that command, you are immediately enter to the below editor:
D KOTESWARARAO, 22
D KOTESWARARAO, 24
D KOTESWARARAO, 25
SYNTAX:
C
LANGUGAE
D KOTESWARARAO, 26
D KOTESWARARAO, 27
Syntax
1) Variable declaration: data_type variable1, variable2,variablen;
2) scanf() scanf(controlstring,&var1,&var2,.&varn);
3) printf() printf(controlstring,var1,var2,.varn);
D KOTESWARARAO, 29
if else statement:
c)
nested if-else statement: The general form of the nested if-else statement is:
if(expression1) { if(expression2) {
D KOTESWARARAO, 30 Associate Professor, CSE Dept.
d)
else if ladder statement: The general form of the else if ladder statement is:
e)
switch statement:
switch (expression) { case constant1: Statement(s) Break; case constant2: Statement(s) Break; .. .. case constantn: constant(s) Break; default:
D KOTESWARARAO, 33 Associate Professor, CSE Dept.
5) LOOPING STATEMENTS:
Components of for: The three expressions in the for statement are optional. However, even if an expression is not present, the associated semicolon should be present. For example: for ( ; test expression ; { Statement-1; Statement-2; Statement-3; . . } )
b)
while loop:
while(expression) {
Statement-block;
D KOTESWARARAO, 35
do {
Statement-block;
} while(expression);
6) Break statement
break;
7) Continue statement
continue;
8) goto statement
D KOTESWARARAO, 36 Associate Professor, CSE Dept.
data_type array_name[size];
7) Function definition:
return_type function_name(argument_list) {
8) Function prototype:
D KOTESWARARAO, 37 Associate Professor, CSE Dept.
return_type function_name(argument_list);
9) Structure:
} structure_variables;
10) Union:
union tag_name {
D KOTESWARARAO, 38 Associate Professor, CSE Dept.
`} union variables;
data_type *variable_name;
D KOTESWARARAO, 39
WEEK-1
D KOTESWARARAO, 40
PROGRAM NO 2: A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C program to generate the first n terms of the sequence.
D KOTESWARARAO, 42
PROGRAM NO 3: Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user. #include<stdio.h> #include<conio.h> void main() { int n,i,j,c=0; clrscr(); printf("Enter n value "); scanf("%d",&n); printf("\nPrime Numbers between 1 to %d \n\n\n",n); for(i=2;i<=n;i++) { c=0; for(j=2;j<=i/2;j++) { if(i%j==0)
D KOTESWARARAO, 44 Associate Professor, CSE Dept.
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
D KOTESWARARAO, 45
D KOTESWARARAO, 46
WEEK-2
D KOTESWARARAO, 47
D KOTESWARARAO, 49
D KOTESWARARAO, 51
WEEK-3
D KOTESWARARAO, 52
PROGRAM NO 6: Write a C program to find the factorial of a given integer using non
recursive #include<stdio.h>
D KOTESWARARAO, 53 Associate Professor, CSE Dept.
OUTPUT:
D KOTESWARARAO, 54 Associate Professor, CSE Dept.
Enter n value 5
Factorial of 5 is 120
#include<stdio.h> #include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value "); scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); }
D KOTESWARARAO, 55 Associate Professor, CSE Dept.
Factorial of 6 is 720
#include<conio.h> #include<stdio.h>
D KOTESWARARAO, 56 Associate Professor, CSE Dept.
D KOTESWARARAO, 57
PROGRAM NO 9: Write a C program to find the GCD of two integers using recursive
functions
#include<conio.h> #include<stdio.h> int gcd(int,int); void main() { int m,n; clrscr(); printf("enter any two numbers"); scanf("%d%d",&m,&n); printf("gcd of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b)
D KOTESWARARAO, 58 Associate Professor, CSE Dept.
OUTPUT: enter any two numbers 34 21 gcd of the given two numbers 1
D KOTESWARARAO, 60
D KOTESWARARAO, 61
WEEK-4
D KOTESWARARAO, 62 Associate Professor, CSE Dept.
D KOTESWARARAO, 63
for(counter = 1; counter <= tim_intrval; counter++) { printf("\n\t\t\tAT T%d TIME(sec) : ",counter); scanf("%d",&time); printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time); scanf("%f",&velos); printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time); scanf("%f",&accl); distance += (velos*time + (accl*pow(time,2))/2); }
printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance); getch(); } OUTPUT: <===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========> NO OF TIME INTERVALS : 3
PROGRAM NO 12: Write a C program, which takes two integer operands and one
operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)
D KOTESWARARAO, 66
OUTPUT: enter a and b values15 3 enter the any operator(+,-,*,/)* Multiplication of two numbers : 45
D KOTESWARARAO, 68
WEEK-5
D KOTESWARARAO, 69 Associate Professor, CSE Dept.
D KOTESWARARAO, 70
PROGRAM NO 13: Write a C program to find both the larges and smallest number in a
list of integers. #include<stdio.h> #include<conio.h> void main() { int a[20],i,n,max,min; clrscr(); printf("Enter n values "); scanf("%d",&n); printf("\nEnter %d elements ",n); for(i=0;i<n;i++) { printf("\nEnter %d position element : ",i); scanf("%d",&a[i]); }
D KOTESWARARAO, 71
Enter 6 elements Enter 0 position element : 23 Enter 1 position element : 67 Enter 2 position element : 45 Enter 3 position element : 34 Enter 4 position element : 13 Enter 5 position element : 87
D KOTESWARARAO, 72 Associate Professor, CSE Dept.
D KOTESWARARAO, 73
D KOTESWARARAO, 76
void read1(int **,int,int); void print(int**,int,int); void mult(int**,int**,int**,int,int,int); int i,j,k; void main() { int **a,**b,**c,r1,c1,r2,c2; clrscr(); printf("Enter First Matrix Order\n"); scanf("%d%d",&r1,&c1); printf("Enter Second Matrix Order\n"); scanf("%d%d",&r2,&c2); a=(int**)malloc(r1*c1*2); b=(int**)malloc(r2*c2*2); c=(int**)malloc(r1*c2*2); printf("Enter First Matrix Elements\n"); read1(a,r1,c1); printf("Enter Second Matrix Elements\n"); read1(b,r2,c2); mult(a,b,c,r1,c1,c2);
D KOTESWARARAO, 77 Associate Professor, CSE Dept.
void mult(int **x,int **y,int **z,int r1,int c1,int c2) { for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { *(*(z+i)+j)=0; for(k=0;k<c1;k++) *(*(z+i)+j)=*(*(z+i)+j)+(*(*(x+i)+k))*(*(*(y+k)+j)); } } }
OUTPUT:
Enter First Matrix Order 22 Enter Second Matrix Order 22 Enter First Matrix Elements
D KOTESWARARAO, 79 Associate Professor, CSE Dept.
D KOTESWARARAO, 80
D KOTESWARARAO, 81
WEEK-6
D KOTESWARARAO, 82 Associate Professor, CSE Dept.
D KOTESWARARAO, 83
PROGRAM NO 16: Write a C program that uses functions to perform the following
operations: i) To insert a sub-string in to given main string from a given position. #include<stdio.h> #include<conio.h> void insert(char[100],char[50],int); void main() { char s[100],ss[50]; int p; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",ss); printf("enter the position\n"); scanf("%d",&p); insert(s,ss,p); getch(); } void insert(char s[100],char ss[50],int p) { int i,j,k; char str[100]; for(i=0,j=0;i<p;i++,j++) str[j]=s[i]; for(i=0;ss[i]!='\0';i++,j++) str[j]=ss[i]; for(i=p;s[i]!='\0';i++,j++) str[j]=s[i];
D KOTESWARARAO, 84 Associate Professor, CSE Dept.
OUTPUT:
enter the main string i you enter the sub string love enter the position 2 After insert main string is i love you ii) To delete n Characters from a given position in a given string. #include<stdio.h> #include<conio.h> void delet(char[100],int,int); void main() { char s[100]; int p,n; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the position\n"); scanf("%d",&p); printf("enter how many character we want delete\n"); scanf("%d",&n);
D KOTESWARARAO, 85 Associate Professor, CSE Dept.
OUTPUT:
enter the main string koteswararao enter the position 7 enter how many character we want delete 2 After delete main string is koteswarao
OUTPUT:
String is palindrome
D KOTESWARARAO, 88
WEEK-7
D KOTESWARARAO, 89 Associate Professor, CSE Dept.
D KOTESWARARAO, 90
PROGRAM NO 18: Write a C program that displays the position or index in the string S
where the string T begins, or 1 if S doesnt contain T. #include<stdio.h> #include<conio.h> main() { char s[100],t[100],i,j,k; clrscr(); printf("enter the string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",t); for(i=0;i<strlen(s);i++) { for(j=0,k=i;j<strlen(t);j++,k++) { if(s[k]==t[j]) continue; else break; } if(j==strlen(t)) { printf("string is found at %d",index+1); exit(0); } } printf("sub string not found in string"); getch(); }
D KOTESWARARAO, 91 Associate Professor, CSE Dept.
PROGRAM NO 19: Write a C program to count the lines, words and characters in a given
text. #include<stdio.h> #include<conio.h> void main() { char text[81],ch; int nwords=0,nlines=0,nchars=0,i; clrscr(); printf("enter text\n"); while(1) { i=0; while((ch=getchar())!='\n') { text[i]=ch; nchars++;
D KOTESWARARAO, 92 Associate Professor, CSE Dept.
D KOTESWARARAO, 94
D KOTESWARARAO, 95
WEEK-8
D KOTESWARARAO, 96
D KOTESWARARAO, 97
D KOTESWARARAO, 98
Pascal's triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
D KOTESWARARAO, 100
WEEK-9
D KOTESWARARAO, 101
D KOTESWARARAO, 102
D KOTESWARARAO, 103
D KOTESWARARAO, 104
WEEK-10
D KOTESWARARAO, 105
#include<stdio.h> #include<conio.h> void main() { char b[100]; int i,l; clrscr(); printf("enter the binary number\n"); scanf("%s",b); for(i=0;b[i]!='\0';i++) if(b[i]=='0'||b[i]=='1') continue; else { printf("the given number is not a binary number"); exit(0); } l=strlen(b); for(i=l-1;i>=0;i--) if(b[i]=='0') continue; else break; for(i=i-1;i>=0;i--) if(b[i]=='0') b[i]='1'; else b[i]='0'; printf(" The 2's complement is %s",b); getch(); } OUTPUT: enter the binary number10111010 The 2's complement is 01000110
D KOTESWARARAO, 109
D KOTESWARARAO, 110
WEEK-11
D KOTESWARARAO, 111
PROGRAM NO 25: Write a C program that uses functions to perform the following
operations: i) Reading a complex number ii) Writing a complex number iii) Addition of two complex numbers iv) Multiplication of two complex numbers
D KOTESWARARAO, 112 Associate Professor, CSE Dept.
void print(complex); complex add(complex,complex); complex sub(complex,complex); complex mul(complex,complex); complex div(complex,complex);
void main() { complex a,b,c; int ch; clrscr(); printf("enter first complex number\n"); scanf("%f%f",&a.real,&a.image);
D KOTESWARARAO, 113
First Complex Number 3.00+i4.00 Second Complex Number 4.00+i5.00 Addition of Two Complex Numbers 7.00+i9.00
D KOTESWARARAO, 117 Associate Professor, CSE Dept.
D KOTESWARARAO, 118
WEEK-12
D KOTESWARARAO, 119
D KOTESWARARAO, 120
D KOTESWARARAO, 121
if(argc!=3) { puts("Improper number of arguments."); exit(0); } fp = fopen(argv[1],"r"); if(fp == NULL) { puts("File cannot be opened."); exit(0); }
WEEK-13
D KOTESWARARAO, 124 Associate Professor, CSE Dept.
D KOTESWARARAO, 125
PROGRAM NO 28: Write a C program that uses functions to perform the following
operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; typedef struct node *NODEPTR; NODEPTR p; NODEPTR getnode(); void freenode(NODEPTR p); void ins_start(NODEPTR p,int x); void ins_end(NODEPTR p,int x); void ins_middle(NODEPTR p,int x); int del_start(NODEPTR p); int del_middle(NODEPTR p); int del_end(NODEPTR p); void print(NODEPTR p); void main() { int ch,x;
D KOTESWARARAO, 126 Associate Professor, CSE Dept.
} void ins_end(NODEPTR p,int x) { NODEPTR q; q=getnode(); q->info=x; if(p->next==NULL) q->next=NULL; else { while(p->next!=NULL) p=p->next; q->next=NULL; } p->next=q; } int del_start(NODEPTR p) { int x; NODEPTR q; if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; p->next=q->next; freenode(q); return x; } int del_middle(NODEPTR p) { int x,i=1,n; NODEPTR q; printf("enter position of the deleted element"); scanf("%d",&n); while(p!=NULL && i<n) { p=p->next; i++;
D KOTESWARARAO, 129 Associate Professor, CSE Dept.
D KOTESWARARAO, 133
WEEK-14
D KOTESWARARAO, 134 Associate Professor, CSE Dept.
D KOTESWARARAO, 135
PROGRAM NO 29: Write a C program that uses functions to perform the following
operations on doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways #include<stdio.h> #include<conio.h>
NODEPTR p;
D KOTESWARARAO, 136 Associate Professor, CSE Dept.
NODEPTR getnode(); void freenode(NODEPTR p); void ins_start(NODEPTR p,int x); void ins_end(NODEPTR p,int x); void ins_middle(NODEPTR p,int x); int del_start(NODEPTR p); int del_middle(NODEPTR p); int del_end(NODEPTR p); void printforward(NODEPTR p); void printbackward(NODEPTR p); void main() { int ch,x; p=getnode(); p->next=NULL; p->prev=NULL; clrscr(); do { printf("\n1.ins_start\n2.ins_middle\n3.ins_end"); printf("\n4.del_start\n5.del_middle\n6.del_end"); printf("\n7.print_forward\n8.print_backward\n9.exit\n");
D KOTESWARARAO, 137 Associate Professor, CSE Dept.
void ins_start(NODEPTR p,int x) { NODEPTR q,r; q=getnode(); q->info=x; if(p->next==NULL) { q->next=NULL; q->prev=p; p->prev=NULL; } else { r=p->next; r->prev=q; q->next=r; q->prev=p; } p->next=q; } void ins_middle(NODEPTR p,int x) {
D KOTESWARARAO, 140 Associate Professor, CSE Dept.
}
D KOTESWARARAO, 141 Associate Professor, CSE Dept.
void printforward(NODEPTR p) { NODEPTR q; q=p->next; if(p->next==NULL) { printf("LIST IS EMPTY"); exit(0); } while(q!=NULL) { printf("%2d->",q->info); q=q->next;
D KOTESWARARAO, 145 Associate Professor, CSE Dept.
D KOTESWARARAO, 146
1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print_forward 8.print_backward 9.exit enter your choice1 enter inserted element20
D KOTESWARARAO, 147
D KOTESWARARAO, 150
WEEK-15
D KOTESWARARAO, 151
PROGRAM NO 30: Write C programs that implement stack (its operations) using
i) Arrays
D KOTESWARARAO, 152
void push(struct stack *s,int n) { if(s->top==MAXSIZE-1) printf("stack is full"); else { s->top++; s->item[s->top]=n; } } int pop(struct stack *s) { return s->item[s->top--]; } void print(struct stack *s) { int i; if(!empty(s)) { printf("stack elements"); for(i=s->top;i>=0;i--)
D KOTESWARARAO, 155 Associate Professor, CSE Dept.
1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 35 | ---| 15 | ---1.push
D KOTESWARARAO, 157 Associate Professor, CSE Dept.
1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 50 | ---| 35 | ---| 15 | ---1.push 2.pop 3.print
D KOTESWARARAO, 158 Associate Professor, CSE Dept.
the retrived elemenet from stack is 50 1.push 2.pop 3.print 4.exit enter your option4
PROGRAM NO 31: Write C programs that implement stack (its operations) using
ii) Pointers #include<stdio.h> #include<conio.h>
struct stack
D KOTESWARARAO, 159 Associate Professor, CSE Dept.
stack s; NODEPTR getnode(); void freenode(NODEPTR p); void push(stack s,int x); int pop(stack s); int empty(stack s); void print(stack s);
void main() { int ch,x; s->top=NULL; clrscr(); do { printf("\n1.PUSH\n2.POP\n3.PRINT\n4.EXIT\n"); printf("enter your choice");
D KOTESWARARAO, 160 Associate Professor, CSE Dept.
void freenode(NODEPTR p) { free(p); } int empty(stack s) { if(s->top==NULL) return 1; else return 0; } void push(stack s,int x) { NODEPTR q; q=getnode(); q->info=x; if(s->top==NULL) { q->next=NULL; s->top=q; }
D KOTESWARARAO, 162 Associate Professor, CSE Dept.
int pop(stack s) { int x; NODEPTR q; if(empty(s)) { printf("stack is underflow"); exit(0); } q=s->top; x=q->info; s->top=q->next; freenode(q); return x;
D KOTESWARARAO, 163 Associate Professor, CSE Dept.
void print(stack s) { NODEPTR q; q=s->top; if(s->top==NULL) { printf("STACK IS EMPTY"); exit(0); } printf(" ----\n"); printf("|NULL|\n"); while(q!=NULL) { printf(" ----\n"); printf("| %d |\n",q->info); q=q->next; } printf(" ----"); } OUTPUT:
D KOTESWARARAO, 164
1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element30
D KOTESWARARAO, 167
WEEK-16
D KOTESWARARAO, 168 Associate Professor, CSE Dept.
D KOTESWARARAO, 169
PROGRAM NO 32: Write C programs that implement Queue (its operations) using
i) Arrays #include<stdio.h> #include<conio.h> #define MAX 100 struct queue { int front,rear; int item[MAX]; }; struct queue *q; void insert(struct queue *q,int x); int delete(struct queue *q);
D KOTESWARARAO, 170 Associate Professor, CSE Dept.
void insert(struct queue *q,int x) { if(q->rear==MAX-1) { printf("queue is overflow\n"); getch(); } else { q->rear++; q->item[q->rear]=x; } }
D KOTESWARARAO, 173
1.insert 2.remove 3.print 4.exit enter your choice3 35 67 12 1.insert 2.remove 3.print 4.exit enter your choice2 deleted element from queue is 35 1.insert 2.remove
D KOTESWARARAO, 175 Associate Professor, CSE Dept.
1.insert 2.remove 3.print 4.exit enter your choice3 67 12 45 1.insert 2.remove 3.print 4.exit enter your choice4
D KOTESWARARAO, 176 Associate Professor, CSE Dept.
NODEPTR getnode(); void freenode(NODEPTR p); void insert(struct queue *pq,int x); int delete(struct queue *pq);
D KOTESWARARAO, 177 Associate Professor, CSE Dept.
void main() { int ch,x; pq->rear=pq->front=NULL; clrscr(); do { printf("\n1.insert\n2.delete\n3.print\n4.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); insert(pq,x); break; case 2: x=delete(pq); printf("deleted element from list %d",x); break; case 3: print(pq);
D KOTESWARARAO, 178 Associate Professor, CSE Dept.
int delete(struct queue *pq) { int x; NODEPTR p; if(empty(pq)) { printf("queue is underflow"); exit(0); } p=pq->front; x=p->info; pq->front=p->next;
D KOTESWARARAO, 180 Associate Professor, CSE Dept.
void print(struct queue *pq) { NODEPTR q; q=pq->front; if(pq->front==NULL) { printf("QUEUE IS EMPTY"); exit(0); }
D KOTESWARARAO, 181 Associate Professor, CSE Dept.
OUTPUT:
1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element10
D KOTESWARARAO, 184
WEEK-17
D KOTESWARARAO, 185 Associate Professor, CSE Dept.
D KOTESWARARAO, 186
PROGRAM NO 34: Write a C program that uses Stack operations to perform the
following: i) Converting infix expression into postfix expression #include<stdio.h> #include<conio.h> #include<ctype.h>
D KOTESWARARAO, 187
void main() { char infix[MAX]; s->top=-1; clrscr(); printf("enter the infix expression"); gets(infix); intopost(infix); printf("the postfix expression is %s",postfix); getch(); }
int prec(char op) { switch(op) { case '+': case '-': return (10); case '*': case '/': return (20); case '$': return (30);
D KOTESWARARAO, 190 Associate Professor, CSE Dept.
void push(struct stack *s,char a) { if(s->top==MAX-1) printf("stack is full"); else s->item[++s->top]=a; } int pop(struct stack *s) { if(s->top==-1) { printf("stack is underflow"); exit(0); } else return (s->item[s->top--]); } OUTPUT:
D KOTESWARARAO, 191 Associate Professor, CSE Dept.
PROGRAM NO 35: Write a C program that uses Stack operations to perform the
following: ii) Evaluating the postfix expression #include<stdio.h> #include<conio.h> #include<math.h> #define MAXCOLS 100 struct stack { int top; int item[MAXCOLS]; }; double eval(char[]); double pop(struct stack *s); void push(struct stack *s,double n); int empty(struct stack *s); int isdigit(char);
D KOTESWARARAO, 192 Associate Professor, CSE Dept.
double eval(char expr[]) { int c,position; double opnd1,opnd2,value; struct stack *opndstk; opndstk=(struct stack *)malloc(sizeof(struct stack)); opndstk->top=-1; for(position=0;(c=expr[position])!='\0';position++) if(isdigit(c))
D KOTESWARARAO, 193 Associate Professor, CSE Dept.
double oper(int symb,double op1,double op2) { switch(symb) { case '+': return(op1+op2); case '-': return(op1-op2); case '*': return(op1*op2);
D KOTESWARARAO, 194 Associate Professor, CSE Dept.
void push(struct stack *s,double n) { if(s->top==MAXCOLS-1) printf("stack is overflow"); else { s->top++; s->item[s->top]=n; } }
int empty(struct stack *s) { if(s->top==-1) return 1; else return 0; } OUTPUT: enter postfix expression 345+*
the original postfix expression 345+* the result of postfix expression 27.000000
D KOTESWARARAO, 196
WEEK-18
D KOTESWARARAO, 197
D KOTESWARARAO, 198
PROGRAM NO 36: Write a C program that uses functions to perform the following:
i) Creating a Binary Tree of integers ii) Traversing the above binary tree in preorder, inorder and postorder. #include<stdio.h> #include <stdlib.h> #include<conio.h> struct treenode { int ele; struct treenode *l_child, *r_child; }; struct treenode *insert_node(struct treenode *t,int a); void TraverseInorder(struct treenode *t); void TraversePreorder(struct treenode *t); void TraversePostorder(struct treenode *t); /*main function*/ void main() { struct treenode *root_node = NULL; int num,value; int choice; clrscr(); printf("----------------------------------------------------\n"); printf("\t\t\tMENU\n"); printf("-----------------------------------------------------\n"); printf("[1] Create a Binary Tree and Use Inorder Traversal\n"); printf("[2] Create a Binary Tree and Use Preorder Traversal\n"); printf("[3] Create a Binary Tree and Use Postorder Traversal\n"); printf("-----------------------------------------------------\n"); printf("Enter your choice:"); scanf("%d",&choice); if(choice>0 & choice<=3)
D KOTESWARARAO, 199 Associate Professor, CSE Dept.
D KOTESWARARAO, 203
D KOTESWARARAO, 204
WEEK-19
D KOTESWARARAO, 205
D KOTESWARARAO, 206
PROGRAM NO 37: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers : i) Linear search #include<stdio.h> #include<conio.h> int lsearch(int[],int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=lsearch(a,n,s); if(key!=-1) printf("Element found at %d position ",key+1); else
D KOTESWARARAO, 207 Associate Professor, CSE Dept.
PROGRAM NO 38: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers : ii) Binary search /* Binary Search using Non-recursive */ #include<stdio.h> #include<conio.h> int bsearch(int[],int,int); void main()
D KOTESWARARAO, 208 Associate Professor, CSE Dept.
OUTPUT:
Enter the n value 5 enter 5 elements in sorted order 34 45 68 69 76 enter search element69 element found at 4 position
D KOTESWARARAO, 210
PROGRAM NO 39: Write C programs that use both recursive and non recursive functions
to perform the following searching operations for a Key value in a given list of integers :
D KOTESWARARAO, 211
#include<stdio.h> #include<conio.h> int brsearch(int[],int,int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements in sorting order ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=bsearch(a,s,0,n-1); if(key!=-1) printf("Element found at %d position ",key+1); else printf("Element not found "); getch();
D KOTESWARARAO, 212 Associate Professor, CSE Dept.
int bsearch(int x[10],int s,int low,int high) { int mid; if(low>high) return -1; mid=(low+high)/2; if(s==x[mid]) return mid; else if(s<x[mid]) bsearch(x,s,low,mid-1); else bsearch(x,s,mid+1,high);
OUTPUT:
Enter the n value 6 Enter 6 elements in sorting order 12 23 34 45 56 67 Enter search element52 Element not found
D KOTESWARARAO, 213 Associate Professor, CSE Dept.
D KOTESWARARAO, 214
D KOTESWARARAO, 215
WEEK-20
D KOTESWARARAO, 216
PROGRAM NO 40: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: i) Bubble sort #include<stdio.h> #include<conio.h> void bubble(int[],int); void main() {
D KOTESWARARAO, 217 Associate Professor, CSE Dept.
void bubble(int x[10],int n) { int temp,flag=1,i,j; for(i=0;i<n-1&&flag==1;i++) { flag=0; for(j=0;j<n-i-1;j++) { if(x[j]>x[j+1]) { flag=1; temp=x[j];
D KOTESWARARAO, 218 Associate Professor, CSE Dept.
OUTPUT:
D KOTESWARARAO, 219
PROGRAM NO 41: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: ii) Quick sort
D KOTESWARARAO, 220 Associate Professor, CSE Dept.
#include<stdio.h> #include<conio.h> void quick(int a[100],int lb,int ub); int partition(int a[100],int lb,int ub); void main() { int x[100],i,n; clrscr(); printf("Enter how many elements we want"); scanf("%d",&n); printf("Enter %d elements\n ",n); for(i=0;i<n;i++) scanf("%d",&x[i]); clrscr(); printf("Given elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); quick(x,0,n-1); printf("\n Sorted Elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); getch();
D KOTESWARARAO, 221 Associate Professor, CSE Dept.
void quick(int a[100],int lb,int ub) { int i,j; if(lb>=ub) return; j=partition(a,lb,ub); quick(a,lb,j-1); quick(a,j+1,ub); } int partition(int a[100],int lb,int ub) { int s,down,temp,up; s=a[lb]; up=ub; down=lb; while(down<up) { while(a[down]<=s && down<ub) down++; while(a[up]>s) up--;
D KOTESWARARAO, 222 Associate Professor, CSE Dept.
OUTPUT:
D KOTESWARARAO, 223
WEEK-21
D KOTESWARARAO, 224
D KOTESWARARAO, 225
PROGRAM NO 43: Write C programs that implement the following sorting methods to
sort a given list of integers in ascending order: ii) Merge sort #include<stdio.h> #include<conio.h> void mergesort(int,int); void merge(int,int,int); int a[10],b[10]; void main() {
D KOTESWARARAO, 227 Associate Professor, CSE Dept.
void mergesort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; mergesort(low,mid); mergesort(mid+1,high); merge(low,mid,high);
D KOTESWARARAO, 228 Associate Professor, CSE Dept.
D KOTESWARARAO, 230
D KOTESWARARAO, 231
WEEK-22
D KOTESWARARAO, 232
for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated"); scanf("%f", &x); h=arr_x[1]-arr_x[0]; for(i=0; i<=n-1; i++) diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/ for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/ for(i=0; i<=n-j; i++) diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1]; i=0; while(!(arr_x[i]>x)) /* Finding x0 */ i++; i--; p=(x-arr_x[i])/h; y=arr_y[i]; for (k=1; k<=Order_of_diff; k++) { numerator *=p-k+1; denominator *=k; y +=(numerator/denominator)*diff_table[i][k]; } printf("When x=%6.1f, y=%6.2f\n",x, y); getch(); } OUTPUT: Enter the value of n 4 Enter the values of x and y 12 23 34 45 56 Enter the value of x at which value of y is to be calculated2 When x= 2.0, y= 3.00
D KOTESWARARAO, 235 Associate Professor, CSE Dept.
D KOTESWARARAO, 236
WEEK-23
D KOTESWARARAO, 237 Associate Professor, CSE Dept.
D KOTESWARARAO, 238
PROGRAM NO 46: Write C programs to implement the linear regression and polynomial
regression algorithms. #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n);
D KOTESWARARAO, 239 Associate Professor, CSE Dept.
WEEK-24
D KOTESWARARAO, 241 Associate Professor, CSE Dept.
D KOTESWARARAO, 242
D KOTESWARARAO, 255
D KOTESWARARAO, 256
D KOTESWARARAO, 257
C - Language Viva
1) What is the computer? Ans: A computer is an electronic device, processing the data according to set of instructions, and produce the results, may be store in the system.
2) What is the CPU? Ans: CPU stands for Central Processing Unit, Responsible for controlling all activities of the computer system.
4) What are the output devices? Ans: printer, monitor, speaker etc.
5) What is the ALU? Ans: ALU stands for Arithmetic and Logic unit. To perform the Arithmetic and Logic operations.
8) What is the algorithm? Ans: an algorithm is a step by step procedure to solve the problem.
9) What is the Flowchart? Ans: A graphical representation of the algorithm is called a flowchart.
10) What are the program development steps? Ans: a) problem definition b) Problem design c) Coding d) Debugging e) Maintenance
13) What are the features of C language? Ans: a) C is a robust Associate Professor, CSE Dept.
D KOTESWARARAO, 259
14) What are the applications of C language? Ans: System Software like Compiles, interpreter, spread sheets, database ets Application software and Games.
15) What is the identifier? Ans: identifiers refers to name of the variables, functions, and arrays. These are user defined names.
16) What is the keyword? Ans: keywords have special meaning that is define by the system.
17) What is the variable? Ans: variable is date name may be stores the data values, it refers to the memory location.
18) What are the rules to create the variables? Ans : a) Variable name must begin with a letter or underscore. b) Variable consists only letters, digits, underscore. c) The variable should not be a keyword. d) White spaces is not allowed. D KOTESWARARAO, 260 Associate Professor, CSE Dept.
19) What is the Data Type? Ans: Data type specifies to the system which type of values to be stores and how much memory to be allocated to the variable.
20) What are the basic data types? Ans: int, char, float, double.
21) What is the size of the each basic data types? Ans: int 2 Bytes, char 1 Bytes, float 4 Bytes, double 8 Bytes.
22) What is the constant? Ans: Constant is fixed value does not change during the execution of the program.
23) What is the integer constant? Ans: An integer constant refers to a sequence of digits.
24) What is the character constant? Ans: A single character constant contains a single character enclosed within pair of single quote marks. 25) What is the real constant? A number followed by a decimal point and fractional part.
26) What is the string constant? Ans: A string constant is a sequence of character enclosed in double quotes.
27) What is the expression? D KOTESWARARAO, 261 Associate Professor, CSE Dept.
28) What is the operator? Ans: Operator is symbol that tells to the computer to perform certain operation on the operand.
30) What is the sizeof()? Ans: The operator is used to find out the how many bytes allocated for variable, constant or data type.
32) What is the pre increment and post increment? Ans: preincrement means first perform he increment after then perform the operation. postincrement means first perform he operations after then perform the increment.
33) What is the pre decrement and post decrement? Ans: predecrement means first perform he decrement after then perform the operation. postdecrement means first perform he operations after then perform the decrement.
34) What is the header file? Ans: header file contains several built in functions.
35) What is the stdio.h? D KOTESWARARAO, 262 Associate Professor, CSE Dept.
36) What is the conio.h? Ans: console input and output header file.
37) What is the standard input and output function? Ans: scanf(), printf().
38) What meant by formatted input and output functions? Ans: formatted input output functions are read or write (print) any type of data values.
39) What are the formatted input and output functions? Ans: scanf(), printf().
40) What are unformatted input and output functions? Ans: unformatted input output functions are used to read or write only characters. Ex: getchar(),getch(),gets(),putchar(),puts().. etc.
41) What is the Block? Ans: a block is a group of statement into a single unit.
42) What are the conditional statements? Ans: a) simple if, b) if-else, c) nested if else d) else if ladder e) switch case.
43) What is the difference between else-if ladder and switch case? D KOTESWARARAO, 263 Associate Professor, CSE Dept.
44) What is the loop? Ans: loop is a block of statements which are repeatedly executed for certain number of times.
45) How many loops available in C language? Ans: 3, while, do-while, for.
46) What is the difference between while and do-while? Ans: while is the pretest condition, do-while is the posttest condition. In do-while the statements will be executed at least once.
47) What is the break statement? Ans: exit from the block.
48) What is continue statement? Ans: when execute the continue statement skip the next statement and control goes to next iteration.
49) What is the need of the exit? Ans: exit from the program.
50) What is the array? Ans: Array is a group of related data items that share a common name. (or) array is a collection of homogenous elemements.
D KOTESWARARAO, 264
52) What is the index? ans: index a variable. it can be used to access the element in the array.
54) What is the function? Ans: function is a self contained block, it perform the particular task.
55) What are the advantages of the function? Ans: a) reduce the length of the program. b) easy to understand c) easy to debug d) easy to maintain e) reusability.
56) What is meant by actual arguments?? Ans: which are the arguments passed in the calling function is called actual arguments.
D KOTESWARARAO, 265
58) What is the prototype? Ans: function prototypes tells the compiler the number and type of arguments that are to be passed to function and the type of value that is to be returned by the function.
60) What meant by storage classes? Ans: storage classes specifies the scope and life time of the variable.
61) How many storage classes available in C language? ans: 4, auto, extern, static, register.
62) What is the difference between auto and extern? Ans: auto means the variables access within that function, extern means the variable access throughout the program.
63) What is the recursive function? Ans: A function call itself is called a recursive function.
64) What is the Pointer? Ans: Pointer is a variable which stores the address of the another variable.
D KOTESWARARAO, 266
66) How to allocate dynamic memory for the variable? Ans: malloc()
67) How to delete dynamic memory for the variable? Ans: free()
68) What is the Structure? Ans: A Structure is a collection of one or more variables, possibly of different data types.
69) What is the difference between array and structure? Ans: Array is a collection of similar data items but structure is a collection of different data items.
70) How to access the structure variable? Ans: structure variable can be accessing using . or -> operator.
71) What is the union? Ans: Union is a collection of one or more variables, possibly of different data types.
D KOTESWARARAO, 267
73) What is the preprocessor? Ans: preprocessor is program to process the data before pass to the compiler.
74) What is the need of the preprocessor? Ans: fast execution of the program.
76) What is the file? Ans: A file is collection of records. The main purpose of the file is to store the data permanently.
78) What are the modes to be used in the files? Ans: r read, w write , a append.
79) What is the compiler? Ans: compiler is program it translates the source program into object program.
80) What interpreter? Ans: Interpreter is program it translates the source program into object program. D KOTESWARARAO, 268 Associate Professor, CSE Dept.
81) What is the difference between compiler and interpreter? Ans: compiler translate the source program at a time but the interpreter translate line by line.
82) What is the bit? Ans: bit is a binary digit. Example: o and 1.
84) What is the operating system? Ans: The system software that controls the computer.
85) What is the software? Ans: Software is the set of programs and applications that tell the computer what to do.
86) What is the hardware? Ans: Hardware is physical component of a system, basically anything that you can touch with your fingers.
88) What is a Stack? Ans: Stack is a collection of ordered items in which insertion and deletion can be performed at one end is called as top of the stack. D KOTESWARARAO, 269 Associate Professor, CSE Dept.
89) What is a Queue? Ans: Queue is a collection of ordered items in which insertion can be performed at one end is called as rear and deletion can be performed other end is called as front.
90) What are the operations performed on Stacks? Ans: push and pop
91) what are the operations performed on Queue? Ans: insertion and Deletion.
92) What is Push()? Ans: Insert the element at the top of the stack
93) What is Pop()? Ans:Delete the top element at the top of the stack.
94) what are the applications of Stack? Ans: a) Infix to Postfix conversion b) Postfix Evaluation c) Paranthesis matching d) Function calls e)recurssion
95) What are the applciations of Queue? ans: simulation b) CPU scheduling c) Client/server systems d) page replacement algorithms.
D KOTESWARARAO, 270
97) What ia an Abstract Data Type ? Ans: abstract data type is a mathematical model of data structures.
98) what is Linked list? Ans: Linked list is a collection of nodes each node having link with another node(s).
99) What is Single Linked list? Ans: Single linked list is a collection of nodes, each node having two fields one is info, another one is next field.
100) What is Double Linked List? Ans:Double linked list is a collection of nodes, each node having three fields one is info, another two are next and prev fields.
101) what is the drawback of Single Linked list? Ans: only forward accessing, it is not possible to accessing backward.
102) What is the difference between array and linked list? Ans: array is a static, linked list is a dynamic
103) what are the types of Data structures? Ans: 1.Linear Data Structures 2. Non-Linear Data Structures.
104) What is mean by searching? D KOTESWARARAO, 271 Associate Professor, CSE Dept.
105) what is meant by sorting? Ans: sorting means arrange the elements in particular order.
106) What is Space complexity? Ans: It is defined as the amount of memory need to run to completion by an program. 107) what is Time complexity? Ans: It is defined as the amount of Computer time need to run to completion by an program. 108) what is Time complexity of Linear search? Ans: Best Case: O(1) Average Case: O(n) Worst Case: O(n)
109) what is Time complexity of Binary search? Ans: Best Case:O(1) Average Case:O(logn) Worst Case:O(logn)
110) what is Time complexity of Quick sort? Ans: Best Case:O(nlogn) Average Case:O(lnogn) Worst Case:O(n2)
111) what is Time complexity of Merge sort? D KOTESWARARAO, 272 Associate Professor, CSE Dept.
114) what is Infix expression? Ans: The operator between two operands.
115) what is Postfix expression? Ans: The operator after two operands
117) What is Prefix expression? Ans: The operator precending two operands
ROM - acronym for Read Only Memory; memory that can only be read from and not written to.
UPS - acronym for Uninterruptible Power Source
D KOTESWARARAO, 273
D KOTESWARARAO, 274
D KOTESWARARAO
Phone No:
9441544277
Email
Id:d_koteswararao@yahoo.co.in
ThankQ
D KOTESWARARAO, 275 Associate Professor, CSE Dept.
***************************
D KOTESWARARAO, 276