5 Arrays V2
5 Arrays V2
C-Programming Lanuage
5. Arrays
Course code:
1 CS 1003
Severin Kakeu
February 2024
Content
1. Introduction
2. Declaring array
3. Initializing array
4. Accessing array elements
5. Multi-dimensional arrays
6. Two-dimensional arrays
7. Accessing two-dimensional array elements
8. Passing arrays as function arguments
9. Return array from function
10.Exercises
11.Assignments
2
1. Introduction
C language provides a data structure called the array, which can store a fixed-size
sequential collection of elements of the same type. An array is used to store a collection
of data, but it is often more useful to think of an array as a collection of variables of the
same type.
All arrays TABLE consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
Instead of declaring individual variables, such as TABLE0, TABLE1, ..., and TABLE99,
you declare one array variable such as numbers and use TABLE[0], TABLE[1], and ...,
TABLE[99] as follows:
TABLE[0] TABLE[1] TABLE[2] TABLE[3] TABLE[4] TABLE[n-j] TABLE[n-1]
Syntax:
type arrayName[arraySize];
Now weight is a variable array which is sufficient to hold up-to 20 double numbers.
4
3. Initializing array
You can initialize array in C either one by one or using a single statement as follows:
Example:
double weight[5] = {100.0, 4.0, 4.4, 15.0, 60.0};
The number of values between braces { } can not be larger than the number of elements
that we declare for the array between square brackets []. Following is an example to
assign a single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:
double weight[] = {100.0, 4.0, 4.4, 15.0, 60.0};
You will create exactly the same array as you did in the previous example.
weight[5] = 50.0;
5
3. Initializing array
The above statement assigns element number 5th in the array a value of 50.0.
Array with 4th index will be 5th i.e. last element because all arrays have 0 as the index
of their first element which is also called base index.
0 1 2 3 4
6
4. Accessing array elements
An element is accessed by indexing the array name. This is done by placing the index
of the element within square brackets after the name of the array. For example:
double myweight = weight[4];
The above statement will take 5th element from the array and assign the value to salary
variable. Following is an example which will use all the above mentioned three
concepts: declaration, assignment and accessing arrays:
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 7; i++ ) {
7
4. Accessing array elements
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ )
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
8
5. Multi-dimensional arrays
C programming language allows multidimensional arrays. Here is the general form of
a multidimensional array declaration:
Syntax:
type name[size1][size2]...[sizeN];
int threedim[5][10][4];
9
6. Two-dimensional arrays
The simplest form of the multi-dimensional array is the two-dimensional array.
A two-dimensional array is, in essence, a list of one-dimensional arrays.
To declare a two-dimensional integer array of size x, y you would write something as
follows:
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C identifier. A
two-dimensional array can be think as a table which will have x number of rows and y
number of columns. A 2-dimentional array a, which contains three rows and four
columns can be shown as below:
10
7. Accessing Two-dimensional array elements
An element in 2-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array. For example:
int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can
verify it in the above diagram.
Let us check below program where we have used nested loop to handle a two
dimensional array:
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
11
7. Accessing Two-dimensional array elements
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; When the above code is compiled and
int i, j; executed, it produces the following
/* output each array element's value */ result:
for ( i = 0; i < 5; i++ )
a[0][0]: 0
{
a[0][1]: 0
for ( j = 0; j < 2; j++ ) a[1][0]: 1
{ a[1][1]: 2
printf("a[%d][%d] = %d\n", i,j, a[i][j] ); a[2][0]: 2
} a[2][1]: 4
a[3][0]: 3
}
a[3][1]: 6
return 0; a[4][0]: 4
} a[4][1]: 8
As explained above, you can have arrays with any number of dimensions, although it is
likely that most of the arrays you create will be of one or two dimensions.
12
8. Passing arrays as arguments of a function
If you want to pass a single-dimension array as an argument in a function, you would
have to declare function formal parameter in one of following three ways and all three
declaration methods produce similar results because each tells the compiler that an
integer pointer is going to be received.
In similar way you can pass multi-dimensional array as formal parameters.
Formal parameters as a pointer as follows. We will study what is pointer in one of
next chapters.
1. First way
void myFunction(int *param)
{
...
}
13
8. Passing arrays as arguments of a function
2. Second way
3. Third way
14
8. Passing arrays as arguments of a function
Example:
Now, consider the following function, which will take an array as an argument along
with another argument and based on the passed arguments, it will return average of the
numbers passed through the array as follows:
17
9. Return array from function
Now, consider the following function which will generate 10 random numbers and
return them using an array and call this function as follows:
#include <stdio.h>
/* function to generate and return random numbers */
int * getRandom( ){
static int r[10];
int i;
/* set the seed */
srand( (unsigned)time( NULL ) );
for ( i = 0; i < 10; ++i)
{
r[i] = rand();
printf( "r[%d] = %d\n", i, r[i]);
}
return r;
}
2. Write a program in C to read n number of values in an array and display them in reverse order.
Test Data :
Input the number of elements to store in the array :3
Input 3 number of elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 7
Expected Output : The values store into the array in reverse are : 7 5 2
3. Write a program in C to find the sum of all elements of the array. Go to the editor
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 2
element - 1 : 5
element - 2 : 8
Expected Output : Sum of all elements stored in the array is : 15 20
10. Exercises
1. Write a program in C to store elements in an array and print them.
Input 10 elements in the array :
element - 0 : 1
element - 1 : 1
element - 2 : 2
2. How to get the size of an array T
3. Does the function length can be apply to the Array structure?
4. Write a program in C to print or display the sum of two given matrices
5. Write a program in C to sort an array of ten integer elements
Expected Output :
The first matrix is :
22
34
The second matrix is :
22
34
6. Write a program in C to find the sum of all elements of the array.
21
11. Assignment (implementation and explanation)
1. Write a program in C to copy the elements of one array into another array. Go to the editor
2. Write a program in C to find the maximum and minimum elements in an array
3. Write a program in C to separate odd and even integers into separate arrays
4. Write a program in C to sort elements of an array in ascending order
5. Write a program in C to insert the values in the array (sorted list)
6. Write a program in C for a 2D array of size 3x3 and print the matrix.
Output:
The matrix is :
123
456
789
7. Write a program in C for adding two matrices of the same size (subtraction of two matrices)
The First matrix is :
12
34
The Second matrix is :
56
78
The Addition of two matrix is :
68
10 12
22