0% found this document useful (0 votes)
3 views16 pages

C++ Chapter7

An array is a collection of similar data items stored at contiguous memory locations, allowing for efficient data management in programming. In C++, arrays can be declared with a specific size and initialized with values, enabling the storage of multiple values in a single variable. The document also explains how to access and manipulate array elements using indices.

Uploaded by

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

C++ Chapter7

An array is a collection of similar data items stored at contiguous memory locations, allowing for efficient data management in programming. In C++, arrays can be declared with a specific size and initialized with values, enabling the storage of multiple values in a single variable. The document also explains how to access and manipulate array elements using indices.

Uploaded by

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

Array

Chapter
7
Array in real life

A collection of data that allows you to store groups


of objects by type.
What is Array in programming?

An array is a group of similar elements or data items


of the same type collected at contiguous memory
locations.
Array syntax

Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store.
Example

Suppose a class has 27 students, and we need to store all


their grades. Instead of creating 27 separate variables, we
can simply create an array:

double grade[27];
Here, grade is an array that can hold a maximum of 27 elements of double type.
C++ Array Declaration

dataType arrayName[arraySize];

int x[6];
• int - type of element to be stored
• x - name of the array
• 6 - size of the array
C++ Array Initialization
In C++, it's possible to initialize an array during declaration.

int x[3];
int x[3] = {19, 10, 8}; x[0]=19;
x[1]=10;
x[2]=8;
• int - type of element to be stored
• x - name of the array
• 3 - size of the array
C++ Array Initialization

Here, we have not mentioned the size of the array.


In such cases, the compiler automatically computes the size.

int x[] = {19, 10, 8, 17, 9, 15};


Access Elements in C++ Array
In C++, each element in an array is associated with a number. The
number is known as an array index. We can access elements of an array
by using those indices.

The array indices start with 0. Meaning x[0] is the first element stored at index 0.
create an array of four integers

Show the data that is stored in index 1


create an array of four integers

Show all data that is stored in array


create an array of five names of cars

Show all elements that is stored in array and


index
create an array of four names of cars

Change the value of the first element


from Volvo to opel
create an array of four types of cars

Change the value of the first element


from Volvo to opel
How to insert array by user
End of chapter

You might also like