0% found this document useful (0 votes)
10 views12 pages

Chapter 4 - Arrays in C++

C++

Uploaded by

Shafi Esa
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)
10 views12 pages

Chapter 4 - Arrays in C++

C++

Uploaded by

Shafi Esa
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/ 12

Exit Exam Tutorial

Part 1: Computer Programming


Episode 4: Arrays in C++
1.4 Arrays in C++
- An array is a collection of data which shares a common
identifier and data type.
- A collection of identical data objects, which are stored in
consecutive memory locations under a common heading or
a variable name.
- In other words, an array is a group or a table of values
referred to by the same name.
- The individual values in array are called elements.
1.4 Arrays in C++
1.4.1 Properties of Arrays
- Arrays in C++ are zero-bounded; that is the index of the
first element in the array is 0 and the last element is N-1,
where N is the size of an array.
- It is illegal to refer to an element outside of the array
bounds, and your program will crash or have unexpected
results, depending on the compiler.
- Array can only hold values of one type.
1.4 Arrays in C++
1.4.2 Array Declaration
- Declaring the name and type of an array and setting the
number of elements in an array is called dimensioning the
array.
- The array must be declared before one uses in like other
variables.
- In the array declaration one must define:
1. The type of the array (i.e, integer, floating-point, char,
etc.)
2. Name of the array.
3. The total number of memory locations to be allocated or
the maximum value of each subscript.
- So, the general syntax for the declaration is:
DataTypename arrayname [array size];
1.4 Arrays in C++
- The array size cannot be a variable whose value is set while
the program is running.
- Thus, to declare an integer with size of 10 having a name of
num is: int num[10];
1.4 Arrays in C++
1.4.3 Initializing Arrays
- You can initialize C++ array elements either one by one or
using a single statement as follows:
- double balance[5] = {1000.0, 2.0, 3.4, 17.0,
50.0};
- Multidimensional arrays are described as arrays of arrays.

0 1 2 3 4
0
1
2
1.4 Arrays in C++
- Matrix represents a bi-dimensional array of 3 per 5 values
of type int. The way to declare this array would be:
- int matrix[3][5];
1.4 Arrays in C++
1.4.4 Accessing Array Elements
- An element is accessed by indexing the array name.
- This is done by placing the index of the element within
square brackets after the name of the array.
- For example: double salary = balance[9];
- The above statement will take 10th element from the array
and assign the value to salary variable.
- Following is an example, which will use all the above-
mentioned three concepts: declaration, assignments and
accessing arrays.
1.4 Arrays in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n[10];

for (int i = 0; i < 10; i++) {


n[i] = i + 100;
}

cout << “Element“ << setw(13) << “Value” << endl;

for (int j = 0; j < 10; j++) {


cout << setw(7) << j << setw(13) << n[j] << endl;
}

return 0;
}
1.4 Arrays in C++
Sample output:
1.4 Arrays in C++
Special Thanks to the publisher and author with:
TOPICS AND THE CONCEPTS:
What is Array?
Accessing Array Elements
Array Declaration
Initializing Arrays
Properties of Arrays

REFERENCES:
C++ Programming: From Problem Analysis to Program Design (D.S. Malik)
Fundamental of Programming in C++ (Walter J. Savitch)

PRESENTED BY:
Mohammed Nebil

HISTORY OF THE PROGRAMMING:


Dennis Ritchie
Bjarne Stroustrup
Anders Hejlsberg

SPECIAL THANKS:
Digital Library of Educations
Federal Democratic Republic of Ethiopia, Ministry of Educations
Ethiopian Education Short Note

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy