Professional Documents
Culture Documents
SEM1 2009/2010
Week 13 & 14
Arrays
Source from Tony Gaddis, Barret Krupnow, Starting out with C++, 5th edition
update. 2007. Pearson Addison-Wesley
7.1
Arrays Hold Multiple Values
Concept of an array
first
element
second
element
third
element
fourth
element
fifth
element
Array Terminology
Array Terminology
Examples:
int tests[5] is an array of 20 bytes,
assuming 4 bytes for an int
long double measures[10]is an array of 80
bytes, assuming 8 bytes for a long double
Size Declarators
Named constants are commonly used as
size declarators.
const int SIZE = 5;
int tests[SIZE];
This eases program maintenance when
the size of the array needs to be changed.
Using Arrays
There are 2 things to do when using
arrays:
a) Declaration and definition of arrays
b) Accessing elements in arrays (in 7.2)
a) For assigning values
b) For getting values
Syntax:
data_typevariable_name[n];//n=no.ofelements
7.2
Accessing Array Elements
subscripts
:0
1
(Program Continues)
Here are the contents of the hours array, with the values entered by the
user in the example output:
Examples
1. Assigning variable n with the value of first element
of an array A
intA[]={1,3,5,7}
intn;
n=A[0];
2. Printing the second element of an array B
intB[]={10,30,50,70}
printf(%d,B[1]);
Examples cont.
3. Assigning the first element of array C with the value of the
second element
intC[]={11,23,35,47};
C[0]=C[1];
4. Printing all elements of an array D
intD[]={1,4,3,6,7,8,9,0,2}
inti;
for(i=0;i<9;i++)
printf(%d\n,D[i]);
Default Initialization
Global array all elements initialized to
0 by default
Local array all elements uninitialized by
default
7.3
Array Initialization
Array Initialization
Arrays can be initialized with an
initialization list:
const int SIZE = 5;
int tests[SIZE] = {79,82,91,77,84};
printf(Month%dhas,(count+1));
printf(%ddays.\n,days[count]);
17
15
11
7.4
Processing Array Contents
Array Assignment
To copy one array to another,
Dont try to assign one array to the other:
newTests = tests;
// Won't work
(%c\n, fName);
int count;
int lowest;
lowest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
When this code is finished, the lowest variable will contain
the lowest value in the numbers array.
Partially-Filled Arrays
Comparing Arrays
To compare two arrays, you must compare
element-by-element:
const int SIZE = 5;
int firstArray[SIZE] = { 5, 10, 15, 20, 25 };
int secondArray[SIZE] = { 5, 10, 15, 20, 25 };
int arraysEqual = 1; // TRUE condition
int count = 0;
// Loop counter variable
// Compare the two arrays.
while (arraysEqual && count < SIZE)
{
if (firstArray[count] != secondArray[count])
arraysEqual = 0;
count++;
}
if (arraysEqual)
printf ("The arrays are equal.\n);
else
printf ("The arrays are not equal.\n);
7.5
Using Parallel Arrays
(Program Continues)
doublegrossPay=hours[index]*payRate[index];
printf("Employee#%d",(index+1));
printf(":$%.2lf\n",grossPay);
}
getch();
return0;
}
7.6
Arrays as Function Arguments
#include<stdio.h>
(Program Continues)
printf("%d",nums[index]);
printf("\n");
7.7
Two-Dimensional Arrays
Two-Dimensional Arrays
Can define one array for multiple sets of
data
Like a table in a spreadsheet
Use two size declarators in definition:
const int ROWS = 4, COLS = 3;
int exams[ROWS][COLS];
columns
exams[0][0] exams[0][1] exams[0][2]
r
o
w
s
/*Numberofdivisions*/
/*Numberofquarters*/
/*Arraywith3rowsand4columns*/
/*Toholdthetotalsales*/
printf("Thisprogramwillcalculatethetotalsalesof\n");
printf("allthecompany'sdivisions.\n");
printf("Enterthefollowingsalesinformation:\n\n");
(Program continues)
for(qtr=0;qtr<NUM_QTRS;qtr++)
{
printf("Division%d",(div+1));
printf(",Quarter%d:$",(qtr+1));
scanf("%lf",&sales[div][qtr]);
}
printf("\n");
}
/*Printblankline*/
/*Nestedloopsusedtoaddalltheelements*/
for(div=0;div<NUM_DIVS;div++)
{
for(qtr=0;qtr<NUM_QTRS;qtr++)
totalSales+=sales[div][qtr];
}
printf("Thetotalsalesforthecompanyare:$");
printf("%.2lf\n",totalSales);
getch();
return0;
}
2D Array Initialization
Two-dimensional arrays are initialized rowby-row:
const int ROWS = 2, COLS = 2;
int exams[ROWS][COLS] = { {84, 78},
{92, 97} };
84 78
92 97
// Header
void getExams(int exams[][COLS], int
rows)
printf("%4d",array[x][y]);
}
printf("\n");
printf(Thecontentsoftable1are:\n);
showArray(table1,TBL1_ROWS);
printf(Thecontentsoftable2are:\n);
showArray(table2,TBL2_ROWS);
1.
12
78
45
14
34
56
66
45
17
67
Print the sum of the same locations numbers in the two arrays.
7.8
Array of Strings
Array of Strings
Use a two-dimensional array of characters
as an array of strings:
const int NAMES = 3, SIZE = 10;
char students[NAMES][SIZE] =
{ "Ann", "Bill", "Cindy" };
/*Thenumberofmonths*/
/*Maximumsizeofeachstring*/
/*Arraywiththenamesofthemonths*/
charmonths[NUM_MONTHS][STRING_SIZE]=
{"January","February","March",
"April","May","June",
"July","August","September",
"October","November","December"};
/*Arraywiththenumberofdaysineachmonth*/
intdays[NUM_MONTHS]={31,28,31,30,
31,30,31,31,
30,31,30,31};
Array of Strings
/*Displaythemonthsandtheirnumbersofdays*/
for(intcount=0;count<NUM_MONTHS;count++)
{
printf("%shas",months[count]);
printf("%ddays.\n",days[count]);
}
getch();
return0;
}
7.9
Arrays with Three or More
Dimensions
Thank You
Q&A