Programming and Data Structures

Similar documents
Fundamentals of Programming

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Data Types and Variables in C language

ANSI C Programming Simple Programs

DEPARTMENT OF MATHS, MJ COLLEGE

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 2 - Introduction to C Programming

CS102: Variables and Expressions

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

6.096 Introduction to C++ January (IAP) 2009

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

General Announcements

VARIABLES AND CONSTANTS

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

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

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

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

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

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

Chapter 1 & 2 Introduction to C Language

General Announcements

Data types, variables, constants

Programming for Engineers Introduction to C

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

UNIT- 3 Introduction to C++

Work relative to other classes

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

ET156 Introduction to C Programming

A Fast Review of C Essentials Part I

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

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

Computer System and programming in C

C Language, Token, Keywords, Constant, variable

Lecture 3. More About C

Basics of Programming

2. Numbers In, Numbers Out

Department of Computer Applications

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

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

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

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

Computers Programming Course 5. Iulian Năstac

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Presented By : Gaurav Juneja

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2: Basic Elements of C++

ET156 Introduction to C Programming

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Creating a C++ Program

Chapter 2: Introduction to C++

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

2. Numbers In, Numbers Out

Basics of Java Programming

Lecture 2 Tao Wang 1

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

CS10001: Programming & Data Structures. Dept. of Computer Science & Engineering

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

Introduction to C# Applications

INTRODUCTION 1 AND REVIEW

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Structured programming

LECTURE 02 INTRODUCTION TO C++

UNIT-2 Introduction to C++

Differentiate Between Keywords and Identifiers

C - Basic Introduction

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

Fundamentals of C. Structure of 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:

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

A First Program - Greeting.cpp

Reserved Words and Identifiers

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Chapter 3. Fundamental Data Types

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

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Computer Programming CS F111

Basic Types and Formatted I/O

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

C Programming a Q & A Approach

Informatica e Sistemi in Tempo Reale

Fundamentals of Programming

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

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

Programming. C++ Basics

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Transcription:

Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur

#include <stdio.h> int main() { int n; scanf( %d,&n); printf( %d,n); return 0; } In and Out only

#include <stdio.h> int main() { int n; scanf( %d,&n); printf( %d,n+n); return 0; }

#include <stdio.h> int main() { int n, nsquare; scanf( %d,&n);//read the value of n nsquare = n*n; printf( %d, nsquare); return 0; }

#include <stdio.h> int main() { int n, nsquare; scanf( %d,&n);//read the value of n nsquare = n*n; printf( Square of n is %d\n, nsquare); return 0; }

#include <stdio.h> Integers variables declared before their usage. int main() Comments within /*.. */ { int n, nsquare; scanf( %d, &n);/* Read the value of n */ nsquare = n*n; Input statement for reading printf( %d, nsquare); variable from the keyboard return 0; } Control character for printing value of m in decimal digits.

#include <stdio.h> int main() { int n, nsquare; scanf( %d,&n);//read the value of n nsquare = n*n; printf( Square of %d is %d\n, n, nsquare); return 0; }

#include <stdio.h> #define PI 3.1416 double area_of_circle(float); double area_of_circle (float { return PI*radius*radius; } A complete C program Include header files radius) Declare global variables, constants and function prototypes Function bodies int main() { int squareside; There must be a main double area; function in any C program. scanf( %d, &squareside); area= area_of_circle(squareside/2); printf( Area of the circle enclosing the square of side %d is: %f\n, squareside, area); return 0; }

#include <stdio.h> #define PI 3.1416 double area_of_circle(float); double area_of_circle (float { return PI*radius*radius; } A complete C program Preprocessor statement. Replace PI by 3.1416 before compilation. radius) Example of a function called as per need from main program. int main() { int squareside; main() function: start of execution double area; scanf( %d, &squareside); Function called area= area_of_circle(squareside/2); printf( Area of the circle enclosing the square of side %d is: %f\n, squareside, area); return 0; }

Basic Data Types in C int :: integer quantity Typically occupies 4 bytes (32 bits) in memory. char :: single character Typically occupies 1 byte (8 bits) in memory. float :: floating-point number (a number with a decimal point) Typically occupies 4 bytes (32 bits) in memory. double :: double-precision floating-point number Precision refers to the number of significant digits after the decimal point.

Program to Multiply Two Floating Point Numbers #include <stdio.h> int main() { double firstnumber, secondnumber, productoftwonumbers; printf("enter two numbers: "); // Stores two floating point numbers in variable firstnumber and secondnumber respectively scanf("%f %f", &firstnumber, &secondnumber); // Performs multiplication and stores the result in variable productoftwonumbers productoftwonumbers = firstnumber Output: * secondnumber; } // Result up to 2 decimal point is displayed using %.2f Enter two numbers: 2.4 printf("product = %.2f", productoftwonumbers); 1.12 return 0; Product = 2.69

The C Character Set The C language alphabet: Uppercase letters A to Z Lowercase letters a to z Digits 0 to 9 Certain special characters:! # % ^ & * ( ) - _ + = ~ [ ] \ ; : { },. < > /? blank

Identifiers Identifiers Names given to various program elements (variables, constants, functions, etc.) May consist of letters, digits and the underscore ( _ ) character, with no space between. First character must be a letter. An identifier can be arbitrary long. Some C compilers recognize only the first few characters of the name (16 or 31). Case sensitive area, AREA and Area are all different.

Valid and Invalid Identifiers Valid identifiers X abc simple_interest a123 LIST stud_name Empl_1 Empl_2 avg_empl_salary Invalid identifiers 10abc hello simple interest (area) %rate

Keywords Keywords Reserved words that have standard, predefined meanings in C. Cannot be used as identifiers. OK within comments. Standard C keywords: auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Augmented Data Type Some of the basic data types can be augmented by using certain data type qualifiers: short long signed unsigned Typical examples: short int long int unsigned int

Integer type Type Storage size (in byte) Value range char 1-128 to 127 or 0 to 255 unsigned char 1 0 to 255 signed char 1-128 to 127 int 2 or 4-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 unsigned int 2 or 4 0 to 65,535 or 0 to 4,294,967,295 short 2-32,768 to 32,767 unsigned short 2 0 to 65,535 long 4-2,147,483,648 to 2,147,483,647 unsigned long 4 0 to 4,294,967,295

Integer type unsigned char 1 byte 8 bits 00000000 to 11111111 0 to 255 11111111 V 1 2 7 + 1 2 6 + 1 2 5 + 1 2 4 + 1 2 3 + 1 2 2 + 1 2 1 + 1 2 0 signed char 1 byte 8 bits 00000000 to 11111111-128 to 127 1111111 1 2 6 + 1 2 5 + 1 2 4 + 1 2 3 + 1 2 2 + 1 2 1 + 1 2 0

Floating-point type Type Storage size (in byte) Value range Precision float 4 1.2E-38 to 3.4E+38 6 decimal places double 8 2.3E-308 to 1.7E+308 15 decimal places long double 10 3.4E-4932 to 1.1E+4932 19 decimal places E or e means 10 to the power of

ASCII Table

Extended ASCII Table (American Standard Code for Information Interchange)

Some Examples of Data Types int char float 0, 25, -156, 12345, 99820 a, A, *, /, 23.54, 0.00345, 25.0 2.5E12, 1.234e-5 E or e means 10 to the power of

C Program to Find ASCII Value of a Character #include <stdio.h> int main() { char c; printf("enter a character: "); Output Enter a character: G ASCII value of G = 71 // Reads character input from the user scanf( %c", &c); // %d displays the integer value of a character // %c displays the actual character printf("ascii value of %c = %d", c, c); return 0; }

Constants Constants Numeric Constants Character Constants integer floating-point single character A constant is a value or an identifier whose value cannot be altered in a program. string

Integer Constants Consists of a sequence of digits, with possibly a plus or a minus sign before it. Embedded spaces, commas and non-digit characters are not permitted between digits. Maximum and minimum values (for 32-bit representations) Maximum :: 2147483647 Minimum :: 2147483648

Floating-point Constants Can contain fractional parts. Very large or very small numbers can be represented. 23000000 can be represented as 2.3e7 Two different notations: 1. Decimal notation 25.0, 0.0034,.84, -2.234 2. Exponential (scientific) notation 3.45e23, 0.123e-12, 123E2 e means 10 to the power of

Single Character Constants Contains a single character enclosed within a pair of single quote marks ( ). Examples :: 2, +, Z Some special backslash characters \n new line \t horizontal tab \ single quote \ double quote \\ backslash \0 null

String Constants Sequence of characters enclosed in double quotes ( ). The characters may be letters, numbers, special characters and blank spaces. Examples: nice, Good Morning, 3+6, 3, C Differences from character constants: C and C are not equivalent. C has an equivalent integer value while C does not.

An identifier also can be defined as a constant. const double PI = 3.14 Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.

Variables A variable is a container to hold data. To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. int speed = 10; The value of a variable can be changed, hence the name 'variable'. In C programming, you have to declare a variable before you can use it.

Example int a, b, c; char x; a = 3; b = 50; c = a b; x = d ; b = 20; a = a + 1; x = G ; Variables Constants

Declaration of Variables There are two purposes: 1. It tells the compiler what the variable name is. 2. It specifies what type of data the variable will hold. General syntax: data-type variable-list; Examples: int velocity, distance; int a, b, c, d; float temp; char flag, option;

Address and Content speed int speed; speed=234; 1349 1350 11101010 1351 1352 speed 234 &speed 1350 Binary of 234 Every variable has an address (in memory), and its contents.

Address and Content In C terminology, in an expression speed refers to the contents of the memory location. &speed refers to the address of the memory location. Examples: printf ( %f %f %f, speed, time, distance); scanf ( %f %f, &speed, &time);

An Example #include <stdio.h> int main() { float speed, time, distance; Address of speed scanf ( %f %f, &speed, &time); distance = speed * time; printf ( \n The distance traversed is: \n, distance); return 0; Content of speed }

Assignment Statement Used to assign values to variables, using the assignment operator (=). General syntax: variable_name = expression; Examples: velocity = 20; b = 15; temp = 12.5; A = A + 10; v = u + f * t; s = u * t + 0.5 * f * t * t;

Assignment Statement Assignment during declaration int speed = 30; char flag = y ; Multiple variable assignment a = b = c = 5; flag1 = flag2 = y ; speed = flow = 20.0;

Operators in Expressions Operators Arithmetic Operators Relational Operators Logical Operators

Arithmetic Operators X= 25; Y=23; Addition :: + Subtraction :: Division :: / Multiplication :: * Modulus :: % X + Y 48 X Y 2 X * Y 575 X / Y? X % Y??

Program to Compute Quotient and Remainder #include <stdio.h> int main(){ int dividend, divisor, quotient, remainder; Output printf("enter dividend: "); scanf("%d", &dividend); printf("enter divisor: "); scanf( %d", &divisor); Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1 // Computes quotient quotient = dividend / divisor; // Computes remainder remainder = dividend % divisor; printf("quotient = %d\n", quotient); printf("remainder = %d", remainder); } return 0;

Operator Precedence In decreasing order of priority 1. Parentheses :: ( ) 2. Unary minus :: 5 3. Multiplication, Division, and Modulus 4. Addition and Subtraction For operators of the same priority, evaluation is from left to right as they appear. Parenthesis may be used to change the precedence of operator evaluation.

Examples: Arithmetic expressions v = u + f t; v = u+(f t); X = x y / z X = (x y)/z A = a + b c d / e A = b c + d % e A = ((a+b) ((c d)/e)) A = ((( b) c)+(d%e))

Integer Arithmetic When the operands in an arithmetic expression are integers, the expression is called integer expression, and the operation is called integer arithmetic. Integer arithmetic always yields integer values.

Real Arithmetic Involving only real or floating-point operands (including double, long double). Since floating-point values are rounded to the number of significant digits permissible, the final value is an approximation of the final result. A = 22/7*7*7 = (((22/7)*7)*7) = 153.86 =(((22*7)/7)*7) = 154 The modulus operator cannot be used with real operands.

Arithmetic integer /real An expression contains only integer operands Integer arithmetic will be performed. An expression contains only real operands Real arithmetic will be performed. An expression contains integer and real both the operands Real arithmetic will be performed.

Type casting A faulty reciprocal finder #include <stdio.h> The division 1/n is of integers (quotient). int main () The format %d is for printing integers. { int n; scanf("%d",&n); printf("%d\n",1/n); return 0; }

Type casting #include <stdio.h> int main () { int n; scanf("%d",&n); printf("%f\n",1.0/n); return 0; } #include <stdio.h> int main () { int n; float x; scanf("%d",&n); x=(float)1/n; printf("%f\n",x); return 0; }

Type casting Integer to real int a=10; float b; b=(float)a; Real to real float b; double c=3.14; b=(float)c; Real to integer int a; float b=3.14; a=(int)b; Real to real float b; double c; c=22.0/7.0; b=(float)c;

#include <stdio.h> int main() { } int number; Program to Print an Integer // printf() dislpays the formatted output printf("enter an integer: "); // scanf() reads the formatted input and stores them scanf("%d", &number); // printf() displays the formatted output printf("you entered: %d", number); return 0; Output: Enter a integer: 25 You entered: 25

#include <stdio.h> int main() { } long long n; int count = 0; printf("enter an integer: "); scanf("%lld", &n); while(n!= 0) { } // n = n/10 n /= 10; ++count; Program to Count Number of Digits in an Integer Output printf("number of digits: %d", count); Enter an integer: 3452 Number of digits: 4