Stacks. Chapter 3. Exercises. 1. See Figure 3-1. FIGURE 3-1 Solution to Exercise 1
|
|
- Kory Barnett
- 5 years ago
- Views:
Transcription
1 Chapter 3 Stacks Exercises 1. See Figure 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 -) C +) D B - C + b. Prefix: A * B + C * D ((A * B) + (C * D)) (+ (*A B) (* C D)) + * A B * C D Postfix: A * B + C * D ((A * B) + (C * D)) ((A B *) (C D *) +) A B * C D * + c. Prefix: (A + B) * C - D * F + C ((((A + B) * C) - (D * F)) + C) (+ (- (* (+ A B) C) (* D F)) C) + - * + A B C * D F C Postfix: (A + B) * C - D * F + C ((((A + B) * C) - (D * F)) + C) ((((A B +) C *) (D F *) -) C +) A B + C * D F * - C + d. Prefix: (A - 2 * (B + C) - D * E) * F 9
2 10 Chapter 3 Stacks 5. a. (((A - (2 * (B + C))) - (D * E)) * F) (* (- (- A (* 2 (+ B C))) (* D E)) F) * - - A * 2 + B C * D E F Postfix: (A - 2 * (B + C) - D * E) * F (((A - (2 * (B + C))) - (D * E)) * F) (((A (2 (B C +) *) -) (D E *) -) F *) A 2 B C + * - D E * - F * A B * C - D * b. A B C + * D * * Note: Stacks are shown in top bottom order. a. See Table 3-1. b. See Table 3-2. Original Stack Output D - B + C B + C D B + C - D + C - D B C + D B - + D B - C D B - C + TABLE 3-1 Solution to Exercise 7a c. See Table 3-3. d. See Table 3-4. Original Stack Output A * B + C * D * B + C * D A B + C * D * A + C * D * A B C * D + A B * * D + A B * C D * + A B * C * + A B * C D A B * C D * + TABLE 3-2 Solution to Exercise 7b
3 Exercises 11 Original Stack Output (A+B)*C-D*F+C A+B)*C-D*F+C ( +B)*C-D*F+C ( A B)*C-D*F+C +( A )*C-D*F+C +( AB *C-D*F+C AB+ C-D*F+C * AB+ -D*F+C * AB+C D*F+C - AB+C* *F+C - AB+C*D F+C *- AB+C*D +C *- AB+C*DF C + AB+C*DF*- + AB+C*DF*-C AB+C*DF*-C+ TABLE 3-3 Solution to Exercise 7c Original Stack Output (A-2*(B+C)-D*E)*F A-2*(B+C)-D*E)*F ( -2*(B+C)-D*E)*F ( A 2*(B+C)-D*E)*F -( A *(B+C)-D*E)*F -( A2 (B+C)-D*E)*F *-( A2 B+C)-D*E)*F (*-( A2 +C)-D*E)*F (*-( A2B C)-D*E)*F +(*-( A2B )-D*E)*F +(*-( A2BC -D*E)*F *-( A2BC+ D*E)*F -( A2BC+* *E)*F -( A2BC+* D E)*F *-( A2BC+* D )*F *-( A2BC+* DE *F A2BC+* DE*- F * A2BC+* DE*- * A2BC+* DE*-F A2BC+* DE*-F* TABLE 3-4 Solution to Exercise 7d
4 12 Chapter 3 Stacks Problems 9. See Program 3-1. PROGRAM 3-1 Solution to Problem 9 /* =================== revlist ======================= This program will reverse a list of integers read from the keyboard by pushing them into a stack and retrieving them one by one. Written by: Date: #include <stdio.h> #include <stdlib.h> #include "stacksadt.h" int main (void) int data; int* dataptr; STACK* stack = createstack(); printf ("\nprint list of integers reversed.\n\n"); printf ("Please enter an integer: "); scanf ("%d", &data); do dataptr = (int*)malloc(sizeof(int)); if (!dataptr) printf ("\n\a**int* malloc failed**\n\n"); return -1; } // if dataptr *dataptr = data; if (!pushstack (stack, dataptr)) printf ("\n\a**allocation failed**\n\n"); return -1; } // if overflow printf ("\nenter next integer <EOF> to stop: "); } while ((scanf("%d", &data)!= EOF) &&!fullstack(stack)); printf ("\n\n The reversed list:\n\n "); while (dataptr = popstack(stack)) printf ("%d ", *dataptr); printf ("\n\n return 0; } // main --- End of Program ---\n\n"); 11. See Program 3-2. PROGRAM 3-2 Solution to Problem 11 /* This program will read a decimal number from the keyboard and convert it to hexadecimal form. Written by: Date:
5 Problems 13 PROGRAM 3-2 Solution to Problem 11 (continued) #include <stdio.h> #include <stdlib.h> #include "stacksadt.h" int main (void) int number; int* digitptr; STACK* stack; printf ("Convert a decimal number to hex.\n\n"); stack = createstack(); printf ("Please enter an integer: "); scanf ("%d", &number); while (number > 0) digitptr = (int*)malloc(sizeof(int)); if (!digitptr) printf ("\n\a**int* malloc failed**\n\n"); return -1; } // if digitptr *digitptr = number % 16; if (!pushstack (stack, digitptr)) printf ("\n\a**allocation Error**\n\n"); return -1; } // if overflow number /= 16; printf ("\n The hexadecimal number:\n 0x"); while (digitptr = popstack(stack)) switch (*digitptr) case 10 : printf ("%c", 'A'); case 11 : printf ("%c", 'B'); case 12 : printf ("%c", 'C'); case 13 : printf ("%c", 'D'); case 14 : printf ("%c", 'E'); case 15 : printf ("%c", 'F'); default : printf ("%d", *digitptr); } // switch stack not empty printf ("\n --- End of Program ---\n"); return 0; } // main
6 14 Chapter 3 Stacks 13. See Program 3-3. PROGRAM 3-3 Solution to Problem 13 /* This program changes an infix formula to a postfix formula. Written by: Date: #include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include "stacksadt.h" #define MAX_STRING 81 // Prototype Declarations bool getformula (char *instring); int convert (char *instring, char *outstring); int priority (char ch); void printformula (char *outstring); int main (void) int error; char instring [ MAX_STRING ] = '\0' }; char outstring [ MAX_STRING ] = '\0' }; printf ("Change infix formula to postfix.\n\n"); while (getformula(instring)) error = convert (instring, outstring); if (error) printf ("\n\a**error in the formula**\n\n"); else printformula (outstring); printf ("\n === End of Program ==="); return 0; } // main /* =================== getformula =================== Reads an infix formula from the keyboard. Pre formula is variable to receive formula Post infix expression read & stored Returns true if expression entered, false if 'quit' bool getformula (char* formula) printf ("Enter infix formula <quit>)\n" " Infix Formula : "); fgets(formula, MAX_STRING - 1, stdin); formula[strlen(formula) -1] = '\0'; if (strcmp("quit", formula) == 0 strcmp("quit", formula) == 0) return false; return true; } // getformula
7 Problems 15 PROGRAM 3-3 Solution to Problem 13 (continued) /* =================== convert =================== Convert an infix formula to a postfix formula. Pre instring is the infix formula outstring is the postfix formula Post instring has been converted to postfix Returns -1 if memory allocation failure 0 if successful. int convert(char* instring, char* outstring) char current; char* curptr; char* topptr; char* pin; char* pout; STACK* stack; stack = createstack(); pin = instring; pout = outstring; current = *pin; while (current!= '\0') if (!isspace(current)) switch (current) case '(' : curptr = (char*)malloc(sizeof(char)); *curptr = current; if (!pushstack(stack, curptr)) printf("\n\a**stk1 Overflow\n"); return -1; } // if!push case '*' : case '/' : case '+' : case '-' : topptr = stacktop(stack); while (!emptystack(stack) && priority(current) <= priority(*topptr)) *pout = *(char*)popstack(stack); *pout = ' '; topptr = stacktop (stack); curptr = (char*)malloc(sizeof(char)); *curptr = current; if (!pushstack(stack, curptr)) printf("\n\a**stk2 overflow\n"); return -1; } // if!push case ')' : topptr = stacktop(stack); while (!emptystack(stack) *pout = *(char*)popstack(stack);
8 16 Chapter 3 Stacks PROGRAM 3-3 Solution to Problem 13 (continued) *pout = ' '; topptr = stacktop (stack); topptr = popstack(stack); default : *pout = current; *pout = ' '; } // switch } // if current!= space pin++; current = *pin; not end of input string while (!emptystack (stack)) *pout = *(char*)popstack(stack); *pout = ' '; *pout = '\0'; return 0; } // convert /* =================== priority =================== This function tests the priority of an operator Pre op is an operator (+, -, (, *, or /) Post returns priority of operator 2 for * and / 1 for + and - 0 for ( int priority (char op) switch (op) case '(' : return 0; case '+' : case '-' : return 1; case '*' : case '/' : return 2; } // switch } // priority /* =================== printformula =================== This function prints the postfix formula. Pre : outstring is the postfix formula. Post : The formula has been printed. Returns : Nothing void printformula (char* outstring) printf (" Postfix Formula: %s\n", outstring); return; } // printformula
9 Problems See Program 3-4. PROGRAM 3-4 Solution to Problem 15 /* This program evaluates a postfix expression. Written by: Date: #include <stdio.h> #include <limits.h> #include <ctype.h> #include <string.h> #include "stacksadt.h" #define MAX_STRING 81 // Prototype Declarations bool getformula (char* instring); int evalpostfix (char* instring); int calculate (int operand1, char op, int operand2); void printresult (char* instring, int result); int main (void) int result; char instring [ MAX_STRING ] = '\0' }; printf ("Evaluate a postfix expression.\n\n"); while (getformula(instring)) result = evalpostfix (instring); if (result == INT_MIN) // Stack overflow return result; else if (result == INT_MIN + 1) printf("\n\a**error in formula**\n\n"); else printresult (instring, result); printf ("\n return 0; } // main === End of Program ===\n"); /* =================== getformula =================== This function prompts for and reads an postfix expression from the keyboard. Pre expression is variable for the expression Post Postfix expression read & stored Returns true if an expression entered false if 'quit' bool getformula (char* expression) printf ("Enter a postfix expression <quit>\n" " Postfix Expression: "); fgets(expression, MAX_STRING - 1, stdin); expression[strlen(expression) -1] = '\0'; if (strcmp("quit", expression) == 0 strcmp("quit", expression) == 0) return false;
10 18 Chapter 3 Stacks PROGRAM 3-4 Solution to Problem 15 (continued) return true; } // getformula /* =================== evalpostfix =================== This function evaluates a postfix expression. Pre expr is postfix expression Post expression evaluated & result returned Return INT_MIN + 1 if postfix expression invalid INT_MIN if a memory allocation fails result of evaluation if successful int evalpostfix(char* expr) char* pcurrent; int inputnum; int* numptr; int operand1; int operand2; int result ; STACK* stack; stack = createstack(); pcurrent = expr; while (*pcurrent!= '\0') if (isdigit(*pcurrent)) inputnum = strtol(pcurrent, &pcurrent, 10); numptr = (int*)malloc(sizeof(int)); *numptr = inputnum; if (!pushstack(stack, numptr)) printf ("\n\a**stack overflow\n"); return INT_MIN; } // if!push } // if isdigit else switch (*pcurrent) case '+' : case '-' : case '*' : case '/' : operand2 = *(int*)popstack (stack); operand1 = *(int*)popstack (stack); result = calculate (operand1,*pcurrent, operand2); numptr = (int*)malloc(sizeof(int)); *numptr = result; if (!pushstack (stack, numptr)) printf ("\n\a**stack overflow\n"); return INT_MIN; } // if!pushok default : // Must be space } // switch pcurrent++; } // else expr // if the stack has other than 1 item left, the // original postfix expression was wrong. if (stackcount(stack)!= 1)
11 Problems 19 PROGRAM 3-4 Solution to Problem 15 (continued) printf ("\n\a**error in postfix expression\n"); result = INT_MIN + 1; } // if count!= 1 else result = *(int*)popstack (stack); return result; } // evalpostfix /* =================== calculate =================== This function returns the result of a mathematical operation based on 4 possible operator characters that can be passed by the calling function. Pre op is mathematical operator (+ - * /) operand1 & operand2 are the operands Post operation performed Return Returns the result of the operation int calculate (int operand1, char op, int operand2) switch (op) case '+' : return (operand1 + operand2); case '-' : return (operand1 - operand2); case '*' : return (operand1 * operand2); case '/' : return (operand1 / operand2); } // switch } // calculate /* =================== printresult =================== The result of the calculation has been printed. Pre instring is postfix formula. Post result printed. void printresult (char* instring, int result) printf ("\n %s = %d\n", instring, result); return; } // printformula 17. See Algorithm 3-1. ALGORITHM 3-1 Solution to Problem 17 algorithm copystack (sourcestack, newstack) This algorithm copies the source stack to the destination stack, preserving the top-to-base ordering. Pre sourcestack is a valid stack Post newstack is a copy of sourcestack 1 set tempstack to createstack 2 loop (not empty sourcestack) 1 data = popstack (sourcestack) 2 pushstack (tempstack, data) 3 end loop Now copy tempstack to newstack 4 loop (not empty tempstack) 1 data = popstack (tempstack) 2 pushstack (newstack, data) 5 end loop 6 destroystack (tempstack) end copystack
12 20 Chapter 3 Stacks 19. See Program 3-5. PROGRAM 3-5 Solution to Problem 19 /* This program tests the palindrome function. Written by: Date: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include "stacksadt.h" #define MAX_STRING 81 // Prototype Declarations bool palindrome (char* string); void packstring (char* string); int main (void) char string [ MAX_STRING ]; printf (" === Welcome to Palindromes ===\n\n"); printf ("\nplease enter a string : "); while ((scanf("%[^\n]", string))!= EOF) switch (palindrome(string)) case true : printf (" \"%s\" is a palindrome.\n", string); case false : printf (" \"%s\" is NOT a palilndrome.\n", string); } // switch getchar(); // Clear \n printf ("\nenter next string or <EOF> : "); printf ("\n === End of Palindrome ===\n"); return 0; } // main /* =================== palindrome =================== This function tests to if a string is a palindrome. Pre string is the string to test Post string has been tested Returns true if the string is a palindrome false if the string is not a palindrome bool palindrome (char* string) char* walker; char* charptr; char teststring1[ MAX_STRING ]; char teststring2[ MAX_STRING ]; STACK* stack; stack = createstack();
13 Problems 21 PROGRAM 3-5 Solution to Problem 19 (continued) strcpy (teststring1, string); packstring(teststring1); walker = teststring1; while (*walker!= '\0') charptr = (char*)malloc(sizeof(char)); *charptr = *walker; if (!pushstack (stack, charptr)) printf ("Stack O/F in palindrome\n"); exit (100); } // if walker++; walker = teststring2; while (!emptystack(stack)) *walker = *(char*)popstack(stack); walker++; *walker = '\0'; destroystack (stack); return (strcmp(teststring1, teststring2) == 0); } // palindrome /* =================== packstring =================== This function removes spaces and punctuation and converts all characters to upper case. Pre string is a valid string Post string has been packed void packstring (char* string) char* current; char* next; current = next = string; while (*next!= '\0') if (isalpha(*next)) *current = toupper(*next); current++; } // if next++; *current = '\0'; return; } // packstring 21. See Algorithm 3-2. ALGORITHM 3-2 Solution to Problem 21 algorithm reversestack (stack) Reverse the contents of a stack. Pre stack is valid Post contents have been reversed 1 set temp1 to createstack
14 22 Chapter 3 Stacks ALGORITHM 3-2 Solution to Problem 21 (continued) 1 set temp2 to createstack 2 loop (stack not empty ) 1 popstack (stack, data) 2 pushstack (temp1, data) 3 end loop 4 loop (temp1 not empty) 1 popstack (temp1, data) 2 pushstack (temp2, data) 5 end loop 6 loop (temp2 not empty) 1 popstack (temp2, data) 2 pushstack (stack, data) 7 end loop end reversestack
Stacks. 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 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 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 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 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 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 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 informationMultiple Choice Questions ( 1 mark)
Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s
More informationStacks. Revised based on textbook author s notes.
Stacks Revised based on textbook author s notes. Stacks A restricted access container that stores a linear collection. Very common for solving problems in computer science. Provides a last-in first-out
More informationtoday cs3157-fall2002-sklar-lect05 1
today homework #1 due on monday sep 23, 6am some miscellaneous topics: logical operators random numbers character handling functions FILE I/O strings arrays pointers cs3157-fall2002-sklar-lect05 1 logical
More informationESC101N: Fundamentals of Computing End-sem st semester
ESC101N: Fundamentals of Computing End-sem 2010-11 1st semester Instructor: Arnab Bhattacharya 8:00-11:00am, 15th November, 2010 Instructions 1. Please write your name, roll number and section below. 2.
More informationBİL200 TUTORIAL-EXERCISES Objective:
Objective: The purpose of this tutorial is learning the usage of -preprocessors -header files -printf(), scanf(), gets() functions -logic operators and conditional cases A preprocessor is a program that
More informationLecture 02 C FUNDAMENTALS
Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void
More informationDeductive Verification of Data Structures
Deductive Verification of Data Structures Jens Gerlach DEVICE-SOFT Workshop Berlin, 21./22. October 2010 1 Introduction I will mostly talk about a particular and well known data type stack The principles
More informationChapter 8 - Characters and Strings
1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions 8.5 Standard Input/Output Library
More informationChapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.
Chapter 19 Program Design 1 Introduction Most full-featured programs are at least 100,000 lines long. Although C wasn t designed for writing large programs, many large programs have been written in C.
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 informationB.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University
Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are
More informationSTACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:
STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The
More informationThese are reserved words of the C language. For example int, float, if, else, for, while etc.
Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.
More informationUnited States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017
United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017 1. Do a page check: you should have 8 pages including this cover sheet. 2. You have 50 minutes
More informationThe Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.
The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The
More informationStacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.
(Section 2) Linked list implementation of stack Typical Application of stacks Evaluation of expressions (infix, postfix, prefix) References and further details By: Pramod Parajuli, Department of Computer
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 informationData Structures & Algorithm Analysis. Lecturer: Souad Alonazi
Data Structures & Algorithm Analysis Lec(3) Stacks Lecturer: Souad Alonazi What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element
More informationFundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University
Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler
More informationThe detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.
Chapter 1. An Overview 1.1 Programming and Preparation Operating systems : A collection of special programs Management of machine resources Text editor and C compiler C codes in text file. Source code
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2018-2019 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,
More informationSome Applications of Stack. Spring Semester 2007 Programming and Data Structure 1
Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1 Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2 What is Polish Notation? Conventionally,
More informationChapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;
Chapter 6 Data Structure Introductory notes are included in PowerPoint file. The programs illustrated are as follows: //elephnt1.c // This example is from C for Scientists and Engineers #include
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 6: EVALUATION of EXPRESSIONS 2017 Fall Evaluation of Expressions Compilers use stacks for the arithmetic and logical expressions. Example: x=a/b-c+d*e-a*c If a=4, b=c=2,
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 informationCS 2412 Data Structures. Chapter 9 Graphs
CS 2412 Data Structures Chapter 9 Graphs Some concepts A graph consists of a set of vertices and a set of lines (edges, arcs). A directed graph, or digraph, is a graph in which each line has a direction.
More informationComputer System and programming in C
1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign
More informationArithmetic Operators. Portability: Printing Numbers
Arithmetic Operators Normal binary arithmetic operators: + - * / Modulus or remainder operator: % x%y is the remainder when x is divided by y well defined only when x > 0 and y > 0 Unary operators: - +
More informationChapter 4 Expression & Operators
Chapter 4 Expression & Operators 1 Aim To give understanding on: Expression and operator concept math.h and stdlib.h built-in function Objective Students should be able to: understand concepts and fundamentals
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 informationCS16 Exam #1 7/17/ Minutes 100 Points total
CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding
More informationFunction a block of statements grouped together, to perform some specific task
Function a block of statements grouped together, to perform some specific task Each program written in C / C++ includes at least one function with pre-defined name: main( ). In our previous programs, we
More informationConditional Statement
Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?
More informationTHE FUNDAMENTAL DATA TYPES
THE FUNDAMENTAL DATA TYPES Declarations, Expressions, and Assignments Variables and constants are the objects that a prog. manipulates. All variables must be declared before they can be used. #include
More informationExpressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators
Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical
More informationStacks Calculator Application. Alexandra Stefan
Stacks Calculator Application Alexandra Stefan Last modified 2/6/2018 1 Infix and Postfix Notation The standard notation we use for writing mathematical expressions is called infix notation. The operators
More information15. Stacks and Queues
COMP1917 15s2 15. Stacks and Queues 1 COMP1917: Computing 1 15. Stacks and Queues Reading: Moffat, Section 10.1-10.2 Overview Stacks Queues Adding to the Tail of a List Efficiency Issues Queue Structure
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 informationStacks and Queues. CSE Data Structures April 12, 2002
Stacks and Queues CSE 373 - Data Structures April 12, 2002 Readings and References Reading Section 3.3 and 3.4, Data Structures and Algorithm Analysis in C, Weiss Other References 12-Apr-02 CSE 373 - Data
More informationALGORITHM 2-1 Solution for Exercise 4
Chapter 2 Recursion Exercises 1. a. 3 * 4 = 12 b. (2 * (2 * fun1(0) + 7) + 7) = (2 * (2 * (3 * 0) + 7) + 7) = 21 c. (2 * (2 * fun1(2) + 7) + 7) = (2 * (2 * (3 * 2) + 7) + 7) = 45 2. a. 3 b. (fun2(2, 6)
More informationCOSC2031 Software Tools Introduction to C
COSC2031 Software Tools Introduction to C Instructor: Matt Robinson matt@cs.yorku.ca http://www.cs.yorku.ca/course/2031/ From Last Day What this course is about A (brief) History of Unix and C Some sample
More informationWARM UP LESSONS BARE BASICS
WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions
More informationWhere does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).
Suppose we have a circular array implementation of the queue,with ten items in the queue stored at data[2] through data[11]. The current capacity of an array is 12. Where does the insert method place the
More informationC mini reference. 5 Binary numbers 12
C mini reference Contents 1 Input/Output: stdio.h 2 1.1 int printf ( const char * format,... );......................... 2 1.2 int scanf ( const char * format,... );.......................... 2 1.3 char
More informationC: How to Program. Week /May/28
C: How to Program Week 14 2007/May/28 1 Chapter 8 - Characters and Strings Outline 8.1 Introduction 8.2 Fundamentals of Strings and Characters 8.3 Character Handling Library 8.4 String Conversion Functions
More informationCharacters and Strings
Characters and Strings 60-141: Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Character constants A character in single quotes,
More informationProgramming in C Quick Start! Biostatistics 615 Lecture 4
Programming in C Quick Start! Biostatistics 615 Lecture 4 Last Lecture Analysis of Algorithms Empirical Analysis Mathematical Analysis Big-Oh notation Today Basics of programming in C Syntax of C programs
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 Infix to Postfix Example 1: Infix to Postfix Example 2: Postfix Evaluation
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 informationThis is CS50. Harvard University Fall Quiz 0 Answer Key
Quiz 0 Answer Key Answers other than the below may be possible. Binary Bulbs. 0. Bit- Sized Questions. 1. Because 0 is non- negative, we need to set aside one pattern of bits (000) for it, which leaves
More informationMODULE 2: Branching and Looping
MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written
More informationmith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut
mith College CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 2 Hours D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More informationTutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White
Tutorial 7 Question 1. Write a C program which declares an array of 20 integers, and initialises them such that the first value is 1, the second 2, and so on, with the last value being 20. Use a loop to
More informationFloating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions
Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point
More informationFormal Languages and Automata Theory, SS Project (due Week 14)
Formal Languages and Automata Theory, SS 2018. Project (due Week 14) 1 Preliminaries The objective is to implement an algorithm for the evaluation of an arithmetic expression. As input, we have a string
More information8. Characters, Strings and Files
REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence
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 informationArrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2
CMPT 125 Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2 Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous
More informationCSCI 2132 Software Development. Lecture 20: Program Organization
CSCI 232 Software Development Lecture 20: Program Organization Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 22-Oct-208 (20) CSCI 232 Previous Lecture Generating permutations
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 informationCOP 2000 Introduction to Computer Programming Mid-Term Exam Review
he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can
More informationTechnical Questions. Q 1) What are the key features in C programming language?
Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility
More informationFundamentals of Programming
Fundamentals of Programming Lecture 4 Input & Output Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline printf scanf putchar getchar getch getche Input and Output in
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationThis code has a bug that allows a hacker to take control of its execution and run evilfunc().
Malicious Code Insertion Example This code has a bug that allows a hacker to take control of its execution and run evilfunc(). #include // obviously it is compiler dependent // but on my system
More informationStacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface
Stacks and Queues Stack - Abstract Data Type. Stacks and queues ubiquitous -structure in computing. Part of many important algorithms. Good example of abstract types. Good example to practice programming
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi CE 43 - Fall 97 Lecture 4 Input and Output Department of Computer Engineering Outline printf
More informationn Group of statements that are executed repeatedly while some condition remains true
Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,
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 Conceptual Idea
More information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationCOP 3502 Section 2 Exam #2 Version A Spring /23/2017
COP 3502 Section 2 Exam #2 Version A Spring 2017 3/23/2017 Lecturer: Arup Guha Directions: Answer all multiple choice questions on the scantron. Each question has a single correct answer. In case of ambiguities,
More informationIntroduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms
Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationCopyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 18 I/O in C Standard C Library I/O commands are not included as part of the C language. Instead, they are part of the Standard C Library. A collection of functions and macros that must be implemented
More informationMidterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:
Page 1 of 8 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2015 Midterm Examination # 2 Wednesday, March 18, 2015 ANSWERS Duration of examination: 75 minutes STUDENT
More informationSchool of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015
Page 1 of 8 School of Computer Science 60-141-01 Introduction to Algorithms and Programming Winter 2015 Midterm Examination # 1 Wednesday, February 11, 2015 Marking Exemplar Duration of examination: 75
More information17CS33:Data Structures Using C QUESTION BANK
17CS33:Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C Learn : Usage of structures, unions - a conventional tool for handling a group of logically
More informationSlide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide
More informationupper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.
The char Type The C type char stores small integers. It is 8 bits (almost always). char guaranteed able to represent integers 0.. +127. char mostly used to store ASCII character codes. Don t use char for
More informationFinal Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science
Faculty of Computer Science Page 1 of 21 Final Exam Term: Fall 2018 (Sep4-Dec4) Student ID Information Last name: First name: Student ID #: CS.Dal.Ca userid: Course ID: CSCI 2132 Course Title: Instructor:
More informationInfix to Postfix Conversion
Infix to Postfix Conversion Infix to Postfix Conversion Stacks are widely used in the design and implementation of compilers. For example, they are used to convert arithmetic expressions from infix notation
More informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More information3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)
Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.
More informationPersonal SE. Functions, Arrays, Strings & Command Line Arguments
Personal SE Functions, Arrays, Strings & Command Line Arguments Functions in C Syntax like Java methods but w/o public, abstract, etc. As in Java, all arguments (well, most arguments) are passed by value.
More informationFirst Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...
First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and
More informationContinued from previous lecture
The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations
More informationChapter 8: Character & String. In this chapter, you ll learn about;
Chapter 8: Character & String Principles of Programming In this chapter, you ll learn about; Fundamentals of Strings and Characters The difference between an integer digit and a character digit Character
More informationThere are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate
More informationPROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017
PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017 Objectives Recap C Differences between C and C++ IO Variable Declaration Standard Library Introduction of C++ Feature : Class Programming in C++ 2 Recap C Built
More informationProgramming in C++ 5. Integral data types
Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long
More informationLab Exam 1 D [1 mark] Give an example of a sample input which would make the function
CMPT 127 Spring 2019 Grade: / 20 First name: Last name: Student Number: Lab Exam 1 D400 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return -1? Answer:
More informationUNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. C Program to Check for balanced parenthesis by using Stacks...3 2. Program
More information