0% found this document useful (0 votes)
16 views

Module 4A Arrays

Uploaded by

Daniel Joseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Module 4A Arrays

Uploaded by

Daniel Joseph
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Course Title: Applied Computer

Programming II

Course Code: GEC 225


Omega Semester
2022/2023
Module 4
Arrays and Pointers
4.1.0 Arrays
⚫An array in C language is a collection
of similar data items stored at contiguous
memory locations, with elements that
can be accessed randomly using the
indices.
⚫They are variables that can store
multiple values of primitive data types
such as int, float, double, char, etc.

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]

First Element Last Element

Fig. 4.1: Structure of an 1-Dimensional Array


4
⚫As illustrated in Fig. 4.1, array elements
are accessed by using an integer index.

⚫The index starts with 0 and goes till size


of array minus 1 (i.e. n-1).

⚫Note that the name of the array is a


pointer to the first element of array.

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.

int arrSample2 [ ] = {10, 20, 30, 40,50};

⚫ Note that for this example, the


compiler creates an array of size 5 and
initializes the elements with the specified
integer values.

7
iii) Array declaration by specifying size
and initializing elements, e.g.

int arrSample3 [10] = {10, 20, 30, 40, 50};

⚫Note that for the example above, the


compiler creates an array of size 10,
initializes the first 5 elements as
specified above while the remaining 5
elements are set to 0.
8 Monday, 30 August 21
4.1.1 Merits and Demerits of Arrays in C Language
a) Merits
i) It allows random and easy access to the elements
using array index.

ii) It helps to achieve less line of codes since a single

array of multiple elements can be created.

iii) It is easy to sort numbers with less line of codes.

iv) It is easy to traverse through an array with a single

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];

printf("\n\nEnter the elements of the array\n");


//Use for loop to accept values from users
for (int i = 0; i < intElement; ++i)
{
printf("\nEnter value for element %d\n",i+1);
scanf("%f", &arrValues[i]);
}
16
Example 4.1.2 Solution Cont’d
printf("\n\n The entered values in the array are: \n");
//Call the function to print the elements of the array
printValues(arrValues, intElement);
//Call the function to compute the Sum of the elements
sumResult = computeSum(arrValues, intElement);
printf("\n The sum of the array elements is: %f\n\n",sumResult);
return 0;
}

//Function to calculate the sum of the array elements


float computeSum(float arrValues[],int intElement)
{
float valSum = 0;
for (int i = 0; i < intElement; ++i)
{
valSum = valSum + arrValues[i];
}
return valSum;
}
17
Example 4.1.2 Solution Cont’d
//Function to print the elements of the array
void printValues(float arrValues[],int intElement)
{
//Use for loop to print the elements of the array
for (int i = 0; i < intElement; ++i)
{
printf("Value %d:\n",i+1);
printf("%f\n\n", arrValues[i]);
}
return;
}

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]

Row arrayName[1][0] arrayName[1][1] ... arrayName[1][m-1]


2

… ... ... ... ...

Row arrayName[n-1][0] arrayName[n-1][1] ... arrayName[n-1][m-1]


n

Fig. 4.2: Structure of an 2-Dimensional Array

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

You might also like