CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Modular Programming With Functions
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Chapter 3 - Structured Program Development
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
C++ for Engineers and Scientists Third Edition
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Structured Program Development in C
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CMSC 1041 Functions II Functions that return a value.
C Lecture Notes 1 Structured Program Development.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 CS161 Introduction to Computer Science Topic #8.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
COMP Loop Statements Yi Hong May 21, 2015.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Decision making If.. else statement.
Chapter 10 Programming Fundamentals with JavaScript
CprE 185: Intro to Problem Solving (using C)
Chapter 2 - Introduction to C Programming
Revision Lecture
Chapter 2 - Introduction to C Programming
Chapter 10 Programming Fundamentals with JavaScript
Chapter 2 - Introduction to C Programming
Structured Program
Chapter 3 - Structured Program Development
3 Control Statements:.
Decision making If statement.
Chapter 3 - Structured Program Development
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Understand the interaction between computer hardware and software
EPSII 59:006 Spring 2004.
Structural Program Development: If, If-Else
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review

One More Loop Example

Loops, final example A projectile launched with initial velocity v 0 at an angle a will travel a horizontal distance d given by Find the angle that maximizes the distance. d

Loops, final example d_max = 0; alpha_max = 0; for(alpha = 0; alpha <= 90; alpha += 5) { alpha_radians = alpha*PI/180; d = v0*v0*sin(2*alpha_radians)/G; if(d > d_max){ d_max = d; alpha_max = alpha; }

Generating Random Numbers

Generating random values Some programs require generation of “random” numbers A “pseudorandom” number generator is an algorithm that generates a sequence of numbers within a range –For example: integers between 1 and 10 A “pseudorandom” generator actually creates the numbers in a deterministic way, but they appear to be random –After a while a psuedorandom generator would repeat the sequence

Generating random values The numbers generated by a “pseudorandom” generator approximate properties of random numbers –For example: values are uniformly distributed within the range Pseudorandom number generator requires a “seed” value that determines the values that will be generated –Giving different seed values generates different sequences –If you give the same seed, you will get the same sequence

Generating random values in C C’s stdlib includes a pseudorandom number generator –Include for rand and srand functions Use rand() to get pseudorandom value from 0 to RAND_MAX (defined in too) –But always same sequence unless change seed value –Pass an unsigned int seed to srand function: srand(seed) How to get a different seed every different time? Use time! –C has a library: with a time function –Using srand( time(NULL) ) will provide a different seed based on time Can use % operator to restrict range. E.g., see …/demos/rolldice.c …/demos/rolldice.c

Midterm Review and Practice

What did we learn so far? We covered the first 3 chapters of the book: –General concepts about computers –Basic concepts about C programs: basic types, assignment, input-output, etc. –C control structures, stepwise refinement Let’s do a review and some practice problems

Chapter 1: General concepts about computers Basic architecture Basic concepts about computers: –what is: hardware, software, operating system, compiler, assembly language, programming language, source code, object code, software life cycle, …

Practice problem Match the following words and definitions Compiler, ALU, operating system kernel, processor, machine language 1.A software component that manages the interface between the hardware and the software applications 2.The part of a computer that performs mathematical computations 3.A program that translates programs written in high level language to a lower level (or machine) language

Chapter 2: Basic C concepts Basic C concepts: –representation of numeric values, different base representations –representation of characters, ASCII code –basic types, type-casting –variable declarations, identifiers –overflow, underflow –evaluation of numeric expressions (precedence, associativity) –assignment statement –input/output with printf and scanf

Practice problem Is there a syntax error in the following declarations, and what is it? int x_5;/* OK */ int _y;/* OK */ int 2y;/* syntax error */ int y2; /* OK */ float/* something */ z,y = 4 ;/* OK */ fl/* something */oat x; /* syntax error */ float int1;/* OK */

Practice problem What will be printed? int x = 25; float y; float z = 3.0; printf(“%x”,x); prints: 19 printf(“%o”,x); prints: 31 printf(“%d”,x); prints: 25 y = x / 3; printf(“%+10.3f”, y); prints: y = x / z; printf(“%-+10.3f”, y); prints:

Practice problem What will be printed? int x; x = 12; printf(“%d”, x); prints: 12 x = 012; printf(“%d”, x); prints: 10 x = 0x12; printf(“%d”, x); prints: 18

Chapter 3: Structured programming, control flow Relational operators, relational expressions, logical operators, logical expressions Control flow structures in C –if, if/else, while, for, do/while –Use of { and } to group statements into a block Stepwise refinement –Top-down approach for solving a programming problem

Practice problem Determine if the given expressions evaluate to true or false float a = 5.5; float b = 1.5; float k = -3; a < k/* true */ a + b >= 6.5/* true */ k != a – b/* true */ b – k > a/* false */ ! ( a == 3 * b)/* true */ -k <= k + 6;/* true */ a 5/* true */ fabs(k) > 3 || k < b – a/* false */

Practice problem Fully parenthesize the expressions based on the evaluation order a < k a + b >= 6.5 k != a – b b – k > a ! ( a == 3 * b) -k <= k + 6; a 5 fabs(k) > 3 || k < b - a

Practice problem Parenthesized version a < ( k) (a + b) >= 6.5 k != (a – b) (b – k) > a ! (a == (3 * b)) (-k) <= (k + 6); (a 5) (fabs(k) > 3) || (k < (b – a))

Practice problem int x = 2, y = 10, z = 5; if (x < y / z) x = 1; /* program point 1 */ else x = 0; /* program point 2 */ if (z < y - x) if (y > 2*x) y = 0; /* program point 3 */ else if (y < 20) y = 1; /* program point 4*/ z = (x + 1) / (y + 2) + 7; Which program points are reached? What are the values of x, y, z after executing?

Practice problem Determine the number of times the following for loops are executed for (k = 3; k <= 10; k++) { … }/* 8 times */ for (k = 3; k <= 10; ++k) { … }/* 8 times */ for (k = -4; k <= 14; k += 2) { … } /* 10 times */

Practice problems What is the value of count after the nested for loops are executed? int k, j, count = 0; for (k = -1; k <= 3; k++) for (j = 3; j >= 1; j--) count++; Outer loop executes 5 times Inner loops executes 3 times for each execution of the outer loop So, count++ is executed 5×3 = 15 times At the end the value of count is 15

Practice problems Write program segments which do the following: Write a program that reads in a test score and prints a letter grade using an if-else control structure. 100 = 90 gets grade A 90 > points >= 80 gets grade B 80 > points >= 70 gets grade C 70 > points >= 60 gets grade D 60 > points >= 80 gets grade F points > 100 or points < 0 prints error message Compute the sum from 1 to n for a given n Example: user inputs 6, output should be 21 becuase = 21 Compute the maximum value among 3 numbers Example: user inputs 3, 7, 1. Output 7. Compute the maximum value among n (arbitrary) numbers Example: user inputs 4, 6, 15, 9, 7, 14, 6. Output should be 15. Sort three numbers Example: user inputs 3, 7, 1. Output 1, 3, 7 (ascending order)

Project Submission and Questions