CC112 Structured Programming

Similar documents
Structured Programming. Dr. Mohamed Khedr Lecture 4

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

Fundamentals of Programming Session 4

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Introduction to Computing Lecture 03: Basic input / output operations

Unit 4. Input/Output Functions

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Programming for Engineers Introduction to C

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

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

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

Input/Output Week 5:Lesson 16.1

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

Chapter 2, Part I Introduction to C Programming

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

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

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

Display Input and Output (I/O)

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

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

Should you know scanf and printf?

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

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

BSM540 Basics of C Language

AMCAT Automata Coding Sample Questions And Answers

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

Some Computer Preliminaries

C: How to Program. Week /Mar/05

ET156 Introduction to C Programming

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

CSCI 2132 Software Development. Lecture 8: Introduction to C

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

Fundamentals of C. Structure of a C Program

Chapter 2 - Introduction to C Programming

C Program Structures

ET156 Introduction to C Programming

Lesson 3 Introduction to Programming in C

Fundamental of Programming (C)

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

Number Systems, Scalar Types, and Input and Output

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

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

Data Types and Variables in C language

Basic Types and Formatted I/O

Formatted Input/Output

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

ANSI C Programming Simple Programs

Arithmetic Expressions in C

2 nd Week Lecture Notes

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

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

Computer System and programming in C

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

BSM540 Basics of C Language

2. Numbers In, Numbers Out

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

EC 413 Computer Organization

Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

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

Programming. C++ Basics

COMP Assignment 1

C introduction: part 1

Lecture 2: C Programming Basic

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

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

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Fundamentals of Programming

C Overview Fall 2015 Jinkyu Jeong

2. Numbers In, Numbers Out

Fundamental of Programming (C)

At the end of this module, the student should be able to:

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Programming and Data Structures

COMP s1 Lecture 1

DECLARAING AND INITIALIZING POINTERS

File Input/Output. Similarly, for reading from files and writing to files, we'll use the functions

CSE 303 Lecture 8. Intro to C programming

Fundamentals of C Programming

Programming refresher and intro to C programming

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

LESSON 4. The DATA TYPE char

Informatica e Sistemi in Tempo Reale

Introduction to the C Programming Language

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Fundamentals of Programming

C Overview Fall 2014 Jinkyu Jeong

Chapter 2 THE STRUCTURE OF C LANGUAGE

CpSc 1111 Lab 4 Formatting and Flow Control

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

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

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

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

Transcription:

Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer Engineering Department CC112 Structured Programming Lecture 3 1

LECTURE 3 Input / output operations 2

LECTURE OUTLINE i. Input/Output operations Function Output function Place holders Escape sequences Input function Output formatting ii. Programming Examples 3

I. INPUT/OUTPUT OPERATIONS FUNCTION Output Function (printf ) The printf function displays the value of its format string and the values of the expressions in the print list in the same order from left-to-right. They are given between two double quotations. SYNTAX printf( format string, print list ) ; printf(format string); 4

OUTPUT FUNCTION 5

I. INPUT/OUTPUT OPERATIONS FUNCTION EXAMPLE Place holder printf( That equals %f kilometers. \n, kms); printf( enter the distance in miles> ); printf( Hello, World?\n ); Escape sequence Double quotations 6

Placeholders A placeholder is a symbol beginning with % in a format string that indicates where to display the output value Placeholder %c %d %f Variable type char int float 7

Escape sequence The backslash (\) is called an escape character. Indicates that printf is supposed to do something unusual. When encountering a backslash, printf looks to the next character and combines it with the backslash to form an escape sequence. Escape sequence \n \t Action Starts new line tab 8

EXAMPLE printf( This is one line \n ); printf( and \n this \t is \t another \n ); This is one line and this is another 9

Input Function (scanf ) The scanf function copies into memory data entered during the program execution. The order of the placeholders must correspond to the order of the variables in the input list. The data must be entered in the same order in the input list. You should insert one or more blank characters or carriage returns between numeric items. 10

INPUT FUNCTION SYNTAX scanf( format string, input list ) ; Examples : Place holder scanf( %f, &miles); Ampersand 11

INPUT FUNCTION 12

OUTPUT FORMATTING In C, you can control the APPEARANCE of number on the screen. Output formatting does not affect the VALUE of a variable, just how it appears to the user on the screen. 13

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. 14

INTEGER FORMATTING EXAMPLE int len = 234 ; printf( Length is %5d, len); Output is: Length is 234 Note: The stands for a blank 15

INTEGER FORMATTING EXAMPLE Value Form at D isplayed Output Value Form at D isplayed 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 16

OUTPUT FORMATTING FLOAT 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 17

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 18

II. 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. 19

EXAMPLE 1: #include <stdio.h> # define one_tree_space 4 void main() { 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 trees\n, no_of_tree); } 20

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. 21

EXAMPLE 2: #include <stdio.h> # define PI 3.141593 int main(void) { double radius, area, circumference; printf( Enterradius 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); } 22

EXAMPLE 3: Write a program to create two integer variables, and store in them the values 3 and 5. Then calculate their sum and their product and display the result. Use comments. 23

EXAMPLE 3: SAMPLE 1 /* * This program gets the sum and product of 2 integers */ #include <stdio.h> /* library containing printf */ int main ( ) { int first ; /* declaring first variable */ int second ; /* declaring second variable */ int sum ; /* variable to hold the sum */ int product ; /* variable to hold the product */ first = 3 ; /* assignment statement */ second = 5 ; sum = first + second ; printf( The sum is %d \n, sum ); /* output */ product = first * second ; printf( The product is %d \n, product ); return (0); } 24

EXAMPLE 3: SAMPLE 2 /* * This program gets the sum and product of 2 integers */ #include <stdio.h> /* library containing printf */ int main ( ) { int first = 3 ; /* declaring and assigning first variable */ int second = 5 ; /* declaring and assigning second variable */ int sum, product ; sum = fiirst + second ; product = first * second ; printf( The sum is %d \n The product is %d \n, sum, product ); } return ( 0 ) ; 25

26