© Janice Regan, CMPT 128, January 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
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.
Introduction to C Programming
Programming and Data Structure
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Strings.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9: Arrays and Strings
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
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.
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.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
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
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
 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.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
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?
Chapter 7: User-Defined Functions II
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Computer Programming BCT 1113
7 Arrays.
CISC181 Introduction to Computer Science Dr
7 Arrays.
Arrays and Matrices.
Presentation transcript:

© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays

© Janice Regan, CMPT Simple and Composite Variables  We have studied simple variables  A simple variable describes a single value  A simple variable has an identifier  A simple variable has a type that describes the properties of the value of the variable, the permissible operations for the variable, and the representation of the variable in computer memory  We can also have composite variables  These variables describe a group of values  Arrays: all values in the group have the same type  Structures: different values in the group can have different types

© Janice Regan, CMPT Composite Variables  composite variables describe a group of values  1 dimensional arrays or variables of a particular type (all entries must have the same type)  multi dimensional arrays or variables of a particular type (all entries must have the same type)  Structures containing groups of variables that may be of different types  Composite variables can  Used as arguments of functions  Used as return values of functions

© Janice Regan, CMPT One-Dimensional (1-D) Arrays  An array is an indexed data structure  All variables stored in an array are of the same data type  An element of an array is accessed using the array name and an index or subscript  The name of the array is the address of the first element and the subscript is the offset  In C++, the subscripts always start with 0 and increment by 1

© Janice Regan, CMPT Declaration of a 1-D array  An array is defined using a declaration statement. type array_name[size];  allocates memory for size elements  subscript of first element is 0  subscript of last element is size-1  size must be a constant

© Janice Regan, CMPT Example Array Declaration in C++ int list[10];  allocates memory for 10 integer variables. Ten adjacent locations in memory are allocated  subscript of first element is 0  subscript of last element is 9  C++ does not perform any bounds checking on arrays list[0] list[1] list[2] list[3] list[4] list[5] list[6] list[7] list[9] list[8]

© Janice Regan, CMPT Example Array Declaration in C ++ double v2[6];  allocates memory for 6 double variables. Memory is in a single block  subscript of first element is 0  subscript of last element is 5  C++ does not perform any bounds checking on arrays v2[0] v2[1] v2[2] v2[3] v2[4] v2[5]

© Janice Regan, CMPT Avoid a common problem (1)  C++ does not perform any bounds checking on arrays  This means that you can accidentally change the values of other variables by changing a value you refer to as an element of the array, which is not actually part of the array int myArray[5] = {0}; int count = 6; myArray[5] = 3;

© Janice Regan, CMPT Avoid a common problem (2) int myArray[5] = {0}; int count = 6; After these declarations memory looks like myArray[5] = 3; After the assignment statement above myArray[0] 0 myArray[1] 0 myArray[2] 0 myArray[3] 0 myArray[4] 0 count 6 myArray[0] 0 myArray[1] 0 myArray[2] 0 myArray[3] 0 myArray[4] 0 count 3

© Janice Regan, CMPT Avoid a common problem (3)  C++ does not perform any bounds checking on arrays  By changing an element of the array myArray that is outside the memory area allocated to the array we have changed the value of a completely different variable  It is imperative that you be very careful to avoid using elements of the array that are not within the declared array.

© Janice Regan, CMPT Initializing 1-D Arrays  Arrays can be initialized at the time they are declared.  You can specify individual values for each element of an array // put 0.15 into element 0, 0.25 into element 1 // and 0.3 into element 2 of the array taxrate double taxrate[3] ={0.15, 0.25, 0.3}; // put one character in each element of the array char list[5] = {‘h’,’e’,’l’,’l’,’o’};

© Janice Regan, CMPT Initializing 1-D Arrays  Declare your array without specifying the size  The length of the array will be determined by the number of elements you initialize // the size of array s is 3 // because 3 elements are initialized int s[ ] = {5,0,-5}; l

© Janice Regan, CMPT Initializing 1-D Arrays  You can specify individual values for the first n elements of an array.  In this case the remaining unspecified elements will be zero (or equivalent) // values in array taxrate will be // {0.15, 0.25, 0.3, 0.0, 0.0, 0.0} double taxrate[6] ={0.15, 0.25, 0.3};

© Janice Regan, CMPT Initializing 1-D Arrays  You can specify individual values for the first n elements of an array.  In this case the remaining unspecified elements will be zero (or equivalent) // values in array name will be // {‘J’, ‘a’, ‘n’, ‘i’, ‘c’, ‘e’, ‘\0’, ‘\0’, ‘\0’, ‘\0’} // Two ways to initialize char name[10] ={‘J’, ‘a’, ‘n’, ‘i’, ‘c’, ‘e’}; OR char name[10] = “Janice”;

© Janice Regan, CMPT Initializing 1-D Arrays  You can specify that all elements of an array should be 0 // assigns zero to all 100 elements // first element is initialized to 0.0 // remaining elements are initialized to 0. double vector[100] = {0.0};

© Janice Regan, CMPT Initializing 1-D Arrays  Avoid a common error  You cannot specify that all elements of an array should be 1.0 (or any other non-zero value) in a declaration statement, by declaring only the first value // first element is initialized to 1.0 // remaining elements are initialized to 0.0 double vector[100] = {1.0};

© Janice Regan, CMPT Initializing a 1-D array  You can initialize a simple variable either in the declaration statement, or using separate assignment statements following the declaration statements.  You can also initialize the values of the elements of an array using separate assignment statements following the declaration of the array  A for loop (a counting loop since we know how many elements need initializing) is the most natural way to initialize or otherwise manipulate an array

© Janice Regan, CMPT Initializing or using 1-D arrays int myinteger[100]; int index; // Initialize each element of array myinteger to 1 for(index=0; index<100; index++) { myinteger[index] = 1; }

© Janice Regan, CMPT Using a 1-D array for loops are often used to assign values to the elements of an array const int TEN =10.0; double mydouble[TEN] = {0.0}; int index; for(index=0; index<TEN; index++) { list[index] += sqrt( index) * TEN; }

© Janice Regan, CMPT Putting data into a 1-D Array  Another common way of assigning values to the elements of an array is to read data values from a file directly into the array  Each value read from the file is assigned to a single array element (for example motion[6])  A single value stored in location i in the array time is referred to as time[i]  Note that checks to determine the file was opened correctly and that data was read correctly have been omitted from the example, they should not be omitted from your code

© Janice Regan, CMPT Array Input from a data file int k; double time[10]; double motion[10]; ifstream sensor3Stream; sensor3Stream.open(“sensor3.txt”); for(k=0; k<10; k++) { sensor3Stream >> time[k] >> motion[k]; }

© Janice Regan, CMPT Array Input from a data file int k; double time[10]; double motion[10]; FILE *sensor3FilePointer; sensor3FilePointer = fopen(“sensor3.txt”); for(k=0; k<10; k++) { fscanf ( sensor3FilePointer, %lf %lf“, time[k], motion[k] ); }

© Janice Regan, CMPT Arrays as function parameters  Arrays, or parts of arrays, can be passed as arguments to functions.  An element of an array can be used as a simple variable parameter It can be passed by value or by reference  An entire array can be used as a parameter of a function It can only be passed by reference

© Janice Regan, CMPT D Array elements as parameters  Individual elements of an array can be passed by value as arguments that are of simple variable types.In the illustrated function call  Formal parameter par1 of function donothing has the value of element 2 of the array (array[2])  Formal parameter par2 of function donothing has the value of element 4 of the array (array[4]) void donothing( int par1, int par2 ); int main(void) { int array[5] = {1,2,3,4,5}; donothing(array[2], array[4]); return 0; }

© Janice Regan, CMPT D arrays as parameters  Arrays are always passed to a function by reference  The array name is the address of the first element of the array  The maximum size of the array must be specified at the time the array is declared.  The actual number of array elements that are used will vary, so the actual size of the array is usually passed as another argument

© Janice Regan, CMPT Function with array argument double max( double array[ ], int actual_size ) { double max; int index; max = 0; for(index = 0; index < actual_size; index++) { if(max < array[index]) { max = array[index]; } return max; }

© Janice Regan, CMPT Using the function double max( double array[ ], int actual_size ); int main(void) { ifstream exp1; double x[100]; int count=0; exp1.open("inputfile.dat"); while( exp1 >> x[count] && count < 100) { count++; } cout << "Maximum value: “ << max(x, count)<< endl; exp1.close(); return 0; }