Arrays. Array Basics. Chapter 8 Spring 2017, CSUS. Chapter 8.1
|
|
- Evan Reginald McCoy
- 6 years ago
- Views:
Transcription
1 Arrays Chapter 8 Spring 2017, CSUS Array Basics Chapter 8.1 1
2 Array Basics Normally, variables only have one piece of data associated with them An array allows you to store a group of items of the same data type together in memory 1/21/2017 Sacramento State - CSc 10A 3 Array Basics Why? Instead of creating multiple similar variables such as employee1, employee2, employee3 and so on It s more efficient to create just one variable with a shared, but multiple values 1/21/2017 Sacramento State - CSc 10A 4 2
3 Metaphor for Arrays Think of an array as a set of mailboxes Each mailbox belongs to the same variable Each mailbox has a unique number 1/21/2017 Sacramento State - CSc 10A 5 Metaphor for Arrays... or think of arrays as a group of boxes Each box belongs to the same variable Each box has a unique number 1/21/2017 Sacramento State - CSc 10A 6 3
4 Array Terminology Each value located in an array is called an element Each can be accessed using an unique number called an index (also called a subscript) 1/21/2017 Sacramento State - CSc 10A 7 What Value Do We Start With? So, what are the valid values for the index? Most languages use 0-indexing other languages use 1-indexing the success of C set the standard nowadays, most major languages use 0-indexing (e.g. Visual Basic) 1/21/2017 Sacramento State - CSc 10A 8 4
5 Zero Indexing This means the first element in any array has the index 0 So, even though this will be odd and strange, it is something you must learn to live with 1/21/2017 Sacramento State - CSc 10A 9 Creating Arrays Chapter 8.1 5
6 Creating Arrays Arrays are created pretty much the same as any other variable However, since the array can contain multiple values, you must specify its size 1/21/2017 Sacramento State - CSc 10A 11 Book Pseudocode: Array Declare total number of elements Declare type name [ size ] Real, Integer, or String 1/21/2017 Sacramento State - CSc 10A 12 6
7 Examples Declare String employees[50] Declare Real salesamounts[7] 1/21/2017 Sacramento State - CSc 10A 13 Accessing Elements 1/21/2017 Sacramento State - CSc 10A 14 7
8 Array Declarations age Declare Real age[3] /21/2017 Sacramento State - CSc 10A 15 Accessing Each Cell After an array is created you can read/write any element You can also access the entire array using the variable name 1/21/2017 Sacramento State - CSc 10A 16 8
9 Accessing Each Cell The notation is incredibly simple Simply follow the array name by square brackets and the index of the element you want 1/21/2017 Sacramento State - CSc 10A 17 How You Access an Element Variable Name name [index] 0, 1, 2, 1/21/2017 Sacramento State - CSc 10A 18 9
10 Example Variables sacstate totalcost Normal variables test[4] name[2] Array elements 1/21/2017 Sacramento State - CSc 10A 19 Array Elements 1/21/2017 Sacramento State - CSc 10A
11 Array Example What Happens? Declare Integer test[2] Set test[0] = 1947 Set test[1] = 42 Display test[0] Display test[1] 1/21/2017 Sacramento State - CSc 10A 21 Array Example What Happens? Declare Integer test[2] test Set test[0] = 1947 Set test[1] = /21/2017 Sacramento State - CSc 10A 22 11
12 Array Example What Happens? Declare Integer test[2] Set test[0] = 1947 Set test[1] = test /21/2017 Sacramento State - CSc 10A 23 Array Example What Happens? Declare Integer test[2] Set test[0] = 1947 Set test[1] = test /21/2017 Sacramento State - CSc 10A 24 12
13 Array Example Declare Integer test[2] Set test[0] = 1947 Set test[1] = 42 Display test[0] Display test[1] 1/21/2017 Sacramento State - CSc 10A 25 Array Example Output /21/2017 Sacramento State - CSc 10A 26 13
14 Array Initialization Just like regular variables, arrays can be initialized to 0 or specific values Not all languages support this however, the big ones such as C#, Java, and Visual Basic do even though the notation varies a bit 1/21/2017 Sacramento State - CSc 10A 8-27 Example Declare String days[7] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 1/21/2017 Sacramento State - CSc 10A 28 14
15 Bounds Checking Sometimes the program will use an invalid index Naturally, this is attempting to access data that does not exist Array bounds checking prevents the use of an invalid subscript 1/21/2017 Sacramento State - CSc 10A 8-29 Example Declare String days[7] days[7] = "Saturday" Invalid because there is no 7 index 1/21/2017 Sacramento State - CSc 10A 30 15
16 Bounds Checking A common error is running a loop one time more than is necessary, exceeding the bound of the array This is an off-by-one error and is happens quite often (especially so because we use 0-indexing) 1/21/2017 Sacramento State - CSc 10A 8-31 Loops and Arrays Chapter
17 For Loops and Arrays For Loops are extremely well suited for iterating through all the values of an array In fact, one of the reasons For Loops exists is to interact with arrays 1/21/2017 Sacramento State - CSc 10A 33 Loops and Arrays Using For Loops, it is easy to access all the elements of an array linearly The loop variable is used as the index in the array 1/21/2017 Sacramento State - CSc 10A 34 17
18 Declare String name[4] Declare Integer n name[0] = "Tappa Kegga Bru" name[1] = "Cuppa Kappa Chino" name[2] = "Hu Delta Phart" name[3] = "Eta Lotta Pi" For n = 0 TO 3 Display name[n] End For 1/21/2017 Sacramento State - CSc 10A 35 Greek Example Output Tappa Kegga Bru Cuppa Kappa Chino Hu Delta Phart Eta Lotta Pi 1/21/2017 Sacramento State - CSc 10A 36 18
19 Loop Example Declare Integer n Declare String days[7] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" For n = 0 to 6 Display days[n] End For 1/21/2017 Sacramento State - CSc 10A 37 Loop Example Output Sunday Monday Tuesday Wednesday Thursday Friday Saturday 1/21/2017 Sacramento State - CSc 10A 38 19
20 Loop Example 2 Declare Integer n Declare String days[7] = "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" For n = 0 to 6 Display n, days[n] End For 1/21/2017 Sacramento State - CSc 10A 39 Loop Example 2 Output 0 Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday 1/21/2017 Sacramento State - CSc 10A 40 20
21 Declare Real score[3] Set score[0] = 85 Set score[1] = 98 Set score[2] = 61 For n = 0 to 2 If score[n] >= 70 Then Display score[n], " passes" Else Display score[n], " fails" End If End For 1/21/2017 Sacramento State - CSc 10A 41 Array Example 2 Output 85 passes 98 passes 61 fails 1/21/2017 Sacramento State - CSc 10A 42 21
22 The For Each Loop Some languages provide a For Each loop It works with an array, iterating once for each array element During each iteration, the loop copies an element's value to a variable. 1/21/2017 Sacramento State - CSc 10A 8-43 For Each Example Constant Integer SIZE = 4 Declare Integer numbers[size] = 5, 10, 15, 20 Declare Integer num For Each num In numbers Display num End For 1/21/2017 Sacramento State - CSc 10A 44 22
23 Partially Filled Array Chapter 8.1 Partially Filled Array Sometimes an array is only partially filled To avoid processing the unfilled elements, you must use an integer variable that holds the number of items stored in the array 1/21/2017 Sacramento State - CSc 10A 46 23
24 Partially Filled Array When the array is empty, 0 is stored in this variable The variable is incremented each time an item is added to the array The variable's value is used as the array's size when stepping through the array. 1/21/2017 Sacramento State - CSc 10A 8-47 Declare Real score[100] Declare Integer Count 100 Capacity Display "How many tests?" Input Count Not using all of it For n = 0 to Count - 1 End For Input score[n] 1/21/2017 Sacramento State - CSc 10A 48 24
25 Array Example 2 Output How many tests? /21/2017 Sacramento State - CSc 10A 49 Sequentially Searching an Array Chapter
26 Sequentially Searching an Array A sequential search algorithm is a simple technique for finding an item in a string or numeric array One of the most common and ways of locating data 1/21/2017 Sacramento State - CSc 10A 8-51 How it Works Uses a loop to sequentially step through an array Compares each element with the value being searched for Stops when the value is found or the end of the array is hit 1/21/2017 Sacramento State - CSc 10A 52 26
27 Example Set found = False Set index = 0 While not found AND index <= SIZE - 1 If array[index] == searchvalue Then Set found = True Else End if found Set index = index + 1 End If End While If not, look at next item (next index) 1/21/2017 Sacramento State - CSc 10A 53 Processing the Contents of an Array Chapter
28 Processing the Contents of an Array It is common to use an array to store multiple values to be analyzed The information is first stored, and then analyzed later by a different loop 1/21/2017 Sacramento State - CSc 10A 8-55 Calculating the Average Loops are used to accumulate the values create a total Then, the total is simply divided by the size 1/21/2017 Sacramento State - CSc 10A
29 Calculate the Average Set total = 0 For n = 0 to SIZE 1 total = total + score[n] End For Set average = total / SIZE 1/21/2017 Sacramento State - CSc 10A 57 Highest and Lowest Value Finding the highest & lowest values in an array is common It basically works by scanning the array and setting a high (or low value) based on the current value 1/21/2017 Sacramento State - CSc 10A
30 Steps Involved Highest Value Create a variable to hold the highest value Assign the value at element 0 to the highest Use a loop to step through the rest of the elements Each iteration, a comparison is made to the highest variable If the element is greater than the highest value, that value is then the assigned to the highest variable 1/21/2017 Sacramento State - CSc 10A 8-59 Highest Value Set highest = scores[0] For n = 1 to count 1 If score[n] > highest Set highest = score[n] End if End For 1/21/2017 Sacramento State - CSc 10A 60 30
31 Lowest Value Set lowest = scores[0] For n = 1 to count 1 if score[n] < lowest Set lowest = score[n] end if End For 1/21/2017 Sacramento State - CSc 10A 61 Example 2 The following is a full program that uses many of the techniques we covered arrays counter for partially filled arrays sentinel loops and more So, if you understand this program, then you understand the material 1/21/2017 Sacramento State - CSc 10A 62 31
32 Constant Integer SIZE = 100 Declare Integer values[size] Declare Integer count = 0 Declare Integer number Declare Integer Index Display "Enter a number, or -1 to quit." Input number While (number!= -1 AND count < SIZE) Set values[count] = number Set count = count + 1 Display "Enter a number, or -1 to quit." Input number End While Display "Here are the values you entered:" For index = 0 To count - 1 Display values[index] End For 1/21/2017 Sacramento State - CSc 10A 8-63 Parallel Arrays Chapter
33 Parallel Arrays Often more than one piece of information needs to be saved for the same "object" One common approach is to use multiple arrays one for each type of data 1/21/2017 Sacramento State - CSc 10A 65 Parallel Arrays These are called parallel arrays They are separate arrays but we used the same index for each By using the same index, we can establish a relationship between them 1/21/2017 Sacramento State - CSc 10A 66 33
34 Parallel Arrays 1/21/2017 Sacramento State - CSc 10A 8-67 Example: Part 1 Input Constant SIZE = 10 Declare String name[size] Declare Real cash[size] For n = 0 to SIZE 1 Input name[n] Input cash[n] End For 1/21/2017 Sacramento State - CSc 10A 68 34
35 Example: Part 2 Search Set best = 0 For n = 1 to SIZE 1 If cash[n] > cash[best] Set best = n End If End For 1/21/2017 Sacramento State - CSc 10A 69 Example: Part 3 Results Display name[highest], Display "got the most donations of $", cash[best] 1/21/2017 Sacramento State - CSc 10A 70 35
36 Example Output Tappa Kegga Bru Lambda Lambda Lambda Eta Lotta Pi Lambda Lambda Lambda got the most donations of $ /21/2017 Sacramento State - CSc 10A 71 Two Dimension Arrays Chapter
37 Two Dimension Arrays A two-dimensional array is like several identical arrays put together Suppose a teacher has six students who take five tests 1/21/2017 Sacramento State - CSc 10A 73 Two-Dimensional Arrays 1/21/2017 Sacramento State - CSc 10A
38 Multiple Sizes Needed Two size variables are required when declaring two dimensional arrays Accessing is done with two loops, and both indexes 1/21/2017 Sacramento State - CSc 10A 75 Declaring Arrays Constant Integer ROWS = 3 Constant Integer COLS = 4 Declare Integer values[rows][cols] 1/21/2017 Sacramento State - CSc 10A 76 38
39 Using Loops For row = 0 To ROWS -1 For col = 0 To COLS 1 Display "Enter a number." Input values[row][col] End For End For 1/21/2017 Sacramento State - CSc 10A 77 Arrays of Three or More Dimensions Chapter
40 Arrays of Three or More Dimensions Arrays can also be three or more dimensions In fact, there are no limitations once the number of dimensions 1/21/2017 Sacramento State - CSc 10A 79 Arrays of Three or More Dimensions Declare Real seats[3][5][8] 1/21/2017 Sacramento State - CSc 10A
41 Arrays of Three or More Dimensions 1/21/2017 Sacramento State - CSc 10A 81 41
Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis
Chapter 6: Arrays Starting Out with Games and Graphics in C++ Second Edition by Tony Gaddis 6.1 Array Basics An array allows you to store a group of items of the same data type together in memory Why?
More informationCondition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.
Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes
More informationRepetition Structures
Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements
More informationOther conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen
Other conditional and loop constructs Fundamentals of Computer Science Keith Vertanen Overview Current loop constructs: for, while, do-while New loop constructs Get out of loop early: break Skip rest of
More informationChapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1
Chapter 2 Input, Processing, and Output Fall 2016, CSUS Designing a Program Chapter 2.1 1 Algorithms They are the logic on how to do something how to compute the value of Pi how to delete a file how to
More informationArrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming
Arrays: Higher Dimensional Arrays CS0007: Introduction to Computer Programming Review If the == operator has two array variable operands, what is being compared? The reference variables held in the variables.
More informationA Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays
A Beginner s Guide to Programming Logic, Introductory Chapter 6 Arrays Objectives In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested
More informationCS 302 ALGORITHMS AND DATA STRUCTURES
CS 302 ALGORITHMS AND DATA STRUCTURES A famous quote: Program = Algorithm + Data Structure General Problem You have some data to be manipulated by an algorithm E.g., list of students in a school Each student
More informationProgramming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester
Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019 Multiple selection choose one of two things if/else choose one from many things multiple selection using
More information11 Data Structures Foundations of Computer Science Cengage Learning
11 Data Structures 11.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define a data structure. Define an array as a data structure
More information7 Arrays. 7.1 Arrays Hold Multiple Values TOPICS. CONCEPT: An array allows you to store and work with multiple values of the same data type.
7 Arrays TOPICS 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C++ 7.4 Array Initialization 7.5 Processing Array Contents 7.6 Focus on Software Engineering: Using
More informationProgramming Logic and Design Sixth Edition
Objectives Programming Logic and Design Sixth Edition Chapter 6 Arrays In this chapter, you will learn about: Arrays and how they occupy computer memory Manipulating an array to replace nested decisions
More informationPseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:
Pseudocode There are 3 programming/pseudocode constructs: 1. Sequence: It refers that instructions should be executed one after another. 2. Selection: This construct is used to make a decision in choosing
More informationDATA STRUCTURES USING C
DATA STRUCTURES USING C Lecture-7 Data Structures Lecture Objectives After studying this chapter, the student should be able to: Define a data structure. Define an array as a data structure and how it
More informationLecture Outline. Rainfall Solution Attempt 1. Motivation for arrays. Introducing the Array. Motivation
Lecture Outline IMS1906 Programming in VB.NET Week 7 Lecture 1 & 2 Introduction to Arrays Arrays Part 1 and II Angela Carbone Monash University School of Information Management and Systems Defining Arrays
More informationChapter 9 Introduction to Arrays. Fundamentals of Java
Chapter 9 Introduction to Arrays Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the enhanced
More informationREPETITION CONTROL STRUCTURE LOGO
CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of
More informationBoolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;
Programming in C++ If Statements If the sun is shining Choice Statements if (the sun is shining) go to the beach; True Beach False Class go to class; End If 2 1 Boolean Data Type (false, ) i.e. bool bflag;
More informationMichele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring
Michele Van Dyne Museum 204B mvandyne@mtech.edu http://katie.mtech.edu/classes/csci136 CSCI 136: Fundamentals of Computer Science II, Spring 2016 1 Review of Java Basics Data Types Arrays NEW: multidimensional
More informationClass C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;
Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f; Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter
More informationDECISION CONTROL AND LOOPING STATEMENTS
DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of
More informationUnderstanding the problem
2.1.1 Problem solving and design An algorithm is a plan, a logical step-by-step process for solving a problem. Algorithms are normally written as a flowchart or in pseudocode. The key to any problem-solving
More information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.
Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More informationLooping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include
1 Programming in C Looping Subtasks We will examine some basic algorithms that use the while and if constructs. These subtasks include Reading unknown quantity of data Counting things Accumulating (summing)
More informationIntroduction to Programming
Introduction to Programming ( 數 ) Lecture 4 Spring 2005 March 11, 2005 Topics Review of if statement The switch statement Repetition and Loop Statements For-Loop Condition-Loop Reading: Chap. 5.7~ Chap.
More information7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:
Chapter 7: 7.1 Arrays Arrays Hold Multiple Values Arrays Hold Multiple Values Array - Memory Layout A single variable can only hold one value int test; 95 Enough memory for 1 int What if we need to store
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationCOMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016
COMP-202: Foundations of Programming Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016 Review What is the difference between a while loop and an if statement? What is an off-by-one
More informationCS450 - Structure of Higher Level Languages
Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.
More informationPointers in C/C++ 1 Memory Addresses 2
Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................
More informationArrays. CSE / ENGR 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures.
CSE / ENGR 142 Programming I Arrays Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 1998
More informationCS 302: Introduction to Programming in Java. Lecture 9
1 CS 302: Introduction to Programming in Java Lecture 9 2 No class on Wednesday in Observance of Fourth of July 3 Announcement Programming Assignment #1 Due 11:59pm Sunday July 8 th Follow style and commenting
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More information*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN
More informationWeek 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018
Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Spring 2018 Jill Seaman 1 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements...
More informationData Structure. Chapter 11. Objectives After studying this chapter, students should be able to: 11-1 ARRAYS
11.1 Chapter 11 Data Structure Objectives After studying this chapter, students should be able to: Define a data structure. Define an array as a data structure and how it is used to store a list of data
More informationCONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice
CONTENTS: Arrays Strings COMP-202 Unit 5: Loops in Practice Computing the mean of several numbers Suppose we want to write a program which asks the user to enter several numbers and then computes the average
More informationCSC 337. JavaScript Object Notation (JSON) Rick Mercer
CSC 337 JavaScript Object Notation (JSON) Rick Mercer Why JSON over XML? JSON was built to know JS JSON JavaScript Object Notation Data-interchange format Lightweight Replacement for XML It's just a string
More informationObjectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.
1 September 26 September One: Sets Introduction to Sets Define a set Introduction: If we have a set "collection" of fruits : Banana, Apple Grapes. 4 F={,, } Banana is member "an element" of the set F.
More informationJava How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 10/e Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. Data structures Collections of related data items. Discussed in depth in Chapters 16 21. Array objects Data
More informationArrays. CSE 142 Programming I. Chapter 8. Another Motivation - Averaging Grades. Motivation: Sorting. Data Structures. Arrays
CSE 142 Programming I Chapter 8 8.1 Declaration and Referencing 8.2 Subscripts 8.3 Loop through arrays Arrays 8.4 & 8.5 Arrays arguments and parameters 8.6 Example 8.7 Multi-Dimensional Arrays 2000 UW
More informationComputer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons
Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite
More informationwhile for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)
Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationPASCAL. PASCAL, like BASIC, is a computer language. However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured).
PASCAL 11 OVERVIEW OF PASCAL LANGUAGE PASCAL, like BASIC, is a computer language However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured) Let look at a simple {BLOCK
More informationLearning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays
Chapter 5 Arrays Learning Objectives Introduction to Arrays Declaring and referencing arrays For-loops and arrays Arrays in memory Arrays in Functions Arrays as function arguments, return values Programming
More informationIteration Loops. The Do While Loop (Continued) Looping Subtasks
Iteration s The Do While (Continued) ing Subtasks This section in your book is from pages 2 to. You might think that, since this in the Do While loop section, it only refers to those kinds of loops. This
More information} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.
Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.
More informationIndicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.
1. C#, C++, C, and Java use the symbol as the logical OR operator. a. $ b. % c. ^ d. 2. errors are relatively easy to locate and correct because the compiler or interpreter you use highlights every error.
More informationWeek 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.
Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than
More informationChapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );
Chapter 4 Loops Sections Pages Review Questions Programming Exercises 4.1 4.7, 4.9 4.10 104 117, 122 128 2 9, 11 13,15 16,18 19,21 2,4,6,8,10,12,14,18,20,24,26,28,30,38 Loops Loops are used to make a program
More informationUniversity of Massachusetts Lowell
University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?
More informationCS 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 informationStepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017
Stepwise Refinement Lecture 12 COP 3014 Spring 2017 February 2, 2017 Top-Down Stepwise Refinement Top down stepwise refinement is a useful problem-solving technique that is good for coming up with an algorithm.
More informationTOPICS TO COVER:-- Array declaration and use.
ARRAYS in JAVA TOPICS TO COVER:-- Array declaration and use. One-Dimensional Arrays. Passing arrays and array elements as parameters Arrays of objects Searching an array Sorting elements in an array ARRAYS
More informationEnters system mode. Example The following example creates a scheduler named maintenancesched and commits the transaction:
Deployment, on page 1 Deployment Creating a Schedule UCS-A /system # create scheduler Creates a scheduler and enters scheduler mode. UCS-A /system/scheduler # commit-buffer The following example creates
More informationExample: Which of the following expressions must be an even integer if x is an integer? a. x + 5
8th Grade Honors Basic Operations Part 1 1 NUMBER DEFINITIONS UNDEFINED On the ACT, when something is divided by zero, it is considered undefined. For example, the expression a bc is undefined if either
More informationLesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)
Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen) 1 Lesson 9: Introduction Objectives: To Arrays Write programs that handle collections of similar items. Declare
More informationC++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections
C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example
More informationRepetition CSC 121 Fall 2014 Howard Rosenthal
Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationModule 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University
Module 5 Arrays Adapted from Absolute Java, Rose Williams, Binghamton University Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type An
More informationStart Fred Functions. Quadratic&Absolute Value Transformations. Graphing Piecewise Functions Intro. Graphing Piecewise Practice & Review
Honors CCM2 Unit 6 Name: Graphing Advanced Functions This unit will get into the graphs of simple rational (inverse variation), radical (square and cube root), piecewise, step, and absolute value functions.
More informationisinstance and While Loops
CS 1110: Introduction to Computing Using Python Lecture 20 isinstance and While Loops [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements A4: Due 4/20 at 11:59pm Should only use our str method
More informationSession 8.2. Finding Winners Using Arrays
1 Session 8.2 Finding Winners Using Arrays Chapter 8.2: Finding Winners Using Arrays 2 Session Overview Find out how the C# language makes it easy to create an array that contains multiple values of a
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationUsing Variables to Write Pattern Rules
Using Variables to Write Pattern Rules Goal Use numbers and variables to represent mathematical relationships. 1. a) What stays the same and what changes in the pattern below? b) Describe the pattern rule
More informationControl Statements. if for while
Control Structures Control Statements if for while Control Statements if for while This This is is called called the the initialization initialization statement statement and and is is performed performed
More informationArmstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer
Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer Prerequisites The Switch-Case Statements Primer assumes knowledge of the MATLAB IDE, MATLAB help, arithmetic operations,
More informationACT! Calendar to Excel
Another efficient and affordable ACT! Add-On by ACT! Calendar to Excel v.6.0 for ACT! 2008 and up http://www.exponenciel.com ACT! Calendar to Excel 2 Table of content Purpose of the add-on... 3 Installation
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)
More informationChapter Goals. Contents LOOPS
CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common
More informationFriday Four Square! Today at 4:15PM, Outside Gates
Control Structures Announcements Programming Assignment #1 due right now. Due on next Wednesday if using a late day. Email due on Sunday night. Programming Assignment #2 out today, due Wednesday, January
More informationArrays. Arizona State University 1
Arrays CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 8 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationCOP 2000 Introduction to Computer Programming Mid-Term Exam Review
he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can
More informationTutorial 06. Conditional statement: if then, if else, switch
College of Computer and Infmation Sciences CSC111 Computer Programming I Exercise 1: Tutial 06 Conditional statement: if then, if, switch What is the output of each of the following code fragments? (given
More information4. Assignment statements Give an assignment statement that sets the value of a variable called total to 20: Answer: total = 20;
First Exam Review, Thursday, February 6, 2014 Note: Do not hand in this lab; but use it for review. Review all materials from notes, slides, examples and labs. Here is an overview of topics with some example
More information: Dimension. Lecturer: Barwick. Wednesday 03 February 2016
18.06.01: Dimension Lecturer: Barwick Wednesday 03 February 2016 What is dimension? Line segments are 1-dimensional; here s one now: Planar regions are 2-dimensional; here s one: Finally, cubes are 3-dimensional:
More informationARRAYS, RECURSION, AND COMPLEXITY
ARRAYS, RECURSION, AND COMPLEXITY Chapter 10 Introduction to Arrays Chapter 11 Classes Continued Chapter 12 Arrays Continued 5 hrs. 5 hrs. 4.5 hrs. Estimated Time for Unit: 14.5 hours CHAPTER 10 INTRODUCTION
More informationPLD Semester Exam Study Guide Dec. 2018
Covers material from Chapters 1-8. Semester Exam will be built from these questions and answers, though they will be re-ordered and re-numbered and possibly worded slightly differently than on this study
More informationJava Programming: from the Beginning
CSc 2310: Principle of Programming g( (Java) Spring 2013 Java Programming: from the Beginning Chapter 5 Arrays 1 Copyright 2000 W. W. Norton & Company. All rights reserved. Outline 5.1 Creating and Using
More informationUNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING
UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a
More information1 Lexical Considerations
Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler
More informationA Look Back at Arithmetic Operators: the Increment and Decrement
A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate
More informationMatrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:
Matrices A Matrix is an array of numbers: We talk about one matrix, or several matrices. There are many things we can do with them... Adding A Matrix (This one has 2 Rows and 3 Columns) To add two matrices:
More informationTopics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop
Topics C H A P T E R 4 Repetition Structures Introduction to Repetition Structures The for Loop: a Count- Sentinels Nested Loops Introduction to Repetition Structures Often have to write code that performs
More informationCS 113 PRACTICE FINAL
CS 113 PRACTICE FINAL There are 13 questions on this test. The value of each question is: 1-10 multiple choice (4 pt) 11-13 coding problems (20 pt) You may get partial credit for questions 11-13. If you
More informationCS 113 MIDTERM EXAM 2 SPRING 2013
CS 113 MIDTERM EXAM 2 SPRING 2013 There are 18 questions on this test. The value of each question is: 1-15 multiple choice (3 pts) 17 coding problem (15 pts) 16, 18 coding problems (20 pts) You may get
More information7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values
Chapter 7: Arrays 7.1 Arrays Hold Multiple Values Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations Declared using
More informationProgramming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3
Programming - 1 Computer Science Department 011COMP-3 لغة البرمجة 1 011 عال- 3 لطالب كلية الحاسب اآللي ونظم المعلومات 1 1.1 Machine Language A computer programming language which has binary instructions
More informationLecture 22. While Loops
Lecture 22 While Loops Announcements for This Lecture Assignments Prelim 2 A5 is now graded Will be returned in lab Mean: 52 Median: 53 Std Dev: 5.5 Passing Grade: 30 A6 due next Tuesday Dataset should
More informationECE 122. Engineering Problem Solving Using Java
ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function
More informationBinary Search and Worst-Case Analysis
Department of Computer Science and Engineering Chinese University of Hong Kong A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems.
More informationArrays in Java Multi-dimensional Arrays
Suppose you are tasked with writing a program to help maintain seating records for a theatre company. The auditorium has 25 rows, each of which contains 30 seats. One utility you need to provide is tracking
More informationChapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++
Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure
More informationAn array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.
Data Structures Introduction An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type. Representation of a large number of homogeneous
More informationStatistics Case Study 2000 M. J. Clancy and M. C. Linn
Statistics Case Study 2000 M. J. Clancy and M. C. Linn Problem Write and test functions to compute the following statistics for a nonempty list of numeric values: The mean, or average value, is computed
More informationPart 1 Your First Function
California State University, Sacramento College of Engineering and Computer Science and Snarky Professors Computer Science 10517: Super Mega Crazy Accelerated Intro to Programming Logic Spring 2016 Activity
More information5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators
Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;
More informationJava Coding 3. Over & over again!
Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java
More information