Professional Documents
Culture Documents
C Arrays: Unit Ix
C Arrays: Unit Ix
C Arrays: Unit Ix
UNIT IX
C ARRAYS
I. Module Overview
A fixed number of data items which are stored contiguously and the elements
can be accessed by an index. It is also a finite ordered collection of homogenous
elements.
123
1st Semester | SY 2020 – 2021
Declaring Arrays
double balance[10];
Initializing Arrays
You can initialize an array in C either one by one or using a single statement
as follows –
The number of values between braces { } cannot be larger than the number
of elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the
initialization is created. Therefore, if you write –
You will create exactly the same array as you did in the previous example.
Following is an example to assign a single element of the array –
balance[4] = 50.0;
The above statement assigns the 5th element in the array with a value of
50.0. All arrays have 0 as the index of their first element which is also called the
base index and the last index of an array will be total size of the array minus 1.
Shown below is the pictorial representation of the array we discussed above −
124
1st Semester | SY 2020 – 2021
The above statement will take the 10th element from the array and assign
the value to salary variable. The following example Shows how to use all the
three above mentioned concepts viz. declaration, assignment, and accessing
arrays –
#include <stdio.h>
int main()
{
int avg = 0;
int sum =0;
int x=0;
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
When the above code is compiled and executed, it produces the following
result –
Enter number 1
10
Enter number 2
10
Enter number 3
20
Enter number 4
40
Average of entered number is: 20
125
1st Semester | SY 2020 – 2021
Suppose, if we want to display the elements of the array then we can use
the for loop in C like this.
In the above example, we have just declared the array and later we
initialized it with the values input by user. However you can also initialize
the array during declaration like this:
126
1st Semester | SY 2020 – 2021
Arrays in Detail
Arrays are important to C and should need a lot more attention. The
following important concepts related to array should be clear to a C programmer
–
1 Multi-dimensional arrays
4 Pointer to an array
You can generate a pointer to the first element of an array by
simply specifying the array name, without any index.
127
1st Semester | SY 2020 – 2021
An array of arrays is known as 2D array. The two dimensional (2D) array in C
programming is also known as matrix. A matrix can be represented as a table of rows
and columns. Before we discuss more about two Dimensional array lets have a look at
the following C program.
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
}
}
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
}
return 0;
}
Output:
128
1st Semester | SY 2020 – 2021
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
OR
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.
We already know, when we initialize a normal array (or you can say one dimensional
array) during declaration, we need not to specify the size of it. However that’s not the
case with 2D array, you must always specify the second dimension even if you are
specifying elements during the declaration. Let’s understand this with the help of few
examples –
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
How to store user input data into 2D array
We can calculate how many elements a two dimensional array can have by using this
formula:
The array arr[n1][n2] can have n1*n2 elements. The array that we have in the example
below is having the dimensions 5 and 4. These dimensions are known as subscripts. So
this array has first subscript value as 5 and second subscript value as 4.
So the array abc[5][4] can have 5*4 = 20 elements.
To store the elements entered by user we are using two for loops, one of them is a
nested loop. The outer loop runs from 0 to the (first subscript -1) and the inner for loops
runs from 0 to the (second subscript -1). This way the the order in which user enters the
elements would be abc[0][0], abc[0][1], abc[0][2]…so on.
#include<stdio.h>
int main(){
/* 2D array declaration*/
int abc[5][4];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<5; i++) {
for(j=0;j<4;j++) {
129
1st Semester | SY 2020 – 2021
130
1st Semester | SY 2020 – 2021
int i;
double avg;
double sum = 0;
return avg;
}
/* function declaration */
double getAverage(int arr[], int size);
int main () {
131
1st Semester | SY 2020 – 2021
return 0;
}
When the above code is compiled together and executed, it produces the
following result −
Average value is: 214.400000
As you can see, the length of the array doesn't matter as far as the function is
concerned because C performs no bounds checking for formal parameters.
Instruction: The students will write a C program for the following problems:
132
1st Semester | SY 2020 – 2021
Sample Output:
Input the number of elements to be stored in the array :3
Input 3 elements in the array:
element - 0: 45
element - 1: 25
element - 2: 21
Maximum element is: 45
Minimum element is: 21
Rubric for Assessment to be used for creating C programs (for review with
the students to be handled):
V. Self-Check
MULTIPLE CHOICE.
133
1st Semester | SY 2020 – 2021
134
1st Semester | SY 2020 – 2021
REFERENCES
https://www.tutorialspoint.com/cprogramming/c_arrays.htm
135