Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Similar documents
Computer System and programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Data Types. Data Types. Integer Types. Signed Integers

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Reserved Words and Identifiers

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Programming in C++ 5. Integral data types

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

2. Numbers In, Numbers Out

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

CS Kangmei Yang. Page 1

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Chapter 3. Fundamental Data Types

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

COMP Primitive and Class Types. Yi Hong May 14, 2015

ROUNDING ERRORS LAB 1. OBJECTIVE 2. INTRODUCTION

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

2. Numbers In, Numbers Out

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

CS102: Variables and Expressions

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

C: How to Program. Week /Mar/05

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

Information Science 1

CS16 Exam #1 7/17/ Minutes 100 Points total

Datatypes, Variables, and Operations

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Hexadecimal Numbers. Journal: If you were to extend our numbering system to more digits, what digits would you use? Why those?

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

ANSI C Programming Simple Programs

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Only to be used for arranged hours. Order of Operations

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Chapter 2 - Introduction to C Programming

BSM540 Basics of C Language

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

printf( Please enter another number: ); scanf( %d, &num2);

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

H192 Midterm 1 Review. Tom Zajdel

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Lecture 3 Tao Wang 1

CIS133J. Working with Numbers in Java

CS110: PROGRAMMING LANGUAGE I

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Homework #3 CS2255 Fall 2012

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

SHARDA UNIVERSITY SCHOOL OF ENGINEERING & TECHNOLOGY Mid Term Examination, (Odd Term, ) SOLUTION

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

CIS 110: Introduction to Computer Programming

2.Simplification & Approximation

ITP 342 Mobile App Dev. Code

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

CS 102 Lab 3 Fall 2012

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Numerical Analysis First Term Dr. Selcuk CANKURT

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

UNIT 7A Data Representation: Numbers and Text. Digital Data

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Work relative to other classes

CS 101, Spring 2016 March 22nd Exam 2

CS 106 Introduction to Computer Science I

CSC-140 Assignment 6

Department of Computer Applications

DECIMALS are special fractions whose denominators are powers of 10.

Chapter 3 Basic Data Types. Lecture 3 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Credit Cards. Validating Credit Cards. Answers

The C++ Language. Arizona State University 1

ET156 Introduction to C Programming

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Chapter 3: Arithmetic for Computers

CS61C : Machine Structures

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

Machine Arithmetic 8/31/2007

Chapter 2 Working with Data Types and Operators

CS 211 Programming Practicum Fall 2018

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

Transcription:

Variables, Data Types, and Arithmetic Expressions Learning Objectives: Printing more than one variable in one printf() Printing formatting characters in printf Declaring and initializing variables A couple of Variable types Input and output of float or double variables Arithmetic Operators you ve probably seen Integer Division Modulus Operator Expressions in printf() Assignment Operator 1

printf() continued Review: printf( You are %i years old\n, age); You can print more than one variable in a single printf(): Example: printf( x = %i, y = %i, z = %i\n, var1, var2, var3); To print formatting characters (% or \), double them up: Example: printf( The percentage is %i%%.\n, perc); If the value of perc is 85, then the output would be: The percentage is 85%. To print a \: Use \\ 2

Declaring and Initializing Variables Variables can be many different types, for example integers, strings, real numbers, even students. Each type will have it s own set of properties and you have to specify the type so the compiler knows how to treat it. For example, integer division is different from real number division. Syntax: variable type then variable name then (optionally) assign a value to the variable Example: int age; //declares an integer variable called age When you declare an integer, you give it a type and a name and the program gives it a memory. address You can also give it a. value Example declaring and Initializing age: int age = 20; //initializing means assigning its first value Remember: No spaces or special characters in variable names! 3

Declaring Variables You can declare more than one variable on the same line: Examples: int age, apples; //declares both age and apples as ints int age=20, apples=53; //also initializes age and apples Note: Once a variable is declared, you don t ever re-declare it. Some Basic Number Types and their limits: Note: unsigned is a modifier that can go with any integer type Integers Type Typical Limits short -32,767 to 32767 int -2,147,483,647 to 2,147,483,647 long -2,147,483,647 to 2,147,483,647 unsigned short 0 to 65535 Floating Point Type Typical Limits float 6 digits of precision, Max exponent = 38 Max Value = 3.402823e+38 double 15 digits of precision, Max exponent = 308 Max Value = 1.797693e+308 long double 15 digits of precision, Max exponent = 308 Max Value = 1.797693e+308 4

Examples: Declaring Variables (For numbers, you will use int and double mostly) //create a double variable called side1 and assign a value of 4.9 to it double side1 = 4.9; //create an integer variable called num and assign a 0 to it. int num = 0; //create an int variable called n2 that can t be negative unsigned int n2; //create two float variables and initialize them to 3.21 and 6.42, respectively float a = 3.21, b = 6.42; //create a double variable called x and assign 5.4 x 10 5 to it double x = 5.4e5; 5

Output of floats and doubles Example that outputs an integer: printf( The integer is: %i \n, myint); %i means you are printing an integer. %f means you are printing a floating point number %lf means you are printing a double %e means you are printing in scientific notation (float or double) Example that outputs a double: printf( The number is: %lf \n, mynum); By default, you will get 6 decimal places when you print a floating point or double variable. To modify that, use. as follows: Example that prints a double with 3 decimal places: printf( The number is: %.3lf \n, mynum); 6

Input of floats and doubles Inputting an integer variable: scanf( %i, &myint); %i means you are reading an integer. %f means you are reading a floating point number %lf means you are reading a double Example that inputs a double: scanf( %lf, &mynum); Do you need to worry about the number of decimal places with scanf? No the user will type in as many as he/she wants. 7

Some Basic Arithmetic Operators Multiplication: 2.2 * 3.0 Division: 2.2 / 3.0 Addition: 2.2 + 3.0 Subtraction: 2.2-3.0 Equal Sign: IMPORTANT: = is the assignment operator, so if you say: x = y + z //this will add variables y and z then assign that //value to x. Note: y+z=x doesn t work!!! Does everything to the right of = then assigns value to 8 variable on the left. You must have a variable on left.

Integer Division Variables have properties based on their type. This means that integers can act differently from doubles. This is especially important in the case of Integer Division. If the numerator and denominator are integers, the program will truncate the result. Examples: Give the value of x after each line executes: x = 10 / 3; x = 2 / 5; x = 3 x = 0 x = 1 / 2 * 3.14159 * 2.2 * 2.2; x = 0 9

Modulus Operator The modulus operator is a %: Examples: x = 21 % 4; y = 52 % z; r = s % y; The numbers or variables to the right and left of the % must be integers. The resulting value of a modulus operation is the remainder after you divide the left by the right. 10

Examples Using the Modulus Operator Give the value of x after each line executes: x = 10 % 3; x = 1 3 goes into 10 three times, leaving a remainder of 1 x = 2 % 5; x = 2 5 goes into 2 zero times, leaving a remainder of 2 Examples of where % is used: x%2 if this is 0, the number is even If this is 1, the number is odd If day 0 is on a Sunday, then day 1 is on a Monday etc. How can you use modulus to determine the weekday of day 123,455? int day = 123,455; int dayofweek = day % 7; (Note: I declared and calculated in one line.) 11

Expressions in printf() Example: printf( You are %i years old\n, thisyr - birthyr); Resulting value printed 12

Assignment Operator Revisited ( = ) This looks like an equal sign, but it is different than the equal sign you are used to!! The compiler marches line-by-line through the program. When it gets to an equal sign (assignment operator), it will: compute everything on the right-hand-side of the equal sign, take the resulting value, and assign it to the variable on the left Therefore, the only thing that can be on the left is a variable! What happens with each of the following statements? x + 2 = 4; Invalid You get an error message x = 3 + 7 * 9; Assigns 66 to x x = x + 3; Takes the value of x, adds 3 to it and reassigns the new value to x 13

Lab Work and Homework There is a lab for today and a Homework assignment due Monday at midnight. Submit the lab before leaving even if you don t get it completed. You will find the links to the lab and to the homework on Dr. Babcock s web page. The link for his web page is on the merged Blackboard course under External Links. Note: You will also find notes on how to download and install Cygwin and Notepad++ on your home computer if you look on Dr. Babcock s web page under Resources. My notes and other instructors notes will also be posted on Dr. Babcock s web page. Don t forget about the Pop Quizzes!! 14