Programming and Data Structure

Similar documents
Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

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

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

Course Outline Introduction to C-Programming

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Computer Programming

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

BİL200 TUTORIAL-EXERCISES Objective:

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

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

General Announcements

Computer Science & Engineering 150A Problem Solving Using Computers

Pointers and scanf() Steven R. Bagley

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

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

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?

Preview from Notesale.co.uk Page 6 of 52

Computational Methods of Scientific Programming Lecture 8. Today s lecture Start C/C++ Basic language features

Technical Questions. Q 1) What are the key features in C programming language?

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

General Announcements

Programming in C Quick Start! Biostatistics 615 Lecture 4

The C language. Introductory course #1

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

EC 413 Computer Organization

COMP 208 Computers in Engineering

Computer System and programming in C

Chapter 1 & 2 Introduction to C Language

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

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

Chapter 1 Getting Started Structured Programming 1

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

The C standard library

Programming Language Basics

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

Functions. Systems Programming Concepts

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

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

Programming and Data Structures

Lecture 2: C Programming Basic

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

Chapter 1 Introduction to Computers and C++ Programming

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

C Programs: Simple Statements and Expressions

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

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

Programming in C. What is C?... What is C?

Programming and Data Structure Laboratory (CS13002)

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

Program Organization and Comments

C Programming Multiple. Choice

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

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

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

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

UEE1302 (1102) F10: Introduction to Computers and Programming

Chapter 11 Introduction to Programming in C

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

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

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

C Introduction. Comparison w/ Java, Memory Model, and Pointers

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

Lesson 3 Introduction to Programming in C

Multiple Choice Questions ( 1 mark)

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS Programming In C

JTSK Programming in C II C-Lab II. Lecture 3 & 4


Chapter 11 Introduction to Programming in C

Introduction to C Language

Tutorial No. 2 - Solution (Overview of C)

Fundamental of Programming (C)

ANSI C Programming Simple Programs

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

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

Fundamentals of C Programming

The detail of sea.c [1] #include <stdio.h> The preprocessor inserts the header file stdio.h at the point.

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS3157: Advanced Programming. Outline

PROGRAMMING IN C AND C++:

C: How to Program. Week /Mar/05

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

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

C Language, Token, Keywords, Constant, variable

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Chapter 11 Introduction to Programming in C

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

EL2310 Scientific Programming

Transcription:

Programming and Data Structure Sujoy Ghose Sudeshna Sarkar Jayanta Mukhopadhyay Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Spring Semester 2012 Programming and Data Structure 1

Our First Program: Convert Centigrade to Fahrenheit // this program takes a centigrade value as input and converts it into fahrenheit int main( ) { float C; float F; printf ( Input in centigrades\n ) ; scanf ( %f, &C); F=C*9/5+32; printf (Fahrenheit value = %f\n, F) ; Spring Semester 2012 Programming and Data Structure 2

A variation: intctof.c // this program takes a centigrade value as input and converts it into fahrenheit int main( ) { int C; int F; printf ( Input in centigrades\n ) ; scanf ( %d, &C); F=C*9/5+32; printf (Fahrenheit value = %d\n, F) ; Spring Semester 2012 Programming and Data Structure 3

Outputs // this program takes a centigrade value as // input and converts it into fahrenheit int main( ) { int C; int F; printf ( Input in centigrades\n ) ; scanf ( %d, &C); F=C*9/5+32; printf (Fahrenheit value = %d\n, F) ; Input Centigrade 4 14 27 FtoC 39.2 57.2 80.6 intftoc Spring Semester 2012 Programming and Data Structure 4

Outputs // intftoc // this program takes a centigrade value as // input and converts it into fahrenheit int main( ) { int C; int F; printf ( Input in centigrades\n ) ; scanf ( %d, &C); F=C*9/5+32; printf (Fahrenheit value = %d\n, F) ; Input Centigrade 4 14 27 FtoC 39.2 57.2 80.6 intftoc 39 57 80 Spring Semester 2012 Programming and Data Structure 5

Outputs // intftoc2 // this program takes a centigrade value as // input and converts it into fahrenheit int main( ) { int C; int F; printf ( Input in centigrades\n ) ; scanf ( %d, &C); F=C/5*9+32; printf (Fahrenheit value = %d\n, F) ; Input Centigrade 4 14 27 FtoC 39.2 57.2 80.6 intftoc 39 57 80 intftoc2 Spring Semester 2012 Programming and Data Structure 6

Outputs // intftoc2 // this program takes a centigrade value as // input and converts it into fahrenheit int main( ) { int C; int F; printf ( Input in centigrades\n ) ; scanf ( %d, &C); F=C/5*9+32; printf (Fahrenheit value = %d\n, F) ; Input Centigrade 4 14 27 FtoC 39.2 57.2 80.6 intftoc 39 57 80 intftoc2 0 50 77 Spring Semester 2012 Programming and Data Structure 7

Find the roots of a quadratic equation Ax 2 + Bx + C = 0 Spring Semester 2012 Programming and Data Structure 8

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1,root2,disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B 4*A*C; disc = sqrt(disc); root1 = ( B+disc)/(2*A); root2 = ( B-disc)/(2*A); printf ("root 1= %f, root2=%f\n,root1, root2); Spring Semester 2012 Programming and Data Structure 9

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1,root2,disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B-4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2=%f\n,root1, root2); Spring Semester 2012 Programming and Data Structure 10

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1, root2, disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B - 4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2=%f\n,root1, root2); Spring Semester 2012 Programming and Data Structure 11

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1, root2, disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B - 4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2=%f\n,root1, root2); Spring Semester 2012 Programming and Data Structure 12

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1, root2, disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B - 4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2=%f\n, root1, root2); Spring Semester 2012 Programming and Data Structure 13

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1, root2, disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B - 4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2 =%f\n, root1, root2); Compile and run this program. gcc quad.c./a.out Input A B C 1 3 2 root1=-1, root2=-2./a.out Input A B C 1 1 1 root1=nan, root2=nan Spring Semester 2012 Programming and Data Structure 14

Solving a Quadratic Equation #include <math.h> // solves Ax^2 + Bx + C = 0 int main( ) { float A,B,C; float root1, root2, disc; printf ( Input A B C \n ); scanf ( %f%f%f, &A, &B, &C) ; disc = B*B - 4*A*C; disc = sqrt(disc); root1 = (-B+disc)/(2*A); root2 = (-B-disc)/(2*A); printf ("root 1= %f, root2 =%f\n, root1, root2); Compile and run this program. gcc quad.c./a.out Input A B C 1 3 2 root1=-1, root2=-2./a.out Input A B C 1 1 1 root1=nan, root2=nan Spring Semester 2012 Programming and Data Structure 15

Why has this happened? This happened because B 2 4AC = 1 3 = 2. The square root operation returns a special value, Not-A- Number (NAN). What is to be done? Spring Semester 2012 Programming and Data Structure 16

new_sqrt.c #include <math.h> // general square roots int main( ) { float r, x, y; printf ( Enter a real number.\n ); scanf ( %f, &r) ; if (r<0) { r = r; x = sqrt(r); printf ( %f i \n, x) ; x=sqrt(r); printf ( %f \n, x) ; if r is negative then we take the sqrt of its negative and print it with an i. The program then exits via return. if r >= 0 then execution proceeds the normal way. Spring Semester 2012 Programming and Data Structure 17

new2_sqrt.c #include <math.h> // general square root int main( ) { float r, x, y; printf ( Enter a real number.\n ); scanf ( %f, &r) ; if (r<0) { r = -r; x = sqrt(r); printf ( %f i \n, x) ; else { x=sqrt(r); printf ( %f \n, x) ; Spring Semester 2012 Programming and Data Structure 19

Program Structure: Some include commands int main() { code block Variables: Memory registers may have names. These must be words beginning with a non-numeral. Declarations: Every variable must be declared to be of a certain type such as int, float. Operations must respect this type. Input and Output is enabled through printf, scanf. Variable contents and strings may be manipulated inorder. Assignments are done by var = expression; Spring Semester 2012 Programming and Data Structure 20

What is C? Developed by Dennis Ritchie AT&T Bell Laboratories 1972 for use with the Unix operating system. (The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 ) Widely used today extends to newer system architectures efficiency/performance low-level access C features: Few keywords Structures, unions compound data types Pointers memory, arrays External standard library I/O, other facilities Compiles to native code Macro preprocessor Spring Semester 2012 Programming and Data Structure 21

The C Programming Language C is a fast, small, general-purpose, structured programming language. C can be used for applications programming as well as for systems programming (e.g., compilers and interpreters, operating systems, database systems, microcontrollers etc.) There are only 32 keywords and its strength lies in its built-in functions. C is highly portable, since it relegated much computerdependent features to its library functions. "C is quirky, flawed, and an enormous success." Ritchie Spring Semester 2012 Programming and Data Structure 22

Versions of C Evolved over the years: 1972 C invented 1978 The C Programming Language published; first specification of language 1989 C89 standard (known as ANSI C or Standard C) 1990 ANSI C adopted by ISO, known as C901999 C99 standard mostly backward-compatible not completely implemented in many compilers 2007 work on new C standard C1X (now called C11) In this course: ANSI/ISO C (C89/C90) Spring Semester 2012 Programming and Data Structure 23

Structure of a.c file /* Begin with comments about file contents */ Insert #include statements and preprocessor definitions Function prototypes and variable declarations Define main() function { Function body Define other function { Function body Spring Semester 2012 Programming and Data Structure 24

Comments Comments: / this is a simple comment / Can span multiple lines /* convert.c : A C program than converts from fahrenheit to centigrade / Completely ignored by compiler Can appear almost anywhere Alternative way to show comments // This function computes the square root // of a floating point number Spring Semester 2012 Programming and Data Structure 25

The #include macro Header files: constants, functions, other declarations read the contents of the header file stdio.h stdio.h: standard I/O functions for console, files : Part of the standard C library other important header files: ctype.h, math.h, stdlib.h, string.h, time.h Spring Semester 2012 Programming and Data Structure 26

Declaring variables Must declare variables before use Variable declaration: int number; float temperature; int - integer data type float - floating-point data type Many other types Spring Semester 2012 Programming and Data Structure 27

Functions Every C program consists of one or more functions. One of the functions must be called main. The program will always begin by executing the main function. Each function must contain: A function heading, which consists of the function name, followed by an optional list of arguments enclosed in parentheses. A list of argument declarations. A compound statement, which comprises the remainder of the function. Spring Semester 2012 Programming and Data Structure 28

Contd. Each compound statement is enclosed within a pair of braces: { and The braces may contain combinations of elementary statements and other compound statements. Comments may appear anywhere in a program, enclosed within delimiters /* and */. Example: a = b + c; /* ADD TWO NUMBERS */ Spring Semester 2012 Programming and Data Structure 29

Function Prototypes General form: return_type function_name(arg1,arg2,...); Arguments: local variables, values passed from caller Return value: single value returned to caller when function exits void signifies no return value/arguments int rand (void); Spring Semester 2012 Programming and Data Structure 30

The main () function main() : entry point for C program Simplest version: no inputs, outputs 0 when successful, and nonzero to signal some error int main (void) ; Two-argument form of main(): access command-line arguments int main(int argc, char argv); Will be discussed later in the course Spring Semester 2012 Programming and Data Structure 31

Sample C program 3 int main() { printf ( \n Our first look at a C program \n ); Curly braces within which statements are executed one after another. Header file includes functions for input/output Main function is executed when you run the program. (Later we will see how to pass its parameters) Statement for printing the sentence within double quotes (.. ). \n denotes end of line. Spring Semester 2012 Programming and Data Structure 32

Sample C program 4 int main() { int a, b, c; a = 10; b = 20; c = a + b; Integers variables declared before their usage. Control character for printing value of a in decimal digits. printf ( \n The sum of %d and %d is %d\n, a,b,c); The sum of 10 and 20 is 30 Spring Semester 2012 Programming and Data Structure 33

Sample C program 5 /* FIND THE LARGEST OF THREE NUMBERS */ int main() { int a, b, c; scanf ( %d %d %d, &a, &b, &c); if ((a>b) && (a>c)) /* Composite condition check */ printf ( \n Largest is %d, a); else { if (b>c) /* Simple condition check */ printf ( \n Largest is %d, b); else printf ( \n Largest is %d, c); Input statement for reading three variables from the keyboard Conditional statement Spring Semester 2012 Programming and Data Structure 34

if statement The if statement is used as: prev_line if (condn) { code block1 else { code block2 next_line Spring Semester 2012 Programming and Data Structure 35

if statement The if statement is used as: prev_line if (condn) { code block1 else { code block2 next_line If condn evaluates to true then the sequence is: prev_line code block1 next_line; Spring Semester 2012 Programming and Data Structure 36

if statement The if statement is used as: prev_line if (condn) { code block1 else { code block2 next_line If condn evaluates to true then the sequence is: prev_line code block1 next_line; otherwise it is: prev_line code block2 next_line; Spring Semester 2012 Programming and Data Structure 37

Preprocessor statement. Replace PI by 3.1415926 before compilation. Sample C program 6 Example of a function called as per need from main programme. #define PI 3.1415926 /* Compute the area of a circle */ int main() { float radius, area; float myfunc (float radius); float myfunc (float r) { float a; a = PI * r * r; return (a); /* return result */ scanf ( %f, &radius); area = myfunc (radius); printf ( \n Area is %f \n, area); Function called. Spring Semester 2012 Programming and Data Structure 38

int main( ) { int a, b, c; main() is also a function a = 10; b = 20; c = a + b; printf ( \n The sum of %d and %d is %d\n, a,b,c); Spring Semester 2012 Programming and Data Structure 39

Clarity Desirable Programming Style The program should be clearly written. It should be easy to follow the program logic. Meaningful variable names Make variable/constant names meaningful to enhance program clarity. area instead of a radius instead of r Program documentation Insert comments in the program to make it easy to understand. But never use too many comments. Spring Semester 2012 Programming and Data Structure 40

Program indentation Contd. Use proper indentation. Structure of the program should be immediately visible. Spring Semester 2012 Programming and Data Structure 41

Indentation Example #1 :: Bad Style #define PI 3.1415926 /* Compute the area of a circle */ int main ( ) { float radius, area; float myfunc (float radius); scanf ( %f, &radius); area = myfunc (radius); printf ( \n Area is %f \n, area); float myfunc (float r) { float a; a = PI * r * r; return (a); /* return result */ Spring Semester 2012 Programming and Data Structure 42

Indentation Example #1 :: Good Style #define PI 3.1415926 /* Compute the area of a circle */ int main() { float radius, area; float myfunc (float radius); float myfunc (float rad) { float circlearea; circlearea = PI * r * r; return (circlarea); /* return result */ scanf ( %f, &radius); area = myfunc (radius); printf ( \n Area is %f \n, area); Spring Semester 2012 Programming and Data Structure 43

Indentation Example #2 :: Good Style /* FIND THE LARGEST OF THREE NUMBERS */ int main() { int a, b, c; scanf ( %d %d %d, &a, &b, &c); if ((a>b) && (a>c)) /* Composite condition check */ printf ( \n Largest is %d, a); else if (b>c) /* Simple condition check */ printf ( \n Largest is %d, b); else printf ( \n Largest is %d, c); Spring Semester 2012 Programming and Data Structure 44

Indentation Example #2 :: Bad Style /* FIND THE LARGEST OF THREE NUMBERS */ int main() { int a, b, c; scanf ( %d %d %d, &a, &b, &c); if ((a>b) && (a>c)) printf ( \n Largest is %d, a); else if (b>c) /* Simple condition check */ printf ( \n Largest is %d, b); else printf ( \n Largest is %d, c); Spring Semester 2012 Programming and Data Structure 45