C++ winter2008(by:J.Razjouyan)

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

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.
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.
Array What it is. How to use it How to declare it How to initialize it.
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.
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.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
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.
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.
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.
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.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Copyright © 2012 Pearson Education, Inc. Chapter 7: Arrays.
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 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.
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.
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Topics: Templates Exceptions
Chapter 7: Arrays.
CHAPTER 3 ARRAYS.
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Array An “average.cpp” program
Programming fundamentals 2 Chapter 1:Array
New Structure Recall “average.cpp” program
Student Book An Introduction
Chapter 7: Arrays.
7 Chapter Arrays.
Object-Oriented Programming (OOP) Lecture No. 32
DATA STRUCTURE : DAT JENIS DATA DAN JENIS DATA ABSTRAK (4JAM)
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Array Data Structure Chapter 6
One-Dimensional Array Introduction Lesson xx
7 Arrays.
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Data type List Definition:
Review for Final Exam.
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.
4.9 Multiple-Subscripted Arrays
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Review for Final Exam.
7 Arrays.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
Array Data Structure Chapter 6
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
Array What it is. How to use it How to declare it How to initialize it.
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
ㅎㅎ Fourth step for Learning C++ Programming Call by value
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

C++ winter2008(by:J.Razjouyan) C++ Arrays C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Memory and Arrays 4 bytes Each int is 4 bytes foo[0] foo[1] int foo[6]; foo[5] C++ winter2008(by:J.Razjouyan)

C++ Arrays start at 0 !!!!!!! The first element is the 0th 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++ winter2008(by:J.Razjouyan)

foo[17] foo[i+3] foo[a+b+c] 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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) Array Example int main(void) { int facs[10]; for (int i=0;i<10;i++) facs[i] = factorial(i); cout << "factorial(" << i << ") is " << facs[i] << endl; } Available on the web in code/arrays/factorials.cpp C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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. Example code available in code/arrays/arraydecls.cpp C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

An array printing function Can pass an array as a parameter. You don't have to say how big it is! void print_array(int a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } On the course home page in code/arrays/printarray.cpp C++ winter2008(by:J.Razjouyan)

What if we want to print doubles? The print_array function is declared to handle only ints. 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++ winter2008(by:J.Razjouyan)

print_array() for doubles void print_array(double a[], int len) { for (int i=0;i<len;i++) cout << "[" << i << "] = " << a[i] << endl; } On the course home page in code/arrays/printarray.cpp C++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 2-D Array: int A[3][4] Col 0 Col 1 Col 2 Col 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++ winter2008(by:J.Razjouyan)

2-D Memory Organization { 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] char A[4][3]; 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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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]; } Available via the course home page in code/arrays/2darray.cpp C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 2-D Array (cont.) You don’t need to specify the size of the first dimension You must include all other sizes! 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); } Available via the course home page in code/arrays/2darray.cpp C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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! Available via the course home page in code/arrays/2darray.cpp C++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) 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++) { << array_average( grades[i], NumHW ) << endl; Available via the course home page in code/arrays/2darray.cpp C++ winter2008(by:J.Razjouyan)

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++ winter2008(by:J.Razjouyan)

C++ winter2008(by:J.Razjouyan) A Bad Idea int read_array( int a[] ) { int i=0; int val; do { cout << “Enter next value, 0 to end\n“; cin >> 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!). Available via the course home page in code/arrays/badidea.cpp C++ winter2008(by:J.Razjouyan)

C++ does not have bounds checking Memory int a[6]; int foo; a[0] a[1] a[2] a[3] a[4] a[5] foo This is the array This is something else C++ winter2008(by:J.Razjouyan)