Sie sind auf Seite 1von 28

Arrays

Arrays
Hold multiple values with the same data type
Arrays with two dimensions are often used to
represent tables of values arranged in rows
and columns
Can be initialized in its declaration much like
one-dimensional array
Declaration
int b[2][2] = {{1,2},{3,4}};

Column 0 Column 1

Row 0 1 2
Row 1 3 4

b[0][0] = 1 b[1][1] = 4
What happens..
int array1[2][3] = {{1,2,3} , {4,5,6}};
int array2[2][3] = {1,2,3,4,5};
int array3[2][3] = {{1,2} , {4}};

Values in array1 by row:


123
456
Values in array2 by row:
123
450
Values in array3 by row:
120
400
Memory Allocation
All array elements are stored consecutively in
memory, regardless of the number of
dimensions
int a[2][3] = {{1,2,3} , {4,5,6}}; a[0][1] 1
a[0][2] 2
a[0][3] 3
a[1][1] 4
a[1][2] 5
a[1][3] 6
Arrays as Parameters

Simple Arrays
prototype: int display(int [ ]);
definition: int display(int arr[ ])
{
}
Multidimensional Arrays
prototype: int display(int [ ][3]);
definition: int display(int arr[ ][3])
{
}
#include <iostream.h>

//demo declaring arrays


void main()
{
int array1[5]; //declaring an array of integers of size 5
float array2[5]; //declaring an array of floating-point numbers
//of size 5
double array3[5]; //declaring an array of doubles
bool array4[5]; //declaring an array of boolean values
char array5[5]; //declaring an array of characters
}
#include <iostream.h>
//demo initializes an array, disregard warning

void main()
{
/* array1 is declared and the values 11, 7 and 9 are stored as its elements*/

int array1[3] = {11,7,9};


/*array2 is declared without specifying its size. the compiler looks at the initializer list and determines the
array size based on the number of values in the list*/

float array2[] = {1.1,2.2,3.3,4.4};


/*array3 is declared to be of size 5 but initializer list contains only two. 1st and 2nd element will have values
1 and 2, respectively. the rest of the elements will be initialized to zero*/

int array3[5] = {1,2};


/*array 4 is declared to be of size 5 but initializer list contains only two. 1st and 2nd elements will have
values a and b, respectively. the rest of the elements will be initialized to empty space ' '*/

char array4[5] = {'a', 'b'};

}
#include <iostream.h>
//demo accessing arrays
//syntax to access arrays: arrayname[index]
void main()
{
int array1[] = {11,7,9};
cout<<array1[0]<<endl; //number inside bracket[] is the index
//number
cout<<array1[2]<<endl; //index number starts at 0 to n-1, where n
//is the array size

cout<<array1[3]<<endl; //tries to access the 4th element, beyond


//the array size
//syntactically correct but will generate a
//runtime error or illogical result
}
#include <iostream.h>
//displaying vales from the array
void main(){
int array[3];

array[2] = 1;
array[1] = 17;
array[0] = 23;

for(int i=0; i<3; i++)


cout<<"array["<<i<<"]: "<<array[i]<<endl;
}
#include <iostream.h>
//inputting values to arrays and displaying values to arrays
void main(){
int array[3];

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


cout<<"Enter number "<<i+1<<": ";
cin>>array[i];
}

for(i=0; i<3; i++)


cout<<"array["<<i<<"]: "<<array[i]<<endl;

}
#include <iostream.h>
#include <conio.h>

void main(){
int array[] = {3,0,22,56,1}, count = 0;

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


if(array[i]%2==0)
count++; }

cout<<"What is count?"<<endl;
getch();
cout<<count<<endl;
}
#include <iostream.h>
//passing arrays
void displayArray(char [], int);
void main()
{
char myArray[5];
cin >> myArray;
displayArray(myArray,5);
}

void displayArray(char a[], int s)


{
for(int i=0; a[i]!='\0'; i++) {
cout << a[i] << endl; }
}
#include <iostream>
using namespace std;
//passing arrays
void displayArray(char [], int); //prototype
void main() {
char myArray[5];
for(int i=0;i<5;i++){
cin >> myArray[i];
}
displayArray(myArray,5); // call and passing array
}
void displayArray(char a[], int s) {
for(int i=0; i<s; i++){
cout << a[i] << endl; }
}
Two Dimensional Arrays

Declaration and Initialization


Computation and Output
Two Dimensional Arrays
A two dimensional array stores data as a
logical collection of rows and columns.
Each element of a two-dimensional array has a row
position and a column position.
To access an element in a two-dimensional array, you
must specify the name of the array followed by:
a row offset a column offset
Declaration and Initialization

The declaration of a two-dimensional array requires


a row size and a column size.
A consecutive block of (row size)*(column size)
memory locations are allocated.
All array elements must be of the same type.
Elements are accessed by two offsets a row offset
and a column offset.
Example
//Declaration
int data[2][3];
Memory View
data ?
?
?
?
?
?
Example
//Declaration
int data[2][3];

row/column form:
col 0 col 1 col 2

row 0 ? ? ?
row 1 ? ? ?
2D Array Definition Syntax

Syntax:
data_type identifier[ [row_size] ][column_size] ={ initialization_list };
//row_size and column_size must be integer constants

Examples
int data[2][5]; //allocates consecutive memory for 10 integer values
double t[2][2] = {{3.0,5.0},{2.1,7.2}}; //allocates and initializes

Valid References Invalid References


cout << data[1][3]; cout << data[2][5]; //invalid offset.
cout << t[1][0]; cout << t[-1][-1]; //invalid offset
Initialization Examples
int temp[4][3] = {50, 70, 60, 48, 75,
62, 51, 69, 60, 52, 78, 63};
int temp[4][3] = {{50, 70, 60}, {48, 75, 62},
{51, 69, 60}, {52, 78, 63}};

int t2[7][4] = {{50, 70, 60}, {48, 75, 62},


{51, 69, 60}, {52, 78, 63}};

int temp[][3] = {{50, 70, 60}, {48, 75, 62},


{51, 69, 60}, {52, 78, 63}};
int temp[][3] = {50, 70, 60, 48, 75, 62, 51,
69, 60, 52, 78, 63};

Copyright 2012 Pearson Education,


Inc.
Example: Input Using cin
Nested for loops are often used when
inputting and assigning values to a two-dimensional
array.
Nested loops are generally useful for getting around the
2D arrays

//Declaration
double table[RSIZE][CSIZE];

for (int i=0; i<RSIZE; i++) //every row


for (int j=0; j<CSIZE; j++ )//every col
cin >> table[i][j];
Example: Assignment
//Declaration
const int RSIZE=3,CSIZE=2;
double v[RSIZE][CSIZE];

for (int i=0; i<RSIZE; i++) //every row


for (int j=0; j<CSIZE; j++ )//every col
v[i][j] = i+j;

V 0 1
1 2
2 3
Example: Computations
Compute the average value of an array
with n rows and m columns.

double sum=0, average;


for (int i=0; i<n; i++)//every row
for (int j=0; j<m; j++ )//every col
sum += array[i][j];
average = sum / (n*m);
Outputting 2D Arrays

Two dimensional arrays are often printed in a


row by row format, using nested for
statements.
When printing the row values of an array, be
sure to print:
whitespace between the values in a row.
a newline character at the end of each row.
Example: Printing

for (int i=0; i<n; i++) {//every row


for (int j=0; j<m; j++ )//every col
cout << array[i][j] << ;
cout << endl; //add end-of-line each row
}
Create a program that stores the scores of 5
students for 3 exams.
After entering the students scores, the
program will compute and display the average
of each students exam scores.

A familiar problem:
This time your programs will have two functions:
computeaverage()

displaytable(), which includes the average


Note: pass the array from the main() to
computeaverage(), pass the new array, with the
average, to displaytable()

Das könnte Ihnen auch gefallen