Structured Programming. Dr. Mohamed Khedr Lecture 4

Similar documents
CC112 Structured Programming

Chapter 4: Basic C Operators

ANSI C Programming Simple Programs

Operators and Expressions:

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

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

Function I/O. Last Updated 10/30/18

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

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

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

Chapter 3. Numeric Types, Expressions, and Output

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

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

C Program Structures

Chapter 2. Outline. Simple C++ Programs

Computer System and programming in C

THE FUNDAMENTAL DATA TYPES

C Programming

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

COMP 208 Computers in Engineering

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

UNIT 3 OPERATORS. [Marks- 12]

Engineering Problem Solving with C++, Etter/Ingber

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

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

Fundamental of Programming (C)

Introduction to C++ Introduction and History. Characteristics of C++

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

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

C++ Programming Lecture 11 Functions Part I

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

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Fundamentals of Programming

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Data Type Fall 2014 Jinkyu Jeong

Data types, variables, constants

Computers Programming Course 5. Iulian Năstac

Programming. C++ Basics

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

Reserved Words and Identifiers

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

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

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Question 2. [5 points] Given the following symbolic constant definition

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Data Types and Variables in C language

Course Outline Introduction to C-Programming

First of all, it is a variable, just like other variables you studied

Identify skills and personality traits of successful problem solving. Apply standard problem-solving techniques to aid in problem solving.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Chapter 7. Basic Types

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

3. Types of Algorithmic and Program Instructions

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

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

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:

Basics of Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Python Programming: An Introduction to Computer Science

UNIT- 3 Introduction to C++

ET156 Introduction to C Programming

2. Numbers In, Numbers Out

1.3b Type Conversion

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

CHAPTER 3 Expressions, Functions, Output

3.1. Chapter 3: The cin Object. Expressions and Interactivity

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C Programming a Q & A Approach

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Chapter 3. Fundamental Data Types

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

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Programming and Data Structures

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Types and Expressions. Chapter 3

Fundamentals of Programming Session 8

C++ Programming: From Problem Analysis to Program Design, Third Edition

Beginning C Programming for Engineers

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Computers Programming Course 6. Iulian Năstac

Variable and Data Type 2

Prepared by: Shraddha Modi

A Fast Review of C Essentials Part I

2. Numbers In, Numbers Out

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Numerical Analysis First Term Dr. Selcuk CANKURT

Transcription:

Structured Programming Dr. Mohamed Khedr http://webmail.aast.edu/~khedr 1

Scientific Notation for floats 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10-4 = 0002.7 = 0.00027 2

Output Formatting Integer formatting Very simple: Add a number between the % and the d in the placeholder to specify the field length. Numbers will appear right-justified with preceding blanks if needed. 3

int len = 234 ; Integer Formatting Example printf( Length is %5d, len); Output is: Length is 234 Note: The stands for a blank 4

Integer Formatting Displaying X using different %d placeholder Value Format Displayed Output Value Format Displayed Output 234 %4d 234-234 %4d -234 234 %5d 234-234 %5d -234 234 %6d 234-234 %6d -234 234 %1d 234-234 %2d -234 5

Output Formatting Double formatting We must indicate both the field width and the EXACT number of decimal places: %7.3 f minimum total field length Exact number of decimal digits Note: The decimal part will be rounded The whole part may be padded with blanks REMEMBER: The value of the number does not change, only its appearance 6

Double Formatting Displaying X using different %6.2f placeholder Value of x Displayed Output Value of x Displayed output -99.42-99.42-25.554-25.55 0.123 0.12 99.999 100.00-9.536-9.54 999.4 999.40 7

Programming Examples Example-1 Write a program to ask the user for the width and length of a piece of land and then tell him how many orange trees he can grow on it. Given that each orange tree requires 4 m 2. 8

Programming Examples Example-1 #include <stdio.h> # define one_tree_space 4 int main(void) { int length,width, area, no_of_tree; printf( Enter length of the land> ); scanf( %d, &length); printf( Enter width of the land> ); scanf( %d, &width); area = length * width; no_of_tree = area / one_tree_space; printf( The available number of trees is %d tress\n, no_of_tree); return(0); } 9

Programming Examples Example-2 Write a program to ask the user for the radius of a circle, and then display its area and circumference, displayed to 3 decimal digits. 10

Programming Examples Example-2 #include <stdio.h> # define PI 3.141593 int main(void) { double radius, area, circumference; printf( Enter radius of the circle> ); scanf( %lf, &radius); area = PI * radius * radius; circumference = 2 * PI * radius; printf( The area of the circle = %.3f\n, area); printf( The circumference of the circle = %.3f\n,circumference); return(0); } 11

Arithmetic Expression Assignment operator syntax Variable = Expression 1) first, Expression on right is evaluated 2) then the resulting value is stored in the memory location of Variable on left NOTE: An automatic type conversion occurs after evaluation but before the value is stored if the types differ for Expression and Variable 12

Arithmetic Expression What is stored? float somefloat;? somefloat somefloat = 12; // causes implicit type conversion 12.0 somefloat 13

Arithmetic Expression What is stored? int someint;? someint someint = 4.8; // causes implicit type conversion 4 someint 14

Explicit Type Conversion (int)4.8 has value 4 (float)5 has value 5.0 (float)(7/4) has value 1.0 (float)7 / (float)4 has value 1.75 15

Using Casts to Prevent Integer Division (Example) #include <stdio.h> int main(void) { int total_score, num_students; double average; printf( Enter sum of students scores> ); scanf( %d, &total_score); printf( Enter sum of students> ); scanf( %d, &num_students); } average = (double) total_score / (double) num_students; printf( Average score is %.2f\n, average); return(0); 16

Math in C Math library The C math library provides a lot of useful predefined math functions Before you use them, remember to include the math library in your code: #include <math.h> function sqrt: y = sqrt ( x ); 17

Math in C Examples of Math functions sin(x) cos(x) tan(x) sqrt(x) log(x) pow(x,y) log10(x) exp(x) fabs(x) floor(x) ceil(x) 18

Math in C complex math example-1 Write a program to get the roots of a quadratic equation, given the 3 coefficients a, b, and c, a x 2 + b x + c = 0 Root 1 = b b 2 4ac 2a disc = pow(b,2) 4 * a * c; Root 2 = b + b 2 4ac 2a root_1 = (-b + sqrt(disc)) / (2 * a); root_2 = (-b - sqrt(disc)) / (2 * a); 19

Math in C complex math example-2 Write a program to get the third side of a triangle (a), given the lengths of the other two sides (b, and c), and the angle α using the formula a 2 = b 2 + c 2 2bc cos α c rad_angle = alpha * PI / 180; b α a a = sqrt(pow(b,2) + pow(c,2) 2 * b * c * cos(rad_angle)); 20

Conditional (ternary) Operator Syntax expr1? expr2 : expr3 If expr1 0, then execute expr2 and ignore expr3 If expr1 = 0, then execute expr3 and ignore expr2 Example: x = i+j? i+1 : j+1 Example: x = 5? 4 : 2; /* x = 4 */ Example: j = 4; i = 2 x = i+j? i+1 : j-1 /* x = 3 */ Example: l =a > b? a : b; /* the larger of a and b */ Example: max =(a > b)?((a>c)?a:c):(b>c)?b:c); /* the maximum number among a, b, and c */ Example: x = a > 0? a: -a; /* the absolute value of a */ 21

Syntax sizeof(expr) sizeof Operator The number of bytes occupied by expr For most computers sizeof(3) 2 or 4 (bytes) (depending on16 bit CPU or 32 bit CPU), where 3 is an integer sizeof(3l) 4 (long int) sizeof(3.0) 8 (double float) Example: double i; printf( %d,sizeof(i)); 8 Usually, this operator is used to get the size of an organized variable (like struct, union, ) This is one of a few functions that are built-in. No #include is required. 22

Syntax &var Get the address of the variable & means the address of var Type of var may be (a) fundamental data type (b) organized data type Address Operator Address Content Example: int i=100; printf( %d %d, &i, i); 1000 1001 1002 100 RAM 1003 1004 1005 23

Arithmetic Operators Shortcut assignment 24

Arithmetic Operators Prefix form Prefix operator 25

Arithmetic Operators Postfix form 26

Assignment Operators Syntax: var = expression; Assign the value of expression to variable (var) Example: int x, y, z; x = 5; y = 7; z = x + y; Z = (x = 5) + (y = 7) much faster int x, y, z; x = y = z = 0; int x = y = z = 0; int i, j; float f, g; i = f = 2.5; g = j = 3.5; same as x = (y = (z = 0)); wrong! int x = 0, y = 0, z = 0; i = 2; f = 2.5; g = 3.0; j = 3; 27

Short Hand Assignment Syntax f = f op g can be rewritten to be f op= g such as: a = a + 2 a += 2, a = a - 2 a -= 2, a = a * 2 a *= 2, a = a / 2 a /= 2, a = a % 2 a %= 2, a = a << 2 a <<= 2, a = a & 2 a &= 2, a = a 2 a = 2, a = a ^ 2 a ^= 2 No blanks between op and = x *= y + 1 is actually x = x * (y+1) rather than x = x * y + 1 Example: q = q / (q+2) q /= q+2 j = j << 2 j <<= 2 Advantage: help compiler to produce more efficient code More complicated examples: int a=1, b=2, c=3, x=4, y=5; a += b += c *= x + y - 6; printf( %d %d %d %d\n,a,b,c,x,y); a += 5 + b += c += 2 + x + y; a += 5 + (b+= c += 2 + x + y); /* result is 12 11 9 4 5 */ /* wrong */ /* result is 22 16 14 4 5 */ 28

Increment / Decrement Operators ++ (increment) -- (decrement) Prefix Operator Before the variable, such as ++n or -n Increments or decrements the variable before using the variable Postfix Operator After the variable, such as n++ or n-- Increments or decrements the variable after using the variable ++n 1. Increment n 2. Get value of n in expression --n 1. Decrement n 2. Get value of n in expression n++ 1. Get value of n in expression 2. Increment n n-- 1. Get value of n in expression 2. Decrement n 29

Increment / Decrement Operators (cont.) Simple cases ++i; i++; (i = i + 1; or i += 1;) --i; i--; (i = i - 1; or i -= 1;) Example: i = 5; i++; (or ++i;) 6 i = 5; i--; (or --i;) printf( %d, i) 4 30

Complicated cases i = 5; i j j = 5 + ++i; 6 11 i = 5; j = 5 + i++; 6 10 i = 5; j = 5 + --i; 4 9 i = 5; j = 5 + i--; 4 10 31

Invalid cases Increment / Decrement Operators (cont.) ++3 3++ --3 3-- ++(x+y+z) (x+y+z)++ --(x+y+z)(x+y+z)-- ++x++ --x-- ++x-- --x++ Note: Can not increment or decrement constant and expression i ++j or i -j (WRONG) i + ++j i + -j i - --j i - ++j (OK) 32

Other Input / Output puts(line) Print a string to standard output and append a newline Example: puts( 12345 ); putchar(c) Print a character to standard output Example: putchar( A ); gets(line) Read a string from standard input (until a newline is entered) Example: char buf[128]; gets(buf); /* space is OK, and the \n won t be read in */ Newline will be replaced by \0 getchar() Get a character from standard input Example: int c; c = getchar(); /* c must be int */ In-memory Format Conversion sprintf(string, control, variables); 33