Arrays Topics Definition of a Data Structure Definition of an Array

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Control Structures Repetition or Iteration or Looping Part II.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
COMP Loop Statements Yi Hong May 21, 2015.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 CSC 211 Data Structures Lecture 4 Dr. Iftikhar Azim Niaz 1.
Control Structures Repetition or Iteration or Looping Part II.
DEVRY CIS 115 F INAL E XAM 3 Check this A+ tutorial guideline at For more classes visit
Chapter VII: Arrays.
Introduction to Computer Programming
Review 1.
UMBC CMSC 104 – Section 01, Fall 2016
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
C++ Programming: CS150 For.
Loop Structures.
New Structure Recall “average.cpp” program
While Loops in Python.
Lecture 07 More Repetition Richard Gesick.
Control Statement Examples
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
While Loops.
The while Looping Structure
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CS1100 Computational Engineering
More Loops.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Coding Concepts (Basics)
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Computer Science 2 Getting an unknown # of …. Into an array.
Starting Out with Programming Logic & Design
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Arrays, Part 1 of 2 Topics Definition of a Data Structure
SELECTIONS STATEMENTS
Arrays I Handling lists of data.
For loops Taken from notes by Dr. Neil Moore
Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
The while Looping Structure
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CISC101 Reminders Assignment 3 due today.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays Topics Definition of a Data Structure Definition of an Array
While Loops in Python.
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Arrays Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization, and Access Program Example Using Arrays

Data Types So far, we have seen only simple variables. Simple variables can hold only one value at any time during program execution, although that value may change. A data structure is a data type that can hold multiple values at the same time. The array is one kind of data structure.

Arrays An array is a group of related data items that all have the same name. Arrays can be of any data type we choose. Each of the data items is known as an element of the array. Each element can be accessed individually.

Array Declaration var numbers = new Array(5) ; The name of this array is "numbers". It does not initialize the array to 0 or any other value. They contain garbage.

Initializing and Modifying Elements Each element in an array has a subscript (index) associated with it. We can put values into the array using indexing. numbers[0] = 5 ; numbers[1] = 2 ; numbers[2] = 6 ; numbers[3] = 9 ; numbers[4] = 3 ; numbers 0 1 2 3 4 5 2 6 9 3 numbers 0 1 2 3 4

Accessing Array Elements For this class, subscripts are integers and always begin at zero. Values of individual elements can be accessed by indexing into the array. For example, alert("The third element = " + numbers[2]); would give the output The third element = 6.

Accessing Array Elements A subscript can also be an expression that evaluates to an integer. numbers[(a + b) * 2] ; Caution! It is a logical error when a subscript evaluates to a value that is out of range for the particular array. Some language will handle an out-of-range error gracefully and some will not.

Filling Large Arrays Since many arrays are quite large, initializing each element individually can be impractical. Large arrays are often filled using a for loop. for ( i = 0; i < 100; i++ ) { values [ i ] = 0; } would set every element of the 100 element array "values" to 0.

More Declarations var scores = new Array(39); var gradeCount = new Array(5); Declares two arrays: scores and gradeCount. Neither array has been initialized. scores contains 39 elements (one for each student in a class). gradeCount contains 5 elements (one for each possible grade, A - F).

Example Using Arrays Problem: Find the average test score and the number of A’s, B’s, C’s, D’s, and F’s for a particular class.

Example Using Arrays . <body> <script type="text/javascript"> <!-- var i; var scoreTotal = 0; var scores = new Array(39); var gradeCount = new Array(5); var averageScore; PrintInstructions();

Example Using Arrays /* Initialize grade counts to zero */ for (i = 0; i < 5; i++) { gradeCount[i] = 0; } /* Fill score array with scores */ for (i = 0; i < 39; i++) scores[i] = parseInt(prompt("Enter score:"));

Example Using Arrays /* Calculate score total and count number of each grade */ for (i = 0; i < 39; i++) { scoreTotal += scores[i]; switch (Math.floor(scores[i]/10)) case 10: case 9: gradeCount[4]++; break; case 8: gradeCount[3]++; case 7: gradeCount[2]++; case 6: gradeCount[1]++; default: gradeCount[0]++; }

Example Using Arrays average = FindAverage (scoreTotal, 39); /* Display the results to the user */ string = "The class average is: "; string += average.toFixed(2) + "%"; string += "\nThe grade distribution is:\n"; string += gradeCount[4] + " A's\n"; string += gradeCount[3] + " B's\n"; string += gradeCount[2] + " C's\n"; string += gradeCount[1] + " D's\n"; string += gradeCount[0] + " F's"; alert(string); //--> </script> </body>

Example Using Arrays /*********************************************************** ** PrintInstructions - prints the user instructions ** Inputs: None ** Outputs: None ************************************************************/ function PrintInstructions() { var string; string = "This program calculates the average score\n"; string += "for a class of 10 students. It also reports the\n"; string += "number of A's, B's, C's, D's, and F's. You will\n"; string += "be asked to enter the individual scores.\n"; alert(string); }

Example Using Arrays /****************************************************** ** FindAverage - calculates an average ** Inputs: sum - the sum of all values ** num - the number of values ** Outputs: the computed average ******************************************************/ function FindAverage(sum, num) { var average; /* Make sure we don’t do division by 0 */ if (num != 0) average = sum / num; } else average = 0; return average;

Improvements ? We’re trusting the user to enter valid grades. Let’s add input error checking. For this program, the highest possible score is 110. If we aren’t handling our array correctly, it’s possible that we may be evaluating garbage rather than valid scores. We’ll handle this by adding all the cases for F’s (0 - 59) to our switch structure and using the default case for reporting errors.

Improved Input with Error Checking /* Fill score array with scores */ for (i = 0; i < 39; i++) { scores[i] = parseInt(prompt("Enter score:")); /* Make sure score is within correct range */ while (scores[i] < 0 || scores[i] > 110) alert("Your number must be between 0 and 110."); }

Improved switch() statment switch (Math.floor(scores[i]/10)) { case 10: case 9: gradeCount[4]++; break; case 8: gradeCount[3]++; case 7: gradeCount[2]++; case 6: gradeCount[1]++; case 5: case 4: case 3: case 2: case 1: case 0: gradeCount[0]++; default: alert("Error in score!"); }

Working Version of Grades Program A working version of the improved program can be found at: http://userpages.umbc.edu/~dblock/arrays.html Note that it will ask for only 10 scores rather than 39.