ESC101: Introduction to Computing

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Methods
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
User-Written Functions
REPETITION CONTROL STRUCTURE
CS1022 Computer Programming & Principles
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
Intro to C Tutorial 4: Arithmetic and Logical expressions
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Algorithm Analysis CSE 2011 Winter September 2018.
User input We’ve seen how to use the standard output buffer
Repetition-Counter control Loop
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Lecture 4B More Repetition Richard Gesick
Control Structures Lecture 7.
Lesson #6 Modular Programming and Functions.
Looping.
Arrays, For loop While loop Do while loop
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
CS1100 Computational Engineering
- Additional C Statements
Structured Program
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arithmetic Operators in C
Chapter 4 - Program Control
Loops in C.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Computing Fundamentals
UMBC CMSC 104 – Section 01, Fall 2016
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Computer programming Lecture 3.
Lesson #6 Modular Programming and Functions.
EPSII 59:006 Spring 2004.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Arrays, Part 1 of 2 Topics Definition of a Data Structure
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

ESC101: Introduction to Computing Recap & More Esc101, Programming

Problem 1 The first line of the input consists of two positive integers m and n. This line is followed by m lines, each containing n integers, signifying an m X n matrix A. Calculate ∑i(∑jAij)2 , 1≤i<m , 1≤j<n . 3 4 4 7 11 2 1 1 2 4 2 9 0 -1 row i columns j Desired output (4+7+11+2)2 + (1+1+2+4)2 + (2+9+0+ (-1))2 e.g. A20 = 2, A12 = 2 Esc101, Programming

Double loops Need something of a double loop here (loop inside a loop). One loop to do the row sum of each row. Once a row is finished, we square the row sum. Another (outer) loop to add the squares of row sum over all rows that have been fully read. Esc101, Programming

Inner loop: Row sum Easy part first: assume we are at the beginning of a row (have not read any numbers yet) and write a loop to calculate the row sum. int a; /* the current integer */ int colindex; /* index of current column */ int rowsum; /* sum of row entries read so far */ int rowsumsq; /* square of the sum of row entries */ rowsum = 0; colindex = 0; while (colindex < n) { /* not finished reading n cols*/ scanf(“%d”, &a); /* read next number */ rowsum = rowsum + a; /* add to rowsum */ colindex = colindex + 1; /* increment colindex */ } rowsumsq = rowsum * rowsum; /*square rowsum */ Esc101, Programming

Outer Loop Structure We have a code that reads the next n integers from the terminal and sums them. Modify it so that it reads the next m integers from the output of the previous code, specifically the value of rowsumsq and sums them. Esc101, Programming

Task: Modify code below so that it reads the next m integers from the output of the previous code, specifically the value of rowsumsq and sums them. int a; /* the current integer */ int colindex; /* index of current column */ int rowsum; /* sum of row entries read so far */ int rowsumsq; /* square of the sum of row entries */ rowsum = 0; colindex = 0; while (colindex < n) { /* not finished reading n cols*/ scanf(“%d”, &a); /* read next number */ rowsum = rowsum + a; /* add to rowsum */ colindex = colindex + 1; /* increment colindex */ } rowsumsq = rowsum * rowsum; /*square rowsum */ Esc101, Programming

Previous code modified to read the next m integers from the output of the previous code, specifically the value of rowsumsq and sums them. Outer Loop: Still in Design Phase: incomplete and informal int rowindex; /* index of current row being read */ int sqsum; /* sum of col entries read so far */ sqsum = 0; rowindex = 0; while (rowindex < m) { /* not finished reading m rows*/ sqsum = sqsum + ``rowsumsq’’; /* add to sqsum */ rowindex = rowindex + 1; /* increment rowindex */ } printf(“%d “,sqsum); rowsumsq comes from previous code. Let’s insert that code here. Esc101, Programming

Inner, Outer Loops Implemented int rowindex=0; /* index of current row being read */ int sqsum=0; /* sum of col entries read so far */ while (rowindex < m) { /* not finished reading m rows*/ int rowsum=0; /* sum of row entries read so far */ int a; /* the current integer */ int colindex=0; /* index of current column */ int rowsumsq; /* square of the sum of row entries */ while (colindex < n) { /* not finished reading n cols*/ scanf(“%d”, &a); /* read next number */ rowsum = rowsum + a; /* add to rowsum */ colindex++; /* increment colindex */ } rowsumsq = rowsum * rowsum; /*square rowsum */ sqsum = sqsum + rowsumsq; /* add to sqsum */ rowindex++; /* increment rowindex */ printf(“%d “,sqsum); Esc101, Programming

Problem 2 Read n, assume n >=2. Read n integers, and print triplets of consecutively positive input integers that are Pythogorean, skipping negative ints. For input Output should be Need a single loop, but several counters. 8 1 -1 3 -3 4 -4 -5 5 3 4 5 Esc101, Programming

int curr, prev, pprev;/. current, prev, pprev positive nos. / int n; / int curr, prev, pprev;/* current, prev, pprev positive nos.*/ int n; /* number of integers */ int i; /* for loop counter */ int count = 0; /* no. of positive ints seen yet */ scanf(“%d”, &n); for (i=0; i < n ; i++) { scanf(“%d”, &curr); if (curr <= 0) continue; /* skip non-positive nos. */ if (count == 0) { pprev = curr; count =1; } else { if (count == 1) { prev = curr; count =2; } else{ /* count is 2 and will remain 2 */ if (pprev*pprev + prev*prev == curr*curr){ /* Pythagorean triple found */ printf(“%d %d %d\n”, pprev, prev, curr);} pprev = prev; prev = curr; } } // end for loop Esc101, Programming

8 -1 1 -3 3 4 -4 -5 5 -3 3 4 -4 -5 5 2 1 4 3 3 4 5 n count i curr prev int curr, prev, pprev,n, i, count = 0; scanf(“%d”, &n); for (i=0; i < n ; i = i+1) { scanf(“%d”, &curr); if (curr <= 0) { continue; } if (count == 0) { pprev = curr; count =1; } else { if (count == 1) { prev = curr; count =2; else { /* count is 2 and will remain 2 */ if (pprev*pprev + prev*prev == curr*curr){ printf(“%d %d %d\n”, pprev, prev, curr);} pprev = prev; prev = curr; } 8 -1 1 -3 3 4 -4 -5 5 Output 3 4 5 n count i curr prev pprev -3 3 4 -4 -5 5 -1 1 2 1 8 4 3 8 7 6 5 4 3 2 1 3 1 Esc101, Programming

A general principle of program development Break up your task into smaller sub-tasks, and those sub-tasks into still smaller sub-tasks and so on until each sub-task is easily solvable in a function/block. Write a function for each of the sub-tasks. Design your program from the top-down, big task to the small tasks. Debug/test your program bottom-up. Debug functions that perform elementary tasks, and then move on to testing more complex functions. (Commonly, printf is used.) Esc101, Programming

Example 3 What is printed by the program? Evaluation of f(f(a,b),b) First evaluate inner f(a,b) for a = 1, b=2. This is 3. So f(f(a,b),b) becomes f(3,2). This is 5. So output is What is printed by the program? int f (int a, int b) { return a+b; } 5 2 Pure expressions do not change the state of the program, e.g., a- b *c/d f(f(a,b), f(f(a,b),a)) main () { int a = 1, b = 2; a = f(f(a,b),b); printf(“%d %d”, a,b); } Expressions with side-effects change the state of the program for example, a = a +1 f(a=b+1,b=a+1) Execution proceeds similar to evaluating mathematical function expression. Care needed to handle expr with side effects.

Let us evaluate function arguments Example 4 Rule: All arguments are evaluated before function call is made. What is printed by the program? BUT ! C doesn’t specify order, in which arguments are evaluated. This is left to the compiler. int f (int a, int b) { return b-a; } main () { int a = 2, b = 1; a = f( a=b+1, b=a+1); printf(“%d %d”, a,b); Let us evaluate function arguments in left to right order. a b 2 1 Expected Output 2 main() 1 3 3 Evaluate f(a=b+1,b=a+1). How should we evaluate it?

Left-right OR right-left We used left to right evaluation. Expected output: Let us compile and run on a CC machine, output is: What happened? The compiler evaluated right to left. Output is int f (int a, int b) { return b-a; } 1 3 main () { int a = 2, b = 1; a = f( a=b+1, b=a+1); printf(“%d %d”, a,b); } -1 3 a b 2 1 4 -1 3 -1 3

What was the mistake? Actually, C does not specify the order in which the arguments of a function should be evaluated. It leaves it to the compiler. Compilers may evaluate arguments in different orders. Both answers are consistent with C language!! What should we do? Write your arguments to functions so that the result is not dependent on the order in which they are evaluated. Better still, write them so that the operand expressions are side-effect free.

For example int f (int a, int b) { return b-a; } main () { int a = 2, b = 1; a=b+1; b=a+1; a = f( a, b ); /* operands do not have side effects */ printf(“%d %d”, a,b); }

Comma– as a separator C allows multiple variables of the same type to be defined as one statement, separated by commas. Defines three integer variables named a,b and c. a b c Examples (independent definitions) int a, b, c; int a = 2, b = 5, c=15; float x = 3.59, y = 4.5; int x = 5, float y = 10.0; Defines three integer variables named a,b and c. Initializes a to 2, b to 5 and c to 15. 2 5 15 a b c Compilation error! 3.59 10.0 x y Defines two float variables named x and y. Initializes x to 3.59 and y to 10.0.

, Comma– as an operator i+2, sum=sum-1; scanf(“%d”,&m), sum=0, i=0; Comma as an operator is a binary operator that takes two expressions as operands. Think of just like + or – or * or / or = or == etc.. Some examples, i+2, sum=sum-1; scanf(“%d”,&m), sum=0, i=0; Execution of expr1 , expr2 proceeds as follows. Evaluate expr1, discard its result and then evaluate expr2 and return its value (and type). expr1 , expr2 ,