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

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

More Recursive Programming

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

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

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Programming with Indexed Variables

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

Dept. of CSE, IIT KGP

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

IEEE 754 Floating-Point Format

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Internal Sort by Comparison II

Internal Sort by Comparison II

Operators And Expressions

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

User Defined Data: Product Constructor

Government Polytechnic Muzaffarpur.

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

AMCAT Automata Coding Sample Questions And Answers

Programming Language B

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Internal Sort by Comparison II

Comments. Comments: /* This is a comment */

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Programming for Electrical and Computer Engineers. Loops

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Lecture 3. More About C

Crude Video Game Simulator Algorithm

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Name Roll No. Section

Arrays and Applications

Expression and Operator

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

Dynamic Allocation of Memory Space

Decision Making and Loops

Computer Programming: Skills & Concepts (CP) Variables and ints

Lecture 6. Statements

Basic Assignment and Arithmetic Operators

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

16.216: ECE Application Programming Fall 2011

Programming and Data Structures

Loops / Repetition Statements

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

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

Stream Model of I/O. Basic I/O in C

DECISION CONTROL AND LOOPING STATEMENTS

Structure of a C Program

Formatted Input/Output

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

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Solutions to Assessment

DS: CS Computer Sc & Engg: IIT Kharagpur 1. File Access. Goutam Biswas. ect 29

Control structures in C. Going beyond sequential

Introduction to C. Systems Programming Concepts

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Programming Language A

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Chapter 3: Arrays and More C Functionality

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

Loops / Repetition Statements

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.

16.216: ECE Application Programming Fall 2013

Boolean Data Lesson #2 Outline

More examples for Control statements

Stack & Queue on Self-Referencing Structures

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

C: How to Program. Week /Mar/05

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

PDS Lab Section 16 Autumn Tutorial 1. Unix Commands pwd The pwd command displays the full pathname of the current directory.

Fundamentals of Programming Session 8

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

MODULE 2: Branching and Looping

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

6 NFA and Regular Expressions

Flow Control. CSC215 Lecture

& Control Flow of Program

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Introduction to C Programming

Control Structure: Loop

EECE.2160: ECE Application Programming Fall 2017

C Arrays Pearson Education, Inc. All rights reserved.

'C' Programming Language

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CS1100 Introduction to Programming

Loops. CSCI 112: Programming in C

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Programming for Engineers Iteration

Tutorial & Laboratory. Programming & Data Structure: CS11001/ Section - 4/D DO NOT POWER ON THE MACHINE

Slicing. Rupesh Nasre. CS6843 Program Analysis IIT Madras Jan 2016

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Programming and Data Structure Laboratory (CS13002)

Transcription:

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 2 ; exp 3 ) statement

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 for Loop exp1 true exp2 false statement exp3

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 for Statement The expression 1 is used for initialization before entering the loop. The expression 2 is to control the loop, whether to enter the loop or to skip it. The expression 3 executed after the execution of the statement part of the loop, is used essentially to update the loop control condition.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 for Statement All three expressions can be omitted. The omitted control expression, expression 2, makes the condition true. The statement can also be null ;.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 Example III We write program to solve the problem of example III, the sum of first n natural numbers.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 #include <stdio.h> int main() // temp29.c { int n, i, sum = 0; printf("enter a +ve integer: "); scanf("%d", &n); for(i=1, sum=0; i<=n; ++i) sum += i; printf("0+... + %d = %d\n", n, sum); return 0; }

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 for Statement for (i=1, sum=0; i<=n; ++i) sum + = i;

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 expression 1 expression 1 is i=1, sum=0. We have used the comma operator (,). This is evaluated left to right and is of lowest precedence among the operators. The value of the expression is the value of the rightmost operand. It is sum = 0 in this case.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 while and for Loops A while-statement can be simulated by a for-statement. while(exp) stmt for(; exp;) stmt

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 while andfor Loops Similarly a for-statement can be simulated by a while-statement and expression statements. for(exp1; exp2; exp3) stmt exp1; while(exp2) {stmt exp3;} This equivalence is not true if there is a continue statement in stmt.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11 Example IV Read n int data and print the largest among them. The first input is the number of data, n. Following inputs are a sequence of n int data. It is essential to print the input.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12 Inductive Definition lrgst(d 1,d 2,,d n ) = d 1 if n = 1, max(d 1,lrgst(d 2,,d n )) if n > 1.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13 Sequence of Operations Read the number of data n( 1). Read the first data in a variable named largest a. In a for-loop read the i th data (i = 2 n). If it is larger than the data present in largest, copy it to largest. a Upto this point of execution, this is the largest data.

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14 #include <stdio.h> int main() // temp30.c { int n, largest, i; printf("enter a +ve integer: "); scanf("%d", &n); printf("enter %d integers:\n", n); scanf("%d", &largest); printf("input data: %d ", largest) ; for(i=2; i<=n; ++i) { int temp ; // local to block scanf("%d", &temp);

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15 } printf("%d ", temp) ; if (temp > largest) largest = temp ; } printf("\nlargest: %d\n", largest); return 0;

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16 Note It is not necessary to know the number of data a priori. We shall use EOF (end-of-file) from the keyboard (Ctrl+D) to terminate the input. Every call to scanf() returns the number of data read. If the Ctrl+D is pressed, scanf() returns a special constant EOF (end-of-file, defined in stdio.h).

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17 #include <stdio.h> int main() // temp31.c { int largest, count = 0, temp; printf("enter integer data\n"); printf("terminate by Ctrl+D\n"); scanf("%d", &largest); // at least one data printf("input data: %d ", largest) ; ++count ; for(; scanf("%d", &temp)!= EOF;) { printf("%d ", temp); if (temp > largest) largest = temp ; ++count;

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18 } } printf("\nlargest among %d data: %d\n", count, largest); return 0;