Arrays Fundamentals
• A problem to input 1000 integers values and print them in reverse order
Arrays Fundamentals
• A problem to input 1000 integers values and print them in reverse order
Arrays Fundamentals
• A problem to input 1000 integers values and print them in reverse order
Range An array is a data
structure that
consists of a group
Index of elements having a
single name that are
accessed by
indexing. In most
programming
languages each
element has the
same data type and
the array occupies a
contiguous area of
storage.
Copying Arrays
Like printing arrays, there is no single statement in the language that says "copy an entire
array into another array". The array elements must be copied individually.
Arrays Fundamentals
1: #include <iostream>
2:
3: int main()
4: {
5: short age;
6: age=23;
7: std::cout << age << std::endl;
8: return 0;
9: }
1: #include <iostream>
2:
3: int main()
4: {
5: short age[4];
6: age[0]=23;
7: age[1]=34;
8: age[2]=65;
9: age[3]=74;
10: return 0;
11: }
Printing Array Elements
One Dimensional Arrays
A structured collection of
components, all of the same
type, that is given a single
name. Each component (array
element) is accessed by an
index that indicates the
component’s position within the
collection.
Arrays Fundamentals
Index
Index 0
• Value [0];
Index 1
• Value [number]; Index 2
• Value [7*number+10];
• Out of bounds array index
Index 13
• Initializing arrays in Declarations;
int
age[5]={23,10,16,34,30};
int age[ ]={23,10,16,34,30};
Aggregate Operations
• Aggregate Array Operations
int x [20];
int y [20];
X=y; // not valid
X == y; // not valid
cout <<x; // not valid
x=x *y; // not valid
return x; // not valid
Dosomething(x); // valid
Two-Dimensional Arrays
• Used to represent items in a table
Tow-dimensional
with rows and columns, provided
array: A collection of
each item in the table is of the
components, all of the
same data type.
same type, structured in
two dimensions. Each
[0] [1] [2] row 0, column 2
component is accessed
[0] by a pair of indexes that
represent the
[1] component’s position in
each dimension.
[2]
Two Dim-Arrays
• Float alpha [NUM_ROWS][NUM_COLS];
• Alpha[0][5]=30.5;
• Syntax
ArrayName [indexExpression][indexExpression]
• Processing of 2-Dim Arrays
for (row=0; row <5;row++)
for (cols=0;cols<5;cols++)
alpha[row][cols]=0.0;
Printing 2-Dim Arrays
for (row=0; row <5;row++)
for (cols=0;cols<5;cols++)
cout << alpha[row][cols];
Two Dim-Arrays
3 3 3 3 3
// Sum the Rows 5 5 5 5 5
int arr[2][5];
…
total=0; // Sum the Columns
for (col =0; col <5;col++) int arr[2][5];
total=total + arr[1][col]; …
cout << “Row sum: “ for (col =0; col <5;col++)
<<total<<endl; { total=0;
for (row =0; row <2;row++)
total=total + arr[row][col];
cout << “Column sum: “ <<total<<endl;}
Arrays as parameters
• At some moment we may need to pass an array to a
function as a parameter. In C++ it is not possible to pass
a complete block of memory by value as a parameter to
a function, but we are allowed to pass its address.
• In order to accept arrays as parameters the only thing
that we have to do when declaring the function is to
specify in its parameters the element type of the array,
an identifier and a pair of void brackets [].
void procedure (int arg[])
int myarray [40];
procedure (myarray);
Array as parameters
Problem
Write a program that find the largest and
smallest value in an array of 10 elements.