Docsity

One-Dimensional and Multi-Dimensional Arrays in C++, Slides of Advanced Computer Programming

Partial preview of the text

Download One-Dimensional and Multi-Dimensional Arrays in C++ and more Slides Advanced Computer Programming in PDF only on Docsity!

Chapter 12

Arrays

Chapter 12 Topics

• Declaring and Using a One-Dimensional Array

• Passing an Array as a Function Argument

• Using const in Function Prototypes

• Using an Array of struct or class Objects

• Using an enum Index Type for an Array

• Declaring and Using a Two-Dimensional Array

• Two-Dimensional Arrays as Function

Parameters

• Declaring a Multidimensional Array

Structured Data Type

A structured data type is a type that

– Stores a collection of individual

components with one variable name

– And allows individual components to be

stored and retrieved by their position

within the collection

Declare variables to

store and total 3 blood pressures

int bp1, bp2, bp3;

int total;

5

bp1 bp2 bp

cin >> bp1 >> bp2 >> bp3;

total = bp1 + bp2 + bp3;

One-Dimensional Array Definition

An array is a structured collection of components (called

array elements), all of the same data type, given a single

name, and stored in adjacent memory locations

The individual components are accessed by using the array

name together with an integral valued index in square

brackets

The index indicates the position of the component within

the collection

Another Example

• Declare an array called temps which will hold up

to 5 individual float values

float temps[5]; // Declaration allocates memory

8

temps[0] temps[1] temps[2] temps[3] temps[4]

number of elements in the array

indexes or subscripts

Base Address

Yet Another Example

• Declare an array called name which will hold up to

10 individual char values

char name[10]; // Declaration allocates memory

10

number of elements in the array

name[0] name[1] name[2] name[3] name[4]..... name[9]

Base Address

Assigning Values to Individual Array Elements

float temps[5]; int m = 4; // Allocates memory

temps[2] = 98.6;

temps[3] = 101.2;

temps[0] = 99.4;

temps[m] = temps[3] / 2.0;

temps[1] = temps[3] - 1.2;

// What value is assigned?

temps[0] temps[1] temps[2] temps[3] temps[4] 11

Now what values are printed?

float temps[5]; // Allocates memory

Int m;

for (m = 4; m >= 0; m--)

cout << temps[m] << endl;

temps[0] temps[1] temps[2] temps[3] temps[4] 13

Variable Subscripts

float temps[5]; // Allocates memory

int m = 3;

What is temps[m + 1]?

What is temps[m] + 1?

14

temps[0] temps[1] temps[2] temps[3] temps[4]

Initializing in a Declaration

int ages[5] ={ 40, 13, 20, 19, 36 };

for (int m = 0; m < 5; m++)

cout << ages[m];

16

ages[0] ages[1] ages[2] ages[3] ages[4]

Passing Arrays as Arguments

  • In C++, arrays are always passed by

reference

  • Whenever an array is passed as an

argument, its base address is sent to

the called function

Using Arrays as Arguments to Functions

Generally, functions that work with arrays

require 2 items of information

  • The beginning memory address of the array (base address)
  • The number of elements to process in the array

Example with Array Parameters

20

#include #include void Obtain (int[], int); // Prototypes here void FindWarmest (const int[], int , int&); void FindAverage (const int[], int , int&); void Print (const int[], int); using namespace std; int main ( ) { // Array to hold up to 31 temperatures int temp[31]; int numDays; int average; int hottest; int m; (^) Docsity.com^20