Array
Array
Arrays
COMP102 Prog. Fundamentals I: Arrays / Slide 2
Arrays
An array is a collection of data elements that are of
the same type (e.g., a collection of integers, collection
of characters, collection of doubles).
COMP102 Prog. Fundamentals I: Arrays / Slide 3
Arrays
1-dimensional array.
Array Applications
Array Declaration
Syntax:
<type> <arrayName>[<array_size>]
Ex. int Ar[10];
The array elements are all values of the type <type>.
The size of the array is indicated by <array_size>, the
number of elements in the array.
<array_size> must be an int constant or a constant
expression. Note that an array can have multiple
dimensions.
COMP102 Prog. Fundamentals I: Arrays / Slide 6
Array Declaration
// array of 10 uninitialized ints
int Ar[10];
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
0 1 2 3 4 5
COMP102 Prog. Fundamentals I: Arrays / Slide 7
Subscripting
Declare an array of 10 integers:
int Ar[10]; // array of 10 ints
To access an individual element we must apply a
subscript to array named Ar.
A subscript is a bracketed expression.
– The expression in the brackets is known as the index.
First element of array has index 0.
Ar[0]
Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
Last element has an index one less than the size of the array.
Ar[9]
Incorrect indexing is a common error.
COMP102 Prog. Fundamentals I: Arrays / Slide 8
Subscripting
// array of 10 uninitialized ints
int Ar[10];
--
Ar[3] = 1;
int x = Ar[3]; 1 --
-- -- --
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8]Ar[9]
COMP102 Prog. Fundamentals I: Arrays / Slide 9
Subscripting Example 1
//For loop to fill & print a 10-int array
#include <iostream>
using namespace std;
int main ( ) {
int index, ar[10]; // array for 10 integers
// Read in 10 elements.
cout << "Enter 10 integers: ";
for(index = 0; index < 10; index ++)
cin >> ar[index];
cout << endl;
cout << "The integers are ";
for(index = 0; index < 10; index ++)
cout << ar[index] << " ";
cout << endl;
return 0;
}
COMP102 Prog. Fundamentals I: Arrays / Slide 10
0 1 2 3 4 5 6 7 8 9
Ar 1 -- 8 6 3 -- -- 5 12 --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8]Ar[9]
COMP102 Prog. Fundamentals I: Arrays / Slide 13
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 6 5 4 3 2 1 0
Ar[3] = -1;
6 -1
0 1 2 3 4 5 6 7 8 9
Ar 9 8 7 -1 5 4 3 2 1 0
Initializing arrays with random
COMP102 Prog. Fundamentals I: Arrays / Slide 14
values
The following loop initializes the array myList with random
values between 0 and 99:
Printing arrays
To print an array, you have to print each element in the array
using a loop like the following:
Copying Arrays
Can you copy array using a syntax like this?
list = myList;
double total = 0;
for (int i = 0; i < ARRAY_SIZE; i++)
{
total += myList[i];
}
COMP102 Prog. Fundamentals I: Arrays / Slide 19
largest element
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < ARRAY_SIZE; i++)
{
if (myList[i] > max)
{
max = myList[i];
indexOfMax = i;
}
}
COMP102 Prog. Fundamentals I: Arrays / Slide 21
Shifting Elements
double temp = myList[0]; // Retain the first element
// Shift elements left
for (int i = 1; i < myList.length; i++)
{
myList[i - 1] = myList[i];
}
// Move the first element to fill in the last position
myList[myList.length - 1] = temp;
#include <iostream> // Random Shuffling
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 10;
int main(){
int myList[ARRAY_SIZE]= {1,2,3,4,5,6,7,8,9,10};
srand(time(0));
for ( int i = 0; i < ARRAY_SIZE; i++)
{ // Generate an index randomly
int index = rand() % ARRAY_SIZE;
int temp = myList[i];
myList[i] = myList[index];
myList[index] = temp;
}
for ( int i = 0; i < ARRAY_SIZE; i++)
cout << myList[i] <<' ';
cout << endl;
return 0;}
#include <iostream> // Create 6 random numbers for Mark 6
#include <cstdlib>
#include <ctime>
using namespace std;
const int ARRAY_SIZE = 6;
int main(){
int used[50] ={0}; //indicate if a random number is used.
int Mark[6], temp;
srand(time(0));
int i = 0;
while (i < 6){
temp = rand()%49 + 1;
if (used[temp] == 0) { //temp is not used
Mark[i] = temp; i++;
used[temp] = 1;
}
for ( int i = 0; i< 6; i++)
cout << Mark[i] << endl;
return 0;
}
#include <iostream> //Print numbers in reversed order
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main(){
srand(time(0));
int numbers[500];
int num_num;