CSC 107 - Programming for Science Lecture 5: Actual Programming.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
CSC Programming for Science Lecture 5: Actual Programming.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
Expressions, Data Conversion, and Input
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Java Data Types Assignment and Simple Arithmetic.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CSC 107 – Programming For Science. The Week’s Goal.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
CSC 107 – Programming For Science. Announcements.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSC Programming for Science Lecture 4: Beginning Programming.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
CSC Programming for Science Lecture 26: Arrays.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
Introduction to Programming Lesson 3. #include #include main ( ) { cout
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Basic Computation
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 4: Expressions and Variables
ITEC113 Algorithms and Programming Techniques
Assignment and Arithmetic expressions
Chapter 2 Basic Computation
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Lecture 4: Expressions and Variables
Presentation transcript:

CSC Programming for Science Lecture 5: Actual Programming

Problem of the Day Why do underground subway stations always have more escalators going up than down?

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

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

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

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

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

What Is The Expression? Simplest expressions is literal value ‘a’ Examples of this type of expression double d; int i; i = 6; i = 7; d = -7; d = ; d ? i ?

What Is The Expression? Examples of other simple expressions double d; int i; i = 6; i = 7; d = -i; i = d; d = ; i = d; d ? i ?

Data Types C defines ordering of legal assignments long double double float long int short char Assignments are always legal

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

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 / is 2985

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 = * 0.1 = 0.8

Priority of Operations Equations can become very complex  What does * 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

Other Operations Abbreviated assignment operators make certain actions easier  Operators are: +=, -=, *=, /=, %= a+= 2; equivalent to a = a + 2; b -= db = b – d; c *= c - dc = c * 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

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;

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

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

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

Your Turn Divide into groups of 3 and complete the daily activity

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