Structure of a C Program

Size: px
Start display at page:

Download "Structure of a C Program"

Transcription

1 ummer School Computer Sc & Engg: IIT Kharagpur 1 Structure of a C Program

2 ummer School Computer Sc & Engg: IIT Kharagpur 2 #include <stdio.h> // cpp directive int hcf(int, int); // Function Interface int main() { // main function int large, small; printf("enter two non-ve integers\n"); scanf("%d%d", &large, &small); printf("hcf(%d, %d) = %d\n", large,small,hcf(large,small)); return 0; } // main ends sample.c

3 ummer School Computer Sc & Engg: IIT Kharagpur 3 int hcf(int sml, int lrg){ // Function int rem; // definition while(sml!= 0){ rem = lrg%sml; lrg = sml; sml = rem; } return lrg; } // Definition ends

4 ummer School Computer Sc & Engg: IIT Kharagpur 4 Structure of an OCAML Program (* sample.ml: An OCAML program *) exception BothZero (* Exception *) exception DataNegative (* Exception *) let rec hcf sml lrg = (* Another function *) if sml = 0 then lrg else (hcf (lrg mod sml) sml);; let main() = (* Function definition *) print_string("enter two non-negative integers\ let m = read_int() and n = read_int() in

5 ummer School Computer Sc & Engg: IIT Kharagpur 5 if m<0 n<0 then raise DataNegative; if m=0 && n=0 then raise BothZero else Printf.printf "hcf(%d, %d) = %d\n" m n (hcf m n);;

6 ummer School Computer Sc & Engg: IIT Kharagpur 6 File Name File name of a C program ends with.c and the file name of an OCAML program ends with.ml in the previous examples they are sample.c and sample.ml.

7 ummer School Computer Sc & Engg: IIT Kharagpur 7 Structure of a C Program It is a collection of preprocessor directives, functions and declarations. A function named main() is mandatory.

8 ummer School Computer Sc & Engg: IIT Kharagpur 8 A Preprocessor Directive #include <stdio.h> A line starting with a sharp sign # is a C preprocessor directive.

9 ummer School Computer Sc & Engg: IIT Kharagpur 9 Function Interface or Prototype int hcf(int, int); The prototype directs the compiler to generate correct invocation code. The function hcf() is used (called/invoked) in the function main() before it is defined.

10 ummer School Computer Sc & Engg: IIT Kharagpur 10 A Function Definition int hcf(int sml, int lrg){ // Function int rem; // definition while(sml!= 0){ rem = lrg%sml; lrg = sml; sml = rem; } return lrg; } // Definition ends A definition specifies the name, parameters, computation and the type of the return value.

11 ummer School Computer Sc & Engg: IIT Kharagpur 11 Parameters and Return Value int hcf(int sml, int lrg) The function hcf takes two parameters, both are of type int and also gives back (returns) a value of type int after the computation a. a Mathematically hcf: int int int.

12 ummer School Computer Sc & Engg: IIT Kharagpur 12 Function main() int main() is also a function and its presence in a C program is mandatory. In this program main() does not take any parameter.

13 ummer School Computer Sc & Engg: IIT Kharagpur 13 Variable Declaration int large, small; large and small are called local variables to the function main(). Similarly rem is a local variable to the function hcf().

14 ummer School Computer Sc & Engg: IIT Kharagpur 14 Statements rem = lrg%sml; lrg = sml; while( ){ } return lrg;

15 ummer School Computer Sc & Engg: IIT Kharagpur 15 Operators Assignment: = Relational:! = Mod: %

16 ummer School Computer Sc & Engg: IIT Kharagpur 16 Library Functions Reads from Keyboard: scanf() Writes on the VDU: printf() Library functions are supplied along with the compiler.

17 ummer School Computer Sc & Engg: IIT Kharagpur 17 Compile and Run $ cc -Wall sample.c $./a.out Enter two non-ve integers HCF(12, 18) = 6

18 ummer School Computer Sc & Engg: IIT Kharagpur 18 Structure of an OCAML Program It is a collection of function definitions and function invocations.

19 ummer School Computer Sc & Engg: IIT Kharagpur 19 Definition of hcf let rec hcf sml lrg = (* function hcf *) if sml = 0 then lrg else (hcf (lrg mod sml) sml);; A definition specifies Name: hcf (global), Parameters: sml lrg (local), computation such as expressions etc.

20 ummer School Computer Sc & Engg: IIT Kharagpur 20 Note The interpreter infers the types of different objects. The type inferencing takes place at the time of compilation. It is known as static type inferencing.

21 ummer School Computer Sc & Engg: IIT Kharagpur 21 Function main() main() is another function (no special status). It is of type unit -> unit, that means it does not take any parameter or returns any value. This may be compared with type void of C.

22 ummer School Computer Sc & Engg: IIT Kharagpur 22 Parameters and Return Value In the function hcf, the parameters are sml and lrg. Both are of type int. This inference is drawn from the comparison of sml with integer 0, and the arithmetic operator mod between fst and sml.

23 ummer School Computer Sc & Engg: IIT Kharagpur 23 Expressions lrg mod sml sml = 0 if sml = 0 then lrg else...

24 ummer School Computer Sc & Engg: IIT Kharagpur 24 Local Binding of Names let m = read int() and n = read int() in m, n are local names (they are not variables like C), they bound to the data of type int read from the keyboard. They are visible only to the remaining portion of the code of main.

25 ummer School Computer Sc & Engg: IIT Kharagpur 25 Library Functions print string() read int() Printf.printf Library functions are supplied along with the interpreter. [read_int() takes one input per line.]

26 ummer School Computer Sc & Engg: IIT Kharagpur 26 CPP Compiler Assembler Linker (sample.c) C Program Object Module/ Library Linker C Preprocessor Modified C Program C Compiler Assembler Assembly Language Program (sample.s) Executable Module (a.out) Object Module (sample.o)

27 ummer School Computer Sc & Engg: IIT Kharagpur 27 Laboratory Session

28 ummer School Computer Sc & Engg: IIT Kharagpur 28 Activity - VIII It is known that the sum of internal angles of a triangle is 180 (π c ), that of a quadrilateral is 360 (2π c ), and for a pentagon it is 540 (3π c ). In general the sum of the internal angles of an n-sided polygon is 180(n 2) degrees (n 2)π c. Write a C program that reads the number of sides of a polygon (n) and prints the value of the sum of internal angles.

29 ummer School Computer Sc & Engg: IIT Kharagpur 29 Activity - VIIIa Write a OCAML function for the previous problem.

30 ummer School Computer Sc & Engg: IIT Kharagpur 30 Quiz on C Program Write answers in your note-book. 1. What is the type of the variable corresponding to the sides of a polygon? 2. Name three operators you used. 3. What is the format string for reading the variable for the number of sides, n?

31 ummer School Computer Sc & Engg: IIT Kharagpur 31 Quiz on OCAML Function Write answers in your note-book. 1. How many operators are there? 2. How many parameters are there? 3. How do you modify the function to get a more meaningful output? 4. How do you modify the program that reads the number of sides of the polygon?

32 ummer School Computer Sc & Engg: IIT Kharagpur 32 Activity - IX Write a C program that reads the constant velocity v (m/sec) of an object and the time t (sec) of its travel. It prints the distance d (m) traversed by the object in t sec. [d = vt]

33 ummer School Computer Sc & Engg: IIT Kharagpur 33 Quiz on C Program Write answers in your note-book. 1. What are the types of the variables corresponding to velocity, time and distance? 2. Name two operators you used. 3. What is the format string for reading the variables.

34 ummer School Computer Sc & Engg: IIT Kharagpur 34 Activity - IXa Write an OCAML that takes the constant velocity v (m/sec) of an object and the time t (sec) of its travel as parameters. It returns the distance d (m) traversed by the object in t sec. [d = vt]

35 ummer School Computer Sc & Engg: IIT Kharagpur 35 Quiz on OCAML Function Write answers in your note-book. 1. What are the operators you have chosen? 2. Modify it to a program that reads the velocity and time from the keyboard and prints the output properly.

36 ummer School Computer Sc & Engg: IIT Kharagpur 36 Activity - X Modify your program that takes v and d as inputs and prints the time taken to travel.

37 ummer School Computer Sc & Engg: IIT Kharagpur 37 Activity - XI Consider the following polynomial p(x) = 2.5x x 2 + 4x + 9 where x is a variable (unknown) a. Write a C program that reads a value of x and evaluates the value of the polynomial for the input value of x. It prints both the value of x and the value of p(x). a It is a cubic polynomial of x.

38 ummer School Computer Sc & Engg: IIT Kharagpur 38 Note Let the input value of x be 2.0. The program computes p(2.0) = = 59.0 The program prints p(2.0) = Note that x 3 x*x*x.

39 ummer School Computer Sc & Engg: IIT Kharagpur 39 Activity - XIa Write an OCAML function to compute the polynomial. It read the value of the input from the keyboard.

40 ummer School Computer Sc & Engg: IIT Kharagpur 40 Quiz Write answers in your note-book. 1. How many multiplications are you performing? Can you reduce their number? In a computer multiplication and division often takes more time than addition and subtraction. 2. What is your print format?

41 ummer School Computer Sc & Engg: IIT Kharagpur 41 Activity - XII Write a C program that reads a number with a decimal point, (floating point number) of the form nnnn.fffff e.g It stores the integral part nnnnn (237 in the example) in a variable of type int and the fractional part,.ffffff (.512), in a variable of type float or double. Finally it prints both the parts separately.

42 ummer School Computer Sc & Engg: IIT Kharagpur 42 Activity - XIIa Following OCAML function does the job: let intfrac x = (int_of_float) x, x -. (float) (int_of_float x));; Write a CAMAL program using the function that reads the data from the keyboard and prints it on the VDU.

43 ummer School Computer Sc & Engg: IIT Kharagpur 43 Activity - XIII Write a C program that takes an input of the form a.b, where both a and b have one or two digits e.g. 1.2, 12.5, 0.19, 2.0, etc. The program prints the number in n p q form e.g /10, /10, 19/100 etc.

44 ummer School Computer Sc & Engg: IIT Kharagpur 44 Activity - XIIIa Write an OCAML program to solve the previous problem.

45 ummer School Computer Sc & Engg: IIT Kharagpur 45 Activity - XIV Write a C program that reads the lengths of the three sides of a triangle a, b and c and uses the Heron s formula to calculate the area of the triangle. The area A = s(s a)(s b)(s c), where s is the semiperimeter of the triangle, s = a+b+c 2.

46 ummer School Computer Sc & Engg: IIT Kharagpur 46 Note You have to include math.h #include <math.h> and link the mathematical function library. $ cc -Wall -lm file.c

47 ummer School Computer Sc & Engg: IIT Kharagpur 47 Activity - XIVa Solve the same problem in OCAML.

48 ummer School Computer Sc & Engg: IIT Kharagpur 48 Activity - XV Write a C program that takes an input of the form a.b, where both a and b have one or two digits e.g. 1.2, 12.5, 0.19, 2.0, etc. The program prints the floating point number of the form b.a e.g. 2.1, 5.12, 19.0, 0.2, etc.

49 ummer School Computer Sc & Engg: IIT Kharagpur 49 Another Version of OCAML HCF Program (* samplea.ml: An OCAML program *) let hcf sml lrg = let l = ref lrg and s = ref sml in while!s <> 0 do let rem = ref (!l mod!s) in l :=!s; s :=!rem done; print_int(!l);;

50 ummer School Computer Sc & Engg: IIT Kharagpur 50 let main() = (* function main *) print_string("enter two non-negative integers\ let m = read_int() and n = read_int() in Printf.printf "hcf(%d, %d) = " m n; hcf m n ; print_newline();; main();;

51 ummer School Computer Sc & Engg: IIT Kharagpur 51 OCAML Program VIIIa (* Assignment VIIIa: Internal angles of a polygon *) let intang = function n -> Printf.printf "Sum of the internal angles of a %d-gone is %d deg\n" n (180*(n-2));;

52 ummer School Computer Sc & Engg: IIT Kharagpur 52 OCAML Program IXa (* Assignment IXa: distance covered with linear velocity *) let dist() = print_string("enter the velocity (m/sec): "); let v = read_float() in print_string("\nenter the time (sec): "); let t = read_float() in print_string("the distance covered with velocity ") Printf.printf "%6.2f m/sec in time %6.2f sec is %6.2f m\n" v t (v *. t) ;;

53 ummer School Computer Sc & Engg: IIT Kharagpur 53 OCAML Program XIa (* Assignment XIa: Evaluating a polynomial *) let polyval() = print_string("enter the value of x: "); let x = read_float() in Printf.printf "The value of the polynomial for x = %f is %f\n" x (x*.(x*.(2.5*.x+.5.5)+.4.)+.9.);;

54 ummer School Computer Sc & Engg: IIT Kharagpur 54 OCAML Program XIIa (* Assignment XIIa: int and frac *) let intfrac() = print_string("enter the value of x: "); let x = read_float() in let n = int_of_float x in Printf.printf "%f = (%d, %f)\n" x n (x -. (float) n);;

55 ummer School Computer Sc & Engg: IIT Kharagpur 55 OCAML Program XIIIa (* ass13a.ml: Int and frac *) let floattorat() = let s = read_line() in let n = int_of_float (float_of_string s) in let t = String.copy s in (* t = s does not work *) s.[0] <- 0 ; s.[1] <- 0 ; if n > 9 then s.[2] <- 0 ; let f = int_of_string s in Printf.printf "%s = %d + %d" t n f; if f < 10 then print_string("/10\n")

56 ummer School Computer Sc & Engg: IIT Kharagpur 56 else print_string("/100\n");;

57 ummer School Computer Sc & Engg: IIT Kharagpur 57 OCAML Program XIVa (* ass14a.ml: Area of a Triangle *) let triarea() = print_string("enter three sides of a triangle: "); let a = read_float() and b = read_float() and c = read_float() in let s = (a+.b+.c)/.2.0 in let area=sqrt (s*.(s-.a)*.(s-.b)*.(s-.c)) in Printf.printf "Sides: %6.2f, %6.2f, %6.2f Area: %6.2f\n" a b c area;;

DS: CS Computer Sc & Engg: IIT Kharagpur 1. roblem Set III. Goutam Biswas

DS: CS Computer Sc & Engg: IIT Kharagpur 1. roblem Set III. Goutam Biswas DS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 P DS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Problem 1 Write a C function to find n!, where n is the parameter. Try with different types of loops.

More information

Basic Assignment and Arithmetic Operators

Basic Assignment and Arithmetic Operators C Programming 1 Basic Assignment and Arithmetic Operators C Programming 2 Assignment Operator = int count ; count = 10 ; The first line declares the variable count. In the second line an assignment operator

More information

IEEE 754 Floating-Point Format

IEEE 754 Floating-Point Format PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 IEEE 754 Floating-Point Format PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Floating-Point Decimal Number 123456. 10 1 = 12345.6 10 0 = 1234.56 10

More information

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 for Statement Another iterative construct in C language is the for-statement (loop). The structure or the syntax of this statement is, for (exp 1 ; exp

More information

Programming and Data Structure Laboratory (CS13002)

Programming and Data Structure Laboratory (CS13002) Programming and Data Structure Laboratory (CS13002) Dr. Sudeshna Sarkar Dr. Indranil Sengupta Dept. of Computer Science & Engg., IIT Kharagpur 1 Some Rules to be Followed Attendance is mandatory. Regular

More information

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation This course has three parts Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation 2 There are three steps involved in converting your idea of what is to be

More information

Programming and Data Structure

Programming and Data Structure 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

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Unit 1: Introduction to C Language Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories

More information

Programming and Data Structures

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

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 1 Tutorial Programming & Data Structure: CS 11001 Section - 1/A DO NOT POWER ON THE MACHINE Department of Computer Science and Engineering

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out REGZ9280: Global Education Short Course - Engineering 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. REGZ9280 14s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write

More information

Question Bank (SPA SEM II)

Question Bank (SPA SEM II) Question Bank (SPA SEM II) 1. Storage classes in C (Refer notes Page No 52) 2. Difference between function declaration and function definition (This question is solved in the note book). But solution is

More information

KS3 - Year 8 & 9 Course Outline: Mathematics

KS3 - Year 8 & 9 Course Outline: Mathematics KS3 - Year 8 & 9 Course Outline: Mathematics EFG D C B visualise and use 2D representations of 3D objects 39 81-82 G1 N1 N2 enlarge 2D shapes, given a centre of enlargement and a positive whole number

More information

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 TOPICS TODAY Assembling & Linking Assembly Language Separate Compilation in C Scope and Lifetime LINKING IN ASSEMBLY

More information

Mathematics Shape and Space: Polygon Angles

Mathematics Shape and Space: Polygon Angles a place of mind F A C U L T Y O F E D U C A T I O N Department of Curriculum and Pedagogy Mathematics Shape and Space: Polygon Angles Science and Mathematics Education Research Group Supported by UBC Teaching

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

This exam is to be taken by yourself with closed books, closed notes, no calculators.

This exam is to be taken by yourself with closed books, closed notes, no calculators. Student ID CSE 5A Name Final Signature Fall 2004 Page 1 (12) cs5a This exam is to be taken by yourself with closed books, closed notes, no calculators. Page 2 (33) Page 3 (32) Page 4 (27) Page 5 (40) Page

More information

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub

More information

Creating, Compiling and Executing

Creating, Compiling and Executing Shell Commands & Vi Compiling C programs Creating, Compiling and Executing Creating program #i n c l u d e i n t main ( ) { p r i n t f ( AA\n ) ; p r i n t f ( A A\n ) ; p r i n t f ( A

More information

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

More information

User Defined Data: Product Constructor

User Defined Data: Product Constructor PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 User Defined Data: Product Constructor PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Built-in Data Types Built-in data types of C language are int,

More information

Introduction to Algorithms and Programming I Lab. Exercises #1 Solution

Introduction to Algorithms and Programming I Lab. Exercises #1 Solution 60-140 Introduction to Algorithms and Programming I Lab. Exercises #1 Solution Objectives are to: 1. Learn basic Unix commands for preparing a source C program file, compiling a C program and executing

More information

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!)

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! Gives

More information

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number: UNIVERSITY OF WINDSOR 60-106-01 Winter 2007 QUIZ # 1 Solution Examiner:Ritu Chaturvedi Dated : Feb 7 th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed.

More information

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

Programming with Indexed Variables

Programming with Indexed Variables PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 Programming with Indexed Variables PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 Variable with One Index: 1-Dimensional Array... what(...) { int a[10]

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

Programming & Data Structure

Programming & Data Structure Functions Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013 Functions Callee : Caller = Bowler : Captain Functions int f(){...

More information

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

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators Expressions 1 Expressions n Variables and constants linked with operators Arithmetic expressions n Uses arithmetic operators n Can evaluate to any value Logical expressions n Uses relational and logical

More information

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Date:.FN / AN Time: 2 hrs Full marks: 60 No. of students: 643 Spring Mid Semester Exams, 2011 Dept: Comp. Sc & Engg. Sub No: CS11001 B.Tech 1 st Year (Core) Sub

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives

More information

IEEE 754 Floating-Point Format

IEEE 754 Floating-Point Format C Programming 1 IEEE 754 Floating-Point Format C Programming 2 Floating-Point Decimal Number 123456. 10 1 = 12345.6 10 0 = 1234.56 10 1 = 123.456 10 2 = 12.3456 10 3 = 1.23456 10 4 (normalised) 0.12345

More information

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Flow Chart. The diagrammatic representation shows a solution to a given problem. low Charts low Chart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as various symbols, and their order by connecting them with arrows. he diagrammatic representation

More information

ANSI C Programming Simple Programs

ANSI C Programming Simple Programs ANSI C Programming Simple Programs /* This program computes the distance between two points */ #include #include #include main() { /* Declare and initialize variables */ double

More information

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept Arrays in C Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur 1 Basic Concept Many applications require multiple data items that have common characteristics.

More information

Exercises C-Programming

Exercises C-Programming Exercises C-Programming Claude Fuhrer (claude.fuhrer@bfh.ch) 0 November 016 Contents 1 Serie 1 1 Min function.................................. Triangle surface 1............................... 3 Triangle

More information

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

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries

More information

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Announcements Exam 1 (20%): Feb. 27 (Tuesday) Tentative Proposal Deadline:

More information

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

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University C: A High-Level Language

More information

1001ICT Introduction To Programming Lecture Notes

1001ICT Introduction To Programming Lecture Notes 1001ICT Introduction To Programming Lecture Notes School of Information and Communication Technology Griffith University Semester 1, 2015 1 M Environment console M.1 Purpose This environment supports programming

More information

SWALLOW SCHOOL DISTRICT CURRICULUM GUIDE. Stage 1: Desired Results

SWALLOW SCHOOL DISTRICT CURRICULUM GUIDE. Stage 1: Desired Results SWALLOW SCHOOL DISTRICT CURRICULUM GUIDE Curriculum Area: Math Course Length: Full Year Grade: 6th Date Last Approved: June 2015 Stage 1: Desired Results Course Description and Purpose: In Grade 6, instructional

More information

Arithmetic Expressions in C

Arithmetic Expressions in C Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic

More information

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be. CS 156 : COMPUTER SYSTEM CONCEPTS TEST 1 (C PROGRAMMING PART) FEBRUARY 6, 2001 Student s Name: MAXIMUM MARK: 100 Time allowed: 45 minutes Note: unless otherwise stated, the questions are with reference

More information

There are three steps involved in converting your idea of what is to be done to a working program

There are three steps involved in converting your idea of what is to be done to a working program PROGRAMMING Before we start on a course in Numerical methods and programming we should look at some of the basic aspects of programming. We will learn here, how to convert some of our ideas into a working

More information

Year 7 Term 1 Intermediate

Year 7 Term 1 Intermediate Year 7 Term 1 Intermediate Overview: Term 1 Learning Objectives Topic: Integers, Powers and Roots Big Questions: - How do you know when you have found all the factors of a number? - What would the HCF

More information

Mathematics. Foundation, term by term schedule Year 7 E1 E2 E3 Year 8 E4 I1 I2 Year 9 I3 I4 A1

Mathematics. Foundation, term by term schedule Year 7 E1 E2 E3 Year 8 E4 I1 I2 Year 9 I3 I4 A1 Mathematics KS3 at Brighouse High School Year 7 and 8 Foundation Curriculum Overview Foundation, term by term schedule Year 7 E1 E2 E3 Year 8 E4 I1 I2 Year 9 I3 I4 A1 E1.1 Multiply and divide by 10, 100

More information

Programming Language A

Programming Language A Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28 From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs,

More information

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

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

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1 BIL 104E Introduction to Scientific and Engineering Computing Lecture 1 Introduction As engineers and scientists why do we need computers? We use computers to solve a variety of problems ranging from evaluation

More information

Introduction to C. Systems Programming Concepts

Introduction to C. Systems Programming Concepts Introduction to C Systems Programming Concepts Introduction to C A simple C Program Variable Declarations printf ( ) Compiling and Running a C Program Sizeof Program #include What is True in C? if example

More information

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal MA 511: Computer Programming Lecture 3: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Last

More information

Name Roll No. Section

Name Roll No. Section Indian Institute of Technology, Kharagpur Computer Science and Engineering Department Class Test I, Autumn 2012-13 Programming & Data Structure (CS 11002) Full marks: 30 Feb 7, 2013 Time: 60 mins. Name

More information

LAB 6 FUNCTIONS I School of Computer and Communication Engineering

LAB 6 FUNCTIONS I School of Computer and Communication Engineering LAB 6 FUNCTIONS I School of Computer and Communication Engineering 1 Universiti Malaysia Perlis 1. OBJECTIVES: 1.1 To apply functions as building blocks of programs. 1.2 To write C programs using functions.

More information

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

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

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that

More information

Introduction Presentation A

Introduction Presentation A CSE 2421/5042: Systems I Low-Level Programming and Computer Organization Introduction Presentation A Read carefully: Bryant Chapter 1 Study: Reek Chapter 2 Skim: Reek Chapter 1 08/22/2018 Gojko Babić Some

More information

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 Name :. Roll No. :..... Invigilator s Signature :.. 2011 INTRODUCTION TO PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give

More information

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

CS16 Exam #1 7/17/ Minutes 100 Points total CS16 Exam #1 7/17/2012 75 Minutes 100 Points total Name: 1. (10 pts) Write the definition of a C function that takes two integers `a` and `b` as input parameters. The function returns an integer holding

More information

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes:

Precedence and Associativity Table. % specifiers in ANSI C: String Control Codes: CMPE108, Homework-2 (C Fundamentals, Expressions, and Selection Structure.) Student Nr:... Name,Surname:...:... CMPE108 Group:... Signature... Please print this homework, and solve all questions on the

More information

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5) Banner number: Name: Midterm Exam CSCI 2132: Software Development March 4, 2019 Marks Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5) Question 6 (5) Total (50) Instructions:

More information

3/13/2012. ESc101: Introduction to Computers and Programming Languages

3/13/2012. ESc101: Introduction to Computers and Programming Languages ESc101: Introduction to Computers and Programming Languages Instructor: Krithika Venkataramani Semester 2, 2011-2012 The content of these slides is taken from previous course lectures of Prof. R. K. Ghosh,

More information

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga C-Programming CSC209: Software Tools and Systems Programming Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Adapted from Dan Zingaro s 2015 slides. Week 2.0 1 / 19 What

More information

YEAR 10- Mathematics Term 1 plan

YEAR 10- Mathematics Term 1 plan Week YEAR 10- Mathematics Term 1 plan 2016-2017 Course Objectives 1 The number system To understand and use 4 rules and order of operation. To understand and use Recurring decimals. Add subtract multiply

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis. Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Lab-4 ---------------------------------------------------------------------------------------------------------------------------------

More information

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; } Modifiers unsigned. For example unsigned int would have a range of [0..2 32 1] on a 32-bit int machine. const Constant or read-only. Same as final in Java. static Similar to static in Java but not the

More information

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

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

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

Fundamentals of Programming Session 8

Fundamentals of Programming Session 8 Fundamentals of Programming Session 8 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

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

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar Examples of Software Programming and Data Structure Lecture 2 Sudeshna Sarkar Read an integer and determine if it is a prime number. A Palindrome recognizer Read in airline route information as a matrix

More information

How to Do Word Problems. Study of Integers

How to Do Word Problems. Study of Integers Study of Integers In this chapter, we are are going to closely look at the number line system and study integers. -3-2 -1 0 1 2 3 4 5 6 An integer is simply a number like 0, 1, 2, 3, and 4, but unlike

More information

There are three steps involved in converting your idea of what is to be done to a working program

There are three steps involved in converting your idea of what is to be done to a working program PROGRAMMING Before we start on a course in Numerical methods and programming we should look at some of the basic aspects of programming. We will learn here, how to convert some of our ideas into a working

More information

C introduction: part 1

C introduction: part 1 What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation

More information

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA COP 3275: Chapter 02 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA Program: Printing a Pun #include int main(void) { printf("to C, or not to C: that is the question.\n");

More information

The C language. Introductory course #1

The C language. Introductory course #1 The C language Introductory course #1 History of C Born at AT&T Bell Laboratory of USA in 1972. Written by Dennis Ritchie C language was created for designing the UNIX operating system Quickly adopted

More information

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313. NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" C Programming Functions Program Organization Separate Compilation Functions vs. Methods

More information

Structured Program Development in C

Structured Program Development in C 1 3 Structured Program Development in C 3.2 Algorithms 2 Computing problems All can be solved by executing a series of actions in a specific order Algorithm: procedure in terms of Actions to be executed

More information

Unit 2: Accentuate the Negative Name:

Unit 2: Accentuate the Negative Name: Unit 2: Accentuate the Negative Name: 1.1 Using Positive & Negative Numbers Number Sentence A mathematical statement that gives the relationship between two expressions that are composed of numbers and

More information

Year 6 Mathematics Overview

Year 6 Mathematics Overview Year 6 Mathematics Overview Term Strand National Curriculum 2014 Objectives Focus Sequence Autumn 1 Number and Place Value read, write, order and compare numbers up to 10 000 000 and determine the value

More information

Lecture 5: C programming

Lecture 5: C programming CSCI-GA.1144-001 PAC II Lecture 5: C programming Mohamed Zahran (aka Z) mzahran@cs.nyu.edu http://www.mzahran.com Brian Kernighan Dennis Ritchie In 1972 Dennis Ritchie at Bell Labs writes C and in 1978

More information

Expressions. Eric McCreath

Expressions. Eric McCreath Expressions Eric McCreath 2 Expressions on integers There is the standard set of interger operators in c. We have: y = 4 + 7; // add y = 7-3; // subtract y = 3 * x; // multiply y = x / 3; // integer divide

More information

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

Technical Questions. Q 1) What are the key features in C programming language? Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives

More information

Compiling and Running a C Program in Unix

Compiling and Running a C Program in Unix CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name

More information

Long Term Maths Plan 2013/2014 Year 6

Long Term Maths Plan 2013/2014 Year 6 Long Term Maths Plan 2013/2014 Year 6 Knowledge, skills and understanding 1. During the key stage, pupils should be taught the Knowledge, skills and understanding through: a. activities that extend their

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names for containers of values don t need to know which register or memory location Provides abstraction of underlying

More information

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

(Following Paper ID and Roll No. to be filled by the student in the Answer Book)

(Following Paper ID and Roll No. to be filled by the student in the Answer Book) F:/Academic/27 Refer/WI/ACAD/10 SHRI RAMSWAROOP MEMORIAL COLLEGE OF ENGG. & MANAGEMENT PAPER ID: 1602 (Following Paper ID and Roll No. to be filled by the student in the Answer Book) Roll No. B.Tech. SEM

More information

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations

More information

Structured programming

Structured programming Exercises 2 Version 1.0, 22 September, 2016 Table of Contents 1. Simple C program structure................................................... 1 2. C Functions..................................................................

More information