Professional Documents
Culture Documents
I B-TECH
D KOTESWARARAO, 2
D KOTESWARARAO, 3
INDEX
S.No. week 1.a) 33 34 c) Prime Numers between 1 to N week 2. a) Evaluate the expression 39 b) 40 week 3.a) 43 b) 44 45 d) 46 GCD of Two numbers using Non-Recursive Factorial of Given Number using Non-Recursive Factorial of Given Number using Recursive Roots of Quadratic equations 35 NAME OF THE PROGRAM Sum of Digits of Given Number PAGE No.
b) Fibonacci Series
D KOTESWARARAO, 4
Pyrmid of Numbers
D KOTESWARARAO, 5
week22 a) To implement the Lagrange Interpolation 171 b) To implement the Newton-Gregory Forward Interpolation 172 week 23 To implement the Linear Regression 177 week 24 a) To implement Simpson Method 181 b) To implement Tr apezoidal Method 187
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, 28
b)
if else statement:
c)
nested if-else statement: The general form of the nested if-else statement is:
if(expression1) {
D KOTESWARARAO, 30 Associate Professor, CSE Dept.
d)
else if ladder statement: The general form of the else if ladder statement is:
if (expression1) { Statement-1;
D KOTESWARARAO, 31 Associate Professor, CSE Dept.
e)
switch statement:
switch (expression) { case constant1: Statement(s) Break; case constant2: Statement(s) Break; .. .. case constantn: constant(s)
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) {
D KOTESWARARAO, 35
do {
Statement-block;
} while(expression);
6) Break statement
break;
7) Continue statement
8) goto statement
data_type array_name[size];
7) Function definition:
return_type function_name(argument_list) {
}
D KOTESWARARAO, 37 Associate Professor, CSE Dept.
8) Function prototype:
return_type function_name(argument_list);
9) Structure:
} structure_variables;
10) Union:
D KOTESWARARAO, 38
`} union variables;
data_type *variable_name;
D KOTESWARARAO, 39
WEEK-1
D KOTESWARARAO, 40
a) Write a C program to find the sum of individual digits of a positive integer. #include<stdio.h> #include<conio.h> void main() { int n,r,sum=0;
D KOTESWARARAO, 41 Associate Professor, CSE Dept.
D KOTESWARARAO, 42
c) 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;
D KOTESWARARAO, 44
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
b) Write a C program to find the roots of a quadratic equation. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d,x1,x2,xr1,xr2,xi1,xi2; clrscr(); printf("enter a,b and c values"); scanf("%d%d%d",&a,&b,&c); d=b*b-4*a*c; if(d>0) {
D KOTESWARARAO, 50 Associate Professor, CSE Dept.
WEEK-3
D KOTESWARARAO, 52
D KOTESWARARAO, 53
OUTPUT:
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);
D KOTESWARARAO, 55 Associate Professor, CSE Dept.
Factorial of 6 is 720
D KOTESWARARAO, 56
#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) { int r; r=a%b; while(r!=0) { a=b; b=r; r=a%b;
D KOTESWARARAO, 57 Associate Professor, CSE Dept.
OUTPUT: Enter any Two Numbers 136 48 GCD of the given two numbers 8
d) 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);
D KOTESWARARAO, 58 Associate Professor, CSE Dept.
OUTPUT: enter any two numbers 34 21 gcd of the given two numbers 1
D KOTESWARARAO, 59
#include<stdio.h> #include<conio.h> void towers(int,char,char,char); void main() { int n; clrscr(); printf("enter n value"); scanf("%d",&n); towers(n,'A','B','C'); } void towers(int n,char frompeg,char topeg,char auxpeg) { if(n==1) { printf("\n%s%c%s%c","move disk 1 from peg ",frompeg," to peg ",topeg); return; } towers(n-1,frompeg,auxpeg,topeg); printf("\n%s%d%s%c%s%c","move disk ",n," from peg ",frompeg," to peg ",topeg); towers(n-1,auxpeg,topeg,frompeg);
D KOTESWARARAO, 60 Associate Professor, CSE Dept.
move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B move disk 3 from peg A to peg C move disk 1 from peg B to peg A move disk 2 from peg B to peg C move disk 1 from peg A to peg C move disk 4 from peg A to peg B move disk 1 from peg C to peg B move disk 2 from peg C to peg A move disk 1 from peg B to peg A move disk 3 from peg C to peg B move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B
D KOTESWARARAO, 61
D KOTESWARARAO, 62
WEEK-4
D KOTESWARARAO, 63
a) The total distance travelled by vehicle in t seconds is given by distance = ut+1/2at2 where u and a are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of u and a. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of u and a. #include <stdio.h> #include <math.h> void main() {
D KOTESWARARAO, 64 Associate Professor, CSE Dept.
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); }
D KOTESWARARAO, 66
b) 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) #include<stdio.h> #include<conio.h> void main() { int a,b; char ch; clrscr(); printf("enter a and b values"); scanf("%d%d",&a,&b); printf("enter the any operator(+,-,*,/)"); fflush(stdin); scanf("%c",&ch); switch(ch) { case '+': printf("Addition of two numbers : %d",a+b); break;
D KOTESWARARAO, 67 Associate Professor, CSE Dept.
OUTPUT: enter a and b values15 3 enter the any operator(+,-,*,/)* Multiplication of two numbers : 45
D KOTESWARARAO, 68
D KOTESWARARAO, 69
WEEK-5
D KOTESWARARAO, 70
a) 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);
D KOTESWARARAO, 71 Associate Professor, CSE Dept.
D KOTESWARARAO, 73
b) Write a Program to do Matrix Addition using Function #include<stdio.h> #include<conio.h> void add(int [10][10],int [10][10],int,int); int i,j; void main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; clrscr(); printf("enter first matrix order\n"); scanf("%d%d",&r1,&c1); printf("enter second matrix order\n"); scanf("%d%d",&r2,&c2); printf("enter First Matrix elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&a[i][j]); printf("enter Second Matrix elements\n");
D KOTESWARARAO, 74 Associate Professor, CSE Dept.
OUTPUT: enter first matrix order 23 enter second matrix order 23 enter First Matrix elements 123 654 enter Second Matrix elements 678 325 Addition of two matrix is 7 9 11 9 7 9
D KOTESWARARAO, 76
#include<stdio.h> #include<conio.h>
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);
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:
D KOTESWARARAO, 79
D KOTESWARARAO, 80
D KOTESWARARAO, 81
D KOTESWARARAO, 82
WEEK-6
D KOTESWARARAO, 83
a) 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(); }
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 b) Write a C program that uses functions to perform the following operations: 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();
D KOTESWARARAO, 85 Associate Professor, CSE Dept.
OUTPUT:
enter the main string koteswararao enter the position 7 enter how many character we want delete
D KOTESWARARAO, 86 Associate Professor, CSE Dept.
c) Write a C program to determine if the given string is a palindrome or not #include<conio.h> #include<stdio.h> #include<string.h> void main() { char str1[20],str2[20]; int d; clrscr(); printf("Enter string\n"); scanf("%s",str1); strcpy(str2,str1); strrev(str2); if(!strcmp(str1,str2)) printf("\n String is palindrome "); else printf("\n String is not palindrome "); getch();
D KOTESWARARAO, 87 Associate Professor, CSE Dept.
OUTPUT:
String is palindrome
D KOTESWARARAO, 88
D KOTESWARARAO, 89
WEEK-7
D KOTESWARARAO, 90
a) 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;
D KOTESWARARAO, 91 Associate Professor, CSE Dept.
b) 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");
D KOTESWARARAO, 92 Associate Professor, CSE Dept.
: %d",nwords); : %d",nlines);
D KOTESWARARAO, 94
D KOTESWARARAO, 95
WEEK-8
D KOTESWARARAO, 96 Associate Professor, CSE Dept.
D KOTESWARARAO, 97
a) Write a C program to generate Pascals triangle. #include<stdio.h> #include<conio.h> void main() { int i,j,n,k; static int p[10][10]; clrscr(); printf("\n Pascals Triangle size n="); scanf("%d",&n); printf("\n Pascal's triangle:\n"); for(i=0;i<n;i++) { for(k=0;k<=30-i*2;k++) printf(" "); for(j=0;j<=i;j++) {
D KOTESWARARAO, 98 Associate Professor, CSE Dept.
D KOTESWARARAO, 100
WEEK-9
D KOTESWARARAO, 101
D KOTESWARARAO, 102
Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+.+xn For example: if n is 3 and x is 5, then the program computes 1+5+25+125. Print x, n, the sum Perform error checking. For example, the formula does not make sense for negative exponents if n is less than 0. Have your program print an error message if n<0,> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int x,n,sum=1,i; clrscr(); printf("enter x and n values\n"); scanf("%d%d",&x,&n); while(n<0) { printf("Exponent value is negative please enter x and n values"); scanf("%d%d",&x,&n); } for(i=1;i<=n;i++) sum=sum+pow(x,i); printf("Sum=%d",sum); } getch(); } OUTPUT: enter x and n values 35 Sum=364
D KOTESWARARAO, 103 Associate Professor, CSE Dept.
D KOTESWARARAO, 104
WEEK-10
D KOTESWARARAO, 105
D KOTESWARARAO, 106
a) 2s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. Thus 2s complement of 11100 is 00100. Write a C program to find the 2s complement of a binary number. #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:
D KOTESWARARAO, 107 Associate Professor, CSE Dept.
b) Write a C program to convert a Roman numeral to its decimal equivalent. #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int *a,len,i,j,k; char *rom; clrscr(); printf("Enter the Roman Numeral:"); scanf("%s",rom); len=strlen(rom); for(i=0;i<len;i++) { if(rom[i]=='I') a[i]=1; else if(rom[i]=='V') a[i]=5; else if(rom[i]=='X') a[i]=10; else if(rom[i]=='L') a[i]=50; else if(rom[i]=='C') a[i]=100; else if(rom[i]=='D') a[i]=500; else if(rom[i]=='M') a[i]=1000; else
D KOTESWARARAO, 108 Associate Professor, CSE Dept.
D KOTESWARARAO, 109
D KOTESWARARAO, 110
WEEK-11
D KOTESWARARAO, 111
D KOTESWARARAO, 112
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 (Note: represent complex number using a structure.) #include<stdio.h> #include<conio.h> #include<math.h> typedef struct { float real,image; }complex;
void print(complex); complex add(complex,complex); complex sub(complex,complex); complex mul(complex,complex); complex div(complex,complex);
void main() {
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, 118
WEEK-12
D KOTESWARARAO, 119
D KOTESWARARAO, 120
a) Write a C program which copies one file to another. #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch; fp1=fopen("first.txt","r"); fp2=fopen("second.txt","w"); if(fp1==NULL) { printf("first.txt file unable to open"); exit(0); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }
D KOTESWARARAO, 121
b) Write a C program to reverse the first n characters in a file. (Note: The file name and n are specified on the command line.)*/
void main(int argc, char *argv[]) { char a[15]; char s[20]; char n; int k; int j=0; int i; int len; FILE *fp;
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); }
D KOTESWARARAO, 123 Associate Professor, CSE Dept.
k=*argv[2]-48; n = fread(a,1,k,fp); a[n]='\0'; len=strlen(a); for(i=len-1;i>=0;i--) { s[j]=a[i]; printf("%c",s[j]); j=j+1; } s[j+1]='\0'; getch(); }
D KOTESWARARAO, 124
WEEK-13
D KOTESWARARAO, 125
b) Write a c Programme to merge two files into a third file(i.e. the contents of the first file
followed by those of the second are put in the third file) #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2,*fp3; char ch; fp1=fopen("first.txt","r"); fp3=fopen("third.txt","w"); while((ch=getc(fp1))!=EOF) putc(ch,fp3); fclose(fp1); fclose(fp3); fp2=fopen("second.txt","r"); fp3=fopen("third.txt","a"); while((ch=getc(fp2))!=EOF) putc(ch,fp3); fclose(fp2); fclose(fp3); }
D KOTESWARARAO, 127 Associate Professor, CSE Dept.
D KOTESWARARAO, 128
WEEK-14
D KOTESWARARAO, 129
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);
D KOTESWARARAO, 130 Associate Professor, CSE Dept.
D KOTESWARARAO, 137
D KOTESWARARAO, 138
WEEK-15
D KOTESWARARAO, 139
1.push 2.pop
D KOTESWARARAO, 144 Associate Professor, CSE Dept.
1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 35 | ---| 15 | ---1.push 2.pop 3.print 4.exit enter your option1 enter element
D KOTESWARARAO, 145 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 4.exit enter your option2
b) Write C programs that implement stack (its operations) using ii) Pointers #include<stdio.h> #include<conio.h>
D KOTESWARARAO, 147
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"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element");
D KOTESWARARAO, 148 Associate Professor, CSE Dept.
void freenode(NODEPTR p) {
D KOTESWARARAO, 149 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; }
void print(stack s) {
D KOTESWARARAO, 151 Associate Professor, CSE Dept.
1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element30
1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice3 ---|NULL| ---| 30 | ---| 20 | ---1.PUSH
D KOTESWARARAO, 153 Associate Professor, CSE Dept.
D KOTESWARARAO, 154
D KOTESWARARAO, 155
WEEK-16
D KOTESWARARAO, 156 Associate Professor, CSE Dept.
D KOTESWARARAO, 157
a) 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); void print(struct queue *q); int empty(struct queue *q); void main() {
D KOTESWARARAO, 158 Associate Professor, CSE Dept.
D KOTESWARARAO, 160
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 3.print 4.exit enter your choice3 67 12
D KOTESWARARAO, 163 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 b) Write C programs that implement Queue (its operations) using ii) Pointers #include<stdio.h> #include<conio.h>
D KOTESWARARAO, 164
NODEPTR getnode(); void freenode(NODEPTR p); void insert(struct queue *pq,int x); int delete(struct queue *pq); int empty(struct queue *pq); void print(struct queue *pq);
void main()
D KOTESWARARAO, 165 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; if(pq->front==NULL) pq->rear=NULL; freenode(p); return x;
D KOTESWARARAO, 168 Associate Professor, CSE Dept.
void print(struct queue *pq) { NODEPTR q; q=pq->front; if(pq->front==NULL) { printf("QUEUE IS EMPTY"); exit(0); }
while(q!=NULL) { printf("%2d->",q->info);
D KOTESWARARAO, 169 Associate Professor, CSE Dept.
OUTPUT:
1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element10
1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element20 1.insert 2.delete 3.print 4.exit
D KOTESWARARAO, 170 Associate Professor, CSE Dept.
D KOTESWARARAO, 172
WEEK-17
D KOTESWARARAO, 173 Associate Professor, CSE Dept.
D KOTESWARARAO, 174
a) 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>
struct stack *s; char postfix[MAX]; void push(struct stack *s,char a);
D KOTESWARARAO, 175 Associate Professor, CSE Dept.
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); case '(': return (2); }
D KOTESWARARAO, 178 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: enter the infix expressionA*(B+C)-D the postfix expression is ABC+*D-
D KOTESWARARAO, 179
b) 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); double oper(int,double,double); void main() {
D KOTESWARARAO, 180
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)) push(opndstk,(double)(c-'0')); else {
D KOTESWARARAO, 181 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); case '/': return(op1/op2); case '$': return(pow(op1,op2)); default: printf("\nillegal operation ");
D KOTESWARARAO, 182 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; } }
double pop(struct stack *s) { if(!empty(s)) return (s->item[s->top--]); else { printf("etack is underflow"); exit(0);
D KOTESWARARAO, 183 Associate Professor, CSE Dept.
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, 184
WEEK-18
D KOTESWARARAO, 185
D KOTESWARARAO, 186
OUTPUT:
b) write a c program that implements the following sorting methods to sort a given list of integers in ascending order. ii) Selection sort
#include<stdio.h> #include<conio.h> void selection(int[],int);
D KOTESWARARAO, 188
getch(); }
void selection(int x[10],int n) { int i,index,j,large; for(i=n-1;i>0;i--) { large=x[0]; index=0; D KOTESWARARAO, 189 Associate Professor, CSE Dept.
D KOTESWARARAO, 190
D KOTESWARARAO, 191
D KOTESWARARAO, 192
WEEK-19
D KOTESWARARAO, 193
D KOTESWARARAO, 196
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, 199
D KOTESWARARAO, 200
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 element 45 Element found at 4 position
D KOTESWARARAO, 202 Associate Professor, CSE Dept.
D KOTESWARARAO, 203
WEEK-20
D KOTESWARARAO, 204
Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Quick sort #include<stdio.h> #include<conio.h> void quick(int a[100],int lb,int ub);
D KOTESWARARAO, 205 Associate Professor, CSE Dept.
OUTPUT:
D KOTESWARARAO, 208
WEEK-21
D KOTESWARARAO, 209
Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Merge sort #include<stdio.h>
D KOTESWARARAO, 210 Associate Professor, CSE Dept.
D KOTESWARARAO, 213
WEEK-22
D KOTESWARARAO, 214
a) Write C programs to implement the Lagrange interpolation #include<stdio.h> #include<conio.h> #define MaxN 90 void main() { float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0;
D KOTESWARARAO, 215 Associate Professor, CSE Dept.
D KOTESWARARAO, 218
D KOTESWARARAO, 219
WEEK-23
D KOTESWARARAO, 220
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);
D KOTESWARARAO, 221 Associate Professor, CSE Dept.
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); printf("Enter the values of x and y:\n"); for(i=0;i<n;i++) scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i<n;i++) sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy); printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1) { reg_coff_xy=corr_coff*(sx/sy); printf("\nThe value of linear regression coefficient is %f",reg_coff_xy); } else if(strcmp(type_coff,"y on x")==1) { reg_coff_yx=corr_coff*(sy/sx); printf("\nThe value of linear regression coefficient is %f",reg_coff_yx); } else printf("\nEnter the correct type of regression coefficient."); getch(); }
D KOTESWARARAO, 222 Associate Professor, CSE Dept.
D KOTESWARARAO, 223
WEEK-24
D KOTESWARARAO, 224
a) Write C program to implement Simpson method. #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80];
D KOTESWARARAO, 225 Associate Professor, CSE Dept.
D KOTESWARARAO, 237
D KOTESWARARAO, 238
D KOTESWARARAO, 239
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.
3) What are the input devices? D KOTESWARARAO, 240 Associate Professor, CSE Dept.
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.
6) What is the CU? Ans: CU stands for Control unit, to control the all functionality of the system.
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 D KOTESWARARAO, 241 Associate Professor, CSE Dept.
13) What are the features of C language? Ans: a) C is a robust b) C is a middle level language c) C is a Structured Programming language d) C is a portable e) C is easily extendable f) C program is efficient and fast g) C have only 32 keywords.
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.
D KOTESWARARAO, 242
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.
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? D KOTESWARARAO, 243 Associate Professor, CSE Dept.
26) What is the string constant? Ans: A string constant is a sequence of character enclosed in double quotes.
27) What is the expression? Ans: An expression is a combination of variables, constants, and operators arranged as per the syntax of the language.
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.
D KOTESWARARAO, 244
34) What is the header file? Ans: header file contains several built in functions.
35) What is the stdio.h? Ans: Standard input output header file. It provides standard input and output functions.
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. D KOTESWARARAO, 245 Associate Professor, CSE Dept.
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? Ans: switch case reduce the complexity of the program.
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. D KOTESWARARAO, 246 Associate Professor, CSE Dept.
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.
51) What is the difference between array and ordinary variable? Ans: ordinary variable can store one value at a time but array variable can stores many values at a time.
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 D KOTESWARARAO, 247 Associate Professor, CSE Dept.
56) What is meant by actual arguments?? Ans: which are the arguments passed in the calling function is called actual arguments.
57) What is meant formal arguments? Ans: which are the arguments declared in the called function is called actual arguments.
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.
D KOTESWARARAO, 248
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.
65) What are the advantages of the Pointer? Ans: a) pointers are more efficient in handling the data tables. b) pointers reduce the length and complexity of the program c) Increasing the execution speed of the program d) dynamic memory allocations e) easily handling the data structures
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? D KOTESWARARAO, 249 Associate Professor, CSE Dept.
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.
72) What is the difference between structure and union? Ans: main difference between structure and union is allocation of the memory. In union all members of a union use the same memory location but the structure using different locations.
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? D KOTESWARARAO, 250 Associate Professor, CSE Dept.
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.
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? D KOTESWARARAO, 251 Associate Professor, CSE Dept.
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.
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.
D KOTESWARARAO, 252
95) What are the applciations of Queue? ans: simulation b) CPU scheduling c) Client/server systems d) page replacement algorithms.
96) What is a Data structures? Ans: Data structures specifies way of organizes the data in memory.
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. D KOTESWARARAO, 253 Associate Professor, CSE Dept.
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? Ans: searchin means to find the element is present or not in the list
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) D KOTESWARARAO, 254 Associate Professor, CSE Dept.
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? Ans: Best Case: O(lnogn) Average Case: O(lnogn) Worst Case: O(lnogn)
114) what is Infix expression? Ans: The operator between two operands.
115) what is Postfix expression? Ans: The operator after two operands
D KOTESWARARAO, 255
ROM - acronym for Read Only Memory; memory that can only be read from and not written to.
UPS - acronym for Uninterruptible Power Source
Objectives: To make the student learn a programming language. To teach the student to write programs in C solve the problems To Introduce the student to simple linear and non linear data structures such as lists, stacks, queues, trees and graphs. Text Books 1. C programming and Data Structures, P. Padmanabham, Third Edition, BS Publications 2. Data Structures: A pseudo code approach with C, second edition R.F. Gilberg and B.A. Forouzan 3. Programming in C, P.Dey & M. Ghosh, Oxford Univ.Press. 4. C and Data Structures, E Balaguruswamy, TMH publications
D KOTESWARARAO, 256
D KOTESWARARAO
D KOTESWARARAO, 257
Phone No:
9441544277
Email
Id:d_koteswararao@yahoo.co.in
ThankQ ***************************
D KOTESWARARAO, 258