Module 4A Arrays
Module 4A Arrays
Programming II
3
⚫They can also store derived data types
such as structures and pointers.
⚫In an array, the lowest address
corresponds to the first element while the
highest address to the last element as
shown in Fig. 4.1.
arrName[n-
arrName[0] arrName[1] arrName[2] … 1]
5
⚫The generic syntax for 1-dimensional
array declaration in C Language is:
dataType arrayName[arraySize];
⚫Based on this generic syntax, the various
ways to declare an array are as follows:
i)Array declaration by specifying size, e.g.
int arrSample1[10];
or
int n = 10;
int arrSample1[n];
6
ii)Array declaration by initializing
elements, e.g.
7
iii) Array declaration by specifying size
and initializing elements, e.g.
loop.
9
b) Demerits
i) It is a static structure, which implies that it only allows
a fixed number of elements that is decided during
declaration.
ii) Insertion and deletion of elements involves storing them
in consecutive memory locations, which involves costly
shifting operation.
iii) Allocation of memory more that is required leads to
wastage of memory space, while allocation of less
memory also cause problem.
10
Example 4.1.1
Write a C program that accepts 10 numerical values
from the users into an array, calculates the sum of the
values and outputs both the values and the sum on the
console.
11
#include <stdio.h> Example4.1.1 Solution
int main()
{
float arrValues[10];
float valSum = 0;
printf("\n\nEnter the elements of the 10 element array\n");
//Use for loop to accept values from users
for (int i = 0; i < 10; ++i)
{
printf("\nEnter value for element %d\n",i+1);
scanf("%f", &arrValues[i]);
valSum = valSum + arrValues[i]; //Compute the sum of the elements
}
printf("\n\n The entered values in the array are: \n");
//Use For loop to print the entered values
for (int i = 0; i < 10; ++i)
{
printf("Value %d:\n",i+1);
printf("%f\n\n", arrValues[i]);
}
printf("\n The sum of the array elements is: %f\n\n",valSum);
return 0;
}
12
4.1.2 Passing Arrays to a Function in C Language
⚫C programming language allows you to pass arrays
to functions.
⚫Only the name of the array is passed as an
argument in order to pass an entire array to a
function.
⚫ You can use the following syntax to call a function with
an array as the parameter:
//Array declaration
dataType arrayName[arraySize];
//Passing of an array to a function
fncResult = fncName(arrayName);
13
⚫The syntax for the function that is called is:
//Function definition with array as argument
dataType fncName(dataType arrayName[ ])
{
Code Statement(s);
}
⚫The use of [] in the function definition informs the
compiler that a one-dimensional array is being
passed to the function.
14
Example 4.1.2
Write a function to print the elements and another
function to compute the sum of the elements of the
array in Example 4.1.1
Note that the number of elements in the array should
be specified by the users and the two functions
should be called from the main program.
15
Example 4.1.2 Solution
#include <stdio.h>
int main()
{
float computeSum(float arrValues[],int intElement);
void printValues(float arrValues[],int intElement);
float sumResult;
int intElement; //Declare the number of elements in the array
printf("\nEnter the number of elements in the array: \n");
scanf("%d", &intElement);
//Declare the array of the specified number of elements
float arrValues[intElement];
18
4.1.3 Multidimensional Arrays in C Language
⚫Beyond the 1-dimesional array, which we have
considered so far, you can create an array of arrays
in C language, which is known as
multidimensional arrays.
⚫The simplest of the multidimensional array is
2-dimensional array with the syntax:
dataType arrayName[n][m];
⚫This can represent a matrix with n rows and m
columns as shown in Fig. 4.2.
19
Column Column … Column
1 2 m
Row ...
1 arrayName[0][0] arrayName[0][1] arrayName[0][m-1]
20
Example 4.1.3
Write a C program to enter the elements of a 3x3
matrix and a function to print out the entered values.
21
Example 4.1.3 Solution
#include <stdio.h>
int main()
{
void dispArrayElements(float arrMatrix[3][3]);
float arrMatrix[3][3];
// Take input using nested for loop
printf("\nEnter the elements of the matrix\n");
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("\nEnter arrMatrix %d %d: ", i + 1, j + 1);
scanf("%f", &arrMatrix[i][j]);
}
}
//Pass multi-dimensional(2-d) array to the display function similar to 1-d array
printf("\nDisplay the elements of the matrix\n");
dispArrayElements(arrMatrix);
return 0;
}
22
Example 4.1.3 Solution Contd’
void dispArrayElements(float arrMatrix[3][3])
{
printf("Displaying:\n");
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
printf("%.2f\t", arrMatrix[i][j]);
}
printf("\n");
}
return;
}
23