A Trimesterly Review of CS 1436 PDF
Document Details
Uploaded by LavishChalcedony281
University of Texas at Dallas
2024
William T. Doan
Tags
Related
- Unit 1-Introduction To Programming And Computer Science PDF
- Computer Science - XI - Part I PDF
- CS 1436.0L3 Programming Fundamentals Sample Test 1 Questions PDF
- CS 110: Computer Science Fundamentals Week 4 PDF
- CS 110: Computer Science Fundamentals Expressions and Arithmetics Lec06_expressions01 PDF
- Computer Science - I Past Paper PDF 2022
Summary
This document contains a trimesterly review of topics in Computer Science 1436. It includes explanations and examples related to stacks, programming functions, decision-making, and input validation. This document aims to prepare students for a course on computer science.
Full Transcript
William T. Doan Another Trimesterly Review of CS 1436 Written for Dr. Brian Wescott Ricks and his 001 and 009 sections Confidential and Proprietary — Not for External Distribution. This material is protected by copyright under the Berne Convention for the Protection of Literary and Artistic Works...
William T. Doan Another Trimesterly Review of CS 1436 Written for Dr. Brian Wescott Ricks and his 001 and 009 sections Confidential and Proprietary — Not for External Distribution. This material is protected by copyright under the Berne Convention for the Protection of Literary and Artistic Works, as well as applicable national and international copyright laws. Unauthorized distribution, reproduction, or any other use without express written permission is strictly prohibited and may result in legal action. October 2024 Springer Berlin Heidelberg NewYork Hong Kong London Milan Paris Tokyo Dedication This work is dedicated to Drs. Daniel Gibney and James Knox Willson III for their sage advice, guidance, and tutelage. Contents 1 Managing the Execution of Multi-Function Programs and the Scope of Local Variables using the Stack............... 1 1.1 Introduction to a Stack.................................. 1 1.1.1 Visualizing the Stack.............................. 1 Exercises................................................... 1 1.2 When a Function is Called................................ 2 1.3 When a Function Returns................................ 2 1.4 Global Variables......................................... 2 2 Making Decisions.......................................... 3 2.1 Decision Statements..................................... 3 2.2 Relational Operators..................................... 3 2.3 Relational Expressions................................... 4 2.3.1 Examples........................................ 4 2.4 The if Statement........................................ 4 2.4.1 Syntax........................................... 4 2.4.2 Example......................................... 5 2.4.3 Flowchart Example................................ 5 2.5 The if/else Statement.................................... 5 2.5.1 Syntax........................................... 6 2.5.2 Example......................................... 6 2.6 Nested if Statements..................................... 6 2.6.1 Example......................................... 6 2.7 Logical Operators....................................... 7 2.7.1 Operators........................................ 7 2.7.2 Example of Logical AND........................... 7 2.7.3 Example of Logical OR............................ 7 2.8 The switch Statement.................................... 7 2.8.1 Syntax........................................... 7 2.8.2 Example......................................... 8 Contents VII 3 Functions.................................................. 9 3.1 Modular Programming................................... 9 3.1.1 Advantages of Modular Programming................ 9 3.2 Defining and Calling Functions............................ 9 3.2.1 Function Definition................................ 9 3.2.2 Syntax........................................... 9 3.3 Function Prototypes..................................... 10 3.3.1 Syntax........................................... 10 3.3.2 Example......................................... 10 3.4 Passing Arguments to Functions........................... 10 3.4.1 Parameters and Arguments......................... 10 3.4.2 Example......................................... 10 3.5 Passing Multiple Arguments.............................. 11 3.5.1 Example......................................... 11 3.6 Passing Data by Value................................... 11 3.6.1 Example......................................... 11 3.7 Returning a Value from a Function........................ 11 3.7.1 Example......................................... 12 3.8 Exercises............................................... 12 3.9 Returning a Boolean Value............................... 12 3.9.1 Example......................................... 12 3.10 Default Arguments...................................... 12 3.10.1 Example......................................... 13 3.11 Using Reference Variables as Parameters................... 13 3.11.1 Example......................................... 13 4 Input Validation and Menus............................... 14 4.1 Validating User Input.................................... 14 4.1.1 Example......................................... 14 4.2 Menus................................................. 15 4.2.1 Menu-Driven Program Organization................. 15 4.2.2 Example......................................... 15 5 Overloading Functions and Stubs.......................... 17 5.1 Overloading Functions................................... 17 5.1.1 Example......................................... 17 5.1.2 Usage............................................ 17 5.2 Stubs and Drivers....................................... 18 5.2.1 Stubs............................................ 18 5.2.2 Example of a Stub................................. 18 5.2.3 Drivers........................................... 18 5.2.4 Example of a Driver............................... 18 VIII Contents 6 Additional Topics.......................................... 19 6.1 Comparing Characters and Strings......................... 19 6.1.1 Comparing Characters............................. 19 6.1.2 Example......................................... 19 6.1.3 Comparing Strings................................ 19 6.1.4 Example......................................... 19 6.2 The Conditional Operator................................ 20 6.2.1 Syntax........................................... 20 6.2.2 Example......................................... 20 1 Managing the Execution of Multi-Function Programs and the Scope of Local Variables using the Stack 1.1 Introduction to a Stack A stack is a last-in, first-out (LIFO) data structure. When an item is retrieved from a stack, the last item inserted (pushed) onto the stack is the first one retrieved (popped). Likewise, the first element inserted is the last one retrieved. 1.1.1 Visualizing the Stack Stack Bottom Main Function B Stack Growth Function A Top of Stack Fig. 1.1. Stack Frames during Function Calls Exercises 1. Explain the difference between stack and heap memory allocation. 2 1 Managing the Execution of Multi-Function Programs and the Scope of Local Variables using the Stack 2. Draw a diagram illustrating the stack frames during the execution of nested function calls. 3. Why is it important to declare variables locally within functions? 1.2 When a Function is Called When a function is called, a stack frame containing the program’s return address and the local variables of the function are pushed onto the stack. The stack frame of the currently executing function hides the stack frame of the calling function, including all the local variables of that function. The executing function has access to its local variables which are on the top frame of the stack. 1.3 When a Function Returns When the function ends execution, the return address is retrieved, and the stack frame is popped. Popping the stack destroys all the local variables of the function. The stack frame of the calling function is now on top of the stack, and all of its local variables are back in scope. 1.4 Global Variables Globals (constants and variables) are not stored on the stack. Global variables are created on the heap. The heap is a larger area of memory where access is less restrictive. 2 Making Decisions 2.1 Decision Statements Decision statements (often called selection statements) allow us to specify alternate courses of execution based upon conditions that exist at run time. In this chapter, we will learn about: Additional operators used in forming decisions: – Relational operators – Equality and inequality operators – Logical operators – The conditional operator Decision/selection statements: – if – if/else – if/else if – switch – The conditional statement 2.2 Relational Operators Relational operators determine whether a specific relationship exists between two values. Operator Relationship Tested > Greater than < Less than >= Greater than or equal to x x < y y >= x x ch1 ch1 < ch2 ch2 >= ch1 ch1 = 70) 2 { 3 passed = true; 4 } Always use braces {} to enclose the block of statements, even if there is only one statement. This helps prevent errors and improves code readability. 2.4.3 Flowchart Example Start Is score >= 70? No Yes passed = true End Fig. 2.1. Flowchart of an if Statement 2.5 The if/else Statement The if/else statement provides two possible paths of execution. One for when the condition is true and another for when the condition is false. 6 2 Making Decisions 2.5.1 Syntax 1 if (expression) 2 { 3 statement(s); // if/true clause 4 } 5 else 6 { 7 statement(s); // else/false clause 8 } 2.5.2 Example 1 if (hoursWorked 40 7 { 8 regularPay = 40.0 * payRate; 9 overtimePay = (hoursWorked - 40.0) * 1.5 * payRate; 10 } 2.6 Nested if Statements An if or if/else statement can be nested inside another if statement. 2.6.1 Example Suppose we are developing a banking program that determines whether a customer qualifies for a special interest rate on a loan. There are two conditions for qualification: 1. The customer must currently be employed. 2. The customer must have recently graduated from college (in the past two years). 1 if (isEmployed) 2 { 3 if (yearsSinceGraduation