Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements
|
|
- Camron Daniels
- 5 years ago
- Views:
Transcription
1 Chapter 5 Looping
2 Building on the foundation Now that we know a little about cout cin math operators boolean operators making decisions using if statements
3 Advantages of Computers Computers are really quick Computers don't get bored They can do the same thing over and over and be happy
4 Loops What is the effect of a loop? { some C++ code; some C++ code; some C++ code; some C++ code; some C++ code; some C++ code; some C++ code; as long as some condition is true
5 While loops while ( num < 10 ) { cout << num = << num << endl; num = num + 1; Bool Exp True False Code Block
6 While loops while ( num < 10 ) cout << Hello there \n ; As before if there is only one line in the body of the loop, the { are not needed. But...what is the problem? Bool Exp True Code Block False
7 While loops while ( num < 10 ) cout << Hello there \n ; As before if there is only one line in the body of the loop, the { are not needed. But...what is the problem? Infinite Loop: a loop that doesn't end Bool Exp True Code Block False
8 What is a difference between if and while Which of the following is the if (left or right)? Bool Exp Bool Exp True False True False Code Block Code Block
9 What is a difference between if and while Which of the following is the if (left or right)? Bool Exp while Bool Exp if False True Code Block False True Code Block
10 Class Exercise Ask the user for 10 integers Return to the user the total of the 10 integers and their average Things to think about: What part needs to be repeated? This will go in the loop body How many variables do you need? Please enter 10 numbers num 1: 23 num 2: num 10: -34 The total of your 10 numbers is: 345 The average for the 10 numbers is: 34
11 Answer int num = 1, total = 0, temp; cout << Please enter 10 numbers \n while ( num <= 10 ) { cout << num << num << : ; cin >> temp ; total = total + temp; num += 1; cout << The total of your 10 numbers is: << total << endl; cout << The average of your 10 number is: << ( total / 10 ) << endl;
12 Counter Controled Loop Counter Controled Loop uses a variable (num) to count and control when the loop stops int num = 1, total = 0, temp; cout << Please enter 10 numbers \n while ( num <=10 ) { cout << num << num << : ; cin >> temp ; total = total + temp; num = num + 1; cout << The total of your 10 numbers is: << total << endl;
13 Another Class Exercise Most of the time our DOS screen is 80 characters wide To help with making a pretty display, make a loop that will print 80 numbers across the screen...but always print from 1 to 10 ( 0 will represent a 10)
14 Answer int count = 1, output = 1; while ( count <= 80 ) { cout << output ; count = count + 1; output = output + 1; if (output > 9 )// reset output to 0 if over 9 output = 0;
15 Another Answer int count = 1, output = 1; while ( count <= 80 ) { cout << output ; count = count + 1; // reset output to 0 if over 9 output = ( output > 9? 0 : output + 1 );
16 Another Answer int num = 1 ; while ( num <= 80 ) { cout << ( num % 10 ) ; num = num + 1;
17 While loops You can put any kind of code in the code block... Bool Exp True False Code Block
18 While loops You can put any kind of code in the code block......even other loops Bool Exp True Code Block False Bool Exp True Code Block False
19 Embedded loops (a loop inside of another loop) In everyday life we have an embedded loop:???? Bool Exp Bool Exp True Code Block False True Code Block False
20 Embedded loops (a loop inside of another loop) In everyday life we have an embedded loop: Time minutes Bool Exp hours Bool Exp True False True False Code Block Code Block
21 Creating Some Time Output int hour = 0, min; while ( hour < 24 ) { min = 0; while ( min < 60 ) { cout << hour << ':' << min << endl; min = min + 1; // end of minute loop hour = hour + 1; // end of hour loop
22 int hour = 0, min; Now What is the Output? while ( hour < 24 ) { min = 0; while ( min < 60 ) { cout << (hour < 10? '0' : ) << hour; cout << ':' ; cout << (min < 10? '0' : ) << min << endl; min = min + 1; // end of minute loop hour = hour + 1; // end of hour loop
23 Random Numbers Generating random numbers is often useful In a game, player do not want the same behavior/moves each time In a simulation of a complex system, random numbers can be used to help generate random events Car crash in a simulation of a highway system Likelihood of a gene in cell mutation Weather simulation
24 Random numbers in real life Examples Generate a random number sequence in the range 1 to 6 Use a six-sided die ( 色子 shǎi zi) Each roll represents a new random number Generate a random number sequence in the range 1 to 2 Use a fair coin Heads: 1, Tails: 2
25 Random Numbers We can write an algorithm for generating what looks like random numbers Because it s an algorithm, we know the rules for generating the next number The generated numbers are not really random They are properly called pseudo-random numbers
26 Stdlib Library Provides functions for generating pseudo-random numbers rand() Returns a pseudo-random unsigned int in the range of 0 to RAND_MAX (32,767) #include <iostream> #include <cstdlib> using namespace std; int main() { int i = 1; while( i <= 5) { cout << rand() << endl; ++i; return 0;
27 Program Output Output
28 Output # Output # Program Output for two runs Every time the program is run we will get the same random numbers
29 Different Sequences To produce a different sequence, use another function (seed random) void srand(unsigned int); Example srand(10); cout << rand() << endl;
30 Different Sequences Consider seed.cpp int main() { cout << "Enter a seed: "; unsigned int seed; cin >> seed; srand(seed); int i = 1; while(i <= 5) { cout << rand() << endl; ++i; return 0;
31 Different Sequences To automatically get a different sequence each time Need a method of setting the seed to a random value The standard method is to use the computer's clock as the value of the seed The function time() can be used Returns an integer value time(0) returns a good value for generating a random sequence
32 Randseed.cpp #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand( time(0) ); int i = 1; while(i <= 5) { cout << rand() << endl; ++i; return 0;
33 Output #1 Program Output for two runs Output # Every time the program is run we will get the different random numbers
34 Sentinel Controlled Loops Continue doing the loop until a special value (the sentinel) is encounter
35 Sentinel Controlled Loops Continue doing the loop until a special value (the sentinel) is encounter Example: Before we asked the user for 10 numbers and then we told the user total of the 10 numbers average of the 10 numbers
36 Sentinel Controlled Loops Continue doing the loop until a special value (the sentinel) is encounter Example: Before we asked the user for 10 numbers and then we told the user total of the 10 numbers average of the 10 numbers Now we want the user to enter as many numbers as they want a negative number is the signal that they are done ( the sentinel )
37 int count = 1, total = 0, input = 0; Will this work? cout << Please enter some positive numbers that I will average \n ; cout << Enter a negative number to signal that you are done \n ; while ( input >= 0 ) { cout << # << count << : ; cin >> input; total = total + input; count = count + 1; count = count 1; try try it it with: with: if ( count > 0 ) { cout << The total of your << count ; cout << numbers is: << total << endl; cout << The average of your << count cout << number is: << ( total / count ) << endl;
38 int count = 1, total = 0, input = 0; Will this work? No cout << Please enter some positive numbers that I will average \n ; cout << Enter a negative number to signal that you are done \n ; while ( input >= 0 ) { cout << # << count << : ; cin >> input; total = total + input; count = count + 1; count = count 1; try try it it with: with: if ( count > 0 ) { cout << The total of your << count ; cout << numbers is: << total << endl; cout << The average of your << count cout << number is: << ( total / count ) << endl;
39 int count = 0, total = 0, input = 0; Will this work? cout << Please enter some positive numbers that I will average \n ; cout << Enter a negative number to signal that you are done \n ; while ( input >= 0 ) { total = total + input; count = count + 1; cout << # << count << : ; cin >> input; count = count 1; try try it it again again with: with: if ( count > 0 ) { cout << The total of your << count ; cout << numbers is: << total << endl; cout << The average of your << count cout << number is: << ( total / count ) << endl;
40 int count = 0, total = 0, input = 0; Will this work? cout << Please enter some positive numbers that I will average \n ; cout << Enter a negative number to signal that you are done \n ; while ( input >= 0 ) { total = total + input; count = count + 1; cout << # << count << : ; cin >> input; try try it it again again with: with: -5-5 count = count 1; if ( count > 0 ) { cout << The total of your << count ; cout << numbers is: << total << endl; cout << The average of your << count cout << number is: << ( total / count ) << endl;
41 Infinite loops ( 无限循环 wú xiàn xún huán) Common Infinite Loops while ( ch = y ) { Remember: non-zero numbers true zero false
42 Infinite loops ( 无限循环 wú xiàn xún huán) Common Infinite Loops while ( ch = y ) { or int i = 0 while ( i < 10 ) { // forgot i++; Remember: non-zero numbers true zero false
43 Other equality notes == is great for comparing integers, but should not be used for floating-point numbers instead use comparison operators ( <, <=, >, >= ) float f; if ( f == 100 ) // f: // do something if ( f >= 100 ) // do something // instead do this
44 Looping Control Structures Three different commands while statement do-while statement...
45 The Do-While Statement Syntax do code block while (bool expr); True Code Block Bool Exp False
46 Using a do while loop char reply; do {... // some code... cout << "Do you want to continue?(y):"; cin >> reply; while(reply == 'y');
47 Why use a...? What is the advantage of a while: do...while:
48 Why use a...? What is the advantage of a while: do...while: body will execute 0 N times body will execute 1 N times
49 Looping Control Structures Three different commands while statement do-while statement for statement
50 Common use of loop int cnt = 0; // initialize while ( cnt < 10 ) { // check...; // do something...body of loop...; cnt++; // update
51 The for loop Syntax for (initialization ; bool expression ; update action) body of the loop Example for (int i = 0; i < 3; i++) { cout << "i is " << i << endl;
52 Executed once at the beginning of the for loop's execution Initialization Bool Exp true false The Bool Exp is evaluated at the start of each iteration of the loop Body Update Action
53 Execution Example i 0 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl;
54 Execution Example i 0 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl;
55 Execution Example i 0 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0
56 Execution Example i 0 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0
57 Execution Example i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0
58 Execution Example i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0
59 Execution Example i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1
60 Execution Example i 1 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1
61 Execution Example i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1
62 Execution Example i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1
63 Execution Example i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1 i is 2
64 Execution Example i 2 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1 i is 2
65 Execution Example i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1 i is 2
66 Execution Example i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1 i is 2
67 Execution Example i 3 for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; cout << "all done" << endl; i is 0 i is 1 i is 2 all done
68 Embedding for loops Just like while loops, for loops can also be embedded inside other loops in fact, due to there compactness (information being easier to read) many embedded loops will be for loops
69 int cntr1 =0, cntr2 =0, cntr3 =0,cntr4 =0, cntr5 =0; cntr1++; for (int i = 1; i <= 10; ++i) { cntr2++; for (int j = 1; j <= 20; ++j) { cntr3++; cntr4++; cntr5++; What is the output? cout << cntr1: << cntr1 << endl << cntr2: << cntr2 << endl << cntr3: << cntr3 << endl << cntr4: << cntr4 << endl << cntr5: << cntr5 << endl;
70 Answer: cntr1: 1 cntr2: 10 cntr3: 200 cntr4: 10 cntr5: 1 Press any key to continue
71 Class Exercise Write code to display a grid. For each position, display the location s row and column number. Put two blanks between each location. Ex: 1,1 1,2 1,3... 1,9 2,1 2,2... 2,9... 8,1 8,2... 8,9 9,1 9,2 9,3...9,9
72 One solution to grid exercise for(int row = 1; row < 10; row++ ){ for(int col = 1; col < 10; col++) { cout << row <<, << col << ; // end of col loop cout << endl; // end of row loop
73 Another use of break break can be used to exit a loop // in some game while ( life > 0 ) {... // play game cout << Do you want to quit(q): ; cin >> userresponse; if (userresponse == q ) break; // end while loop not the best example
74 A better solution // in some game userresponse = c ; while ( life > 0 && userresponse!= q ) { // play game... cin >> userresponse; // end while loop
75 The best solution // in some game do { // play game... cin >> userresponse; while ( life > 0 && userresponse!= q );
76 Another use of break break will only exit out of one loop (if embedded) for ( int row = 1; row < 10; row++ ) { for ( int col = 1; col < 10; col++) { // do something if (???? ) break; // col loop // this will stop the col loop // row loop
77 Continue, the brother of break the key word, continue, will cause execution to skip to the boolean expression while( something == true ) { // do something if ( x > 0 ) continue; // skip to end of block // do some more... // end of while
78 Continue, the brother of break the key word, continue, will cause execution to skip to the boolean expression while( something == true ) { // do something if ( x > 0 ) continue; // skip to end of block // do some more... // end of while
79 Using break and continue in loops First look for a solution that does not use break or continue Only use break and continue if it is the only solution or it makes the code easier to understand Note: always avoid using goto... there is almost always a better way
80
Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved
Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test
More informationLab Instructor : Jean Lai
Lab Instructor : Jean Lai Group related statements to perform a specific task. Structure the program (No duplicate codes!) Must be declared before used. Can be invoked (called) as any number of times.
More informationWhy Is Repetition Needed?
Why Is Repetition Needed? Repetition allows efficient use of variables. It lets you process many values using a small number of variables. For example, to add five numbers: Inefficient way: Declare a variable
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationIslamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.
Islamic University of Gaza Computer Engineering Dept. C++ Programming For Industrial And Electrical Engineering By Instructor: Ruba A. Salamh Chapter Four: Loops 2 Chapter Goals To implement while, for
More informationChapter Four: Loops II
Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing
More informationC++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures
More informationName SECTION: 12:45 2:20. True or False (12 Points)
Name SECION: 12:45 2:20 rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables
More informationTutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations
Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Outline 12.1 Test-Driving the Craps Game Application 12.2 Random Number Generation 12.3 Using an enum in the Craps
More informationMultiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationUnit 7. 'while' Loops
1 Unit 7 'while' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationMultiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationWeek 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.
Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than
More informationFunctions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single
Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module
More informationComputer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite
More informationCPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)
Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow
More information#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }
1. (9 pts) Show what will be output by the cout s in this program. As in normal program execution, any update to a variable should affect the next statement. (Note: boolalpha simply causes Booleans to
More informationChapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?
Chapter 5: Control Structures II (Repetition) Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use countercontrolled, sentinel-controlled,
More informationMultiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)
Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program
More informationStructured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1
ELEC 330 1 Structured Programming Control Structures ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne Algorithm Development Conditional Expressions Selection Statements Loops 206_C3
More informationwhile for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)
Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent
More informationTHE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)
THE INTEGER DATA TYPES STORAGE OF INTEGER TYPES IN MEMORY All data types are stored in binary in memory. The type that you give a value indicates to the machine what encoding to use to store the data in
More informationSolving a 2D Maze. const int WIDTH = 10; const int HEIGHT = 10;
Solving a 2D Maze Let s use a 2D array to represent a maze. Let s start with a 10x10 array of char. The array of char can hold either X for a wall, for a blank, and E for the exit. Initially we can hard-code
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationDesigning Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6
Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #5 due today Lab #3
More informationCS 103 Lecture 3 Slides
1 CS 103 Lecture 3 Slides Control Structures Mark Redekopp 2 Announcements Lab 2 Due Friday HW 2 Due next Thursday 3 Review Write a program to ask the user to enter two integers representing hours then
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationCPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)
Name rue or False (12 Points) 1. (12 pts) Circle for true and F for false: F a) Local identifiers have name precedence over global identifiers of the same name. F b) Local variables retain their value
More information1 Unit 8 'for' Loops
1 Unit 8 'for' Loops 2 Control Structures We need ways of making decisions in our program To repeat code until we want it to stop To only execute certain code if a condition is true To execute one segment
More information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators
Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;
More informationProgramming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester
Programming Language Control Structures: Repetition (while) Eng. Anis Nazer Second Semester 2017-2018 Repetition statements Control statements change the order which statements are executed Selection :
More informationFunctions and Recursion
Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 5: Control Structures II (Repetition) Why Is Repetition Needed? Repetition allows you to efficiently use variables Can input,
More informationMore Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4
More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB HOURS! Thursday, 10 AM 12 PM
More informationdo { statements } while (condition);
Topic 4 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:
More informationPartha Sarathi Mandal
MA 253: Data Structures Lab with OOP Tutorial 1 http://www.iitg.ernet.in/psm/indexing_ma253/y13/index.html Partha Sarathi Mandal psm@iitg.ernet.in Dept. of Mathematics, IIT Guwahati Reference Books Cormen,
More informationName Section: M/W T/TH Number Definition Matching (8 Points)
Name Section: M/W T/TH Number Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Iteration Counter Event Counter Loop Abstract Step
More informationProgramming Language. Functions. Eng. Anis Nazer First Semester
Programming Language Functions Eng. Anis Nazer First Semester 2016-2017 Definitions Function : a set of statements that are written once, and can be executed upon request Functions are separate entities
More informationName Section: M/W T/TH Number Definition Matching (6 Points)
Name Section: M/W T/TH Number Definition Matching (6 Points) 1. (6 pts) Match the words with their definitions. Choose the best definition for each word. Event Counter Iteration Counter Loop Flow of Control
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 informationFORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),
More information5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.
More informationIncrement and the While. Class 15
Increment and the While Class 15 Increment and Decrement Operators Increment and Decrement Increase or decrease a value by one, respectively. the most common operation in all of programming is to increment
More informationElements of C in C++ data types if else statement for loops. random numbers rolling a die
Elements of C in C++ 1 Types and Control Statements data types if else statement for loops 2 Simulations random numbers rolling a die 3 Functions and Pointers defining a function call by value and call
More informationChapter 10 - Notes Applications of Arrays
Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationLAB: WHILE LOOPS IN C++
LAB: WHILE LOOPS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 2 Introduction This lab will provide students with an introduction
More informationC++ Final Exam 2017/2018
1) All of the following are examples of integral data types EXCEPT. o A Double o B Char o C Short o D Int 2) After the execution of the following code, what will be the value of numb if the input value
More informationChapter 17 - Notes Recursion
Chapter 17 - Notes Recursion I. Recursive Definitions A. Recursion: The process of solving a problem by reducing it to smaller versions of itself. B. Recursive Function: A function that calls itself. C.
More informationBITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)
BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18) 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional
More informationFundamentals of Programming CS-110. Lecture 2
Fundamentals of Programming CS-110 Lecture 2 Last Lab // Example program #include using namespace std; int main() { cout
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 2 Monday, March 20, 2017 Total - 100 Points B Instructions: Total of 13 pages, including this cover and the last page. Before starting the exam,
More informationA SHORT COURSE ON C++
Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008 Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping Introduction
More information1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?
Exercises with solutions. 1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl? #include b) What using statement do you always put at the top of
More informationGE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004
GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004 Functions and Program Structure Today we will be learning about functions. You should already have an idea of their uses. Cout
More informationCHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad Outline 1. C++ Iterative Constructs 2. The for Repetition Structure 3. Examples Using the for Structure 4. The while Repetition Structure
More information! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like
Indefinite loops while loop! indefinite loop: A loop where it is not obvious in advance how many times it will execute.! We often use language like " "Keep looping as long as or while this condition is
More informationIntroduction to Programming
Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines
More informationIntroduction to Programming
Introduction to Programming Summer Term 2015 Dr. Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 State: May 6, 2015 Betriebssysteme / verteilte Systeme
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing
More informationChapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition) 1 Objectives Learn about repetition (looping) control structures Explore how to construct and use count-controlled, sentinel-controlled, flag-controlled, and
More informationInput And Output of C++
Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout
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 informationModule Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1
UNIVERSITY OF EAST ANGLIA School of Computing Sciences Main Series UG Examination 2016-17 INTRODUCTORY PROGRAMMING CMP-0005A Time allowed: 2 hours Answer BOTH questions from Section A and ONE question
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationIntroduction to Programming EC-105. Lecture 2
Introduction to Programming EC-105 Lecture 2 Input and Output A data stream is a sequence of data - Typically in the form of characters or numbers An input stream is data for the program to use - Typically
More informationExample 3. #include <iostream> using namespace std; int main()
1 Repetition Structure Examples Example 1 #include float number, sum=0; // number to be read cin >> number; // read first number cout
More informationc++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.
Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object
More information1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?
Instructions: This homework assignment focuses primarily on some of the basic syntax and semantics of C++. The answers to the following questions can be determined from Chapters 6 and 7 of the lecture
More informationGovernment Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING
Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++
More informationProblem Solving: Storyboards for User Interaction
Topic 6 1. The while loop 2. Problem solving: hand-tracing 3. The for loop 4. The do loop 5. Processing input 6. Problem solving: storyboards 7. Common loop algorithms 8. Nested loops 9. Problem solving:
More informationIntroduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)
Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program
More informationLaboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++
Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++ 0.1 Introduction This is a session to familiarize working with the Visual Studio development environment. It
More informationObject-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering
Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25 Visual C++: Problems and Solutions New section on web page (scroll down)
More informationC++ Quick Reference. switch Statements
C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) if (condition) else if (condition1) else if (condition2) else Loop Statements while (condition) do while (condition);
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 informationI SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.
I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK 70. a) What is the difference between Hardware and Software? Give one example for each. b) Give two differences between primary and secondary memory.
More informationOther operators. Some times a simple comparison is not enough to determine if our criteria has been met.
Lecture 6 Other operators Some times a simple comparison is not enough to determine if our criteria has been met. For example: (and operation) If a person wants to login to bank account, the user name
More informationCSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.
Name: Print legibly! Section: COMPUTER SCIENCE 261 PROGRAMMING CONCEPTS EXAM 1 VERSION 1 Fall 2014 150 Points Absolutely no electronic devices may be used during this exam. 1. No cell phones, computers,
More informationC++ Programming Lecture 7 Control Structure I (Repetition) Part I
C++ Programming Lecture 7 Control Structure I (Repetition) Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department while Repetition Structure I Repetition structure Programmer
More informationC++ basics Getting started with, and Data Types.
C++ basics Getting started with, and Data Types pm_jat@daiict.ac.in Recap Last Lecture We talked about Variables - Variables, their binding to type, storage etc., Categorization based on storage binding
More informationThe cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;
The cin Object Short for console input. It is used to read data typed at the keyboard. Must include the iostream library. When this instruction is executed, it waits for the user to type, it reads the
More informationLoops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017
Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationBITG 1113: Array (Part 1) LECTURE 8
BITG 1113: Array (Part 1) LECTURE 8 1 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the fundamentals of arrays 2. Describe the types of array: One Dimensional (1 D)
More informationOverview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program
Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationDELHI PUBLIC SCHOOL TAPI
Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationObject oriented programming
Exercises 12 Version 1.0, 9 May, 2017 Table of Contents 1. Virtual destructor and example problems...................................... 1 1.1. Virtual destructor.......................................................
More informationLab # 02. Basic Elements of C++ _ Part1
Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special
More informationThe following expression causes a divide by zero error:
Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct
More informationC++ For Science and Engineering Lecture 12
C++ For Science and Engineering Lecture 12 John Chrispell Tulane University Monday September 20, 2010 Comparing C-Style strings Note the following listing dosn t do what you probably think it does (assuming
More informationC++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay
C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming
More informationCh 6. Functions. Example: function calls function
Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout
More informationLocal and Global Variables
Lecture 10 Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other.
More informationCS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM
CS 117 Programming II, Spring 2018 Dr. Ghriga Midterm Exam Estimated Time: 2 hours March 21, 2018 DUE DATE: March 28, 2018 at 12:00 PM INSTRUCTIONS: Do all exercises for a total of 100 points. You are
More information