Computer Programming CS F111

Similar documents
Lecture 3 Tao Wang 1

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Operators & Expressions

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

Programming in C++ 5. Integral data types

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

Basics of Programming

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

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Operators and Expressions:

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

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

4. Inputting data or messages to a function is called passing data to the function.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

UNIT 3 OPERATORS. [Marks- 12]

Unit 3. Operators. School of Science and Technology INTRODUCTION

Programming and Data Structures

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

DEPARTMENT OF MATHS, MJ COLLEGE

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Chapter 4: Basic C Operators

Informatics Ingeniería en Electrónica y Automática Industrial

Data Types and Variables in C language

UNIT- 3 Introduction to C++

Operators in C. Staff Incharge: S.Sasirekha

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Variables and Operators 2/20/01 Lecture #

Department of Computer Science

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

Arithmetic Expressions in C

Arithmetic Operators. Portability: Printing Numbers

Chapter 2, Part III Arithmetic Operators and Decision Making

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

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

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

LECTURE 3 C++ Basics Part 2

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Computers Programming Course 6. Iulian Năstac

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

CS313D: ADVANCED PROGRAMMING LANGUAGE

Introduction to Programming

Chapter 2: Overview of C. Problem Solving & Program Design in C

C: How to Program. Week /Mar/05

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

VARIABLES & ASSIGNMENTS

Full file at C How to Program, 6/e Multiple Choice Test Bank

ANSI C Programming Simple Programs

1.3b Type Conversion

CS Programming I: Primitives and Expressions

Chapter 2 - Introduction to C Programming

Information Science 1

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Learning the Language - V

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

Lecture 3. More About C

CS Kangmei Yang. Page 1

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CS102: Variables and Expressions

Chapter 3: Operators, Expressions and Type Conversion

Chapter 2: Data and Expressions

Computer System and programming in C

Arithmetic Expressions Lesson #1 Outline

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Creating a C++ Program

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Eng. Mohammed S. Abdualal

CIS 110: Introduction to Computer Programming

Our Strategy for Learning Fortran 90

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Chapter 2: Introduction to C++

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Engineering Problem Solving with C++, Etter/Ingber

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

CMPT 125: Lecture 3 Data and Expressions

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Fundamentals of Programming

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Introduction to C# Applications

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

C/C++ Programming for Engineers: Working with Integer Variables

General Announcements

Prepared by: Shraddha Modi

Basics of Java Programming

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

Chapter 2: Data and Expressions

Transcription:

Computer Programming CS F111 BITS Pilani Dubai Campus NAND KUMAR

Basics of C Programming BITS Pilani Dubai Campus

Write a program that 1. Asks 5 marks from the user, find the average of the marks and print it with 2 digit precision. 2. Asks 2 Numbers and precision value, display the sum with the specified precision 3. Asks the user to enter the radius of a circle and calculates the Area of the circle as Area=PI * radius * radius display the Area with 4 digit precision

Reading a character Function : getchar(); getc(stdin); Syntax variable_name = getchar(); variable_name = getc(stdin); Example: char ans; ans = getchar(); ans = getc(stdin); It accepts any character keyed in.

Writing a character Function : putchar(int c); putc(int c, stdout); Syntax: putchar(var_name); putc(var_name, stdout); Example: char ans; ans = Y ; putchar(ans); Q. What is the effect of putchar( \n );

Arithmetic Operators Operator Meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo Division

Arithmetic Expression Let int x = 15; int y = 6; Example: x-y is 9 x+y is 21 x*y is 90 x/y is 2 (decimal part truncated) x%y is 3 (remainder of the division) Q. If x and y are declared as float then? Q. If x is integer and y is float then?

Operator / and % 7.0 / 2.0 = 3.5 7.0 / 2 = 3.5 7 / 2.0 = 3.5 7 / 2 = 3 0 / 2 = 0 2 / 0 is Undefined 2 / -3 Varies 7.0 % 2.0 = error error: invalid operands to binary % 7.0 % 2 = error error: invalid operands to binary % 7 % 2.0 = error error: invalid operands to binary % 7 % 2 = 1 For % operation, when the operands are negative, the output depends the compiler GCC behaves the following way -7 % -2 = -1-7 % 2 = -1 7 % -2 = 1 7 % 2 = 1 7 % 0 is Undefined

What is Precedence and Associativity? Operators have rules that are used to determine how expressions are evaluated. Precedence and associativity deal with the evaluation order within expressions Precedence rules specify the order in which operators of different precedence level are evaluated Associativity rules decides the order in which multiple occurrences of the same level operator are applied.

Rules of Evaluation of Expression 1. First, parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression. 3. The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions. 4. The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.

Precedence of Arithmetic Operators Two priority levels of operators High priority *, /, % Low priority +, - Consider the expression 1+2*3 * has higher priority than + So the value of the expression is 7

Precedence of Arithmetic Operators Priority can be overruled by parenthesis (1+2)*3 Expression inside parenthesis are evaluated first so (1+2)*3 gives 9 Now consider the expression 1+2-3+4-5 Note: + and have the same precedence Associativity rule is used to determine how to evaluate the above expression. Associativity for Arithmetic Operators is L to R Try: x = 10-16/4 + 4 * 3-4

Expression with multiple operators Rules for evaluating expressions Parenthesis Rule Evaluated separately. In case of nested parenthesis, evaluation order is from innermost to outermost Operator Precedence Rule Unary +,- *, /, % Binary +,- : first : last : next Associativity Rule Unary operators at same precedence : right associativity Binary operators at same precedence: left associativity Find the order of evaluation: x * y * z + a / b - c * d

Examples: Evaluate the following expressions z = 2*3/4+4/4+8-2+5/7 z =?? y = 4/5*6+2/7+4 y =?? Let int a,b; float c,d; a=5;b=7; c=4.0;d=3.0; x = a/b*(c+d)/a-b+c/d x =??

What if the expression has multiple operators? Consider area = PI * radius * radius ;

What if the expression has multiple operators? Formula for velocity is v = (p 2 -p 1 ) / (t 2 -t 1 )

What if the expression has multiple operators? Consider an expression a=z- (a+b/2) + w * -y

Write the following formulas in C 1) b 2-4ac 2) a+b c+d 3) 1 1+x 2 Specify multiplication explicitly using * Use parenthesis to control the order of operator evaluation