Arrays. circleracers (one racer) Object Creation and "dot syntax" Array Operation Idiom. // the x-position of the racer float x = 0;

Size: px
Start display at page:

Download "Arrays. circleracers (one racer) Object Creation and "dot syntax" Array Operation Idiom. // the x-position of the racer float x = 0;"

Transcription

1 Arrays Object Creation and "dot syntax" Array Operation Idiom CS Arrays 1 circleracers (one racer) // the x-position of the racer float x = 0; void draw() { background(245); // white drawracer(x, 40, 1); x += 1; // draw a racer at position x,y // draw the racer number void drawracer(float x, float y, int number) {... CS Arrays 2

2 circleracers (three racers) // the x-position of the racers float x1 = 0; float x2 = 0; float x3 = 0; void draw() { background(245); // white drawracer(x1, 40, 1); x1 += random(0, 3); drawracer(x2, 80, 2); x2 += random(0, 3); drawracer(x3, 120, 3); x3 += random(0, 3); CS Arrays 3 float x1 = 0; float x2 = 0; float x3 = 0; float x4 = 0; float x5 = 0; float x6 = 0; float x7 = 0; float x8 = 0; float x9 = 0; float x10 = 0; float x11 = 0; float x12 = 0; float x13 = 0; float x14 = 0; float x15 = 0; float x16 = 0; float x17 = 0; float x18 = 0; float x19 = 0; float x20 = 0; drawracer(x1, 40, i); x1 += random(0, 3); drawracer(x2, 80, i); x2 += random(0, 3); drawracer(x3, 120, i); x3 += random(0, 3); drawracer(x4, 160, i); x4 += random(0, 3); drawracer(x5, 200, i); x5 += random(0, 3); drawracer(x6, 240, i); x6 += random(0, 3); drawracer(x7, 280, i); x7 += random(0, 3); drawracer(x8, 320, i); x8 += random(0, 3); drawracer(x9, 360, i); x9 += random(0, 3); drawracer(x10, 400, i); x10 += random(0, 3); drawracer(x11, 440, i); x11 += random(0, 3); drawracer(x12, 480, i); x12 += random(0, 3); drawracer(x13, 520, i); x13 += random(0, 3); drawracer(x14, 560, i); x14 += random(0, 3); drawracer(x15, 600, i); x15 += random(0, 3); drawracer(x16, 640, i); x16 += random(0, 3); drawracer(x17, 680, i); x17 += random(0, 3); drawracer(x18, 720, i); x18 += random(0, 3); drawracer(x19, 760, i); x19 += random(0, 3); drawracer(x20, 800, i); x20 += random(0, 3);; CS Arrays 4

3 Array An array is a special variable that holds multiple values. Each value location is called an element. Each element is accessed using a unique index - The index is a whole number: 0, 1, 2, Variable a Array b[] b[0] b[1] b[2] b[3] b[4] CS Arrays 5 CS Arrays 6

4 Declaring an Array Array declare an array of ints int[] b; array of ints type array name Variable (for comparison) int a; int variable type variable name CS Arrays 7 Creating an Array new is an operator that means create we need to create the elements so they can hold values - normal variables have a single value, no need to create int[] b; create an array of 5 ints b = new int[5]; create type of values length of array CS Arrays 8

5 Declare and Create an Array in One Step int[] b = new int[5]; declare an array of ints called b create an array of 5 ints CS Arrays 9 Declare and Create Array Examples boolean[] boxstates; boxstates = new boolean[4]; float[] scores = new float[4]; int[] hues = new int[10]; int num = 5; float[] x = new float[num]; float[] y = new float[num]; boolean[] s = new boolean[num * 2]; CS Arrays 10

6 Valid or Invalid Array Declaration and Creation? int[] a = new int[2 * 10]; float[] b = new int[5]; boolean c = new boolean[5]; float[5] d; int[] e; e = new int[123]; float[] f = float[12]; int[] g = new int[1]; int[] h = new int[0]; CS Arrays 11 Assigning Values to an Array You assign values to each array element using the index int[] b = new int[5]; element index b[3] = 66; assign 66 to element at index 3 b[3] is the 4 th element CS Arrays 12

7 Initializing an Array Initializing an array is when you assign a value to each element after creating the array All elements are initialized to a default value when created - float and int are 0, boolean is false, String is null int[] b = new int[5]; initializing b[0] = 15; b[1] = 7; b[2] = -10; b[3] = 66; b[4] = 5; b[] b[0] b[1] b[2] b[3] b[4] CS Arrays 13 Creating and Initializing an Array in One Step create an array of 5 ints and initialize it to these values int[] b = {15, 7, -10, 66, 5; b[] b[0] b[1] b[2] b[3] b[4] CS Arrays 14

8 Using Arrays Use array elements just like variables int[] b = new int[5]; b[0] = 15; b[1] = random(-100, 100); println(b[2]); float c = b[0] * 100; b[3] = b[3] + 5; ellipse(b[0], b[1], b[2], b[2]); if (b[3] > 5) {... CS Arrays 15 You must create an array before using it if an array hasn't been created, it has no elements - trying to access an "uncreated array", is a runtime error: int[] b; b[0] = 666; // error b[4] = 678; // error b[]????? CS Arrays 16

9 Valid Index Values index must be between 0 and one less than array length - if array length is 5, index can be 0, 1, 2, 3, or 4 - otherwise, it's a runtime error: int[] b = new int[5]; b[-1] = 123; // error b[5] = 123; // error b[] b[0] b[1] b[2] b[3] b[4] CS Arrays 17 Valid Index Types an index must be an int type otherwise, it's a syntax error int[] b = new int[5]; b[1.0] = 15; // error float f = 2; b[f] = 7; // error b[] b[0] b[1] b[2] b[3] b[4] b[int(f)] = 7; // ok you can convert an int using int() CS Arrays 18

10 (error demos) int[] b = new int[5]; float f = 1; void setup() { println("start"); println(b[5]); // try -1, 1.0, f println("stop"); CS Arrays 19 What is printed to the console? int[] bar = new int[4]; void setup() { bar[0] = 5; bar[1] = 4; bar[2] = 3; bar[3] = 2; A. 1 B. 2 C. 3 D. 4 E. 5 println(bar[1]); CS Arrays 20

11 What is printed to the console? int[] bar = new int[5]; void setup() { bar[0] = 1; bar[1] = 2; bar[2] = 3; bar[3] = 4; bar[4] = bar[bar[1]]; A. 1 B. 2 C. 3 D. 4 E. 5 println(bar[4]); CS Arrays 21 circleracers (with array) // the x-position of the racers float[] x = new float[3]; void draw() { background(245); // white drawracer(x[0], 40, 1); x[0] += random(0, 3); drawracer(x[1], 80, 2); x[1] += random(0, 3); drawracer(x[2], 120, 3); x[2] += random(0, 3); CS Arrays 22

12 Variables for Indices Use variables or return values for indices int[] b = new int[5]; int i = 0; b[i] = 15; b[i + 1] = 7; b[int(random(0, 5))] = ; int() function converts to int, otherwise a syntax error CS Arrays 23 circleracers (with array) // the x-position of the racers float[] x = new float[3]; void draw() { background(245); // white int i = 0; drawracer(x[i], 40, i + 1); x[i] += random(0, 3); i++; drawracer(x[i], 80, i + 1); x[i] += random(0, 3); i++; drawracer(x[i], 120, i + 1); x[i] += random(0, 3); CS Arrays 24

13 circleracers (with array and loop) // the x-position of the racers float[] x = new float[3]; void draw() { background(245); // white int i = 0; while (i < 3) { // the y position is a "function of" i float y = (i + 1) * 40; drawracer(x[i], y, i + 1); x[i] += random(0, 3); i++; CS Arrays 25 How many Xs are printed to the console? for (int i = 0; i < 5; i++ ) { print("x"); A. 0 B. 1 C. 4 D. 5 E. 6 CS Arrays 26

14 What is printed to the console? int[] arr = new int[5]; for (int i = 0; i < 5; i++) { arr[i] = i; println(arr[4]); A. 0 B. 1 C. 4 D. 5 E. nothing, it s an error CS Arrays 27 Array Length Arrays know their own length - Arrays are objects - you access array length using object dot syntax int[] b = new int[5]; println(b.length); // prints 5 say b dot length b[b.length 1] = 123; // ok b[b.length] = 123; // error CS Arrays 28

15 circleracers (with array and loop) // the x-position of the racers float[] x = new float[5]; void draw() { background(245); // white int i = 0; while (i < x.length) { // the y position is a "function of" i float y = (i + 1) * 40; drawracer(x[i], y, i + 1); x[i] += random(0, 3); i++; CS Arrays 29 Array Operation Idiom Looping through an entire array to apply some operation to all elements is a common operation. // initialize array elements to same value for (int i = 0; i < myarray.length; i++) { myarray[i] = 50; // initialize array elements to random value for (int i = 0; i < myarray.length; i++) { myarray[i] = random(0, 100); CS Arrays 30

16 Array Operation: Sum all Elements Compute the total sum for all elements in an array by adding them one by one to an accumulator variable. // compute the sum of all elements float total = 0; for (int i = 0; i < myarray.length; i++) { total += myarray[i]; println("total:", total); CS Arrays 31 Array Operation: Average Element Value Sum all elements in an array and divide total by the length of the array to compute the average element size. // calculate the average value float total = 0; for (int i = 0; i < myarray.length; i++) { total += myarray[i]; float average = total / myarray.length; println("average:", average); CS Arrays 32

17 Array Operation: Find Largest Element Value Search the array to find the largest element value - Start by guessing that the largest element is the first one - check all other elements one-by-one to see if any are larger - if a larger one is found, use it as the largest element // find the largest element // (assuming myarray has at least 1 element) float largest = myarray[0]; for (int i = 1; i < myarray.length; i++) { if (myarray[i] > largest) { largest = myarray[i]; println("largest:", largest); CS Arrays 33 Array Operation: Find Largest Element Index Search the array to find the index with largest element value - Start by guessing that index 0 is the largest element index - check all other elements one-by-one to see if any are larger - if larger one is found, use it s index as the largest element index // find the index of the largest element // (assuming myarray has at least 1 element) int indexoflargest = 0; for (int i = 1; i < myarray.length; i++) { if (myarray[i] > myarray[indexoflargest]) { indexoflargest = i; println("index of largest:", indexoflargest); println("largest:", myarray[indexoflargest]); CS Arrays 34

18 Asymptotic Runtime of Algorithms O(1) and O(N) big Oh How many operations does it take to print one element? print(myarray[i]); How many operations does it take to add all elements? // compute the sum of all elements float total = 0; for (int i = 0; i < myarray.length; i++) { total += myarray[i]; println("total:", total); CS Arrays 35 Arrays as Function Parameters and Arguments float means a single floating point value float[] means an array of floating point values void func(float f) { void func(float[] a) { CS Arrays 36

19 largestvalue (in arrayoperations code) // return the largest element in array a // (assuming a has at least 1 element) float largestvalue(float[] a) { float largest = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] > largest) { largest = a[i]; return largest; CS Arrays 37 arrayforest float[] treex = {166, 90, 77, 30, 110, 131, 55 ; float[] treeleafcolour = new float[treex.length]; void setup() {... // initialize to random tint of green for (int i = 0; i < treeleafcolour.length; i++ ) { treeleafcolour[i] = random(64, 130); void draw() {... // draw trees for forest for (int i = 0; i < treex.length; i++) { tree(treex[i], 95, treeleafcolour[i]); CS Arrays 38

20 EXTRA: Two Arrays, Shifting Elements in Array Textbook Example 9.6 CS Arrays 39 EXTRA: snake (LP Example 9-8) // Shift array values for (int i = 0; i < xpos.length - 1; i++) { xpos[i] = xpos[i + 1]; ypos[i] = ypos[i + 1]; // New location at end of array xpos[xpos.length - 1] = mousex; ypos[ypos.length - 1] = mousey; CS Arrays 40

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1 Conditionals Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary CS105 04 Conditionals 1 Pick a number CS105 04 Conditionals 2 Boolean Expressions An expression that

More information

Interaction Design A.A. 2017/2018

Interaction Design A.A. 2017/2018 Corso di Laurea Magistrale in Design, Comunicazione Visiva e Multimediale - Sapienza Università di Roma Interaction Design A.A. 2017/2018 8 Loops and Arrays in Processing Francesco Leotta, Andrea Marrella

More information

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays:

Tutorial 11. Exercise 1: CSC111 Computer Programming I. A. Write a code snippet to define the following arrays: College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 11 Arrays: A. Write a code snippet to define the following arrays: 1. An int array named nums of size 10.

More information

Appendix: Common Errors

Appendix: Common Errors Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often

More information

Arrays. Eng. Mohammed Abdualal

Arrays. Eng. Mohammed Abdualal Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays

More information

FORM 2 (Please put your name and form # on the scantron!!!!)

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function

More information

In this chapter: What is an array? Declaring an array. Initialization. Array operations using the for loop with an array. Arrays of objects.

In this chapter: What is an array? Declaring an array. Initialization. Array operations using the for loop with an array. Arrays of objects. 9 Arrays Arrays 141 I might repeat to myself slowly and soothingly, a list of quotations beautiful from minds profound if I can remember any of the damn things. Dorothy Parker In this chapter: What is

More information

COS 126 General Computer Science Spring Written Exam 1

COS 126 General Computer Science Spring Written Exam 1 COS 126 General Computer Science Spring 2017 Written Exam 1 This exam has 9 questions (including question 0) worth a total of 70 points. You have 50 minutes. Write all answers inside the designated spaces.

More information

CS 211: Methods, Memory, Equality

CS 211: Methods, Memory, Equality CS 211: Methods, Memory, Equality Chris Kauffman Week 2-1 So far... Comments Statements/Expressions Variable Types little types, what about Big types? Assignment Basic Output (Input?) Conditionals (if-else)

More information

COMPUTER APPLICATION

COMPUTER APPLICATION Total No. of Printed Pages 16 HS/XII/A.Sc.Com/CAP/14 2 0 1 4 COMPUTER APPLICATION ( Science / Arts / Commerce ) ( Theory ) Full Marks : 70 Time : 3 hours The figures in the margin indicate full marks for

More information

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II CS111: PROGRAMMING LANGUAGE II Computer Science Department Lecture 1(c): Java Basics (II) Lecture Contents Java basics (part II) Conditions Loops Methods Conditions & Branching Conditional Statements A

More information

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values Functions parameters return values 06 Functions 1 Functions Code that is packaged so it can be run by name Often has parameters to change how the function works (but not always) Often performs some computation

More information

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY Objectives Declaration of 1-D and 2-D Arrays Initialization of arrays Inputting array elements Accessing array elements Manipulation

More information

CS 106A, Lecture 16 Arrays

CS 106A, Lecture 16 Arrays CS 106A, Lecture 16 Arrays suggested reading: Java Ch. 11.1-11.5 This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

Arrays. Array an ordered collec3on of similar items. An array can be thought of as a type of container.

Arrays. Array an ordered collec3on of similar items. An array can be thought of as a type of container. Chapter 9 Arrays Arrays Array an ordered collec3on of similar items Student test scores Mail boxes at your college A collec3on of books on a shelf Pixels in an image the posi3ons for many stars in a night

More information

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2 Loops Variable Scope Remapping Nested Loops CS105 05 Loops 1 Donald Judd CS105 05 Loops 2 judd while (expression) { statements CS105 05 Loops 3 Four Loop Questions 1. What do I want to repeat? - a rect

More information

CS 110, Section 3 Exam 1 Review

CS 110, Section 3 Exam 1 Review CS 110, Section 3 Exam 1 Review What is printed when each program is executed? o Note, not all programs compile. Some have errors. Explain why the output is generated, or what caused the error. 1. int

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca Don t use == for floats!! Floating point numbers are approximations. How they are stored inside the computer means that

More information

ITEC1620 Object-Based Programming. Lecture 13

ITEC1620 Object-Based Programming. Lecture 13 ITEC1620 Object-Based Programming Lecture 13 References A Simple Class AnInt objecta = new AnInt(); AnInt objectb = new AnInt(); objecta objectb AnInt AnInt value value Value Assignments objecta.value

More information

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays WIT COMP1000 Arrays Arrays An array is a list of variables of the same type, that represents a set of related values For example, say you need to keep track of the cost of 1000 items You could declare

More information

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:.. CS2401 QUIZ 5 February 26, 2014 18 questions / 20 points / 20 minutes NAME:.. Questions on Objects and Classes 1 An object is an instance of a. A. program B. class C. method D. data 2 is invoked to create

More information

Bits and Bytes. How do computers compute?

Bits and Bytes. How do computers compute? Bits and Bytes How do computers compute? Representing Data All data can be represented with: 1s and 0s on/of true/false Numbers? Five volunteers... Binary Numbers Positional Notation Binary numbers use

More information

CISC 1600 Lecture 3.1 Introduction to Processing

CISC 1600 Lecture 3.1 Introduction to Processing CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists

More information

CS18000: Problem Solving And Object-Oriented Programming

CS18000: Problem Solving And Object-Oriented Programming CS18000: Problem Solving And Object-Oriented Programming Class (and Program) Structure 31 January 2011 Prof. Chris Clifton Classes and Objects Set of real or virtual objects Represent Template in Java

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Module 01 Processing Recap. CS 106 Winter 2018

Module 01 Processing Recap. CS 106 Winter 2018 Module 01 Processing Recap CS 106 Winter 2018 Processing is a language a library an environment Variables A variable is a named value. It has a type (which can t change) and a current value (which can

More information

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof. GridLang: Grid Based Game Development Language Language Reference Manual Programming Language and Translators - Spring 2017 Prof. Stephen Edwards Akshay Nagpal Dhruv Shekhawat Parth Panchmatia Sagar Damani

More information

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh Computer Science CS221 Test 2 Name 1. Give a definition of the following terms, and include a brief example. a) Big Oh b) abstract class c) overriding d) implementing an interface 10/21/1999 Page 1 of

More information

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

M1-R4: Programing and Problem Solving using C (JAN 2019)

M1-R4: Programing and Problem Solving using C (JAN 2019) M1-R4: Programing and Problem Solving using C (JAN 2019) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter

More information

Unit IV & V Previous Papers 1 mark Answers

Unit IV & V Previous Papers 1 mark Answers 1 What is pointer to structure? Pointer to structure: Unit IV & V Previous Papers 1 mark Answers The beginning address of a structure can be accessed through the use of the address (&) operator If a variable

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18 C Types Derived Function Array Pointer Structure Union Enumerated 2 tj Arrays Student 0 Student 1 Student 2 Student 3 Student 4 Student 0 Student 1 Student 2 Student 3 Student 4 Student[0] Student[1] Student[2]

More information

CS61B Lecture #2. Public Service Announcements:

CS61B Lecture #2. Public Service Announcements: CS61B Lecture #2 Please make sure you have obtained an account, run register, and finished the survey today. In the future (next week), the password required for surveys and such will be your account password

More information

Loops. When to Use Loops

Loops. When to Use Loops Loops When to Use Loops 1 Iterating with Numbers Many computations involve iterating over numbers: Checking each item in an array Computing sums or products from 0 to n One way to write such loops: Step

More information

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand what an array is Understand how to declare arrays Understand what reference variables are Understand how to pass arrays to methods

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

Arrays. Friday, November 9, Department of Computer Science Wellesley College

Arrays. Friday, November 9, Department of Computer Science Wellesley College Arrays Friday, November 9, CS Computer Programming Department of Computer Science Wellesley College Arrays: Motivation o o o Lists are great for representing a collection of values, but can only access

More information

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from

BEFORE CLASS. If you haven t already installed the Firebug extension for Firefox, download it now from BEFORE CLASS If you haven t already installed the Firebug extension for Firefox, download it now from http://getfirebug.com. If you don t already have the Firebug extension for Firefox, Safari, or Google

More information

CS S-08 Arrays and Midterm Review 1

CS S-08 Arrays and Midterm Review 1 CS112-2012S-08 Arrays and Midterm Review 1 08-0: Arrays ArrayLists are not part of Java proper 08-1: Arrays Library class Created using lower-level Java construct: Array Arrays are like a stripped-down

More information

Iteration and Arrays Dr. Abdallah Mohamed

Iteration and Arrays Dr. Abdallah Mohamed Iteration and Arrays Dr. Abdallah Mohamed Acknowledgement: Original slides provided courtesy of Dr. Lawrence. Before we start: the ++ and -- Operators It is very common to subtract 1 or add 1 from the

More information

CS150 Sample Final Solution

CS150 Sample Final Solution CS150 Sample Final Solution Name: Section: A / B Date: Start time: End time: Honor Code: Signature: This exam is closed book, closed notes, closed computer, closed calculator, etc. You may only use (1)

More information

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages CS4411 Intro. to Operating Systems Exam 1 Fall 2005 (October 6, 2005) 1 CS4411 Intro. to Operating Systems Exam 1 Fall 2005 150 points 10 pages Name: Most of the following questions only require very short

More information

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

AMCAT Automata Coding Sample Questions And Answers

AMCAT Automata Coding Sample Questions And Answers 1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return

More information

CS 161 Exam II Winter 2018 FORM 1

CS 161 Exam II Winter 2018 FORM 1 CS 161 Exam II Winter 2018 FORM 1 Please put your name and form number on the scantron. True (A)/False (B) (28 pts, 2 pts each) 1. The following array declaration is legal double scores[]={0.1,0.2,0.3;

More information

1. Complete these exercises to practice creating user functions in small sketches.

1. Complete these exercises to practice creating user functions in small sketches. Lab 6 Due: Fri, Nov 4, 9 AM Consult the Standard Lab Instructions on LEARN for explanations of Lab Days ( D1, D2, D3 ), the Processing Language and IDE, and Saving and Submitting. Rules: Do not use the

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;} 1. What does mystery(3) return? public int mystery (int n) { int m = 0; while (n > 1) {if (n % 2 == 0) n = n / 2; else n = 3 * n + 1; m = m + 1;} return m; } (a) 0 (b) 1 (c) 6 (d) (*) 7 (e) 8 2. What are

More information

CS 261 Data Structures. Introduction to C Programming

CS 261 Data Structures. Introduction to C Programming CS 261 Data Structures Introduction to C Programming Why C? C is a lower level, imperative language C makes it easier to focus on important concepts for this class, including memory allocation execution

More information

The Pyth Language. Administrivia

The Pyth Language. Administrivia Administrivia The Pyth Language Lecture 5 Please make sure you have registered your team, created SSH keys as indicated on the admin page, and also have electronically registered with us as well. Prof.

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review what an array is Review how to declare arrays Review what reference variables are Review how to pass arrays to methods Review

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

CS 101, Spring 2016 March 22nd Exam 2

CS 101, Spring 2016 March 22nd Exam 2 CS 101, Spring 2016 March 22nd Exam 2 Name: Question 1. [3 points] Which of the following loop statements would most likely cause the loop to execute exactly n times? You may assume that n will be set

More information

Create a Program in C (Last Class)

Create a Program in C (Last Class) Create a Program in C (Last Class) Input: three floating point numbers Output: the average of those three numbers Use: scanf to get the input printf to show the result a function to calculate the average

More information

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std; r6.14 For the operations on partially filled arrays below, provide the header of a func tion. d. Remove all elements that are less than a given value. Sol a. void remove_items_less_than(int arr[], int

More information

CSE120 Wi18 Final Review

CSE120 Wi18 Final Review CSE120 Wi18 Final Review Practice Question Solutions 1. True or false? Looping is necessary for complex programs. Briefly explain. False. Many loops can be explicitly written out as individual statements

More information

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell Overview II (2A) Young Won Lim 8/9/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

Data-Flow Analysis Foundations

Data-Flow Analysis Foundations CS 301 Spring 2016 Meetings April 11 Data-Flow Foundations Plan Source Program Lexical Syntax Semantic Intermediate Code Generation Machine- Independent Optimization Code Generation Target Program This

More information

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls. Jump Statements The keyword break and continue are often used in repetition structures to provide additional controls. break: the loop is terminated right after a break statement is executed. continue:

More information

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),

More information

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (! FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

CS1150 Principles of Computer Science Arrays

CS1150 Principles of Computer Science Arrays CS1150 Principles of Computer Science Arrays Yanyan Zhuang Department of Computer Science http://www.cs.uccs.edu/~yzhuang CS1150 UC. Colorado Springs Opening Problem Read one hundred numbers, compute their

More information

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu Important Review Does the animation leave a trace? Are the moving objects move without a

More information

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or Ch 9. Pointers CS 2308 Spring 2014 Jill Seaman 1 A Quote A pointer is a variable that contains the address of a variable. Pointers are much used in C, partly because they are sometimes the only way to

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca Hello! James (Jim) Young young@cs.umanitoba.ca jimyoung.ca office hours T / Th: 17:00 18:00 EITC-E2-582 (or by appointment,

More information

Functions. Arash Rafiey. September 26, 2017

Functions. Arash Rafiey. September 26, 2017 September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic

More information

Computer programming Code exercises [1D Array]

Computer programming Code exercises [1D Array] Computer programming-140111014 Code exercises [1D Array] Ex#1: //Program to read five numbers, from user. public class ArrayInput public static void main(string[] args) Scanner input = new Scanner(System.in);

More information

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

CS159. Nathan Sprague

CS159. Nathan Sprague CS159 Nathan Sprague What s wrong with the following code? 1 /* ************************************************** 2 * Return the mean, or -1 if the array has length 0. 3 ***************************************************

More information

C Pointers. 6th April 2017 Giulio Picierro

C Pointers. 6th April 2017 Giulio Picierro C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username What is a variable? a named location in the computer s memory mousex mousey width height fontcolor username Variables store/remember values can be changed must be declared to store a particular kind of

More information

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler) 1 Arrays General Questions 1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A. The element will be set to 0. B. The compiler would

More information

AP Computer Science A: while loops, interfaces and inheritance. 1) What is printed out based on Light.java and LightMain.java below and next page:

AP Computer Science A: while loops, interfaces and inheritance. 1) What is printed out based on Light.java and LightMain.java below and next page: PRACTICE test do not turn in AP Computer Science A: while loops, interfaces and inheritance 1) What is printed out based on Light.java and LightMain.java below and next page: public class Light final static

More information

Chapter 6 Single-dimensional Arrays

Chapter 6 Single-dimensional Arrays Chapter 6 Single-dimensional s 1. See the section "Declaring and Creating s." 2. You access an array using its index. 3. No memory is allocated when an array is declared. The memory is allocated when creating

More information

CSE 114 Computer Science I

CSE 114 Computer Science I CSE 114 Computer Science I Iteration Cape Breton, Nova Scotia What is Iteration? Repeating a set of instructions a specified number of times or until a specific result is achieved How do we repeat steps?

More information

CS150 Sample Final. Name: Section: A / B

CS150 Sample Final. Name: Section: A / B CS150 Sample Final Name: Section: A / B Date: Start time: End time: Honor Code: Signature: This exam is closed book, closed notes, closed computer, closed calculator, etc. You may only use (1) the final

More information

Reviewing all Topics this term

Reviewing all Topics this term Today in CS161 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for) Functions (pass by value, pass by reference) Arrays (specifically arrays of characters)

More information

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4 CS 220: Introduction to Parallel Computing Arrays Lecture 4 Note: Windows I updated the VM image on the website It now includes: Sublime text Gitkraken (a nice git GUI) And the git command line tools 1/30/18

More information

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key

CIS 110 Introduction to Computer Programming. 12 February 2013 Midterm. Answer Key CIS 110 Introduction to Computer Programming 12 February 2013 Midterm Answer Key 0. (1 point) Miscellaneous (a) Write your name, recitation number, and PennKey (username) on the front of the exam. (b)

More information

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments Basics Objectives Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments 2 Class Keyword class used to define new type specify

More information

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name: CIS 110 Introduction to Computer Programming Summer 2014 Midterm Name: PennKey (e.g., bhusnur4): My signature below certifies that I have complied with the University of Pennsylvania s Code of Academic

More information

Teaching guide: Subroutines

Teaching guide: Subroutines Teaching guide: Subroutines This resource will help with understanding subroutines. It supports Sections 3.2.2 and 3.2.10 of our current GCSE Computer Science specification (8520). The guide is designed

More information

Lecture 15 CSE11 Fall 2013 Arrays

Lecture 15 CSE11 Fall 2013 Arrays Lecture 15 CSE11 Fall 2013 Arrays Arrays A collection of objects, stored that can accessed by an index (also known as a subscript) Example String [] sarray = new String[20]; defines sarray to have 20 slots.

More information

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018 CS 61B Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018 1 Asymptotic Notation 1.1 Order the following big-o runtimes from smallest to largest. O(log n), O(1), O(n n ), O(n 3 ), O(n log n),

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca order of operations with the explicit cast! int integervariable = (int)0.5*3.0; Casts happen first! the cast converts the

More information

Office 2016 Excel Basics 06 Video/Class Project #18 Excel Basics 6: Customize Quick Access Toolbar (QAT) and Show New Ribbon Tabs

Office 2016 Excel Basics 06 Video/Class Project #18 Excel Basics 6: Customize Quick Access Toolbar (QAT) and Show New Ribbon Tabs **These pdf Notes are for video 6-8. Scroll down to see notes for all three videos. Office 2016 Excel Basics 06 Video/Class Project #18 Excel Basics 6: Customize Quick Access Toolbar (QAT) and Show New

More information

... Lecture 12. Pointers

... Lecture 12. Pointers Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-1 Lecture 12. Pointers Variables denote locations in memory that can hold values; arrays denote contiguous locations int i = 8, sum = -456;

More information