Welcome! COMP s1. Programming Fundamentals

Similar documents
Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

COMP s1 Lecture 1

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

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

Control Structure: Loop

Functions. Arash Rafiey. September 26, 2017

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

COMP 208 Computers in Engineering

Repetition Structures II

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

C Exercises. Ben Vandiver February 28, 2003

Goals of this Lecture

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

EECE.2160: ECE Application Programming Spring 2018

C 101. Davide Vernizzi. April 29, 2011

CSCI 2132 Software Development. Lecture 18: Functions

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

LECTURE 5 Control Structures Part 2

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Software Design and Analysis for Engineers

For instance, we can declare an array of five ints like this: int numbers[5];

Control Structure: Selection

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

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Control Flow, Functions and Basic Linkage

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

C: How to Program. Week /Mar/05

Formatted Input/Output

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

EECE.2160: ECE Application Programming Fall 2017

AMCAT Automata Coding Sample Questions And Answers

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Chapter 2 - Introduction to C Programming

Using Excel This is only a brief overview that highlights some of the useful points in a spreadsheet program.

CS113: Lecture 4. Topics: Functions. Function Activation Records

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Fundamentals of Programming Session 14

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Introduction: The Unix shell and C programming

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Welcome to... CS113: Introduction to C

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Crude Video Game Simulator Algorithm

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

ECE495k Final Sample Soln.

Fundamentals of Programming. Lecture 3: Introduction to C Programming

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

Introduction to Computer Science Unit 3. Programs

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CPE 101. Overview. Programming vs. Cooking. Key Definitions/Concepts B-1

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Chapter 1 & 2 Introduction to C Language

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

CpSc 1111 Lab 4 Formatting and Flow Control

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

CMSC201 Computer Science I for Majors

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Fundamentals of Programming Session 4

printf( Please enter another number: ); scanf( %d, &num2);

2. Numbers In, Numbers Out

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Intermediate Programming, Spring 2017*

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

16.216: ECE Application Programming Fall 2011

16.216: ECE Application Programming Fall 2011

CC112 Structured Programming

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Computer Programming: Skills & Concepts (CP) Functions and Pointers

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

CMSC201 Computer Science I for Majors

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CS1100 Introduction to Programming

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

Transcription:

Welcome! 0 COMP1511 18s1 Programming Fundamentals

COMP1511 18s1 Lecture 4 1 More Functions + Loops Andrew Bennett <andrew.bennett@unsw.edu.au> even more functions while loops

2 Before we begin introduce yourself to the person sitting next to you why did they decide to study computing?

Overview 3 after this lecture, you should be able to handle invalid input to your program understand why we use functions write simple functions understand the basics of while loops (note: you shouldn t be able to do all of these immediately after watching this lecture. however, this lecture should (hopefully!) give you the foundations you need to develop these skills. remember: programming is like learning any other language or skill, it takes consistent and regular practice.)

Admin 4 Don t panic! these slides are on WebCMS3 ( DRAFT ) lecture recordings are on WebCMS3 make sure you have home computing set up make sure you can send and receive uni emails

A challenge for you 5 Guess the Number computer is thinking of a number enter a guess program responds higher or lower or correct! hint to start out with: have a fixed secret number (i.e. int secret = 5 ) scanf their guess rerun the program to guess another number

remember functions? 6

Functions 7 building blocks in our programs self-contained, reusable pieces of code abstraction

Anatomy of a Function 8 return type ( void if no return value) function name parameters (inside parens, comma separated; void if no parameters) statements return statement int addnumbers (int num1, int num2) { int sum = num1 + num2; return sum; }

Functions as Building Blocks 9 for example: a function that takes a number and multiplies it by 2 we can take our number, and put it into the function, and get it out doubled int x = 5; x = doubled (x); key things: input (parameters) output (return value) functions won t change values

Why Functions? 10 Revisiting license.c

Why Functions? 11 main function: want to know what it s doing don t need to know how it s doing it

Side Note: When scanf Goes Wrong 12 what do we do if somebody enters invalid input? (e.g. enters a word, not a number) int a; int b; // What happens if they didn't type in two numbers? int num = scanf("%d %d", &a, &b);

Side Note: When scanf Goes Wrong 13 scanf returns the number of things successfully scanned in e.g. int a; int b; // num will be 2 if both a and b were scanned successfully int num = scanf("%d %d", &a, &b);

Side Note: When scanf Goes Wrong 14 we can wrap this in an if statement: int a; int b; // num will be 2 if both a and b were scanned successfully if (scanf("%d %d", &a, &b)!= 2) { printf("invalid input!\n"); }

Features of Functions 15 a function can have zero or more parameter(s) a function can only return zero or one value(s) * * * a function stores a local copy of parameters passed to it the original values of variables remain unaltered

before we get started: extending the challenge 16

Extending the challenge 17 Guess the Number (v2) computer is thinking of a number enter a guess program responds higher or lower or correct! then asks again and again until you guess correctly hint use a loop to run the code multiple times (coming up next!)

and now for something new 18

Remember if statements? 19 int main (void) { printf ("Enter a number: "); int num; scanf ("%d", &num); if (num < 10) { } printf ("Hello!\n"); } return 0; if the condition is true, then do something, else do something else.

What if we wanted to do something more than once? 20 int main (void) { printf ("Enter a number: "); int num; scanf ("%d", &num); while (num < 10) { } printf ("Hello!\n"); } return 0;

What if we wanted to do something more than once? 21 int main (void) { printf ("Enter a number: "); int num; scanf ("%d", &num); while (num < 10) { printf ("Hello!\n"); num++; } } return 0;

Anatomy of a Loop 22 initialisation condition statements update int i = 0; while (i < 10) { printf ("Hello (number %d)\n", i); i = i + 1; }

Another challenge 24 Guess the Number (v3) human is thinking of a number computer guesses human responds higher or lower or correct! computer guesses again and again until it has guessed correctly