Arrays
Dr Kanta Prasad Sharma
Associate Professor
Amity School of Engineering & Technology
Amity University Uttar Pradesh ,
Greater Noida Campus
Introduction
• Arrays
– Structures of related data items
– Static entity - same size throughout program
• A few types
– C-like, pointer-based arrays
– C++, arrays as objects
Arrays
• Array
– Consecutive group of memory locations
– Same name and type
• To refer to an element, specify
– Array name and position number
• Format: arrayname[ position number ]
– First element at position 0
– n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
• Array elements are like normal variables
c[ 0 ] = 3;
cout << c[ 0 ];
• Performing operations in subscript. If x = 3,
c[ 5 – 2 ] == c[ 3 ] == c[ x ]
Arrays
Name of array (Note that all elements of this array have the same name, c)
c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
Position number of the element within array c
Declaring Arrays
• Declaring arrays - specify:
– Name
– Type of array
– Number of elements
– Examples
int c[ 10 ];
float hi[ 3284 ];
• Declaring multiple arrays of same type
– Similar format as other variables
– Example
int b[ 100 ], x[ 27 ];
Examples Using Arrays
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements become 0
– If too many initializers, a syntax error is generated
int n[ 5 ] = { 0 }
– Sets all the elements to 0
• If size omitted, the initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore n is a 5 element array
Examples Using Arrays
#include <stdio.h>
int main()
{
int kp[] = {25, 50, 75, 100};
printf("%d", kp[0]);
return 0;
}
#include <stdio.h>
int main()
{
int kp[] = {25, 50, 75, 100};
kp[0] = 33;
printf("%d", kp[0]);
return 0;
}
Loop Through an Array
#include <stdio.h>
int main()
{
int kp[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++)
{
printf("%d\n", kp[i]);
}
return 0;
}
Loop Through an Array
#include <stdio.h>
Set Array Size void main()
{
int kp[4];
kp[0] = 25;
kp[1] = 50;
kp[2] = 75;
kp[3] = 100;
printf("%d\n", kp[0]);
}
Loop Through an Array
#include <stdio.h>
Set Array Size void main()
{
int kp[] = {25, 50, 75, 3.15, 5.99};
int i;
for (i = 0; i < 5; i++)
{
printf("%d\n", kp[i]);
}
}
Loop Through an Array
#include <stdio.h>
Get Array Size int main()
{
int kp[] = {10, 25, 50, 75, 100};
printf("%lu", sizeof(kp));
return 0;
}
Examples Using Arrays
• Strings
– Arrays of characters
– All strings end with null ('\0')
– Examples:
char string1[] = "hello";
char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ };
– Subscripting is the same as for a normal array
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
• Input from keyboard
char string2[ 10 ];
cin >> string2;
– Takes user input
– Side effect: if too much text entered, data written
beyond array
Passing Arrays to Functions
• Specify the name without any brackets
– To pass array myArray declared as
int myArray[ 24 ];
to function myFunction, a function call would resemble
myFunction( myArray, 24 );
– Array size is usually passed to function
• Arrays passed call-by-reference
– Value of name of array is address of the first element
– Function knows where the array is stored
• Modifies original memory locations
• Individual array elements passed by call-by-value
– pass subscripted name (i.e., myArray[ 3 ]) to function
Passing Arrays to Functions
• Function prototype:
void modifyArray( int b[], int arraySize );
– Parameter names optional in prototype
• int b[] could be simply int []
• int arraysize could be simply int
Sorting Arrays
• Sorting data
– Important computing application
– Virtually every organization must sort some data
• Massive amounts must be sorted
• Bubble sort (sinking sort)
– Several passes through the array
– Successive pairs of elements are compared
• If increasing order (or identical), no change
• If decreasing order, elements exchanged
– Repeat these steps for every element
Sorting Arrays
• Example:
– Original: 3 4 2 6 7
– Pass 1: 3 2 4 6 7
– Pass 2: 2 3 4 6 7
– Small elements "bubble" to the top
Computing Mean, Median and
Mode Using Arrays
• Mean
– Average
• Median
– Number in middle of sorted list
– 1, 2, 3, 4, 5 (3 is median)
• Mode
– Number that occurs most often
– 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)
Searching Arrays: Linear Search and Binary
Search
• Search array for a key value
• Linear search
– Compare each element of array with key value
– Useful for small and unsorted arrays
• Binary search
– Can only be used on sorted arrays
– Compares middle element with key
• If equal, match found
• If key < middle, repeat search through the first half of the array
n the last half of the array
• If key > middle, repeat search through
– Very fast; at most n steps, where 2^n > # of elements
• 305element array takes at most 5 steps
– 2 > 30
Multiple-Subscripted Arrays
• Multiple subscripts - tables with rows, columns
– Like matrices: specify row, then column.
Column 0 Column 1 Column 2 Column 3
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
• Initialize 1 2
3 4
int b[ 2 ][ 2 ] = { {1, 2}, {3, 4}};
1 0
– Initializers grouped by row in braces
3 4
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
Multiple-Subscripted Arrays
• Referenced like normal
cout << b[ 0 ][ 1 ];
– Will output the value of 0
– Cannot reference with commas
cout << b( 0, 1 );
• Will try to call function b, causing a syntax error