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

Similar documents
Computer Programming & Problem Solving ( CPPS )

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

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

Unit 4. Input/Output Functions

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

BSM540 Basics of C Language

Work relative to other classes

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

Lecture 3. More About C

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

C: How to Program. Week /Mar/05

4 Programming Fundamentals. Introduction to Programming 1 1

UNIT- 3 Introduction to C++

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

Computers Programming Course 5. Iulian Năstac

Chapter 2 - Introduction to C Programming

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

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

A First Program - Greeting.cpp

Should you know scanf and printf?

Visual C# Instructor s Manual Table of Contents

Input/Output Week 5:Lesson 16.1

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

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Number Systems, Scalar Types, and Input and Output

CS 106 Introduction to Computer Science I

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

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

INTRODUCTION 1 AND REVIEW

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

CC112 Structured Programming

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

C - Basic Introduction

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

Chapter 2: Using Data

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

UNIT 3 OPERATORS. [Marks- 12]

Fundamentals of Programming

Chapter 2: Data and Expressions

Fundamental of Programming (C)

Programming in C++ 5. Integral data types

Fundamentals of Programming Session 4

Fundamentals of C. Structure of a C Program

Basics of Java Programming

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

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Chapter 2 THE STRUCTURE OF C LANGUAGE

Department of Computer Applications

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Data Types and Variables in C language

Computer System and programming in C

Fundamental of Programming (C)

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

6.096 Introduction to C++ January (IAP) 2009

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

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

EC 413 Computer Organization

Section 2.2 Your First Program in Java: Printing a Line of Text

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

Programming in C++ 4. The lexical basis of C++

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

CSC 1107: Structured Programming

VARIABLES AND CONSTANTS

Programming for Engineers Introduction to C

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

Introduction to C Language

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

COURSE OUTLINE & WEEK WISE BREAKAGE

DECLARAING AND INITIALIZING POINTERS

Java Notes. 10th ICSE. Saravanan Ganesh

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Java Basic Datatypees

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Declaration and Memory

Programming and Data Structures

Lesson 3 Introduction to Programming in C

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

EL2310 Scientific Programming

Operations. Making Things Happen

Reserved Words and Identifiers

Chapter 1 & 2 Introduction to C Language

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

Chapter 2: Data and Expressions

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

CS149: Elements of Computer Science. Fundamental C++ objects

As stated earlier, the declaration

C++ is case sensitive language, meaning that the variable first_value, First_Value or FIRST_VALUE will be treated as different.

Differentiate Between Keywords and Identifiers

BSM540 Basics of C Language

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

Basic Elements of C. Staff Incharge: S.Sasirekha

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Full file at

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

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

The C++ Language. Arizona State University 1

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

Transcription:

Computer Programming & Problem Solving ( CPPS ) Chapter No 2 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan Muhammad Kashif Shaikh Course Instructors Muhammad Kashif Shaikh mkshaikh@ssuet.edu.pk Assistant Professor, CED Room Number: BS-04 Tel: 111-994-994, Ext. 326 Section A (Computer, Batch 2010) Muzammil Ahmad Khan mukhan@ssuet.edu.pk Assistant Professor, CED Room Number: BS-04 Tel: 111-994-994, Ext. 326 Section B (Computer, Batch 2010) 2 1

Course Instructors Zeeshan Karim muhzahma@ssuet.edu.pk Lecturer, CED Room Number: AG-06 Tel: 111-994-994, Ext. 301 Section C (Computer, Batch 2010) Ali Yousuf ayousuf@ssuet.edu.pk Lecturer, CED Room Number: AG-06 Tel: 111-994-994, Ext. 301 Section D (Computer, Batch 2010) 3 Course Instructors Nadia Bilal nbilal@ssuet.edu.pk Lecturer, CED Room Number: BS-02 Tel: 111-994-994, Ext. 254 Section E (Computer, Batch 2010) Adnan Zahoor azahoor@ssuet.edu.pk Lecturer, CED Room Number: AG-06 Tel: 111-994-994, Ext. 301 Section F (Computer, Batch 2010) 4 2

Course Books Text Book: Turbo C Programming For The PC ( Revised Edition ) By Robert Lafore Reference Books: 1. Let Us C By Yashavant Kanetkar 2. C By Dissection ( Second Edition ) By Al Kelly & Ira Pohl 5 Course Website http://www.ssuet.edu.pk/courses/ce102/c http://taimoor/ce102/c 6 3

Marks Distribution Mid Term 15 Assignments 5 Lab + Quiz + Class Performance + Project 20 Semester Final Paper 60 Total Marks 100 7 Contents Variables and Constants Types of Variables The scanf ( ) function Address Operator The getch ( ) function Different types of Operators Comments 8 4

Variables Variables may be the most fundamental aspect of any computer language. A variable is a space in the computer s memory set aside for a certain kind of data and given a name for easy reference. Therefore variables are used so that the same space in memory can hold different values at different times. 9 Defining a Variable in C Defining a Variable in C int num ; If there are more than one variable to be defined then they can be defined combined or separately. For example: int a, b, c; int a; int b; int c; 10 5

Basic Data Types in C Language C Language supports five basic data types. Data Type 1. Character Description Character data Syntax char No of total Bytes occupied in memory 1 2. Integer 3. Float 4. Double Signed whole Integer Floating point number Double precision floating point number int float double 2 4 8 5. Void Valueless void 0 11 Variable Definitions Variable are generally declared as: type var-name; Here type is the data type and var-name is the variable name. For example: int number; 12 6

Value Assignment to the Variable A programmer can assign a unique value to the variable. The general form of an assignment statement is type var-name = value; OR var-name = value; For example: int year = 2001; number = 6; 13 What happens by writing the statement int number = 6? When you define a variable, the compiler sets aside an appropriate amount of memory to store that variable. In the above case we have specified an Integer variable, so the compiler will set aside 2 bytes of memory. This is large enough to hold numbers from 32768 to 32767 i.e 0-65535 for unsigned integers. int number; unsigned int number; 14 7

Long, Float and Double Data Type For situations when the normal integer is too small, the long integer ( type long or long int ) can be used. It occupies 4 bytes of memory and can hold integers from 2,147,483,648 to 2,147,483,647. There are also 2 kinds of floating point variables. One floating point variable type float, occupies 4 bytes and can hold numbers from 10 e+38 to 10 e-38 with between 6 and 7 digits of precision. A double-precision floating point variable, type double, occupies 8 bytes and can hold numbers from about 10 e+308 to 10 e-308 with about 15 digits of precision. 15 Long Double, Int Data Type There is also a larger long double type. It occupies 10 bytes and can hold numbers from about 10 e+4932 to 10 e-4932. The unsigned int type holds numbers from 0 to 65,535, rather than from 32,768 to 32,767 as the regular int type does. There is no string variable type in C Language. Instead, strings are represented by arrays of characters. 16 8

Program Using Variables void main (void) { int rollno; char section; float gpa = 3.25; rollno = 100; section = A ; clrscr( ); printf ( My name is %s. My Roll no is %d., Ahmad, rollno ); printf ( My section is %c and my GPA is %f., section, gpa); } Output: My name is Ahmad. My Roll no is 100. My section is A and my GPA is 3.25. 17 Field Width Specifiers The printf( ) gives programmer considerable power to format the printed output. By default floating point variable prints with 6 digits to the right of the decimal place. For example: printf ( GPA = %f, 3.25); Output: GPA = 3.250000 18 9

Field Width Specifiers Field Width Specifiers controls how many characters will be printed following the decimal point. For example: printf ( GPA = %2f, 3.25); Output: GPA = 3.25 printf ( GPA = %3f, 3.25); Output: GPA = 3.250 19 Escape Sequences In C Language backslash symbol ( \ ) is considered an Escape character: because it causes an escape from the normal interpretation of a string, so that the next character is recognized as having a special meaning. The following list shows the common escape sequence. \n New line \t Tab ( move 8 characters forward ) \b Backspace \r Carriage Return 20 10

Escape Sequences \f Form feed ( move to top of next page on the printer ) \ Single Quote \ Double Quote \\ Backslash \xdd ASCII code in hexadecimal notation \ ddd ASCII code in Octal notation ( each d represents a digit ) 21 Program Using Escape Sequences void main (void) { clrscr( ); printf ( Hello students, \ Have a nice time. \n ); } Output Hello students, Have a nice time. 22 11

Scanf ( ) Function In C Language, printf ( ) is the output statement where as the scanf ( ) is the input function statement. The scanf ( ) function can accepts input to several variables in one statement. For example: scanf ( %s %d, &name, &age ); Here is a new symbol: & ( Address Operator ) 23 Program Using Scanf ( ) void main (void) { float years, days; printf ( Please type your age in years. ); scanf ( %f, &years ); days = years * 365; printf ( You are %.1f days old., days ); } Output Please type your age in years. 10 You are 3650 days old. 24 12

Purpose of Address Operator What is the purpose of & Operator in C Language? The C compiler requires the arguments of scanf ( ) to be the address of the variables, rather than the variables themselves. Memory of the computer is divided into addressed bytes, and these bytes are numbered from 0 to the upper limit of your memory ( 655357 if you have 640 K memory ). These numbers are called the addresses of the bytes. Each variable occupies a certain location in the memory and its address is that of the first byte it occupies. More about Address Operator Suppose we have, declared an integer variable, num in a program, and assigned it the value 2. If the program later refers to the name of the variable, num, the compiler will give the value stored in that variable, or 2. However, if you refer to the name of the variable preceded by the ampersand, &num, the compiler will give the address where num is stored. Here is the program that demonstrates this operation: 25 26 13

Program Using Address Operator void main (void) { int num; num = 2; printf ( Value = %d, Address = %d, num, &num ); } Output: Value = 2, Address = 5528. 27 The getch ( ) function void main (void) { char ch; } print ( Type any Charater: ); ch = getch ( ); printf ( \n The character you typed was %c, ch); 28 14

The getch ( ) function You do not have to press the [ Return ] Key. It only takes a character. If you typed any wrong character, you cannot move backward to correct it. get get from outside ch character e echo ( write ) getch ( ) vs scanf ( ) 29 Operators Operators are words or symbol that causes a program to do something to variables. In C Language there are basically 4 types of operators. 1. Arithmetic Operator 2. Relational Operator 3. Arithmetic Assignment Operator 4. Increment Operator 30 15

Arithmetic Operator C Language uses 4 types of Arithmetic Operator that are common in most programming languages and one the remainder operators which is not so common. + Addition - Subtraction * Multiplication / Division % Remainder 31 Operator Precedence * and / operators are carried out before + and -. Programmers can alter the order of evaluation using parenthesis. Remainder Operator ( % ) is also called Modulo Operator. For Example: Answer = 13 % 5 Answer = 3 It is possible to include expressions involving Arithmetic Operator directly into printf ( ) and other kinds of statements. 32 16

Program void main (void) { int num; num = 2; printf ( Number plus 4 is %d, num + 4 ); getch ( ); } 33 Arithmetic Assignment Operator These operators are specially for C programmers. With the help of these operators, programmer can compress programming statements. There are 5 types of Arithmetic Assignment Operator + = Addition Arithmetic Assignment Operator -= Subtraction Arithmetic Assignment Operator * = Multiplication Arithmetic Assignment Operator / = Division Arithmetic Assignment Operator % = Remainder Arithmetic Assignment Operator For Example: total = total + number; total + = number; 34 17

Program Arithmetic Assignment Operator void main (void) { int total = 0; int count = 10; printf ( Total = %d \n, total ); total + = count; printf ( Total = %d \n, total ); total + = count; printf ( Total = %d \n, total ); } Output: Total = 0 Total = 10 Total = 20 35 Increment Operator C Language uses another operator that is not common in other languages i.e the Increment Operator. For Example: num + + ; or num - - ; 36 18

Program Using Increment Operator void main (void) { int num = 0; printf ( Number = %d \n, num ); printf ( Number = %d \n, num + + ); printf ( Number = %d \n, num ); getch ( ); } Output: Num = 0 Num = 1 Num = 1 37 Relational Operator Relational Operator checks the relation about the variables. The output of the relational operator will be 1 if the relation is true and 0, if the relation is false. In C Language, there are 6 Relational Operators: < Less than > Greater than < = Less than or Equal to > = Greater than or Equal to = = Equal to! = Not Equal to 38 19

Program Using Relational Operator void main (void) { int age ; age = 15; printf ( Is age less than 20? %d \n, age < 20 ); age = 30; printf ( Is age less than 20? %d \n, age < 20 ); getch ( ); } Output: Is age less than 20? 1 Is age less than 20? 0 39 Precedence between Operators void main (void) { printf ( Answer is %d, 1 < 2 + 4 ); getch ( ); } Output: Answer = 5 (Wrong) Answer = 1 ( Right ) Note Arithmetic Operators have higher precedence i.e they are evaluated before the Relational Operators. 40 20

Comments It is helpful to be able to put comments into the source code that can be ready by humans but are visible but are invisible to the compiler. A Comments begins with the two-character symbol slash-asterisk ( /* ) and ends with an asterisk-slash ( */ ). 41 Program Using Comments /* Program displays the name and age of the student */ /* Sir Syed Ahmad Khan */ void main (void) { int age = 25; printf ( Name is %s. My Age is %d., Ahmad, age ); getch( ); } Output: Name is Ahmad. My Age is 25. 42 21

Class Assignment No 2. 1. What do you mean by variable definition and variable assignment. Give examples. 2. What happens by writing the statement int number = 6 3. Difference between \n, \r, \f and \t. 4. Write a program that take the input of your age and then calculate the total number of hours old. 5. What is the purpose of &? 6. Differentiate between getch ( ) and getche ( ) with example. 7. Write a program that takes a user marks for the 5 subjects and then calculate the total marks obtained by the user. 8. Write a program using: = =, + +, % and < = operators. 43 22