Arrays. Storing Multiple Values

Similar documents
Arrays and Strings. Arash Rafiey. September 12, 2017

Java Programming. Computer Science 112

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Subject: Computer Science

Java Programming. Computer Science 112

Lecture 5 - Control Structure Applications

Quadratic: the time that it takes to sort an array is proportional to the. square of the number of elements.

Review of Important Topics in CS1600. Functions Arrays C-strings

Using Microsoft Excel for Recording and Analyzing Data Noah Segall

Introduction to Computer Science Midterm 3 Fall, Points

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

2.2 Data representation

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Microsoft Excel 2010 Training. Excel 2010 Basics

CS130/230 Lecture 12 Advanced Forms and Visual Basic for Applications

LECTURE 17. Array Searching and Sorting

Visual Basic

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

INDIAN SCHOOL MUSCAT COMPUTER SCIENCE(083) CLASS XI

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

C++ PROGRAMMING SKILLS Part 4: Arrays

Chapter 8 Multi-Dimensional Arrays

Chapter 7 Multidimensional Arrays

Burrows & Langford Chapter 10 page 1 Learning Programming Using VISUAL BASIC.NET

(Subroutines in Visual Basic)

1. Select a cell in the column you want to sort by. In this example, we will sort by Last Name.

Chapter 8 Algorithms 1

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

Principles of Programming. Chapter 6: Arrays

Arrays / Lists. In this section of notes you will be introduced to new type of variable that consists of other types. Types Of Variables

Lab Sheet 4.doc. Visual Basic. Lab Sheet 4: Non Object-Oriented Programming Practice

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Chapter Nine Arrays. Sixth Edition

Please answer questions in the space provided. Question point values are shown in parentheses.

How to declare an array in C?

Using the Advanced Features in Your GradeQuick Gradebooks

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey

The Center for Teaching, Learning, & Technology

[6] [6] [6] [5] [5] [6] [5] [5]

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Unit 3 Fill Series, Functions, Sorting

Unit 3 Functions Review, Fill Series, Sorting, Merge & Center

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

CpSc 1111 Lab 9 2-D Arrays

IF & VLOOKUP Function

Kadi Sarva Vishwavidyalaya, Gandhinagar

I101/B100 Problem Solving with Computers

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

Tutorial: Input Grades in Blackboard

Sub Programs. To Solve a Problem, First Make It Simpler

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

User Manual Mail Merge

Arrays IT 1033: Fundamentals of Programming

Graded Project. Excel 2016

Reading Wonders: Importing Students & Adding Students to your Class

Skittles Excel Project

VBA Collections A Group of Similar Objects that Share Common Properties, Methods and

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

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.

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

Sorting and Filtering Data

DATABASE MANAGERS. Basic database queries. Open the file Pfizer vs FDA.mdb, then double click to open the table Pfizer payments.

Creating and using Moodle Rubrics

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

STUDENT LESSON A12 Iterations

Arrays. CS Feb 2008

JPAMS Attendance. Click Attendance in the Program Navigator list. Under Entry, select Post Attendance.

Variables in VB. Keeping Track

Chapter 6 Pointers and Arrays

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Data Dashboard Navigation for Building Administrators

Declaring a 2D Array

Computer Programming C++ (wg) CCOs

Spring 2017 CS 1110/1111 Exam 1

Introduction to VBA for Excel-Tutorial 7. The syntax to declare an array starts by using the Dim statement, such that:

variables programming statements

Data. Selecting Data. Sorting Data

STRING Represents group of characters Each character 1 byte CIE, AB304 BOOLEAN Logical Datatype, return true or False Memory Required 1

Database Concepts Using Microsoft Access

4. Low level language with some high level features 9) What will happen if in a C program you assign a value to an array element whose subscript

Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.

Using References to Create Complex Structures. The array and hash composers

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Arrays Chapter 6 Chapter 6 1

Chapter 6: Using Arrays

Agenda. Arrays 01/12/2009 INTRODUCTION TO VBA PROGRAMMING. Arrays Matrices.

SIMPLE INPUT and OUTPUT:

Arrays and Other Data Types

CSci 1113 Lab Exercise 6 (Week 7): Arrays & Strings

//If target was found, then //found == true and a[index] == target.

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

C++ Programming Chapter 7 Pointers

Access Groups. Collect and Store. Text Currency Date/Time. Tables Fields Data Type. You Your Friend Your Parent. Unique information

Student Name: (in Capital Letters) CSE Introduction to Programming for Engineers and Scientists. Final Exam

Transcription:

Arrays Storing Multiple Values

Motels If you have a room in a motel and they give you room 5, which room is it? 1 2 3 4 5 6

Hotels If you have room 1541 in the Deluxe Ritz Hilton, which floor would you find your room on?

Numbering Locations Motel Rooms are numbered in series - 1 through 6. Room 3 is the third room. 1 2 3 4 5 6 Third Room

Numbering Computer Memory Locations A series of memory locations can be so numbered This series is called an array Arrays are referenced with a subscript. Rooms 1 2 3 4 5 6 7 8 9 10 11

Arrays So if our array is called Room and we want location 3, we just say Room(3) Rooms 1 2 3 4 5 6 7 8 9 10 11 Subscript

Declaring Arrays We need to declare arrays before we use them. Array name is given Then the length of the array in parenthesis Finally the type is given.

Example Declarations Dim Rooms(1 To 15) as Integer Dim Scores(15) As Single These are numbered zero to 14! Dim Locations(-5 to 20) as String They can start anywhere! Dim Fleebs(1 to 100000) as Long They can be large!

More Examples For holding the grades from a class: Dim grades(1 to 30) as Single For holding the season wins and losses for the Red Wings: Dim wins(1 to 50) as Integer For holding the names of everyone in class: Dim names(1 to 30) as String

Array Limitation Under VB 4.0 & 5.0 Arrays are limited by the available memory Arrays that are larger than RAM will make your program sloooooooooooow!

Question: How would you declare an array called students with 30 elements all of type String? A. Dim String of Students(1 to 30) B. Dim Students as String(1 to 30) C. D. E. Dim Students(1 to 30) as String B or C None of the above

Using an Array Arrays can be used just like other variables, as long as you include their subscript: name(2) = Jeffrey Lockledge grade(3) = grade(3) + 100 result = grade(3) * grade(4) + 2

The Combination of Arrays and Loops To zero out an array of grades: Dim grades(1 to 10) as Single Dim i as Integer For i = 1 to 10 grades(i) = 0 Next i

Question: I want to set the value of each element of an array to its index (i.e. student(1) contains 1 Dim Student(1 To 30) as Single Dim k as Integer For k = 1 to 30 What goes here? Next k A. student(k) = 0 B. student(0) = k C. student(k) = student(k) D. student(k) = k E. None of the above

Just a Note Non-Array variables are called Scalar variables Temp(5) is an array variable Fleeb is a scalar

Sorting 5 9 7 0 3 2 4 1 0 1 2 3 4 5 7 9 Putting numbers in order. Also allows us to put letters in order (alphabetize). Ascending, smallest first. Descending, largest first.

! INTRODUCING! The Sort WARNING This is a thought area, do not enter without a functioning brain.

Pseudo Code Pseudo Code for Sort: Find the smallest element. Put it in the firstest place. Repeat, ignoring the old firstest element.

Sorting Pseudo Code, Refined Find the smallest number: Assume the first number is the smallest. Compare it to each of the numbers remaining in the array. If any numbers are smaller than the first number, swap them with the first position. Now you know the smallest number is at the beginning of the array.

Refined Pseudo Code, Continued Now that you know the smallest number is at the beginning Repeat this process ignoring elements you ve already put in the proper place. When you ve done this with all the elements, the array is sorted.

Refining the Psuedo Code Again For each element in the array Compare each element in the array to the first element. If the first element is bigger, swap the current element with the first element

A Picture 9 6 3 2 5 6 9 3 2 5 3 9 6 2 5 2 9 6 3 5 2 9 6 3 5 2 9 6 3 5 6 9 3 5 3 9 6 5 3 9 6 5 2 3 9 6 5 6 9 5 5 9 6 2 3 5 9 6 6 9 2 3 5 6 9 2 3 5 6 9

Still More Psuedo Code for i = beginning to the end for j = i to end if (i th > j th) then swap the j th and i th endif next j next i

VB Code for Sorting For i = 1 To 10 For j = i To 10 If (arr(i) > arr(j)) Then temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Next j Next i

Question If I m starting the insertion sort what would my array look like on the next value for i? 8 9 5 4 2 1 ->? A. 9 8 5 4 2 1 B. 1 9 5 4 2 8 C. 1 8 5 4 9 2 D. 1 2 4 5 8 9 E. None of the Above

Two Dimensional Arrays What if we wanted to hold a table of information? We can create arrays that form a grid of locations, something like a spreadsheet. These are also referred to as a Matrix

Matrix Illustration 1 2 3 4 5 6 1 2 3 4 5

Declaring a Two Dimensional Array Syntax as a one dimensional array with additional term Dim grades(1 to 5, 1 to 6) as Single This declaration will give us 5 rows and 6 columns to keep data in. Each row might be a student, each column a grade.

1 2 3 4 5 Grade 2D Array Grade Grade Grade Grade Grade Grade 1 2 3 4 5 6 Student Student Student Student Student

Dim grades(1 to 5, 1 to 6) as Single Dim i as Integer, j as Integer For i = 1 to 5 For j = 1 to 6 grades(i,j) = 0 Next j Next i Loops and Two Dimensional Arrays Zeroing out a two dimensional array.

Question To display the values in the diagonal of a two dimensional array, which code could I use? A. For k = 1 to 10 B. For k = 1 to 10 For j = 1 to 10 MsgBox arr(k,k) if(k = j) then Next k MsgBox arr(j,k) end if next j next k C. Either One D. Neither One