UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
|
|
- April Martin
- 6 years ago
- Views:
Transcription
1 UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
2 Table of Contents 1. C Program to Check for balanced parenthesis by using Stacks Program for items implementation of stack using local stack instance & without dynamic memory allocation Program for evaluation of postfix expression Infix to Postfix conversion with support for parenthesis Stack Implementation with dynamic memory allocation for container array Recursion : Factorial Recursion : Binary Search Recursion : nth Fibonacci number...21
3 1. C Program to Check for balanced parenthesis by using Stacks #include <stdio.h> #include <string.h> #define TRUE 1 #define FALSE 0 int getch() return 0; #define STACKSIZE 10 / The Stack defintion used by the program struct Stack int top; int items[stacksize]; ; / Stack is full when top is equal to the last index int full(struct Stack* stack) return stack->top == STACKSIZE - 1; / Stack is empty when top is equal to -1 int empty(struct Stack* stack) return stack->top == -1; / Function to add an item to stack. It increases top by 1 void push(struct Stack* stack, int item) if (full(stack)) return; stack->items[++stack->top] = item; / Function to remove an item from stack. It decreases top by 1 int pop(struct Stack* stack) if (empty(stack))
4 printf("stack empty\n"); return -1000; return stack->items[stack->top--]; int stacktop(struct Stack* stack) if (empty(stack)) printf("stack empty\n"); return -1000; return stack->items[stack->top]; int main() int i; char expr[30], a; printf("enter the expression:\n"); scanf("%s",expr); struct Stack s; int valid = TRUE; for (i = 0; i < strlen(expr); i++) if ((expr[i] == '(') (expr[i] == '') (expr[i] == '[')) push(&s,expr[i]); if ((expr[i] == ')') (expr[i] == '') (expr[i] == ']')) if (empty(&s)) valid =FALSE;
5 a = pop(&s); char b = (a =='('?')':a ==''?'':']'); if(b!= expr[i]) valid = FALSE; if (empty(&s)) printf("stack not empty\n"); valid = FALSE; if (valid) printf("%s is valid!!",expr); printf("%s is not valid.!!",expr); getch();
6 2. Program for items implementation of stack using local stack instance & without dynamic memory allocation. #include <stdio.h> #include <stdlib.h> #include <limits.h> #define STACKSIZE 10 / A structure to represent a stack struct Stack int top; int items[stacksize]; ; / Stack is full when top is equal to the last index int full(struct Stack* stack) return stack->top == STACKSIZE - 1; / Stack is empty when top is equal to -1 int empty(struct Stack* stack) return stack->top == -1; / Function to add an item to stack. It increases top by 1 void push(struct Stack* stack, int item) if (full(stack)) return; stack->items[++stack->top] = item; printf("%d pushed to stack\n", item); / Function to remove an item from stack. It decreases top by 1 int pop(struct Stack* stack) if (empty(stack)) printf("stack empty\n"); return -1000;
7 return stack->items[stack->top--]; int stacktop(struct Stack* stack) if (empty(stack)) printf("stack empty\n"); return -1000; return stack->items[stack->top]; / Not part of standard stack specification. Use for our verification purpose. void display (struct Stack* s) int i; if (s->top == -1) printf ("Stack is empty\n"); return; printf ("\ncurrent contents of the stack is \n"); for (i = s->top; i >= 0; i--) printf ("%d\n", s->items[i]); printf ("\n"); / Driver program to test above functions int main() struct Stack st; st.top = -1; push(&st, 10);
8 push(&st, 20); push(&st, 40); push(&st, 10); push(&st, 20); push(&st, 40); printf("%d is at stack top\n", stacktop(&st)); display(&st); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); display(&st); return 0;
9 3. Program for evaluation of postfix expression // Sample input : 63*5+ (A postfix expression for 6*3+5 ) / Output : 23 #include<stdio.h> #include<ctype.h> / A minimal stack implementation specifically for this program. / Not recommended generally. int stack[20]; int top = -1; void push(int x) stack[++top] = x; int pop() return stack[top--]; // Stack ends int eval(int n1, int n2,char op) int result; switch(op) case '+': result = n1 + n2; break; case '-': result = n2 - n1; break; case '*': result = n1 * n2; break;
10 case '/': result = n2 / n1; break; return result; int main() char exp[20]; char *e; int n1,n2,n3,num; printf("enter the expression:: "); scanf("%s",exp); e = exp; while(*e!= '\0') if(isdigit(*e)) / E.g:Converting character '6' to number 6 using ASCII values num = *e - 48; // 48 => ASCII value of '0' push(num); n1 = pop(); n2 = pop(); n3 = eval(n1,n2,*e); push(n3); e++; printf("\nthe result of expression %s = %d\n\n",exp,pop()); return 0;
11 4.Infix to Postfix conversion with support for parenthesis. #include<stdio.h> #include<ctype.h> / A minimal stack implementation specifically for this program. / Not recommended generally. char stack[20]; int top = -1; void push(char x) stack[++top] = x; char pop() if(top == -1) return -1; return stack[top--]; int priority(char x) if(x == '(') return 0; if(x == '+' x == '-') return 1; if(x == '*' x == '/') return 2; return 0; int main() char exp[20]; char *e, x; printf("enter the expression :: "); scanf("%s",exp); e = exp;
12 while(*e!= '\0') if(isalnum(*e)) printf("%c",*e); if(*e == '(') push(*e); if(*e == ')') while((x = pop())!= '(') printf("%c", x); while(priority(stack[top]) >= priority(*e)) printf("%c",pop()); push(*e); e++; while(top!= -1) printf("%c",pop()); return 0;
13 5.Stack Implementation with dynamic memory allocation for container array. // Stack Implementation with dynamic memory allocation for container array. #include <stdio.h> #include <stdlib.h> // A structure to represent a stack struct Stack int *items; int top; int maxsize; ; // Initializing the stack void initstatck(struct Stack *ptr,int mx) ptr->top = -1; ptr->maxsize = mx; ptr->items = (int*)malloc(mx*sizeof(int)); // De-initialization of stack. void deinit(struct Stack *ptr) free(ptr->items); // Freeing the container memory ptr->top = -1; ptr->maxsize = -1; // Stack is full when top is equal to the last index int full(struct Stack* stack) return stack->top == stack->maxsize - 1; // Stack is empty when top is equal to -1 int empty(struct Stack* stack) return stack->top == -1; // Function to add an item to stack. It increases top by 1 void push(struct Stack* stack, int item) if (full(stack)) return; ++stack->top; *(stack->items+stack->top) = item; // stack->items[stack->top] = item; //Same as above statement printf("%d pushed to stack\n", item);
14 // Function to remove an item from stack. It decreases top by 1 int pop(struct Stack* stack) int ele; if (empty(stack)) printf("stack empty\n"); return -1000; ele = *(stack->items+stack->top); // ele = stack->items[stack->top]; // Same as above statement stack->top--; return ele; int stacktop(struct Stack* stack) if (empty(stack)) printf("stack empty\n"); return -1000; return stack->items[stack->top]; // Not part of standard stack specification. Use for our verification purpose. void display (struct Stack* s) int i; if (s->top == -1) printf ("Stack is empty\n"); return; printf ("\ncurrent contents of the stack is(top to bottom) \n"); for (i = s->top; i >= 0; i--) printf ("%d\n", s->items[i]); printf ("\n"); // Driver program to test above functions int main() struct Stack st; initstatck(&st,10);
15 push(&st, 10); push(&st, 20); push(&st, 40); push(&st, 10); push(&st, 20); push(&st, 40); printf("%d is at stack top\n", stacktop(&st)); display(&st); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); printf("%d popped from stack\n", pop(&st)); display(&st); deinit(&st); return 0;
16 6. Recursion : Factorial #include <stdio.h> int fact_1(int); int fact_2(int); int fact_3(int); int fact_4(int); int main() int num; int f; printf("enter a number to find it's Factorial: "); scanf("%d", &num); f = fact_1(num); printf("\nthe Factorial of %d is %d.\n", num, f); f = fact_2(num); printf("\nthe Factorial of %d is %d.\n", num, f); f = fact_3(num); printf("\nthe Factorial of %d is %d.\n", num, f); f = fact_3(num); printf("\nthe Factorial of %d is %d.\n", num, f); return 0; // Iterative Implementation int fact_1(int n) int i, prod = 1; for(i=n;i>=1;i--) prod = prod*i; return prod; // Recursive Implementation 1 int fact_2(int n) int x,y; if (n == 0)
17 //printf("0! = 1\n"); return 1; x = n-1; //printf("%d! = %d*%d!\t",n,n,x); y = fact_2(x); return n*y; // A Simplified form of the above. int fact_3(int n) if (n == 0) return 1; return n * fact_3(n - 1); // A More simplified form of the above. int fact_4(int n) return n == 0? 1: n * fact_4(n - 1);
18 7. Recursion : Binary Search #include <stdio.h> int binsearch_1(int [], int, int, int); // Iterative Solution int binsearch_2(int a[], int lo, int hi, int x); // Recursive Solution 1 int binsearch_3(int a[], int lo, int hi, int x); // Recursive Solution 2 int main() int x, size, i,pos = -1; int a[25]; printf("enter size of array a: "); scanf("%d", &size); printf("enter the numbers in sorted order:\n"); for(i = 0; i < size; i++) scanf("%d", &a[i]); printf("\n\n"); printf("enter x to search\n"); scanf("%d", &x); pos = binsearch_1(a, 0, size-1, x); if(pos == -1) printf("element not found\n"); printf("element found at index %d\n",pos); pos = binsearch_2(a, 0, size-1, x); if(pos == -1) printf("element not found\n"); printf("element found at index %d\n",pos); pos = binsearch_3(a, 0, size-1, x); if(pos == -1) printf("element not found\n"); printf("element found at index %d\n",pos); return 0;
19 // Iterative implementation int binsearch_1(int a[], int lo, int hi, int x) int mid,pos =-1; while (lo <= hi) mid = (lo + hi) / 2; if (a[mid] == x) pos = mid; break; if (a[mid] > x) hi = mid -1; if (a[mid] < x) lo = mid +1; return pos; // Recursive implementation : Elaborative way int binsearch_2(int a[], int lo, int hi, int x) int mid,pos; if (lo > hi) pos = -1; return pos; mid = (lo + hi) / 2; if (a[mid] == x) pos = mid; if (a[mid] > x) pos = binsearch_2(a, lo, mid - 1, x); if (a[mid] < x)
20 pos = binsearch_2(a, mid + 1, hi, x); return pos; // Recursive implementation : Simplified way int binsearch_3(int a[], int lo, int hi, int x) int mid; if (lo > hi) return -1; mid = (lo + hi) / 2; return (a[mid] == x? mid:a[mid] > x? binsearch_3(a, lo, mid - 1, x) : binsearch_3(a, mid + 1, hi, x));
21 8. Recursion : nth Fibonacci number #include <stdio.h> int fib_1(int); // Iterative Solution int fib_2(int); // Recursive Solution 1 int fib_3(int); // Recursive Solution 2 int fib_4(int); // Recursive Solution 2 int main() int num; int result; printf("enter the nth number in fibonacci series: "); scanf("%d", &num); result = fib_1(num); printf("the %dth number in fibonacci series is %d\n", num, result); result = fib_2(num); printf("the %dth number in fibonacci series is %d\n", num, result); result = fib_3(num); printf("the %dth number in fibonacci series is %d\n", num, result); result = fib_4(num); printf("the %dth number in fibonacci series is %d\n", num, result); return 0; // Iterative Solution int fib_1(int n) int low = 0, high = 1, num, i; if( n <= 1) return n; for (i = 2; i <= n; i++) num = low + high; low = high; high = num; return num;
22 // Recursive Solution. int fib_2(int n) int x,y; if (n <= 1) return n; x = fib_2(n-1); // Recursive Call 1 y = fib_2(n-2); // Recursive Call 2 return x+y; // Simplified recursive solution int fib_3(int n) if (n <= 1) return n; return fib_3(n - 1) + fib_3(n - 2); // More Simplified recursive solution int fib_4(int n) return (n <= 1)?n:fib_4(n - 1) + fib_4(n - 2);
Write a C program to add two Complex numbers using functions illustrating-
Scheme of valuvation Date : 29/8/2017 Marks: 40 Subject & Code : Data Structures and Applications (15CS33) Class : III A&B Name of Faculty : Ms. Saritha Time : 8:30 am to 10 am NOTE: ANSWER All FIVE QUESTIONS
More informationPrepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon
Data Structures &Al Algorithms Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon What is Data Structure Data Structure is a logical relationship existing between individual elements
More informationSTACKS 3.1 INTRODUCTION 3.2 DEFINITION
STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called
More informationThe program simply pushes all of the characters of the string into the stack. Then it pops and display until the stack is empty.
EENG212 Algorithms & Data Structures Fall 0/07 Lecture Notes # Outline Stacks Application of Stacks Reversing a String Palindrome Example Infix, Postfix and Prefix Notations APPLICATION OF STACKS Stacks
More informationApplication of Stack (Backtracking)
Application of Stack (Backtracking) Think of a labyrinth or maze How do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous
More informationUNIT-2 Stack & Queue
UNIT-2 Stack & Queue 59 13. Stack A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More informationUNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
UNIT 3: QUEUE Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. Simple Queue Implementation with arrays...3 2. Circular queue with global container
More informationStack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5
What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationMAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:
Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate
More informationModel Solution for QP CODE : ( 3 Hours )
Model Solution for QP CODE : 24788 ( 3 Hours ) All answers are for reference only. Any alternate answer that solves the problem should be considered eqully valid. 1. (a) Define data structure? Give its
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering
USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering SOLUTION FOR INTERNAL ASSESSMENT TEST 2 Date : 6/10/2017 Marks:
More informationMODULE V: POINTERS & PREPROCESSORS
MODULE V: POINTERS & PREPROCESSORS INTRODUCTION As you know, every variable is a memory-location and every memory-location has its address defined which can be accessed using ampersand(&) operator, which
More informationSolution for Data Structure
Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT
More informationSree Vidyanikethan Engineering College Sree Sainath Nagar, A. Rangampet
Sree Vidyanikethan Engineering College Sree Sainath Nagar, A. Rangampet 517 102 Department of Computer Science and Engineering I B. Tech C Programming Language Lab Title: Stack SVEC/IT/EXPT-CP-21 PROBLEM
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationComputer Programming for Engineering Applica4ons. Intro to Programming 10/17/13 ECE 175. The Stack Data Structure. Examples from Real Life
Computer Programming for Engineering Applica4ons ECE 175 Intro to Programming The Stack Data Structure Stack: An alloca4on of memory to store data (usually temporary data) Data are inserted, and deleted
More informationS.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Data Structure using C
S.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Data Structure using C Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 100 Q.1 (a) Attempt any SIX of the following : [12] Q.1 (a) (i) Define time complexity
More informationLecture 10: Recursive Functions. Computer System and programming in C 1
Lecture 10: Recursive Functions Computer System and programming in C 1 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive
More information/*Addition of two polynomials*/ #include<stdio.h> #include<malloc.h> #include<conio.h> struct link { int coeff; int pow; struct link *next; }; struct
/*Addition of two polynomials*/ #include #include #include struct link int coeff; int pow; struct link *next; ; struct link *poly1=null,*poly2=null,*poly=null; void create(struct
More informationMAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)
WINTER 18 EXAMINATION Subject Name: Data Structure using C Model wer Subject Code: 22317 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given
More informationDarshan Institute of Engineering & Technology for Diploma Studies Unit 3
Linear and Non-Linear Data Structures Linear data structure: Linear data structures are those data structure in which data items are arranged in a linear sequence by physically or logically or both the
More informationC Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 3 Pointers (revisited) int i = 4, j = 6, *p = &i, *q = &j, *r; if (p == &i)...; if (p == (& i))...;... = **&p;... = *(*(& p));... = 9 * *p / *q + 8;... = (((9*(*p)))/(*q)) + 8; *(r = &i)
More informationMAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in themodel answer scheme. 2) The model answer and the answer written by candidate may
More informationStacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.
Stacks CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack Hours: 12 Marks: 18 3.1 Introduction to Stacks 1. Stack as Abstract
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur An Example: Random
More informationNCS 301 DATA STRUCTURE USING C
NCS Data Structure Using C -8- NCS DATA STRUCTURE USING C Unit- Part- Stack Hammad Mashkoor Lari Assistant Professor Allenhouse Institute of Technology www.ncsds.wordpress.com Introduction Stack is a non
More informationFrequently asked Data Structures Interview Questions
Frequently asked Data Structures Interview Questions Queues Data Structure Interview Questions How is queue different from a stack? The difference between stacks and queues is in removing. In a stack we
More information1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code
1. Basics 1. Write a program to add any two-given integer. Algorithm - 1. Start 2. Prompt user for two integer values 3. Accept the two values a & b 4. Calculate c = a + b 5. Display c 6. Stop int a, b,
More informationStacks. Manolis Koubarakis. Data Structures and Programming Techniques
Stacks Manolis Koubarakis 1 Stacks and Queues Linear data structures are collections of components arranged in a straight line. If we restrict the growth of a linear data structure so that new components
More informationUNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
UNIT 4: Linked List Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. 1 Table of Contents 1. Array Implementation of Linked List...3 2. Queue using Array Implementation
More informationData Structures Week #3. Stacks
Data Structures Week #3 Stacks Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation of Stacks Stack Applications October 5, 2015 Borahan Tümer, Ph.D. 2 Stacks (Yığınlar)
More informationINDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator
INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator EXAMINATION ( End Semester ) SEMESTER ( Spring ) Roll Number Section Name Subject Number C S 1 0 0 0 1 Subject Name Programming
More informationDS Assignment II. Full Sized Image
DS Assignment II 1. A) For the Towers of Hanoi problem, show the call tree during the recursive call Towers(3, A, C, B). In the tree, label the root node as Towers (3, A, C, B) while marking all the intermediate
More informationAbstract Data Type: Stack
Abstract Data Type: Stack Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations
More informationOutline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks
Stack Chapter 4 Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks Introduction
More informationLinear Data Structure
Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal
More informationMAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)
WINTER 17 EXAMINATION Subject Name: Data Structure Using C Model Answer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as
More informationMTH 307/417/515 Final Exam Solutions
MTH 307/417/515 Final Exam Solutions 1. Write the output for the following programs. Explain the reasoning behind your answer. (a) #include int main() int n; for(n = 7; n!= 0; n--) printf("n =
More informationMarch 13/2003 Jayakanth Srinivasan,
Statement Effort MergeSort(A, lower_bound, upper_bound) begin T(n) if (lower_bound < upper_bound) Θ(1) mid = (lower_bound + upper_bound)/ 2 Θ(1) MergeSort(A, lower_bound, mid) T(n/2) MergeSort(A, mid+1,
More informationData Structure & Algorithms Laboratory Manual (CS 392)
Institute of Engineering & Management Department of Computer Science & Engineering Data Structure Laboratory for 2 nd year 3 rd semester Code: CS 392 Data Structure & Algorithms Laboratory Manual (CS 392)
More informationFUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value
FUNCTIONS Definition: A is a set of instructions under a name that carries out a specific task, assigned to it. CLASSIFICATION of s: 1. User defined s (UDF) 2. Library s USER DEFINED FUNCTIONS Without
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationFUNCTIONS OMPAL SINGH
FUNCTIONS 1 INTRODUCTION C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function
More informationPrepared by: Shraddha Modi
Prepared by: Shraddha Modi Introduction In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop consist of two segments Body of
More informationA. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS
Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization
More informationFunctions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Functions CS10001: Programming & Data Structures Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Recursion A process by which a function calls itself
More informationLinked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra
Linked List in Data Structure By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra Linked List Like arrays, Linked List is a linear data
More informationPOLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition.
POLYNOMIAL ADDITION AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition. ALGORITHM:- 1. Start the program 2. Get the coefficients and powers
More informationFundamentals of Programming. Lecture 14 Hamed Rasifard
Fundamentals of Programming Lecture 14 Hamed Rasifard 1 Outline Two-Dimensional Array Passing Two-Dimensional Arrays to a Function Arrays of Strings Multidimensional Arrays Pointers 2 Two-Dimensional Array
More informationComputer Science Foundation Exam
Computer Science Foundation Exam May 19, 2018 Section I A DATA STRUCTURES SOLUTION NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max
More informationprintf("this program adds the value 10 to a given integer number.\n\n");
PA1 Sample Solution Program 1 void add10(int *n); //Prototype int n; printf("this program adds the value 10 to a given integer number.\n\n"); printf("please enter an integer number: "); scanf("%d", &n);
More informationS.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear
S.E. Sem. III [CMPN] Data Structures Time : 3 Hrs.] Prelim Paper Solution [Marks : 80 Q.1(a) Explain different types of data structures with examples. [5] Ans.: Types of Data Structure : Data Structures
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Practice Sheet #06 Topic: Recursion in C 1. What string does the following program print? #include #include
More informationArrays and Applications
Arrays and Applications 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2014 Instructor: Dr. Asish Mukhopadhyay What s an array Let a 0, a 1,, a n-1 be a sequence
More informationPointers, Arrays, and Strings. CS449 Spring 2016
Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.
More informationVTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK
Contents: Definition and Examples Representing stacks in C Example: infix, prefix, and postfix Exercises THE STACK Definition and Examples A stack is an ordered collection of items into which new items
More information'C' Programming Language
F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax
More informationName Roll No. Section
Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name
More informationProcedural Programming
Exercise 6 (SS 2016) 04.07.2015 What will I learn in the 6. exercise Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Home exercise 4 (3 points) Write a program which is able to handle
More informationBSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering
BSc.(Hons.) Business Information Systems, BSc. (Hons.) Computer Science with Network Security, BSc.(Hons.) Software Engineering Cohorts BIS/04 Full Time - BCNS/04 Full Time SE/04 Full Time Examinations
More informationUnit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.
Unit III Functions Functions: Function Definition, Function prototype, types of User Defined Functions, Function calling mechanisms, Built-in string handling and character handling functions, recursion,
More informationMAHARASHTRASTATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)
Subject Code: 17330 Model Answer Page 1/ 22 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model
More informationint marks[10]; // fixed size and fixed address No change in Memory address.
Dynamic Memory Allocation : Used When we want to allocate memory during run time. int marks[10]; // fixed size and fixed address No change in Memory address. // fixed size. ( no change in size possible
More informationUNIT 1: POINTERS & DYNAMIC MEMORY ALLOCATION. Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
UNIT 1: POINTERS & DYNAMIC MEMORY ALLOCATION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. Simple pointer program demo...3 2. Chaining of pointers...4
More informationPESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities
INTERNAL ASSESSMENT TEST-3 Date : 12-05-2016 Marks: 40 Subject & Code : Programming in C and data structures (15PCD23) Sec : F,G,H,I,J,K Name of faculty :Dr J Surya Prasad/Mr.Sreenath M V/Ms.Monika/ Time
More information!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./
0!'" 0+'&"$ &0-2$ 10 +3&2),&/3+, #include int main() int i, sum, value; sum = 0; printf("enter ten numbers:\n"); for( i = 0; i < 10; i++ ) scanf("%d", &value); sum = sum + value; printf("their
More informationScheme of valuations-test 3 PART 1
Scheme of valuations-test 3 PART 1 1 a What is string? Explain with example how to pass string to a function. Ans A string constant is a one-dimensional array of characters terminated by a null ( \0 )
More informationStacks. Chapter 3. Exercises. 1. See Figure 3-1. FIGURE 3-1 Solution to Exercise 1
Chapter 3 Stacks Exercises 1. See Figure 3-1 3 5 7 9 11 13 S 1 S 2 FIGURE 3-1 Solution to Exercise 1 3. a. Prefix: D - B + C ((D - B) + C) (+(-D B) C) + - D B C Postfix: D - B + C ((D - B) + C) ((D B -)
More informationLecture Data Structure Stack
Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards
More informationAssociate Professor Dr. Raed Ibraheem Hamed
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 1 What this Lecture is about: Stack Structure Stack
More informationF.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C
F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1 (a) List any four relational operators.
More informationCSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming
CSE101-lec#19 Array searching and sorting techniques Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Introduction Linear search Binary search Bubble sort Introduction The process of finding
More informationVivekananda College of Engineering & Technology. Data Structures and Applications
Vivekananda College of Engineering & Technology [Sponsored by Vivekananda Vidyavardhaka Sangha, Puttur ] Affiliated to Visvesvaraya Technological University Approved by AICTE New Delhi & Govt of Karnataka
More informationUnit #2: Recursion, Induction, and Loop Invariants
Unit #2: Recursion, Induction, and Loop Invariants CPSC 221: Algorithms and Data Structures Will Evans 2012W1 Unit Outline Thinking Recursively Recursion Examples Analyzing Recursion: Induction and Recurrences
More informationS.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear
S.E. Sem. III [INFT] Data Structures & Analysis Time : 3 Hrs.] Prelim Paper Solution [Marks : 80 Q.1(a) Explain different types of data structures with examples. [5] Ans.: Types of Data Structure : Data
More informationMODULE 5: Pointers, Preprocessor Directives and Data Structures
MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two
More informationLECTURE NOTES ON DATA STRUCTURES THROUGH C
LECTURE NOTES ON DATA STRUCTURES THROUGH C Revision 4 July 2013 L. V. NARASIMHA PRASAD Professor and Head E. KRISHNARAO PATRO Associate Professor Department of Computer Science and Engineering Shamshabad,Hyderabad
More informationComputer Science Foundation Exam
Computer Science Foundation Exam December 16, 2016 Section I A DATA STRUCTURES NO books, notes, or calculators may be used, and you must work entirely on your own. Name: UCFID: NID: Question # Max Pts
More informationBangalore South Campus
USN: 1 P E PESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities INTERNAL ASSESSMENT TEST 3 Date: 22/11/2017 Time:11:30am- 1.00 pm
More informationData Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1
Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Stack Operations on a stack Representing stacks Converting an expression
More information1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \
BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,
More informationClass / Sem: I CSE / II Semester Subject Code: CS 6202 Subject: Programming and Data Structures I Prepared by T. Vithya Unit IV - LINEAR DATA STRUCTURES STACKS AND QUEUES Stack ADT Evaluating arithmetic
More informationDATA STRUCTURES USING C
DATA STRUCTURES USING C DATA STRUCTURES USING C LECTURE NOTES Prepared by Dr. Subasish Mohapatra Department of Computer Science and Application College of Engineering and Technology, Bhubaneswar Biju Patnaik
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities
PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities Continuous Internal Evaluation Test 2 Date: 0-10- 2017 Marks: 0 Subject &
More informationStructured programming
Exercises 9 Version 1.0, 13 December, 2016 Table of Contents 1. Remainders from lectures.................................................... 1 1.1. What is a pointer?.......................................................
More informationUNIT VI. STACKS AND QUEUES
UNIT VI. STACKS AND QUEUES Syllabus: /*-------------------------------------------------------------------------*/ Stack: Definition: "Stack is an ordered collection of data elements having similar data
More informationProgramming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring
Programming and Data Structures Mid-Semester - s to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring 2015-16 February 15, 2016 1. Tick the correct options. (a) Consider the following
More informationProblem # 1. calculate the grade. Allow a student the next grade if he/she needs only 0.5 marks to obtain the next grade. Use else-if construction.
ME 172 Lecture 6 Problem # 1 Write a program to calculate the grade point average of a 3.00 credit hour course using the data obtained from the user. Data contains four items: attendance (30), class test
More informationProcedural Programming & Fundamentals of Programming
& Fundamentals of Programming Exercise 4 (SS 2018) 12.06.2018 What will I learn in the 5. exercise Files Math functions Dynamic data structures (linked Lists) Exercise(s) 1 Files A file can be seen as
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Topic: Stack and Queue Practice Sheet #13 Date: 04-04-2017 1. Consider the following sequence of push and pop operations
More informationGovernment Girls Polytechnic, Bilaspur
Government Girls Polytechnic, Bilaspur Name of the Lab: Programming Lab Practical: Data Structure Lab Class: 4 th Semester (Computer Science & Engineering) Teachers Assessment: 30 End Semester Examination:70
More informationProgramming in C Lab
Programming in C Lab 1a. Write a program to find biggest number among given 3 numbers. ALGORITHM Step 1 : Start Start 2 : Input a, b, c Start 3 : if a > b goto step 4, otherwise goto step 5 Start 4 : if
More informationCS13002 Programming and Data Structures, Spring 2005
CS13002 Programming and Data Structures, Spring 2005 End-semester examination Total marks: 60 April 2005 Total time: 3 hours Roll no: Section: Name: This question paper consists of eight pages. Do not
More informationComputer Programming Unit 3
POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very
More informationEECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution
1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can
More information2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution
2/5/218 University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 22: Computer Systems & Programming Control Constructs in C (Partially a Review) Learn Four More Kinds
More informationProgramming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014
Programming & Data Structure Laboratory rrays, pointers and recursion Day 5, ugust 5, 2014 Pointers and Multidimensional rray Function and Recursion Counting function calls in Fibonacci #include
More informationChapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1
Chapter 8 Arrays, Addresses, and Pointers 204112: Structured Programming 204112 Structured Programming 1 Pointer Pointer is a variable that contains an address. If num_ptr is a pointer, *num_ptr means
More informationonly in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.
Instructions: Answer all five questions. Total marks = 10 x 2 + 4 x 10 = 60. Time = 2hrs. Write your answer only in the space provided. Do the rough work in the space provided for it. The question paper
More information