Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Advertisements

EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Structure.
Week 8 Arrays Part 2 String & Pointer
1 Various Methods of Populating Arrays Randomly generated integers.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Programming with Collections Collections in Java Using Arrays Week 9.
Repetition Structures: Nested Loops CSC 1401: Introduction to Programming with Java Week 6 – Lecture 2 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Multidimensional Arrays Histograms CSC 1401: Introduction to Programming with Java Week 11 – Lecture 1 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.
Arrays--data structure. Arrays 7 zConsider how you would store five integer values in the memory of the computer. zOne way to do this is to create five.
File Input and Output CSC 1401: Introduction to Programming with Java Week 4 – Lecture 2 Wanda M. Kunkle.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to programming in java Lecture 21 Arrays – Part 1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays An Array is an ordered collection of variables
One-Dimensional Array Introduction Lesson xx
Introduction To Programming Information Technology , 1’st Semester
Arrays .
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS2011 Introduction to Programming I Arrays (I)
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle

2 Arrays An array is a data structure that allows us to store multiple data items of the same type. An array is a data structure that allows us to store multiple data items of the same type. The data items making up the array are stored in consecutive memory locations inside the computer. The data items making up the array are stored in consecutive memory locations inside the computer. The consecutive memory locations, or elements, all share the same name. The consecutive memory locations, or elements, all share the same name. To reference a particular element in the array, we must specify the name of the array and the position number of that element in the array. (Aside: As with Strings, we start counting with 0.) To reference a particular element in the array, we must specify the name of the array and the position number of that element in the array. (Aside: As with Strings, we start counting with 0.)

3 Array Example

4 Initialization routines Initialization routines Initial values known Initial values known int values[] = new int[5]; values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; int values[] = new int[5]; values[0] = 25; values[1] = 11; values[2] = 38; values[3] = 3; values[4] = 17; int values[] = {25, 11, 38, 3, 17}; int values[] = {25, 11, 38, 3, 17};

5 Array Example Initialization routines: Initialization routines: Initial values not known Initial values not known Initialize element contents to 0 to “play it safe” Initialize element contents to 0 to “play it safe” int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; int values[] = new int[5]; for (int i = 0; i < values.length; i++) values[i] = 0; We’ll assign real values to the array later. We’ll assign real values to the array later. The array “knows” how big it is.

6 Examples of Array Routines Manipulation routines: Manipulation routines: Reading in array values Reading in array values // Read grades into an array “grades” until the user enters // -1, indicating that he is done while ((grade != -1) && (counter < Size)){ out.write("Enter a grade, -1 to end: "); grade = in.readfloat(); if (grade != -1) { grades[counter] = grade; counter++; } // end if } // end while // Read grades into an array “grades” until the user enters // -1, indicating that he is done while ((grade != -1) && (counter < Size)){ out.write("Enter a grade, -1 to end: "); grade = in.readfloat(); if (grade != -1) { grades[counter] = grade; counter++; } // end if } // end while

7 Examples of Array Routines Manipulation routines: Manipulation routines: Printing array values Printing array values // Print the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) out.writeln(grades[i]); // Print the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) out.writeln(grades[i]);

8 Examples of Array Routines Manipulation routines: Manipulation routines: Summing array values Summing array values // Total the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) total += grades[i]; // Total the grades in array “grades” consisting of // “counter” values for (i = 0; i < counter; i++) total += grades[i];

9 Examples of Array Routines Manipulation routines: Manipulation routines: Averaging array values Averaging array values // Average the grades in array “grades” consisting of // “counter” values and display the average average = total/counter; out.writeln("\nAverage of grades entered: " + average + "\n"); // Average the grades in array “grades” consisting of // “counter” values and display the average average = total/counter; out.writeln("\nAverage of grades entered: " + average + "\n");

10 Sample Program Let’s look at a sample program that demonstrates the usage of these routines: Let’s look at a sample program that demonstrates the usage of these routines: arrayRoutines.java arrayRoutines.java arrayRoutines.java