0% found this document useful (0 votes)
59 views21 pages

C++ For Engineers and Scientists: Arrays

This chapter discusses arrays in C++. It covers one-dimensional arrays, how to declare and initialize them, and how to reference elements using indexes. Two-dimensional arrays are also discussed, including how they have rows and columns and how to declare and process them using nested for loops. Common programming errors with arrays like accessing out of bounds elements or failing to initialize them are also summarized.

Uploaded by

Mahamed Hussein
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views21 pages

C++ For Engineers and Scientists: Arrays

This chapter discusses arrays in C++. It covers one-dimensional arrays, how to declare and initialize them, and how to reference elements using indexes. Two-dimensional arrays are also discussed, including how they have rows and columns and how to declare and process them using nested for loops. Common programming errors with arrays like accessing out of bounds elements or failing to initialize them are also summarized.

Uploaded by

Mahamed Hussein
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

C++ for

Engineers and Scientists

Third Edition

Chapter 7
Arrays
Objectives

In this chapter, you will learn about:


• One-dimensional arrays
• Array initialization
• Declaring and processing two-dimensional arrays

C++ for Engineers and Scientists, Third Edition 2


One-Dimensional Arrays
• One-dimensional array: A list of related values
with the same data type, stored using a single
group name (called the array name)
• Syntax:
dataType arrayName[number-of-items]
• By convention, the number of items is first declared
as a constant, and the constant is used in the array
declaration

C++ for Engineers and Scientists, Third Edition 3


One-Dimensional Arrays (continued)
• Examples:
const int NUMELS = 6;
int volts[NUMELS];

const int ARRAYSIZE = 4;


char code[ARRAYSIZE];

Figure 7.1 The volts and code arrays in memory

C++ for Engineers and Scientists, Third Edition 4


One-Dimensional Arrays (continued)
• Element: An item in the array
– Array storage of elements is contiguous
• Index (or subscript) of an element: The position of
the element within the array
– Indexes are zero-relative
• To reference an element, use the array name and
the index of the element

Figure 7.2 Identifying array elements


C++ for Engineers and Scientists, Third Edition 5
One-Dimensional Arrays (continued)
• Index represents the offset from the start of the
array
• Element is also called indexed variable or
subscripted variable
• Subscripted variable can be used anywhere that a
variable can be used
• Expressions can be used within the brackets if the
value of the expression
– Yields an integer value
– Is within the valid range of subscripts

C++ for Engineers and Scientists, Third Edition 6


One-Dimensional Arrays (continued)
• All of the elements of an array can be processed by
using a loop
• The loop counter is used as the array index to
specify the element
• Example:
sum = 0;
for (i=0; i<5; i++)
sum = sum + temp[i];

C++ for Engineers and Scientists, Third Edition 7


Input and Output of Array Values
• Array elements can be assigned values interactively
using a cin stream object
• Out of range array indexes are not checked at
compile-time
– May produce run-time errors
– May overwrite a value in the referenced memory
location and cause other errors
• Array elements can be displayed using the cout
stream object

C++ for Engineers and Scientists, Third Edition 8


Input and Output of Array Values
(continued)

C++ for Engineers and Scientists, Third Edition 9


Input and Output of Array Values
(continued)

C++ for Engineers and Scientists, Third Edition 10


Array Initialization
• Array elements can be initialized in the array
declaration statement
• Example:
int temp[5] = {98, 87, 92, 79, 85};
• Initialization:
– Can span multiple lines, because white space is
ignored in C++
– Starts with array element 0 if an insufficient number of
values is specified
• If initializing in the declaration, the size may be
omitted

C++ for Engineers and Scientists, Third Edition 11


Array Initialization (continued)
• char array will contain an extra null character at
the end of the string
• Example:
char codes[] = “sample”;

Figure 7.4 Initializing a character array with a string adds a terminating \0 character

C++ for Engineers and Scientists, Third Edition 12


Array Initialization (continued)

C++ for Engineers and Scientists, Third Edition 13


Declaring and Processing
Two-Dimensional Arrays
• Two-dimensional array: Has both rows and
columns
– Also called a table
• Both dimensions must be specified in the array
declaration
– Row is specified first, then column
• Both dimensions must be specified when
referencing an array element

C++ for Engineers and Scientists, Third Edition 14


Declaring and Processing
Two-Dimensional Arrays (continued)
• Example:
int val[1][3];

Figure 7.5 Each array element is identified by its row and column position

C++ for Engineers and Scientists, Third Edition 15


Declaring and Processing
Two-Dimensional Arrays (continued)

• Two-dimensional arrays can be initialized in the


declaration by listing values within braces,
separated by commas
• Braces can be used to distinguish rows, but are not
required

C++ for Engineers and Scientists, Third Edition 16


Declaring and Processing
Two-Dimensional Arrays (continued)
• Nested for loops are used to process two-
dimensional arrays
– Outer loop controls the rows
– Inner loop controls the columns

C++ for Engineers and Scientists, Third Edition 17


Declaring and Processing
Two-Dimensional Arrays (continued)

C++ for Engineers and Scientists, Third Edition 18


Common Programming Errors
• Failing to declare the array
• Using a subscript that references a non-existent
array element (out of bounds)
• Failing to use a counter value in a loop that is large
enough to cycle through all array elements
• Failing to initialize the array

C++ for Engineers and Scientists, Third Edition 19


Summary
• Single dimension array is a data structure that
stores a list of values having the same data type
• Array elements are stored in contiguous memory
locations, and referenced by array name and index
position
• Two-dimensional array has rows and columns
• Arrays may be initialized when they are declared

C++ for Engineers and Scientists, Third Edition 20


Summary (continued)
• Arrays may be passed to a function by passing the
name of the array as the argument
• Individual array elements as arguments are passed
by value
• Arrays passed as arguments are passed by
reference, not by value

C++ for Engineers and Scientists, Third Edition 21

You might also like