CHAPTER 2 Arrays and Vectors.

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Advertisements

Multiple-Subscripted Array
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
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.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Fundamental Programming: Fundamental Programming Introduction to C++
Objective: Students will be able to: Declare and use variables Input integers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Module 1: Array ITEI222 - Advance Programming Language.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
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.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 Exercises.
Pointers What is the data type of pointer variables?
Lecture 11 Multi-dimensional Arrays
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional 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.
Two Dimensional Array Mr. Jacobs.
Array, Strings and Vectors
Command Line Arguments
Functions and an Introduction to Recursion
Chapter 1. Introduction to Computers and Programming
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
Pointers and Pointer-Based Strings
Introduction to C++ October 2, 2017.
Student Book An Introduction
Multi-dimensional Array
ARRAYS An array is a sequence of data item of homogeneous value(same type). Arrays are of two types: 1. One-dimensional arrays 2. Multi-Dimensional arrays.
JavaScript: Functions.
Pointers Psst… over there.
Lecture 8 – 9 Arrays with in a class
Pointers Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Engineering Problem Solving with C++, Etter/Ingber
One-Dimensional Array Introduction Lesson xx
Arrays Skill Area 315 Part A
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
CS1201: Programming Language 2
Templaets It is a new concept which enables us to define “generic class “ and “generic function” to provide the “ generic programming” We are familiar.
1020: Introduction to Programming Mohamed Shehata November 22, 2017
CS-161 Computer Programming Lecture 14: Arrays I
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
CISC181 Introduction to Computer Science Dr
4.9 Multiple-Subscripted Arrays
Lecture 12 Oct 16, 02.
2-d Arrays.
Multidimensional array
Arrays of Two-Dimensions
CS1201: Programming Language 2
Functions and an Introduction to Recursion
Passing Arrays to functions
Life is Full of Alternatives
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
CHAPTER 2 Arrays and Vectors.
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.
Functions Imran Rashid CTO at ManiWeber Technologies.
Using string type variables
Pointers & Functions.
TOPIC: FUNCTION OVERLOADING
C++ Array 1.
Arrays Imran Rashid CTO at ManiWeber Technologies.
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.
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

CHAPTER 2 Arrays and Vectors

Introduction to Arrays An array is a sequence of variable that can store value of one particular data type. An array is a consecutive group of memory locations that all have the same type. To refer to a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array.

Declaring Arrays Format: type array_name[size]; Consider this code. int test[85]; The above code creates an array which can hold 85 elements of int type.

Array Elements The maximum number of elements an array can hold depends upon the size of an array. Consider this code below: int age[5]; This array can hold 5 integer elements. Notice that the first element of an array is age[0] not age[1]. This array has 5 elements and notice fifth is age[4] not age[5].

Array Initialization Array can be initialized at the time of declaration. For example float test[5] = {12, 3, 4, -3, 9}; It is not necessary to write the size of an array during array declaration. float test[] = {12, 3, 4, -3, 9};

Example Program Enter 5 numbers: 4 -3 5 2 First number: 4 #include <iostream> using namespace std; int main() { int n[5]; cout<<"Enter 5 numbers: "; for (int i = 0; i < 5; ++i) { cin>>n[i]; } cout<<"First number: "<<n[0]<<endl; cout<<"Last number: "<<n[4]; return 0; Output: Enter 5 numbers: 4 -3 5 2 First number: 4 Last number: 0

Passing Arrays to functions Arrays can be passed to a function as an argument. Consider this example to pass one-dimensional array to a function: #include <iostream> using namespace std; void display(int marks[5]); int main() { int marks[5] = {88, 76, 90, 61, 69}; display(marks); return 0; } void display(int m[5]) { cout<<"Displaying marks: “ <<endl; for (int i = 0; i <5; ++i) cout<<"Student "<<i+1<<": “ <<m[i]<<endl; }

Passing Arrays to functions Output: Displaying marks: Student 1: 88 Student 2: 76 Student 3: 90 Student 4: 61 Student 5: 69 When an array is passed as an argument to a function, only the name of an array is used as argument. display(marks); Also notice the difference while passing array as an argument rather than variable. void display(int m[5]); The C++ programming language handles passing array to a function in this way to save memory and time.

Multidimensional Arrays  C++ allows programmer to create array of an array known as multidimensional arrays. Consider this example: int x[3][4]; Here, x is a two dimensional array. This array can hold 12 (3x4) elements.

Multidimensional Arrays You can think this array as table with 3 row and each row has 4 column.

Multidimensional Array Initialization You can initialize a multidimensional array in more than one way. Consider this examples to initialise two dimensional array. int test[2][3] = {2, 4, -5, 9, 0, 9}; Better way to initialise this array with same array elements as above. int test[2][3] = { {2, 4, 5}, {9, 0 0}};

Example Program #include <iostream> using namespace std; int main() { int test[3][2] = { {2, -5}, {4, 0}, {9, 1} }; for(int i = 0; i < 3; ++i) for(int j = 0; j < 2; ++j) cout<< "test["<< i << "][" << ;j << "] = " << test[i][j]<<endl; } return 0; Output: test[0][0] = 2 test[0][1] = -5 test[1][0] = 4 test[1][1] = 0 test[2][0] = 9 test[2][1] = 1