0% found this document useful (0 votes)
16 views13 pages

PF Lab09

PUCIT LAB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views13 pages

PF Lab09

PUCIT LAB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CC-112L

Programming Fundamentals

Lab 09

Arrays – I

Version: 2.0.0

Release Date: 01-12-2023

Department of Information Technology University


of the Punjab

Lahore, Pakistan
Array:
An array is a group of elements of the same type stored contiguously in memory. The
following figure shows an integer array containing five elements:

To refer to a particular location or element in the array, we specify the array’s name, followed
by the element’s position number in square brackets ([]). The first element is located at position
number 0. The position number is called the element’s subscript (or index). A subscript must
be a non-negative integer or integer expression.
Defining Array:
When you define an array, you specify its element type and number of elements. The
following definition reserve five elements for integer array “A”, which has subscripts
(indexes) in the range 0-9.
int A[10];
Array Elements:
Like variables, uninitialized array elements contain garbage values. The following figure
uses for statements to set array elements to one.

Initializer List:
We can initialize an array’s elements when defining the array by providing a comma separated
list of array initializers in braces “{}”.
If there are fewer initializers than array elements, the remaining elements are initialized to 0.
Static Local Array:
A static local array is created and initialized once, not each time the function is called. This
reduces program execution time. If static array is not explicitly initialized, then that array’s
elements are initialized to zero by default.
Following code contains a static local array:

The execution of above program is as follows:


• Execution starts from the main()
• At line 5 execution shifts to the function
• At line 11 array elements are initialized to 1,2,3 respectively
• At line 13 & 19, Array element are displayed 1,2,3 & 6,7,8 respectively
• As line 7 execution shifts again to the function
• Now at line 11 array elements are not reinitialized and they retain the value 6,7,8 respectively
• At line 13 & 19, Array element are displayed 6,7,8 & 11,12,13 respectively
Automatic Local Array:
A static automatic array is reinitialized each time the function is called.

The execution of above program is as follows:

• Execution starts from the main()


• At line 5 execution shifts to the function
• At line 11 array elements are initialized to 1,2,3 respectively
• At line 13 & 19, Array element are displayed 1,2,3 & 6,7,8 respectively
• As line 7 execution shifts again to the function
• Now at line 11 array elements are reinitialized to the value 1,2,3 respectively
• At line 13 & 19, Array element are displayed 1,2,3 & 6,7,8 respectively
Passing Arrays to Functions:
To pass an array argument to a function, specify the array’s name without any
brackets. E.g., If array has been defined as:

int Array[5];
The function call will be
function (Array, 5);
And function definition will be
void function (int Array[], int size);
Passing Individual Element:
To pass an individual array element to a function, the function call use the subscripted array
name in argument.
function (Array[2]);
and function definition will be
void function (int element);

Multidimensional Arrays:
Arrays can have multiple subscripts. A common use of multidimensional arrays is to represent
tables of values consisting of information arranged in rows and columns. To identify a particular
table element, we specify two subscripts:
• The first (by convention) identifies the element’s row.
• The second (by convention) identifies the element’s column.
Arrays that require two subscripts to identify a particular element commonly are called two-
dimensional arrays. Multidimensional arrays can have more than two subscripts.
Illustrating a Two-Dimensional Array:
The array contains three rows and four columns, so it’s said to be a 3 x 4 array. In general, an
array with m rows and n columns is called an m x n array. Every element in array a is identified
by a name of the form a[i][j], where a is the array name, and i and j are the subscripts that
uniquely identify each element. The element names in row 0 all have the first subscript 0.
Pictorial Representation:
Initializing a Double-Subscripted Array:
You can initialize a multidimensional array when you define it. For example, you can define
and initialize the two-dimensional array int b [2][2] with:
int b [2][2] = {{1, 2}, {3, 4}};
The values in the initializer list are grouped by row in braces. The values in the first set of
braces initialize row 0 and the values in the second set of braces initialize row 1. So, the values
1 and 2 initialize elements b [0][0] and b [0][1], respectively, and the values 3 and 4 initialize
elements b [1][0] and b [1][1], respectively. If there are not enough initializers for a given row,
that row’s remaining elements are initialized to 0. So, the definition:
int b [2][2] = {{1}, {3, 4}};
would initialize b [0][0] to 1, b [0][1] to 0, b [1][0] to 3 and b [1][1] to 4.
Example Code:

Output:
Explanation:
array1 Definition: The program defines three arrays of two rows and three columns. The
definition of array1 (line 6) provides six initializers in two sublists. The first sublist initializes
row 0 to the values 1, 2 and 3, and the second sublist initializes row 1 to the values 4, 5 and 6.
array2 Definition: The definition of array2 (line 10) provides five initializers in two sublists,
initializing row 0 to 1, 2 and 3, and row 1 to 4, 5 and 0. Any elements that do not have an
explicit initializer are initialized to zero automatically, so array2[1][2] is initialized to 0.
array3 Definition: The definition of array3 (line 14) provides three initializers in two sublists.
The first row’s sublist explicitly initializes the row’s first two elements to 1 and 2 and implicitly
initializes the third element to 0. The second row’s sublist explicitly initializes the first element
to 4 and implicitly initializes the last two elements to 0.

Variable Array Length:


To define an array you’ve to specify its size at compilation time. But what if you cannot
determine an array’s size until execution time? For cases in which an array’s size is not known at
compilation time rather it would be defined on the execution time, C programming language
provides variable length arrays (VLAs). Lengths of these arrays are determined by expressions
evaluated at execution time. Please note that the sizeof operator when used in variable length
array, it operates at run time instead of at compile time.

Example Code:
Output:

Explanation:
In the above program, we have declared and printed three variable length arrays. First, we
asked the user to enter the size for a one-dimensional array, two-dimensional and, multi-
dimensional array.
There are two user-defined functions oneDArray () and twoDArray () for printing arrays.
Function oneDArray () takes size as parameter whereas, twoDArray takes row and col as its
parameter. First, all the elements in the first row of the multi-dimensional array are filled, then
proceed for the second row and so on.
Lab Tasks

Task 01: Unique Elements


Write a C program which:
• Defines an array of size “5”
• Set Values of array by taking input from user
• Displays the unique elements on the Console
• If no element is unique then displays the message “No element is unique”

Input Output
23456 Unique Elements: 2, 3, 4, 5,
6
89 89 55 34 55 Unique Element: 34
33 33 33 33 33 No element is unique

Note: You may find it helpful to make this function


bool isUnique(int arr[], int size, int element)

Task 02: Reverse an Array


Write a C program which:
• Defines an Array of size “5”
• Initialize the array by taking input from user
• Reverse the elements of array i.e., element at first index should be on the last index and vice
versa

Input Output (After Reversing


Array)
23456 65432
9 13 12 5 99 99 5 12 13 9

Task 03: Fill the blank area


The following program:
• Defines an array
• Print the elements by calling a function
Task 04: Dry Run Programs.
(a) Dry Run the following programs and fill up the trace table:

Line No Output on Console


6 1,7
7 9,6
8 9,12
9 1,7
10 9,18

Task 05: Average Marks


Part (a)
Create a C program that: stores marks of 3 students for 5 tests in a two-dimensional array such
that each row in the array represents test scores for one student. Display the average of each
student’s test scores on console.
Sample Output:
Part (b):
Modify the above program such that it calculates and display average marks of each test.
Sample Output:

You might also like