Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
Introduction to C Programming
Chapter 8: Arrays.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
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.
CIS 130 Numeric Arrays Chapter 10. What is an array? Collection of data storage locations –stored adjacently in memory All the pieces of data share a.
Chapter 10.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
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.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 12: Arrays.
C++ for Engineers and Scientists Third Edition
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
Problem: A company employs a group of fifty salespersons (with reference numbers ) who are paid commission if their individual sales exceeds two-thirds.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© 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.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Windows Programming Lecture 03. Pointers and Arrays.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Two Dimensional Array Mr. Jacobs.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Multi-Dimensional Arrays
Array Data Structure Chapter 6
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

Chapter 9 – One-Dimensional Numeric Arrays

Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer constant or expression following identifier –Subscript or index u Loops commonly used for manipulation Lesson 9.1

One-Dimensional Arrays u Declaration indicates name and reserves space for all elements u Values assigned to array elements using assignment statements u Array names classified as identifiers u Programmer sets size of array explicitly Lesson 9.1

Array Length u Determined by expression or value enclosed in brackets in declaration u General form for declaration type name[value]; u Value in brackets can be constant variable, expression, or literal Lesson 9.1 const int N = 26; double b[N]; int a[25]; int b[5+2];

Array Length u Must be integer constant greater than 0 u Only integer type variables (with modifiers) –int, char –signed, unsigned, short, long u Always within brackets following identifier u Examples: int c[32]; int c[-25], b[43.5]; Lesson 9.1 Valid Invalid

Array Subscripts u First index or subscript is 0 u int a[2]; –Data type of elements is int –Name of array is a –Number of elements is 2 –Valid subscripts are 0 and 1 u Address as a[0] and a[1] Lesson 9.1

Printing Array Elements u Use cout u Treat like single variable u Print one element at a time u Example: cout << "a[0] = " << a[0] << endl; Lesson 9.1

Initialization u In declarations enclosed in curly braces Lesson 9.2 int a[5] = {11,22}; Declares array a and initializes first two elements and all remaining set to zero int b[ ] = {1,2,8,9,5}; Declares array b and initializes all elements and sets the length of the array to 5

Working With Arrays u Remember subscripts must calculate to an integral value u Normally use loop to control array processing u Common loop is for loop –Start at zero –Continue while loop control variable < N t N is the size of the array Lesson 9.2

Using An Array u Declare int test[3]; –sets aside 3 storage locations u use index to reference the variables test[0] = 86; test[1] = 92; test[2] = 90; Lesson 9.2

Example u Declare and fill an array which will hold 5 double numbers double num[5]; for (int a = 0; a > num[a]; Initialize by reading from the keyboard Initialize by assigning values in loop num[a] = 0; Lesson 9.2

Input/Output u Typically use files u Reading from file, number of elements ? –Guard against reading past end of file –Use function eof( ) which returns 1 when end u Reading from file, number of elements known Lesson 9.3

Loop to Read Data Into an Array for (j = 0; !infile1.eof ( ); j++) { infile1 >> a[j]; } Example of for loop Lesson 9.3 Number of elements unknown!

Loop to Read Data Into an Array length = 0; infile >> data; while ((length < max_array_size) && ! infile.eof ( )) { a[length] = data; ++length; infile >> data; } Lesson 9.3 Example of while loop Number of elements unknown!

File Input with Known Number of Elements First line of file contains number of array elements. infile1 >> num_elem; for (j = 0; j < num_elem; j++) { infile1 >> a[j]; }

File Input – Sentinel Value u Particular predefined value contained in file that indicates end of data group u Loop and read data value by value until sentinel is read for (j = 0; a[j] != -1; j++) { infile1 >> a[j]; } Where –1 is the sentinel value.

Loop to Print Data From Array (scores is array of 20 test scores) for (int j = 0; j < 20; ++j) { cout << scores[ j ] << endl; } Lesson 9.3

Arrays and Functions u Pass address of array to function instead of element values u Function declaration –Data type and empty brackets int[ ] u Function call –Array name with no brackets x u Function header –Data type, name, and empty brackets int x[ ] Lesson 9.4

Passing Array Information u Three pieces of information –Size of single array element t Indicated by data type –Address of array t Indicated by array name in function call –Location to store array address t Indicated by identifier followed by brackets in function header Lesson 9.4

General Form Lesson 9.4 rtype function (type [ ], int); function (array, num); rtype function (type b[ ], int num_elem) Declaration Call Header Function return typeFunction name Type of values stored in array Type of second argument Array name in calling function Number of array elements b is identifier used to represent array within function num_elem used to represent number of array elements in function

Classes with Array Data Members u Automatically made accessible to public class member functions u Use enumeration to size data member arrays –Comma-separated list of identifiers –Identifier automatically assigned constant integer value –Value assigned depends on order in enumeration list Lesson 9.5

Enumeration u Keyword enum u Makes code more readable u Example: enum {sun = 1, mon, tues, wed, thur, fri, sat}; –Assigns integer constant 1 to sun, 2 to mon, etc. u Example of usage: int day; day = wed; Lesson 9.5 Same as day = 4;

Find Smallest Value in Array small = scores [0]; for (int j = 1; j < 20; ++j) { if (scores[ j ] < small) small = scores [ j ]; } Lesson 9.5

Arrays of Objects u Declared using class name, object name, and brackets enclosing integer constant Vehicle truck[3]; –Three objects (truck[0], truck[1], truck[2] of class Vehicle u Call member function –object, dot operator, and function name truck[0].set_data (50, 2, 3); Lesson 9.6

Arrays of Objects u Good when multiple pieces of information are linked u Use assignment statement to copy one object array element to another truck[1] = truck[0]; Lesson 9.6

Summary u Create and manipulate arrays u Trace and debug loops that manipulate arrays u Reserve memory during program execution u Use arrays to solve problems Learned how to: