Assignment Operators Topics Increment and Decrement Operators

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Modular Programming With Functions
Computer Programming w/ Eng. Applications
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to C Programming
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.
Fundamentals of C and C++ Programming Control Structures and Functions.
© 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?
CMSC 1041 Functions II Functions that return a value.
© 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.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
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.
Recap……Last Time [Variables, Data Types and Constants]
CMSC 104, Version 8/061L14AssignmentOps.ppt Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips Reading Section.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
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.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
2.3 Output Formatting. Outputting Format Specify the number of spaces, “c”, used to print an integer value with specifier %cd, e.g., %3d, %4d. E.g. printf.
Variables, Operators, and Expressions
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Chapter Topics The Basics of a C++ Program Data Types
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 4 C Program Control Part I
C Functions Pepper.
Lecture 7: Repeating a Known Number of Times
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Basic Elements of C++.
Iterative Constructs Review
Arithmetic & other operations
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
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
Iterative Constructs Review
Assignment Operators Topics Increment and Decrement Operators
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Computer Programming Techniques Semester 1, 1998
Assignment Operators Topics Increment and Decrement Operators
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
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
More Loops Topics Counter-Controlled (Definite) Repetition
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

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

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

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

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.

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.

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.

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

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

A while loop with ++i i=0; while ( + + i < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10

A while loop with i++ i=0; while ( i ++ < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10 11

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++ )

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.

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.

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.

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

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?

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?

Assignment Operators = += -= *= /= %= Statement Equivalent Statement = += -= *= /= %= 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 ;

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

Code Example Using /= and ++ Counting the Digits in an Integer #include <stdio.h> 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 ;

Output of the code example: num digits printf temp 4327 0 4327 4327 1 432 432 2 43 43 3 4 4 4 0 There are 4 digits in 4327 “Boxes Indicate Printed Output”

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?

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. Confirm that the correct value(s) has been read in. Check the results of arithmetic computations immediately after they are performed.

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

Commonly Used Header Files header file Contains function prototypes for <stdio.h> the standard input/output library functions & information used by them <math.h> the math library functions <stdlib.h> the conversion of number to text, text to number, memory allocation, random numbers and other utility functions <time.h> manipulating time and date <ctype.h> functions that test characters for certain properties and that can convert case others see page 154 of text

Math Library double sqrt (double x); returns the square root of x double pow (double x, double y) 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) trigonometric sine of x (x in radians) All math library functions take doubles as arguments and return doubles others on page 145 - 146 of text

Common stdlib functions void exit (int x); prematurely ends program execution void srand (unsigned int x); “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); returns an unsigned pseudo-random integer in the range of 0 to 65535 or 0 to 4294967295 depending on the size of an integer on the system your on num = rand( );

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 num = rand ( ) % 6 How about 1 to 6? num = 1 + rand( ) % 6; How about 5 to 20? num = 5 + rand ( ) % 16;

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

srand ( ) and rand ( ) #include <stdio.h> Since we are always #include <stdlib.h> using the value 67 to seed the #define SEED 67 generator, 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); }

<time.h> 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) ) ;