L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Computer Programming w/ Eng. Applications
Introduction to C Programming
True or false A variable of type char can hold the value 301. ( F )
An Introduction to Programming with C++ Fifth Edition
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
1 Shortcuts for Lazy Programmers! Topics Increment and Decrement Operators Assignment Operators.
Introduction to C Programming
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CMSC 1041 Functions II Functions that return a value.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Recap……Last Time [Variables, Data Types and Constants]
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Chapter 9: Value-Returning Functions
Chapter Topics The Basics of a C++ Program Data Types
Lecture 7: Repeating a Known Number of Times
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Basic Elements of C++.
Iterative Constructs Review
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Basic Elements of C++ Chapter 2.
Chapter 5 - Functions Outline 5.1 Introduction
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections 5A.5

L132 Increment and Decrement Operators The increment operator ++ The decrement operator -- Precedence: lower than (), but higher than * / and % Associativity: right to left Increment and decrement operators can only be applied to variables, NOT to constants or expressions

L133 Precedence of Operators in this Lect. ( )left to right right to left * / %left to right + - left to right >=left to right == !=left to right = += -= *= /= %= right to left

L134 Increment Operator If we want to add one to a variable, we can say: count = count + 1 ; Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR ++count ; Both do the same thing. They change the value of count by adding one to it.

L135 Post-Increment Operator The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a postincrement). int amount, count ; count = 3 ; amount = 2 * count++ ; amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.

L136 Pre-Increment Operator If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.

L137 Code Example Using ++ #include int main ( ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) { printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ; }

L138 Same result as above, more concise # include int main ( ) { int counter = 0 ; /* initialization */ while ( ++counter <= 10 ) /* condition & inc. */ { printf (“%d ”, counter ) ; } return 0; }

L139 A while loop with ++i i=0; while ( + + i < 11 ) { printf(“%d ”, i ); } Output is:

L1310 A while loop with i++ i=0; while ( i ++ < 11 ) { printf(“%d ”, i ); } Output is:

L1311 The Increment expression in the for The following are all equivalent in the incrementing portion of the for structure: counter = counter + 1 counter += 1 ++counter counter++ Incrementing occurs after the body of the for is executed, so counter++ seems more appropriate. for ( counter = 1 ; counter < 10 ; counter++ )

L1312 Decrement Operator If we want to subtract one from a variable, we can say: count = count - 1 ; Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR --count ; Both do the same thing. They change the value of count by subtracting one from it.

L1313 Post-Decrement Operator The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a postdecrement). int amount, count ; count = 3 ; amount = 2 * count-- ; amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.

L1314 Pre-Decrement Operator If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.

L1315 A Hand Trace Example int answer, value = 4 ; Code Value Answer 4garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value - - ; - -value ; answer = - -value * 2 ; answer = value - - / 3 ;

L1316 Practice Given int a = 1, b = 2, c = 3 ; What is the value of this expression? ++a * b - c - - What are the new values of a, b, and c?

L1317 More Practice Given int a = 1, b = 2, c = 3, d = 4 ; What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d?

L1318 Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ;a %= 2 ; b = b + ( c + 2 ) ;b += c + 2 ; d = d * ( e - 5 ) ;d *= e - 5 ;

L1319 Practice with Assignment Operators int i = 1, j = 2, k = 3, m = 4 ; Expression Value i += j + k j *= k = m + 5 k -= m /= j * 2

L1320 Code Example Using /= and ++ Counting the Digits in an Integer #include int main ( ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“ %d \n ”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d.\n”, digits, num) ; return 0 ; }

L1321 Output of the code example: num digits printf temp There are 4 digits in 4327 “Boxes Indicate Printed Output”

L1322 Counter Controlled Repetition requires: 1) The name of a control variable (loop counter) 2) The initial value of the control variable. 3) The increment (or decrement) by which the control variable is modified each time through the loop 4) The condition that tests for the final value of the control variable. continue?

L1323 Debugging Tips Trace your code by hand (a hand trace), keeping track of the value of each variable. Insert temporary printf() statements so you can see what your program is doing. o Confirm that the correct value(s) has been read in. o Check the results of arithmetic computations immediately after they are performed.

L1324 Header Files Header files contain function prototypes for all of the functions found in the corresponding library It also contains definitions of constants and data types used in that library

L1325 Commonly Used Header Files header fileContains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions manipulating time and date functions that test characters for certain properties and that can convert case otherssee page 154 of text

L1326 Math Library double sqrt (double x); o returns the square root of x double pow (double x, double y) o x raised to the y power pow (3.0, 2.0) is 9.0 pow (8.0, 1.0 / 3) is 2.0 double sin (double x) o trigonometric sine of x (x in radians) All math library functions take doubles as arguments and return doubles others on page of text

L1327 Common stdlib functions void exit (int x); o prematurely ends program execution void srand (unsigned int x); o “seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudo- random number srand (200); int rand (void); o returns an unsigned pseudo-random integer in the range of 0 to or 0 to depending on the size of an integer on the system your on o num = rand( );

L1328 Manipulating what rand() returns Since rand( ) returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes Suppose we want only random numbers in the range from 0 to 5 o num = rand ( ) % 6 How about 1 to 6? o num = 1 + rand( ) % 6; How about 5 to 20? o num = 5 + rand ( ) % 16;

L1329 srand ( ) and rand ( ) The pseudo-random number generator needs an unsigned int as it’s seed Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers To get different random numbers each time we run our program, we have to give a different seed each time

L1330 srand ( ) and rand ( ) #include Since we are always #include using the value 67 to seed the #define SEED 67generator, the same numbers will be produced whenever we main ( ) run our program. { int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%d\n”, num); }

L1331 One of the most useful functions in the time library is the time( ) function It returns the time of day as seconds Since this number is different every time we call it, a common use is as a seed for the random number generator Each time we run our program, a different sequence of random numbers will be produced srand (time ( NULL) ) ;