CS114-009 - Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Writing and Testing Programs Drivers and Stubs Supplement to text.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
Chapter 4 Summation.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Programming is instructing a computer to perform a task for you with the help of a programming language.
High-Level Programming Languages: C++
Chapter 6 Looping.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
CS Class 13 Today  File I/O (reading from and writing to files)  Reading until EOF  Formatting output  Nested Loops Announcements  Programming.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Chapter 02 (Part III) Introduction to C++ Programming.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
Count and add list of numbers From user input and from file.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Objective: Students will be able to: Declare and use variables Input integers.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Chapter 2 Assignment and Interactive Input
CS 1428 Exam I Review.
Control Statements Kingdom of Saudi Arabia
Chapter 4 Loops Case Studies
Quiz Next Monday.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Counting Loops.
Computing Fundamentals
Let’s all Repeat Together
CS1201: Programming Language 2
Programs written in C and C++ can run on many different computers
Fundamental Programming
Reading from and Writing to Files
CS31 Discussion 1D Winter19: week 3
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files Part 2
CS 1428 Exam I Review.
Reading from and Writing to Files
Presentation transcript:

CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements  Programming Project 1 due Thurs. by midnight ( to  Exam 1 – New date??  Read pages ( ) for next time

FYI iostream is a header file using for input/output in C++ that is part of the C++ standard library  Collection of classes, constants, functions, objects and templates If you didn’t have using namespace std  You would need to add std:: in front of cout to tell it to use the std (standard) library  e.g. std::cout << “hello” << endl;  more than on library can use same name

When software goes wrong… What does software do?  Payrolls, bills, businesses…  Phones & other ‘net’ systems  Elevator controls  Anti-lock brake controllers  Insulin pumps, heart pacemakers  X-ray therapy equipment  Aircraft control (“fly by wire”) What happens when software fails?  Bills and paychecks wrong  When phones die…  Elevators…  Auto brakes…  Insulin pumps…  X-ray machines with bad software have killed Therac-25 (100 times dose)  Aircraft Commercial passenger planes have crashed

How important is it for software to work? If software doesn’t work,  Things go wrong,  Businesses fail,  People lose their jobs,  People are injured,  People even die… Many people have died!

How can we make software work? Careful planning: algorithms, good teaming skills, good communication Teaching those skills Making sure students know what we’re teaching.

Class exercises Write a C++ program that reads in five integers and prints out their product (the result of multiplying them together)  Example , output , output , output 12 Write a C++ program that reads in two integers and prints out all the numbers from the first to the second  The first value read will always be less than (or equal to) the second  Example 8 13 Output: Important: Figure out your algorithm (on paper) before you start to code these problems.

Count-controlled loops You know exactly how many times the loop will execute  Have this value in a variable Example int main( ) { int num, count = 0, sum = 0; while (count < 10) { cout << “Enter a num: ”; cin >> num; sum = sum + num; count = count + 1; } cout << “The sum is :” << sum << endl; return 0; }

Don’t know when the data will stop, you just know the last value. Sentinel-controlled loops (input) Read data until input is –1 int main( ) { int sum=0, data; cout<< “Enter data (-1 to quit)” << endl; cin >> data; while (data != -1) { sum = sum + data; cin >> data; } cout << “Sum is: “ << sum << endl; return 0; } –1

Class exercise Write a C++ program that reads in a sequence of 1’s and 0’s, stopping when the value 2 is read, and then prints out the value that occurred the most often.  Consider the algorithm: This is only a minor change from the previous example.  What needs to change?

Tracing – What is the output? #include using namespace std; int main ( ) { int k = 0, num = 1; while (k < 4) { cout << num << endl; k = k + 1; num = num * 10; } cout << "final num = " << num << endl; return 0; }

Tracing What is output by the program below? #include using namespace std; int main ( ) { int k = 0, num = 1; while (k < 4) { cout << num << endl; k = k + 1; num = num * 10; } cout << "final num = " << num << endl; return 0; } knumOutput final num = 10000

More tracing #include using namespace std; int main( ) { int a= 5, b = 0; while (a > 0) { cout << a << “ “; cout << b << endl; b = b + a; a = a – 1; } cout << b << endl; return 0; } Variables Output AB

Class exercises (do at least two) Write a C++ program that reads in 20 integers, counts the number of odd and even numbers, and prints out the number of odd and even numbers  Hint: what does num%2 do? Write a C++ program that reads in 10 integers and prints out the largest and smallest values seen Write a C++ program that reads in 20 characters (you will only see vowels in the input – a, e, i, o, and u) and prints out the vowel that occurred the most often Important: figure out your algorithm before you start to code these problems

End of Class 07 Read pages for next time If you have not yet read Chapters 1, 2 & 3 you need to do it now.