0% found this document useful (0 votes)
58 views3 pages

Arrays: Programming Fundamentals

An array is a collection of elements of the same type stored consecutively in memory. Arrays allow storing multiple values of the same type under one variable name. To declare an array, the type is written followed by the array name and the number of elements in square brackets. Array elements can be initialized with values within curly brackets. Individual elements are accessed using the array name and index within square brackets. The sizeof operator returns the total number of bytes used by the array.

Uploaded by

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

Arrays: Programming Fundamentals

An array is a collection of elements of the same type stored consecutively in memory. Arrays allow storing multiple values of the same type under one variable name. To declare an array, the type is written followed by the array name and the number of elements in square brackets. Array elements can be initialized with values within curly brackets. Individual elements are accessed using the array name and index within square brackets. The sizeof operator returns the total number of bytes used by the array.

Uploaded by

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

10/30/2017

Arrays
Programming Fundamentals • Variables we declared in our earlier programs can
store a single data item of the specified type
• We can have a variable that stores an integer or a
variable that stores a character
• An array can store several data items of the same
Arrays & Strings type
• We can have an array of integers, an array of
characters – in fact an array of any type of data

Programming Fundamentals- Arrays & 2


Strings

Arrays Arrays
• For example: We can store 5 values of type int without
• An Array is a series of elements (variables) of the
having to declare 5 different variables each one with a
same type placed consecutively in memory that can
different identifier
be individually referenced by adding an index to a
• Instead of that, using an array we can store 5 different
unique name values of the same type int with one unique identifier
(variable name)

Programming Fundamentals- Arrays & 3 Programming Fundamentals- Arrays & 4


Strings Strings

Arrays Declaration of Array


An array containing 5 integer values of type int • An array must be declared before it is used
called myDataArray
• A typical declaration for an array in C++ is:
– type array_name [no_of_elements];
0 1 2 3 4 – For example:
myDataArray
int myDataArray[5];
Each element of type int

Programming Fundamentals- Arrays & 5 Programming Fundamentals- Arrays & 6


Strings Strings

1
10/30/2017

Declaration of Array Initializing Arrays


• The elements field within brackets [ ] when declaring • when we declare an Array, we have the
an array must be a constant value
possibility to assign initial values to each one
of its elements using curly brackets { }.
• Since arrays are blocks of static memory of a given
size and the compiler must be able to determine – For example:
exactly how much memory must assign to the array
before any instruction is considered. int myDataArray[5]={34,21,77,5,120};

Programming Fundamentals- Arrays & 7 Programming Fundamentals- Arrays & 8


Strings Strings

Accessing the values of an Array


Initializing Arrays
• at any point of the program in which the array
int myDataArray[5]={34,21,77,5,120}; is visible we can access individually anyone
of its values for reading or modifying it as if it
was a normal variable
0 1 2 3 4
myDataArray 34 21 77 5 120 • The format is the following:
– array_name[index];

Programming Fundamentals- Arrays & 9 Programming Fundamentals- Arrays & 10


Strings Strings

Accessing the values of an Array Setting Array elements to Zero

• Example: • To avoid junk values, its easy to initialize a


– To store the value 75 in the third element of whole array elements to zero
myDataArray, statement would be: • For Example,
myDataArray[2] = 75;
– To pass the value of the third element of
myDataArray to the variable a, we could write: double junk[size] = {0};

a = myDataArray[2];

Programming Fundamentals- Arrays & 11 Programming Fundamentals- Arrays & 12


Strings Strings

2
10/30/2017

Defining Array Size with the Initializer


Exercise List

1. Write a program that reads marks of 10 • We can omit the size of the array in the
quizzes from user and displays the average declaration of an array, provided we supply
quiz marks. initializing values
2. Write a program that reads maximum up to • Number of elements in the array will then be
10 quiz marks from user and displays the same as the initializing values
average quiz marks. • For example,
int values[] = {2,3,4};

Programming Fundamentals- Arrays & 13 Programming Fundamentals- Arrays & 14


Strings Strings

Finding the Number of Array


Elements Exercise
• sizeof() operator can supply the number of bytes that
a variable occupies 3. Read quiz marks of 10 students and display
• We can use it to find out total number of elements in the following;
the array, for example – average marks,
• sizeof(values) returns the total number of bytes used
– number of students above the average
by the array, sizeof(values[0]) returns number of
bytes used by the first element of the array – number of students below the average

int values [] = {1,2,3,4,5}; Note: use single loop to calculate above and
below the average
Programming Fundamentals- Arrays & 15 Programming Fundamentals- Arrays & 16
Strings Strings

You might also like