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

Welcome! COMP s1. Programming Fundamentals

COMP s1 Lecture 1

Welcome! COMP s1. Programming Fundamentals

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

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

Introduction. C provides two styles of flow control:

Welcome to... CS113: Introduction to C

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

Introduction to Programming. Lecture 2: Introduction to C

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

CpSc 1111 Lab 5 Formatting and Flow Control

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

Fundamentals of Programming

Informatica e Sistemi in Tempo Reale

Repetition and Loop Statements Chapter 5

Computer Systems Lecture 9

Repetition Structures II

Laboratory 5: Implementing Loops and Loop Control Strategies

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

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

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

Display Input and Output (I/O)

Chapter 13 Control Structures

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

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

Procedures, Parameters, Values and Variables. Steven R. Bagley

Arrays in Functions!

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

CMSC 104 -Lecture 9 John Y. Park, adapted by C Grasso

Chapter 13 Control Structures

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

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).

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

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

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

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

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

COMP1917: 09 Arrays and Strings

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

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

Lecture Transcript While and Do While Statements in C++

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

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

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

Text Input and Conditionals

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

Lecture 1: Overview

COMP 208 Computers in Engineering

1 Anatomy of a Program 4

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

Fundamentals of Programming Session 4

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

(Refer Slide Time: 00:26)

CS61C : Machine Structures

Topic 6: A Quick Intro To C

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

Arrays and Pointers in C. Alan L. Cox

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

C 101. Davide Vernizzi. April 29, 2011

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Signals, Instruments, and Systems W3. C Programming & Memory Management in C

Procedural Programming

C Programming & Memory

1007 Imperative Programming Part II

The Compilation Process

Loops. CSCI 112: Programming in C

Introduction to Scientific Computing and Problem Solving

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

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

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

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

CC112 Structured Programming

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

Java Program Structure and Eclipse. Overview. Eclipse Projects and Project Structure. COMP 210: Object-Oriented Programming Lecture Notes 1

UIC. C Programming Primer. Bharathidasan University

for Loop Lesson 3 Outline

Chapter 13. Control Structures

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

QUIZ on Ch.8. What is kit?

Physics 306 Computing Lab 1: Hello, World!

Formatted Input/Output

Variables and literals

Control Structure: Selection

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

Fundamentals of Programming Session 12

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Control Structure: Loop

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

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

Introduction to Computing Lecture 09: Functions (Part II)

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Transcription:

Welcome! 0 COMP1511 18s1 Programming Fundamentals

COMP1511 18s1 Lecture 6 1 Loops + Arrays Andrew Bennett <andrew.bennett@unsw.edu.au> loops inside loops stopping loops arrays

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

Feedback 3 upload lecture code upload/incorporate diagrams in lecture recordings more diagrams go through programs as a whole before running them lecture subtitles

Overview 4 after this lecture, you should be able to write programs using nested while loops to solve simple problems understand how to stop loops using loop counters and sentinel variables understand the basics of arrays understand the basics of designing a solution to a problem (time permitting) (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, it takes consistent and regular practice.)

Admin 5 Don t panic! lecture recordings are on WebCMS3 I ll try to add drawings/diagrams + upload to YouTube course style guide published weekly test due wednesday don t be scared! assignment 1 coming soon more on that tomorrow! don t forget about help sessions! see course website for details

Loops 6 what if we want to do something multiple times? Use a loop! keep doing this while this condition is true

Anatomy of a Loop 7 initialisation set up our variables condition while something statements things we do inside our loop update move along to the next iteration

Anatomy of a Loop 8 // initialisation int i = 0; // condition while (i < n) { // statements -- do something in the loop printf("hello!\n"); } // update i++;

Stopping: Loop Counters 9 // Print out "hello, world!" n times, // where n is chosen by the user. int num; printf ("Enter a number: "); scanf ("%d", &num); int i = 0; while (i < num) { printf ("hello, world!\n"); i = i + 1; }

Stopping: Sentinel Value (Flag) 10 // Print out the number that the user entered // Stop when they type 0 int n = 1; while (n!= 0) { printf ("You entered: %d\n", n); scanf("%d", &n); }

Nested Loops 11 while (something) { while (somethingelse) { } }

Nested Loops 12 int i = 0; while (i < n) { int j = 0; while (j < n) { // do something printf("!!"); j++; } i++; }

revisiting: variables 13

Variables 14 variables are like a box that can hold a value of a certain type int i = 5; 5

Variables 15 we can have as many variables as we like int i = 5; 5 int age = 18; 18 double pi = 3.14; 3.14

Lots of Variables 16 sometimes we want to store a lot of related variables int mark_student0 = 85; 85 int mark_student1 = 90; 90 int mark_student2 = 45; 45

Lots of Variables 17 sometimes we want to store a lot of related variables int mark_student0 = 85; int mark_student1 = 90; int mark_student2 = 45; double averagemark = (mark_student1 + mark_student2 + mark_student3)/3; printf("the average mark was: %lf\n", averagemark); This doesn t scale!

introducing: arrays 18

Arrays 19 A series of boxes with a common type, all next to each other

Arrays 20 A series of boxes with a common type, all next to each other 0 1 2 3 4 5 6 7 8 9 10 11

Why? 21 Suppose we need to compute statistics on class marks int mark_student0, mark_student1, mark_student2,...; mark_student0 = 85; mark_student1 = 90; mark_student2 = 45;... becomes unfeasible if dealing with a lot of values we d need hundreds of individual variables!

Why? 22 Solution: Use an array! int mark[1160]; mark[0] = 85; mark[1] = 90; mark[2] = 45;...

Arrays in C 23 a collection of array elements each element must be the same type we refer to arrays by their index valid indices for elements are no real limit on number of elements we cannot assign, scan, or print whole arrays but we can assign, scan, and print elements

Arrays in C 24 // Declare an array with 10 elements // and initialises all elements to 0. int myarray[10] = {0}; 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9

Arrays in C 25 int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray[5] = 17; 3 0 0 0 0 17 0 0 0 0 0 1 2 3 4 5 6 7 8 9

Arrays in C 26 int myarray[10] = {0}; // Put some values into the array. myarray[0] = 3; myarray[5] = 17; myarray[10] = 42; // <-- Error 3 0 0 0 0 17 0 0 0 0??? 0 1 2 3 4 5 6 7 8 9

Reading an Array 27 scanf() can t read an entire array. this will only read 1 number: #define ARRAY_SIZE 42... int array[array_size]; scanf ("%d", &array); instead, you must read the elements one by one: int i = 0; while (i < ARRAY_SIZE) { scanf ("%d", &array[i]); i++; }

Printing an Array 28 printf() also can t print an entire array. this won t compile #define ARRAY_SIZE 42... int array[array_size]; printf ("%d", array); instead, you must print the elements one by one: int i = 0; while (i < ARRAY_SIZE) { printf ("%d", array[i]); i++; }

Copying an Array 29 given: #define ARRAY_SIZE 5 int array1[array_size] = {1, 4, 9, 16, 25}; int array2[array_size]; this won t compile array2 = array1; instead, you must copy the elements one by one: int i = 0; while (i < ARRAY_SIZE) { array2[i] = array1[i]; i++; }