Download presentation
Presentation is loading. Please wait.
Published byAshlie Clark Modified over 8 years ago
1
CSC 107 - Programming for Science Lecture 5: Actual Programming
2
Problem of the Day Why do underground subway stations always have more escalators going up than down?
3
The Week’s Goal At the end of today’s lecture, you should be able to write (small, useless) C programs Become much more interesting Monday
4
Variables Variables name memory location for program to store data Variable’s initial value is unknown Assignments update data stored at memory location Variable’s value used whenever program uses variable
5
Data Types Each variable also has data type Specifies how program treats variable’s value C defines 6 numeric data types Integer types: short, int, long Decimal types: float, double, long double Does NOT specify ranges for each type char data type holds a character C will only allow certain assignments Assign an integer to a decimal variable --- easy Assign decimal to integer variable --- not easy
6
Making Programs Interesting Declaring variables boring after 1 st 6 years Really want to do something with variable C includes statements to do this Lecture today will cover first statement Assignments
7
Variable declaration creates “box” to store data Assignments place values into this box General form of assignment is variable = expression; Expression will first be evaluated Computing expression results in single value Variable set to final value of expression
8
What Is The Expression? Simplest expressions is literal value 12 56 12.345 -56 ‘a’ Examples of this type of expression double d; int i; i = 6; i = 7; d = -7; d = 34.5691; d ? i ? 67 -7.034.5691
9
What Is The Expression? Examples of other simple expressions double d; int i; i = 6; i = 7; d = -i; i = d; d = 34.5691; i = d; d ? i ? 67 -7.0 34.5691
10
Data Types C defines ordering of legal assignments long double double float long int short char Assignments are always legal
11
Arithmetic Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Computes remainder of division between two integers 2 % 5 is 2 5 % 2 is 1 94 % 47 is 0
12
Integer Division Dividing two integers computes an integer No difference if values are literals or variables Result will be truncated not rounded E.g. Only keeps the integer part of division 2 / 5 is 0 5 / 2 is 2 94 / 47 is 2 -5 / 2 is -2 102483342 / 34322 is 2985
13
Floating Point and Mixed Division Result of arithmetic with 2 decimal numbers will be decimal number 4.2 / 2.1/* Will not be an int */ double d = 1.0; 5.7 / d; Result of arithmetic with decimal and integer will also be decimal number 4.0 + 2 = 6.0 8 * 0.1 = 0.8
14
Priority of Operations Equations can become very complex What does 4 + 5 * 6 * 9 - PI + E equal? 1. ( ) Solve innermost first 2. Pos/Neg. +/- Solve from right to left 3. * / % Solve from left to right 4. +/- (add/minus) Solve from left to right My suggestion: use lots of parentheses
15
Other Operations Abbreviated assignment operators make certain actions easier Operators are: +=, -=, *=, /=, %= a+= 2; equivalent to a = a + 2; b -= db = b – d; c *= 196 + c - dc = c * 196 + c - d; d /= 0.3;d = d / 0.3; e % = 4;e = e % 4; Abbreviated assignments have the lowest priority of any operator Expression always evaluated first
16
How To Shoot Yourself in Foot C also includes increment (++) and decrement (--) operators Can only be used with variables Used in expression to save typing add’l line When used before the variable: v = ++b % c; is equivalent to b = b + 1; v = b % c; c = f * --h; is equivalent to h = h - 1; c = f * h;
17
How To Shoot Yourself in Foot When used after the variable: v = b++ - c; is equivalent to v = b - c b = b + 1; x = y % j--; is equivalent to x = y % j; j = j - 1; Problem with using these operators: What does this calculate? x = y +++++ c * c
18
Tracing A Program Important tool when writing, understanding, & debugging code Shows step-by-step execution of program Includes line executed, values of all variables, and other important information Each line executed is row in table 1 st column specifies line executed Each variable then has its own column
19
Program Trace 1 int x = 4 + 2; 2 int y = 8 * 1; 3 double z = y – 3; 4 x++; 5 z -= x; 6 y = y + 1 / 2; 7 z = 6.0 / 4 + x * x; 8 y = (x – 3) * (y + 2); Line#xyz
20
Your Turn Divide into groups of 3 and complete the daily activity
21
For Next Lecture Read through Section 2.4 of book Do not need to understand all the details But important knowing what is not understood Review homework assignment for week 2 Covers material from this week’s lectures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.