COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Similar documents
Study recommendations for the Mid-Term Exam - Chapters 1-5

Study recommendations for Quiz 5 - Chapter 5

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Introduction to C Programming

Structured Program Development in C

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Chapter 3 Structured Program Development

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Programming for Engineers Iteration

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Full file at C How to Program, 6/e Multiple Choice Test Bank

Operators. Java operators are classified into three categories:

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

3 The L oop Control Structure

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Structured Program Development

n Group of statements that are executed repeatedly while some condition remains true

Flow Control. CSC215 Lecture

Control Statements. Musa M. Ameen Computer Engineering Dept.

Lecture 3. More About C

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Operators And Expressions

CS1100 Introduction to Programming

Increment and the While. Class 15

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

CT 229 Java Syntax Continued

Operators & Expressions

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Fundamentals of Programming Session 7

Fundamental of Programming (C)

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Chapter 4. Flow of Control

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Introduction. C provides two styles of flow control:

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Operators and Expressions:

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

5. Selection: If and Switch Controls

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Unit 3. Operators. School of Science and Technology INTRODUCTION

Computer Programming I - Unit 5 Lecture page 1 of 14

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Lecture 6. Statements

CSI33 Data Structures

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 4 C Program Control

Assignment: 1. (Unit-1 Flowchart and Algorithm)

QUIZ: What value is stored in a after this

1.3b Type Conversion

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

A Look Back at Arithmetic Operators: the Increment and Decrement

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Java Notes. 10th ICSE. Saravanan Ganesh

Decision Making -Branching. Class Incharge: S. Sasirekha

Chapter 3 Structure of a C Program

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

C++ Programming: From Problem Analysis to Program Design, Third Edition

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Problem Solving and 'C' Programming

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Dept. of CSE, IIT KGP

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Technical Questions. Q 1) What are the key features in C programming language?

Fundamentals of Programming

CS102: Variables and Expressions

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

C++ Programming: From Problem Analysis to Program Design, Third Edition

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

CS112 Lecture: Repetition Statements

Chapter 4: Basic C Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CS110D: PROGRAMMING LANGUAGE I

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

More Programming Constructs -- Introduction

Java enum, casts, and others (Select portions of Chapters 4 & 5)

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Chapter 4 C Program Control

Transcription:

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 use one 8.5"x11 sheet of notes in any size type or hand-written front and back of the page. Key erms & Concepts: ( See also http://www.gibson.vero-beach.fl.us/classes/cop2000/glos45.html ) Boolean data is a logical type of data with only two values: rue and alse Ordinal data is a type of data in which all of the values within the set are known and in a predictable order. Ordinal data types include: all integer data types, char and bool, but not float. Relational operators are those used to evaluate the relationships between items of data, including: == - Equal to, the opposite of which is written in C as!= (or not equal to). > - Greater than, the opposite of which is written in C as <= (less than or equal to). < - Less than, the opposite of which is written in C as >= (greater than or equal to). Relational expressions are formulae such as X==A+B that compare items of data (including constants, symbolic constants, variables, or expressions) and produce an integer (1 for true, 0 for false) result. Boolean (logical) operators such as && (and), (or) and! (not) are those used to combine Boolean operands into Boolean (logical) expressions such as (X==0 && Y>10) to produce a single Boolean result. A condition is any expression that produces a Boolean result. Branching is the act of breaking out of the normal sequence of steps in an algorithm to allow an alternative process to be performed or to allow the repetition of process(es). Control Structures are organized groups of statements that are followed in a specific order, including: Sequence in which the flow follows only one path from one step to another without branching. he use of the sequential control structure is indicated in C by the use of a compound statement in which a series of statements is enclosed (or "blocked") between the braces and. Selection in which alternative paths (legs) may be followed based on the result of a condition. When any leg is completed, flow of control must branch forward to the end of the structure. When only two options are involved, the appropriate C command to provide two legs would be if condition statement(s)-to-perform-if-true; else statement(s)-to-perform-if-false; When multiple options are required, you may place one selection structure inside the leg of another in a practice called nesting. In the special situation that you are testing for many possible values in a single storage location you may use the case structure (read below for more information). Repetition (also known as "loops") in which statements may be repeated based on the result of a condition. In situations where you want to first perform a step and then test a condition to see if you should branch back to repeat it, use the structure (more below). In situations where you want to first test a condition to see if you should perform a step and then perform it and branch back to repeat the test, use the structure (more below). Each execution of the steps in a loop is called a pass. he variable that is used to control loop passage/exit is called the control variable. Accumulation is the process of building a running sub-total through repeated adjustments to a variable. A variable used to indicate a specific situation or status within a program is called a flag. Revised Order of Precedence able (including arithmetic, relational and Boolean operators): () Highest / irst postfix ++ postfix -- ^! prefix ++ prefix -- unary + unary - unary & casts * / % + - < > <= >= ==!= && Lowest / Last ruth ables for Boolean Operators: (Op = Operand, a relational expression or Boolean value) Op.A Op.B Op.A && Op.B Op.A Op.B Op.A Op.B Op.A! ( Op.A ) Page 1 Revision 2010-10

SELECION SRUCURES: Structures such as the one on the left are coded in C using the reserved word if. Set X to 1 Set A to 0 A > 0 and A < 9 Replace X with triple its original value. he diamond shape encloses the condition that is being tested (in this example, a Boolean expression). Remember that the C if statement can only execute one statement on each leg ( or ), so the pair of steps on the alse leg must be coded inside of a and pair to make them a single compound statement. hus the C code would be: if (A>0 && A<9) X=X*3; else X=1; A=0; Note the use of the parentheses around the condition in the code above. Additional parentheses are often necessary because of the order of precedence of operators in C (see the table on the previous page). Remember also that the alse leg may be empty, but not the rue leg. If your flowchart shows the opposite, then reverse the labeling of the rue and alse legs and reverse the logic of the condition inside the diamond. If you can't determine what the logical opposite of the condition would be, simply use the NO operator like this:! ( A>0 && A<9 ) CASE Selection C's switch statement can be used in the special situation that you are testing for many possible values in a single ordinal storage location. In the example below, five possible paths might be followed depending on the value stored in the character storage location X. Each path (leg) is selected based on the individual value(s) that might be stored in X. he C code would be: switch (X) A 1 A 2 Y 1 X "A" "B" "C" "D","E" else A 3 A 4 A 0 case 'A': A=1; break; case 'B': A=2; Y=1; break; case 'C': A=3; break; case 'D': case 'E': A=4; break; default: A=0; break; Notice: (1) the paretheses around the character expression (X) (2) the inclusion of "break;" after each option serving to brace any multiple statements on each leg (3) the braces used to enclose the full list of options. he use of indentation and the positions of the carriage returns in the code are irrelevant. Page 2 Revision 2010-10

REPEIION SRUCURES: Structure: We studied two repetition structures (loops); one known as (a.k.a. while-do) and another known as (a.k.a. do-while). he primary difference between them is in where the test that controls the loop is performed. Leading decision loops test before each pass and are coded in C using while (condition) body_statements; railing decision loops test after each pass and are coded in C using do body_statements; while (condition); We can design a loop that test somewhere in the middle, but that is consider very poor programming practice. Control: We studied two methods of controlling loop passage and exit. Counting control is based on the testing of values that have been set and altered by the programmer. Programmers know in advance how many times any counting loop will pass (execute its body) because all values of its control variable are predictable. Sentinel control is based on the testing of external values that have been read into the program. Programmers do not know in advance how many times a sentinel loop will pass (execute its body) because the values of its control variable are unpredictable. Boundary Values: he values of variables just prior to the entry into a loop and just after the exit from a loop are called boundary values. Steps inside a loop can be organized so that the value of a variable is changed after it is displayed, so don't expect that variables will always contain the last value that you saw displayed. Counting Loops: All counting loops have at least four parts: initialization - prior to the loop entry, a first value of the control variable is set body - where the step(s) to be repeated belong increment (or decrement) - where the control variable is increased (or decreased) test - where the control variable is tested to determine whether the loop should pass or exit. he bottom three steps are not always in the order listed above. he order of the steps will effect the boundary values of the control variable. he two examples below show loops that will count from 1 to 9 and display the value of the control variable (also called a counter in a counting loop). Notice the exit value on N will be 10. N 1 N=1; N 1 N=1; while (N<=9) do N<=9 printf ("%d\n",n); N printf ("%d\n",n); N N = N+1; N = N+1; N N+1 N N+1 N <= 9 while (N<=9); Page 3 Revision 2010-10

If a loop: (1) uses the Leading Decision structure, (2) uses counting control, and (3) increments as the last step of its pass, C offers a special "automatic loop" statement combining the statements above (on the left) into the single statement: for (N=1; N<=9; N=N+1) printf ("%d\n", N); Note: if the body of the loop has more than one statement to repeat, block it inside of braces. SAMPLE QUESIONS: (AND ANSWERS) Circle any SYNAX (not run-time) ERRORS (there may be more than one) in each of the following C code segments. If a segment is valid answer "OK". Assume all variables have been correctly declared. scanf ("%d", &A); if ( A<10 && >20 ) printf ("ok"); >20 is not a complete relational expression for (I=1, I<=79, I++) printf ("-"); the commas (,) should be semi-colons (;) X=1: scanf ("%d",&y); if X > Y printf ("OK") else printf ("NO"); missing parentheses around condition and missing semi-colon after if statement Indicate the Boolean results (RUE or ALSE) for each of the following expressions, given: #define S "123" /* a string of digits */ int A=0, B=2, C=-3; A==0 B<2 && C<0!(A>C) && A<C A!=0 B<2 S>"8" rue alse alse alse S>100 Logically invalid (data type mismatch) Draw a flowchart that matches the following code. Answer: Start X = 3 if (X == 3) Y = 4; Z = 5; End Z 5 Y 4 Notice that the Z = 5 step follows the selection. It is not part of the true leg because it was not included inside of a pair of braces. he semicolon following the Y = 4 statement terminated the if statement. Page 4 Revision 2010-10

SAMPLE QUESIONS: (AND ANSWERS) Given the following declarations for employee data within a program: int A; /* Age */ char D; /* Highest Degree Earned (only possible values are uppercase: 'N' = None, 'B' = Bachelor's, 'M' = Master's, or 'D' = Ph.D.) */ char G; /* Gender (only uppercase 'M' or '' are possible) */ Write a compound If statement in C which will display the message "OK" only when the employee is a female, between the ages of 20 and 30 (inclusive) and holds a college degree (Bachelor's, Master's or Ph.D.). Do NO check for case. Assume that previous statements have guaranteed uppercase values only, so that you need only check for uppercase values. if ( G== && A>=20 && A<=30 && ( D== B D== M D== D ) ) printf ("OK"); Use the following for Statement to answer the next 3 questions (A-C). for (Count=Start; Count<=inish; Count++) printf ("%d", Count); (A) Which variable is the "control variable"? Count (B) What does Count++ mean? _Increment Count by 1 after a pass is complete (C) If Start contains a value of -4 and inish contains the value 0, how many times will the loop execute? Answer: 5 lowchart a program with a loop that accumulates whole numbers entered by the user until a -1 is entered and then identifies and displays the sum. hen write the C code to the right of it. Start /* accumulate.c */ #include <stdio.h> ot 0 int N, ot; N is not -1 Sum is: ot Number (-1 to stop)? N N is not -1 ot ot + N End void main (void) ot = 0; do printf ("Number (-1 to stop)? "); scanf ("%d", &N); if (N!= -1) ot = ot + N; while (N!= -1); printf ("Sum is: %d\n", ot); Page 5 Revision 2010-10

or each question below, circle any and all loop structures for which the claims are true: (A) he test for each repetition is performed after the pass through the loop body. (B) he body will always be executed at least once. (C) his structure is appropriate for sentinel controlled loops that use a "prime read". (D) Sentinel controlled loops using this structure need an extra test to guard the body from the sentinel value. or each question below, circle any and all loop control methods for which the claims are true: (A) his control method is based on values assigned by the programmer, not given by the user. counting sentinel (B) his control method is based on values entered by the user. counting sentinel (C) he number of passes that the loop will perform can always be predetermined when the loop starts. counting sentinel Page 6 Revision 2010-10