You are on page 1of 22

8-2 Using Arrays in C

In this section, we first show how to declare and define


arrays. Then we present several typical applications
using arrays including reading values into arrays,
accessing and exchanging elements in arrays, and
printing arrays.
Topics discussed in this section:
Declaration and Definition
Accessing Elements in Arrays
Storing Values in Arrays
Index Range Checking

Computer Science: A Structured Programming Approach Using C 1


FIGURE 8-6 The Scores Array

Computer Science: A Structured Programming Approach Using C 2


FIGURE 8-7 Declaring and Defining Arrays

Computer Science: A Structured Programming Approach Using C 3


How to declare an array?

Syntax: dataType arrayName[arraySize];

For example: float mark[5];

Here, we declared an array, mark, of floating-point type. And its size


is 5. Meaning, it can hold 5 floating-point values.
It's important to note that the size and type of an array cannot be
changed once it is declared.

Computer Science: A Structured Programming Approach Using C 4


Note
Only fixed-length arrays can be initialized when they are
defined. Variable length arrays must be initialized by
inputting or assigning the values.

Computer Science: A Structured Programming Approach Using C 5


FIGURE 8-8 Initializing Arrays

Computer Science: A Structured Programming Approach Using C 6


Initialization of C Array

The simplest way to initialize an array is by using the index of each element. We can
initialize each element of the array by using the index. Consider the following example.
1.marks[0]=80;//initialization of array  
2.marks[1]=60;  
3.marks[2]=70;  
4.marks[3]=85;  
5.marks[4]=75;  

                                                                                                                                             
                 

Computer Science: A Structured Programming Approach Using C 7


Access Array Elements
You can access elements of an array by indices.
Suppose you declared an array mark as above. The first element is mark[0], the
second element is mark[1] and so on.

Few keynotes:
Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element

If the size of an array is n, to access the last element, the n-1 index is used. In
this example, mark[4]

Suppose the starting address of mark[0] is 2120d. Then, the address of


the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so
on.
This is because the size of a float is 4 bytes.

Computer Science: A Structured Programming Approach Using C 8


C array example

Output:

Computer Science: A Structured Programming Approach Using C 9


Example:Calculate Average

Computer Science: A Structured Programming Approach Using C 10


Output

Computer Science: A Structured Programming Approach Using C 11


Note
One array cannot be copied to another using assignment.

Computer Science: A Structured Programming Approach Using C 12


FIGURE 8-9 Exchanging Scores—the Wrong Way

Computer Science: A Structured Programming Approach Using C 13


FIGURE 8-10 Exchanging Scores with Temporary Variable

Computer Science: A Structured Programming Approach Using C 14


PROGRAM 8-2 Squares Array

Computer Science: A Structured Programming Approach Using C 15


PROGRAM 8-2 Squares Array

Computer Science: A Structured Programming Approach Using C 16


8-7 Two-Dimensional Arrays
The arrays we have discussed so far are known as one-
dimensional arrays because the data are organized
linearly in only one direction. Many applications
require that data be stored in more than one
dimension. One common example is a table, which is
an array that consists of rows and columns.

Topics discussed in this section:


Declaration
Passing A Two-Dimensional Array

Computer Science: A Structured Programming Approach Using C 17


FIGURE 8-34 Two-dimensional Array

Computer Science: A Structured Programming Approach Using C 18


Declaration of two dimensional Array in C

syntax to declare the 2D array :

data_type array_name[rows][columns];  

Consider the following example.

int twodimen[4][3];  

Here, 4 is the number of rows, and 3 is the number of columns.

Computer Science: A Structured Programming Approach Using C 19


Initialization of 2D Array in C
Initialization of 2D Array

There are two ways to initialize a two Dimensional arrays during declaration.
int arr[4][3]={{1,2,3},
{2,3,4},
{3,4,5},
{4,5,6}}; 

1. int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };OR
2. int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Although both the above declarations are valid, I recommend you to use the first
method as it is more readable, because you can visualize the rows and columns of 2d
array in this method.

Computer Science: A Structured Programming Approach Using C 20


Two dimensional(2D) Array Example

Computer Science: A Structured Programming Approach Using C 21


Output

Computer Science: A Structured Programming Approach Using C 22

You might also like