Chapter 04-2 – Array
Introduction to Array
An array is a group of memory locations related
by the fact that they all have the same name and
the same type
To refer to a particular location or element in the
array, we specify the name of the array and the
position number (index/subscript) of the
particular element in the array
The size of an array is static (fixed) throughout
program execution
The compiler must reserve storage (space) for
each element/item of a declared array
Eg. Program 3-11 (ex2.ppt)
Department of Engineering 2
Let say we have an array called a
Department of Engineering 3
Array Declaration
You need to make a declaration before using
an array
Array declaration is made by specifying the
data type, it’s name and the number of space
(size) so that the computer may reserve the
appropriate amount of memory
General syntax:
data_type array_name[size];
Examples:
int my_array[100];
char name[20];
double bigval[5*200];
int a[27], b[10], c[76];
Department of Engineering 4
Array Initialization
There are 2 ways to initialize an array: during
compilation and during execution
1. During compilation:
int arr[] = {1, 2, 3, 4, 5}; unsized array
We can define how many elements that we want since the
array size is not given
int arr[3] = {90, 21, 22};
We can define only 3 elements since the array size is
already given.
2. During execution:
int arr[3], j;
for (j = 0; j < 3; j++)
arr[j] = 0;
Department of Engineering 5
Examples Using Array
#include <stdio.h>
#define SIZE 5 Output:
int main() { Element Value
int temp[SIZE], i;
printf(“%s%13s\n”, “Element”, 0 0
“Value”);
for (i = 0; i < SIZE; i++) 1 9
temp[i] = i*9; 2 18
for (i = 0; i < SIZE; i++)
printf(“%7d%13d\n”, i, temp[i]); 3 27
return 0;
} 4 36
Press any key to continue
In the example above, an array with the name temp and size 5
has been declared. The elements in the array has been given
the value position*9. The first for loop is equivalent to this:
temp[0] = 0*9, temp[1] = 1*9, temp[2] = 2*9, temp[3] =
3*9, temp[4] = 4*9
Department of Engineering 6
Another Example
//Histogram printing program
#include <stdio.h>
#define SIZE 10
int main(){
int n[SIZE] = {19, 3, 15, 7, 11, 9, 13, 15, 17, 1};
int i, j;
printf("%s%13s%17s\n", "Element", "Value", "Histogram");
for(i=0; i<SIZE; i++) {
printf("%7d%13d ", i, n[i]);
for(j=1; j<=n[i]; j++) {
printf("%c", '*');
}
printf("\n");
}
return 0;
}
Department of Engineering 7
Another Example (Cont.)
/*
Element Value Histogram
0 19 *******************
1 3 ***
2 15 ***************
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 15 ***************
8 17 *****************
9 1 *
Press any key to continue
*/
Department of Engineering 8
Another Example (Cont.)
#include <stdio.h>
#define SIZE 10
int main( ) {
int i = 0, list[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, total = 0;
for (; i < SIZE; i++) {
total += list[i];
}
printf(“Total of array element values is %d\n”, total);
return 0;
}
Output:
Total of array element values is 45
Department of Engineering 9
Assigning Value to Array
Element
We can assign a value to a specific array element by using
its index number
Example: let’s say we have an array that represent the
number of inhabitant in 5 unit apartments
int apartment[5]={3,2,6,4,5};
The above initialization indicates that there are 3 people
living in apartment 0, 2 people living in apartment 1 and so
on
Let say that we have a new born in apartment 3, so we
need to change the number of inhabitant living in
apartment three
apartment[3] = apartment[3] + 1;
Now, we have the following values in our array:
3,2,6,5,5
Department of Engineering 10
Reading Values from Array
Elements
We can read a value from a specific array element by
referring to the index
For example, let’s say we want to know how many people
leaving in apartment 3, we could simple do this:
int apartment[5] = {3,2,6,4,5};
int no_of_people;
no_of_people = apartment[3];
printf(“Apartment3 has %d people”, no_of_people);
The above C code will produce the following output:
Apartment 3 has 4 people
Department of Engineering 11
Example 1: finding total
inhabitants
#include <stdio.h> "for" loops are ideal for processing
#define size 5 elements in the array
int main(void){
int apartment[size] = {3,2,6,4,5};
int index, total = 0;
for (index = 0; index < size; index++){
total = total + apartment[index];
}
printf("There are total of %d inhabitants",total);
return 0;
}
Output:
There are total of 20 inhabitants
Department of Engineering 12
Example 2: list down number of
inhabitants in each apartment
#include <stdio.h>
int main( ){
int apartment[5] = {3,2,6,4,5};
int index, total = 0;
printf("%-7s %-15s\n","Apt No", "No of people");
for (index = 0; index < 5; index++){
printf("%4d %10d\n",index,
apartment[index]);
}
return 0;
}
Department of Engineering 13
Example 2 Output
Apt No No of people
0 3
1 2
2 6
3 4
4 5
Department of Engineering 14
Relationship with Pointers
The name of an array is actually a pointer to the first
element in the array
Therefore, if we have:
int test[3] = {9, 10, 11};
printf(“%d”, *test);
The output would be: 9
There are a few ways to traverse an array:
Using index Using pointers
int test[3] = {9, 10, 11}, k; int test[3] = {9, 10, 11}, k;
int *ptr= test;
for (k = 0; k < 3; k++) for (k = 0; k < 3; k++, ptr++)
printf(“%d\n”, test[k]); printf(“%d\n”, *ptr);
Difference: Can change the contents of ptr, as in
ptr++ whereas the identifier "test" is not a variable
Department of Engineering 15
Passing Arrays to a Function
A function that is going to receive an array
as one of the arguments can be declared
in 2 ways:
void Process(char name[ ]) OR
void Process(char *name) pointer
Any changes to the array inside the
function will also change the actual array
Either the array is passed using [ ] or
using *, the array can be accessed in the
same way
Department of Engineering 16
Passing Arrays to a Function
(Cont.)
When we want to pass an array to a
function, we need to know these 3 things
How to write the function prototype?
How to do function call?
How does the function header would look like?
Assume that we have the following array
declaration
int marks[10] = {0};
Department of Engineering 17
Passing Arrays to a Function
(Cont.)
Say for example, we want to write a function, called get
marks, which will read marks from the user and store the
marks inside the array
Function prototype:
/* data type with square bracket */
void get_marks(int [ ]);
void get_marks(int *); /*treating array as pointer */
Function call:
get_marks(marks); /* just pass the array name */
Function header:
void get_marks(int marks[ ])
void get_marks(int *marks) /*treating array as pointers */
Department of Engineering 18
Example 1: Parameter Passed as
an Array
#include <stdio.h>
#define size 10
void get_marks(int [ ]);
double calc_average(int [ ]);
int main( ){
int marks[size] = {0}; /*initializing the array*/
get_marks(marks); /* function call */
printf(“Average for marks given is %lf”,
calc_average(marks)); //}
return 0;
}
Department of Engineering 19
Example 1: Parameter Passed as
an Array (Cont.)
void get_marks(int marks[ ]){
int i;
for (i = 0; i < size; i++){
printf("Marks student %d:", i + 1);
scanf("%d",&marks[i]);
}
}
double calc_average(int marks[ ]){
int total = 0, i;
for (i = 0; i < size; i++){
total = total + marks[i];
}
return (double)total / size;
}
Department of Engineering 20
Example 2: Parameter Passed as
Pointers
A function could also receive/treat array parameter as
pointer
#include <stdio.h>
#define size 10
void get_marks(int *); Observe the
double calc_average(int *); function prototypes
int main(){
int marks[size] = {0};
get_marks(marks);
printf("Average for marks given is %lf\n", calc_average(marks));
return 0;
}
Department of Engineering 21
Example 2: Parameter Passed as
Pointers (Cont.)
void get_marks(int *marks){ Manipulating the memory address
int i;
for (i = 0; i < size; marks++){
printf("Marks student %d:", i + 1);
scanf("%d", marks);
}
}
double calc_average(int *marks){
int i, total = 0;
for (i = 0; i < size; i++, marks++){
total = total + *marks;
}
return (double)total / size;
} Pointer variable
Department of Engineering 22
Two-Dimensional Arrays
A table.
Data arranged in rows and columns.
Department of Engineering 23
Two-Dimensional Array
Department of Engineering 24
Two-Dimensional array indexing
Department of Engineering 25
Fill Two-D array
void fillArray (int table[][MAX_COLS], int numRows)
{
// Statements
for (int row = 0; row < numRows; row++)
{
table [row][0] = row * 10;
for (int col = 1; col < MAX_COLS; col++)
table [row][col] = table [row][col - 1] + 1;
} // for
return;
} // fillArray
Department of Engineering 26
Memory Layout
Department of Engineering 27
Fill 2-D array
#include <stdio.h>
#define MAX_ROWS 5
#define MAX_COLS 4
// Prototype Declarations
void fillArray (int table[][MAX_COLS], int numRows);
Department of Engineering 28
Fill 2-D array
int main (void)
{
// Local Declarations
int row;
int col;
int ary[MAX_ROWS][MAX_COLS];
// Statements
fillArray(ary, MAX_ROWS);
printf("Data: \n");
for (row = 0; row < MAX_ROWS; row++)
{
for (col = 0; col < MAX_COLS; col++)
printf("%8d", ary [row][col]);
printf("\n");
} // for
return 0;
} // main
Department of Engineering 29
Convert Table to 1-D array
#include <stdio.h>
#define ROWS 2
#define COLS 5
int main (void)
{
// Local Declarations
int table [ROWS] [COLS] =
{
{00, 01, 02, 03, 04},
{10, 11, 12, 13, 14}
}; // table
int line [ROWS * COLS];
Department of Engineering 30
Convert Table to 1-D array
for (int row = 0; row < ROWS; row++)
for (int column = 0; column < COLS; column++)
line[row * COLS + column] = table[row][column];
for (int row = 0; row < ROWS * COLS; row++)
printf(" %02d ", line[row]);
return 0;
}//main
/* Results:
00 01 02 03 04 10 11 12 13 14
*/
Department of Engineering 31