Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Programming and Data Structure
1 C++ Syntax and Semantics The Development Process.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Kernighan/Ritchie: Kelley/Pohl:
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
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.
Programming with Collections Collections in Java Using Arrays Week 9.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Lesson 7 Arrays CS 1 Lesson 7 -- John Cole1. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored.
Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
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.
Chapter 8 Arrays and Strings
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
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.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 7: Arrays. 7.1 Arrays Hold Multiple Values.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
New Structure Recall “average.cpp” program
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored? If you have 40 values to work with do you need 40 variables? To solve this issue there is a storage structure called ‘array’. An array can be likened to a list or a table that contains multiple values of like data types. A ‘single dimension’ array is like a list. Syntax of declaration statement: int grades[10]; int is the data type of values stored in the array 10 is the maximum number of items that can be stored and is referred to as the size of the array. grades is the name of the array [like a variable name] and subject to the same naming rules as those for variables

Things to Note About Arrays The different values stored in an array are called ‘ elements ’ Elements are referenced by the name of the array and a sequence number [Ex. grades[n] where n is an integer] The first element is always element number 0! So grades[0] is the first grade stored in array grades and grades[5] is actually the 6 th grade stored in the array. (Now you know why CS folk start counting at 0) Array elements are stored sequentially in memory. Arrays can be of any valid data type (double, float, char, int, etc.) The array will occupy its size * sizeof(its data type) bytes of sequential memory.

Visual Concept int grades[10]; // 10 is the size of the array Grades[0] grades[5] grades[9] 0, 5 and 9 above are each referred to as a subscript or an index Subscripts or indeces are used to reference a particular element in an array. Example of assignment statements involving arrays: i = 3; grades[i] = 88; grades[i + 1] = 100; grades[0] = 96; grades[3] is referred to verbally as grades sub

Example of Simple Programs Using an Array for Data Storage (also – how to find the smallest or largest value in an array)

Initializing Arrays Arrays can be initialized just like other variables. We will use this feature seldom in this course except for ‘array constants’ but you need to understand the nuances of array initialization. Syntax: data_type array_name[size] = {element0, element1, …}; The number of elements in {}s cannot exceed size. The list of values can be less than size in which case the array is partially initialized. If an array is partially initialized the part not initialized is always set to 0. If an array is not initialized the memory storage contains just what other uninitialized variables contain – junk.

Parallel Arrays Parallel arrays are groups of arrays having the same size which may or may not necessarily have the same data type. Data stored in the same subscripted cell in each array in the group are all related. Ex: int employee_id[20]; float hrly_wage_rate[20]; bool full_time[20]; employee_id[8] and hrly_wage_rate [8] and full_time [8] would all refer to the same employee.

No No’s! Take great care to not run off either end of an array A loop is required to assign the values stored in one array to another array int array1[SIZE], array2[SIZE}; array 1 = array2;// NO NO NO! You CANNOT compare one array to another. You can only compare elements of the arrays if (array 1 == array2) // NO NO NO! You must step through the elements of an array to output it cout << array;// NO NO NO!

Stepping Through an Array with a Known Number of Valid Elements If you know how many elements are stored in an array a ‘for’ loop is the best control structure to use to access each element in the array. Ex. Suppose we want to output to the console the contents of an array. Via code earlier in the program we have determined that the array contains n valid data entries. for (int k = 0; k < n; ++k) cout << array[k] << endl;

Reading an Unknown Number of Data Values into an Array To read an unknown number of values into an array from a file or the console you CANNOT use a ‘for’ loop. Either a ‘while’ or ‘do.. while’ must be used with a check to make sure that you do not try to store data outside the array storage area. Most often you will use a ‘while’ loop if a ‘for’ loop is not feasible. This loop should count the number of items actually stored in the array so that ‘for’ loops can be using for any further processing