C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

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.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
Array What it is. How to use it How to declare it How to initialize it.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Multiple-Subscripted Array
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
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.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Computer programming Lecture 5. Lecture 5: Outline Arrays [chap 7 – Kochan] –The concept of array –Defining arrays –Initializing arrays –Character arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Group of consecutive memory locations –Same name and type To refer to an element, specify.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
POINTERS.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 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.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
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.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
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.
Chapter 8: Arrays. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
CHAPTER 3 ARRAYS.
New Structure Recall “average.cpp” program
Object-Oriented Programming (OOP) Lecture No. 32
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
7 Arrays.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
C++ winter2008(by:J.Razjouyan)
Arrays Arrays A few types Structures of related data items
C++ Pointers and Strings
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Presentation transcript:

C++ Spring 2000 Arrays1 C++ Arrays

C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element of the array. The contents of each element are of the same type. –Could be an array of int, double, char, … We can refer to individual elements by giving the position number (index) of the element in the array.

C++ Spring 2000 Arrays3 Memory and Arrays int foo[6]; foo[0] foo[1] foo[5] 4 bytes Each int is 4 bytes

C++ Spring 2000 Arrays4 C++ Arrays start at 0 !!!!!!! The first element is the 0 th element! If you declare an array of n elements, the last one is number n-1. If you try to access element number n it is an error! If only millenniums started at 0 …

C++ Spring 2000 Arrays5 Array Subscripts The element numbers are called subscripts. foo[i] Array name subscript A subscript can be any integer expression: These are all valid subscripts: foo[17] foo[i+3] foo[a+b+c]

C++ Spring 2000 Arrays6 Array Example int main(void) { int facs[10]; for (int i=0;i<10;i++) facs[i] = factorial(i); for (int i=0;i<10;i++) cout << "factorial(" << i << ") is " << facs[i] << endl; }

C++ Spring 2000 Arrays7 Declaring An Array element_type array_name[number_of_elements]; element_type can be any C++ variable type. array_name can be any valid variable name. number_of_elements can be an expression.

C++ Spring 2000 Arrays8 Initialization You can initialize an array when you declare it (just like with variables): int foo[5] = { 1,8,3,6,12}; double d[2] = { 0.707, 0.707}; char s[] = { 'R', 'P', 'I' }; You don’t need to specify a size when initializing, the compiler will count for you.

C++ Spring 2000 Arrays9 An array printing function void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } Can pass an array as a parameter. You don't have to say how big it is!

C++ Spring 2000 Arrays10 What if we want to print doubles? The print_array function is declared to handle only int s. We can write another function that can be used to print doubles. We have to write another function (we can't use the same one). –Not really true – this is what templates can do for you!

C++ Spring 2000 Arrays11 print_array() for doubles void print_array(double a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; }

C++ Spring 2000 Arrays12 Which is it? We now have two functions with the same name: void print_array(double a[], int len); void print_array(int a[], int len); This is fine – as long as the prototypes are different everything works. This is called "overloading", using the same name for two (or more) different functions.

C++ Spring 2000 Arrays13 Arrays of char are special C++ provides a special way to deal with arrays of characters: char string1[] = "RPI without PI is like meat without eat"; char arrays can be initialized with string literals.

C++ Spring 2000 Arrays14 Arrays of Arrays You can create an array of arrays: int a[2][2]; for (int i=0;i<2;i++) for (int j=0;j<2;j++) a[i][j] = i+j;

C++ Spring 2000 Arrays15 2-D Array: int A[3][4] Col 0Col 1Col 2Col 3 Row 0 A[0][0]A[0][1]A[0][2]A[0][3] Row 1 A[1][0]A[1][1]A[1][2]A[1][3] Row 2 A[2][0]A[2][1]A[2][2]A[2][3]

C++ Spring 2000 Arrays16 2-D Memory Organization char A[4][3]; A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] A[2][0] A[2][1] A[2][2] A[3][0] A[3][1] A[3][2] A[0] A[1] A[2] A[3] { { { { A is an array of size 4. Each element of A is an array of 3 chars

C++ Spring 2000 Arrays17 2-D Array Example const int NumStudents = 10; const int NumHW = 3; double grades[NumStudents][NumHW]; for (int i=0;i<NumStudents;i++) { for (int j=0;j<NumHW;j++) { cout << “Enter HW “ << j << “ Grade for student number “ << i << endl; cin >> grades[i][j]; }

C++ Spring 2000 Arrays18 2-D Array (cont.) double student_average( double g[][NumHW], int stu) { double sum = 0.0; for (int i=0;i<NumHW;i++) sum += g[stu][i]; return(sum/NumHW); } You don’t need to specify the size of the first dimension You must include all other sizes!

C++ Spring 2000 Arrays19 Another way double array_average( double a[], int len) { double sum = 0.0; for (int i=0;i<len;i++) sum += a[i]; if (len==0) return(0); else return(sum/len); } Division by 0 is bad idea!

C++ Spring 2000 Arrays20 Two ways to do it // Using student_average with grades for (int i=0;i<NumStudents;i++) cout << “Student #” << i << “ has average “ << student_average( grades, i ) << endl; -or- // Using array_average with grades for (int i=0;i<NumStudents;i++) { cout << “Student #” << i << “ has average “ << array_average( grades[i], NumHW ) << endl;

C++ Spring 2000 Arrays21 Arrays and pass-by-reference Arrays are always passed by reference –Inside a function any changes you make to array values are for keeps! –You can write functions that modify the contents of an array. –You need to make sure that a function knows how big the array is!!!

C++ Spring 2000 Arrays22 A Bad Idea int read_array( int a[] ) { int i=0; int val; do { cout > val; if (val) a[i++] = val; } while (val); return(i); // returns the number or numbers } The problem is that the function might go beyond the size of the array. Result: Segmentation Violation (or worse!).

C++ Spring 2000 Arrays23 C++ does not have bounds checking This is the array This is something else Memory a[0] a[1] a[2] a[3] a[4] a[5] foo int a[6]; int foo;