C 101. Davide Vernizzi. April 29, 2011

Similar documents
Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

n Group of statements that are executed repeatedly while some condition remains true

Loops / Repetition Statements

Loops / Repetition Statements

Control Structure: Loop

Chapter 5: Control Structures

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

DECISION CONTROL AND LOOPING STATEMENTS

Repetition and Loop Statements Chapter 5

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

ECE264 Fall 2013 Exam 1, September 24, 2013

Decision Making and Loops

A Look Back at Arithmetic Operators: the Increment and Decrement

Functions. Arash Rafiey. September 26, 2017

Dept. of CSE, IIT KGP

Structured Programming. Dr. Mohamed Khedr Lecture 9

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

Lecture 6. Statements

Lecture 10. Daily Puzzle

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Chapter 4. Flow of Control

Welcome! COMP s1. Programming Fundamentals

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

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

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

COMP 208 Computers in Engineering

CS1100 Introduction to Programming

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Prepared by: Shraddha Modi

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

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

DECISION MAKING STATEMENTS

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

Decision Making -Branching. Class Incharge: S. Sasirekha

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Welcome! COMP s1. Programming Fundamentals

Computer Programming. Decision Making (2) Loops

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Formatted Input/Output

ECE264 Fall 2013 Exam 2, October 24, 2013

Simple if Simple if-else Nested if - else Notes for if-else statement Logical operators

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Repetition Structures II

Introduction to C Programming

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

The C language. Introductory course #1

AMCAT Automata Coding Sample Questions And Answers

YOLOP Language Reference Manual

Computer Science & Engineering 150A Problem Solving Using Computers

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

At the end of this module, the student should be able to:

Crude Video Game Simulator Algorithm

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Introduction to C. Systems Programming Concepts

ARRAYS(II Unit Part II)

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

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

Introduction to Computing Lecture 05: Selection (continued)

Chapter 2, Part III Arithmetic Operators and Decision Making

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Module 4: Decision-making and forming loops

C: How to Program. Week /Mar/05

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

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Lab 2: Structured Program Development in C

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

Looping. Arizona State University 1

CS2141 Software Development using C/C++ Debugging

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Programming for Engineers Iteration

A control structure refers to the way in which the Programmer specifies the order of executing the statements

Lecture 3. More About C

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

Conditional Statement

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Computer System and programming in C

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

Chapter 2 - Introduction to C Programming

Week 2 Introduction to Computer and Algorithm (Part 2)

Control Statements. If Statement if statement tests a particular condition

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Welcome! COMP s1. Programming Fundamentals

Structured Program Development in C

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

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

Chapter 3 Structured Program Development

Lab ACN : C++ Programming Exercises

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

Structured Program Development

Arrays and Strings. Arash Rafiey. September 12, 2017

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

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

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Transcription:

C 101 Davide Vernizzi April 9, 011 1 Input/Output Basic I/O. The basic I/O shown here can be used to get input from the user and to send output to the user. Note that usually input must be verified before it can be used. 1 printf (" Insert val "); scanf ("%d", & val ); Basic I/O with control. The following I/O expands the basic one with a simple loop for verifying that the user inserted a correct value. In the example we want a positive number. 1 do { printf (" Insert a positive number "); 3 scanf ("%d", & num ); 4 if ( num < 0) { printf (" You inserted a negative number. Try again \n"); } 8 } while ( num < 0); The do-while loop keeps asking the number until it is correclty inserted. Using a do-while ensures that it is executed at least once. Note that the condition of the loop is the opposite than what we accept, since the do-while loops when condition is true*/ Common mistakes. at beginning. 1 printf (" Insert val "); scanf ("%d", val ); These are common mistakes that are usually done 1

3 4 printf (" You inserted %d and %f\n"); printf (" You inserted %d and %f\n", f); printf (" You inserted %d and %f\n", f, val ); printf (" You inserted %d and %f\n", &val, &f); In line & is missing. In line 4 there are no parameters. In line there are not enough parameters. In line there are wrong parameters type. In line the & must not go in the printf. Loops.1 Defined loops Defined loops reiterate a block of instructions a defined number of times. Note that the number of iterations may be defined by the programmer (using a #define) or by the user (get the number in input from the user). Usually a #for statement is used for defined loops: 1 # define TIMES /*... */ 8 for (i =1; i <= TIMES ; i ++) { 9 /*... */ 10 } 11 1 /*... */ 13 14 } In line 1 we defined the number of loops and we used this definition in the for in line 8. Also a while statement can be used for defined loops: 1 # define TIMES

/*... */ 8 i =1; 9 while (i <= TIMES ) { 10 /*... */ 11 1 i ++; 13 } 14 1 /*... */ 1 1 } In line 8 we initialize the loop iterator, in line 9 we check if we finished the loop, and in line 1 we increment the loop iterator. Example 1. int num =0; int j =0; 8 Ask user a number num and write the first num numbers. 9 /* Get input from the user */ 10 printf (" Insert num "); 11 scanf ("%d", & num ); 1 13 /* for version */ 14 for (i =1; i <= num ; i ++) { 1 printf ("%d\n", i); 1 } 1 18 /* while version */ 19 j = 1; 0 while (j <= num ) { 1 printf ("%d\n", j); j ++; 3 } 3

4 return 0; } Why we did not use the #define here? Example. int num =0; int j =0; 8 Do the same of Example 1, but in a reversed order. 9 /* Get input from the user */ 10 printf (" Insert num "); 11 scanf ("%d", & num ); 1 13 /* for version */ 14 for (i= num ; i >=1; i - -) { 1 printf ("%d\n", i); 1 } 1 18 /* while version */ 19 j = num ; 0 while (j >=1) { 1 printf ("%d\n", j); j - -; 3 } 4 return 0; } Note the usage of iterators.. Undefined loops Undefined loops reiterate a block of instructions a undefined number of times. The number of iterations is not known a-priori, but depends on a condition. Usually a while or do-while statement is used for defined loops: 4

1 int main () { 3 4 /*... */ while ( cond ) { /*... */ 8 } 9 10 do { 11 } while ( cond ); 1 13 /*... */ 14 1 } In line 11 note that the do-while requires the ; (instead the while does not require it). It is possible to use a for for undefined loops, but it is not usual: 1 int main () { 3 4 /*... */ for (; cond ;) { /*... */ 8 } 9 10 /*... */ 11 1 } Example 3. Read numbers until the user inserts a 0 and compute the sum of the number inserted. 4 int num =0; int sum =0; do {

8 scanf ("%d", & num ); 9 sum += num ; 10 } while ( num!=0); 11 1 printf (" Sum = %d\n", sum ); 13 14 return 0; 1 } We used the do-while because there is at least one input from the user. Note that we initialized the sum variable. Example 4. of do-while. 4 int num =0; int sum =0; Do the same of Example 3, but using while and for instead /* while version */ 8 sum = 0; 9 10 scanf ("%d", & num ); 11 sum += num ; 1 13 while ( num!= 0) { 14 scanf ("%d", & num ); 1 sum += num ; 1 } 1 18 printf (" Sum = %d\n", sum ); 19 0 /* for version */ 1 sum = 0; 3 scanf ("%d", & num ); 4 sum += num ; for (; num!=0;) { scanf ("%d", & num ); 8 sum += num ; 9 }

30 31 printf (" Sum = %d\n", sum ); 3 33 return 0; 34 }.3 Nested loops It is possible to nest a loops inside other loops: 1 # define N 10 4 int i, j; /*... */ 8 for (i =1; i <=N; i ++) { 9 for (j =1; j <=N; j ++) { 10 /*... */ 11 } 1 } 13 14 /*... */ 1 1 } Note that in case of for (or other defined loops) different iterators must be used. It is also possible to nest different types of loops together: 1 # define N 10 4 int i, j; /*... */ 8 while ( cond ) { 9 for (j =0; j<n; j ++) { 10 /*... */ 11 } 1 }

13 14 /*... */ 1 1 } Example. Print the multiplication table until 10. # define N 10 3 4 int main () { int i, j; int val ; 8 for (i =1; i <=N; i ++) { 9 for (j =1; j <=N; j ++) { 10 val = i*j; 11 printf ("%3d ", val ); 1 } 13 printf ("\n"); 14 } 1 return 0; 1 } Note the indentation! Example. Ask user a number num and write the first num numbers. Repeat until user inserts 0. int num =0; do { 8 /* Get input from the user */ 9 printf (" Insert num "); 10 scanf ("%d", & num ); 11 1 /* for version */ 13 for (i =1; i <= num ; i ++) { 14 printf ("%d\n", i); 8

1 } 1 } while ( num!= 0); 1 18 return 0; 19 } 3 Arrays Arrays I/O. For getting arrays in input and for putting arrays in output, usually a for is used: 1 # define DIM 10 int array [ DIM ]; for (i =0; i<dim ; i ++) { 8 printf (" Insert the vector element % d ", 9 i); 10 scanf ("%d", & array [i ]); 11 } 1 13 for (i =0; i<dim ; i ++) { 14 printf (" Vector element %d: %d\n", 1 i, array [i ]); 1 } 1 18 return 0; 19 } In line 1 we defined the length of the array which we used in the for in line and line 13. Unless you have very special reasons, always define the size of the array and use the defined size and never use the number directly. Example. Ask user a number num (max 10), get num numbers in input from the user and print them in reversed order. # define DIM 10 3 4 int main () { 9

int i; int num ; int array [ DIM ]; 8 9 do { 10 printf (" Insert a number ( max 10) "); 11 scanf ("%d", & num ); 1 if ( num > 10) { 13 printf (" Max 10!\ n"); 14 } 1 } while ( num > 10); 1 1 for (i =0; i<num ; i ++) { 18 printf (" Insert the vector element % d ", 19 i); 0 scanf ("%d", & array [i ]); 1 } 3 for (i=( num -1);i >=0; i - -) { 4 printf (" Vector element %d: %d\n", i, array [i ]); } 8 return 0; 9 } Note that it is essential to verify the input in this case because we could operate outside the array. Why we start from (num-1) in the second for? 10