C Programming UNIT 3.2 Arrays
C Programming UNIT 3.2 Arrays
Language
UNIT 3 (Arrays)
Index
Single and two-dimensional arrays
Initializers
Array parameters
Example of iterative programs using arrays
Use of array in matrix multiplication
Arrays in C language
An array is a kind of data structure that can store a fixed-size sequential
collection of elements of the same type.
An array is used to store a collection of data.
OR
An array is a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ...,
and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual
variables. A specific element in an array is accessed by an index.
Arrays are used to store multiple values in a single variable,
instead of declaring separate variables for each value.
Properties of Array
The array contains the following properties.
1. Each element of an array is of the same data type and carries the
same size, i.e., int = 4 bytes.
2. Elements of the array are stored at contiguous memory locations.
3. Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the data element’s size.
Advantage of C Array
1. Code Optimization: Less code to access the data.
2. Ease of traversing: By using the for loop, we can retrieve the elements
of an array easily.
3. Ease of sorting: To sort the elements of the array, we need a few lines
of code only.
4. Random Access: We can access any element randomly using the
array.
Disadvantage of C Array
1. Fixed Size: Whatever size, we define at the time of declaration of the
array, we can't exceed the limit. So, it doesn't grow the size
dynamically like LinkedList which you will learn in Data Structure
subject.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the
elements and the number of elements required by an array as follows
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an
integer constant greater than zero and type can be any valid C data
type.
For example, to declare a 10-element array called balance of type
int, use this statement −
int balance[10];
Initializing Arrays
You can initialize an array in C either one by one or using a single statement
as follows
int balance[5] = {1000, 2, 3, 7, 50};
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 −
int balance[] = {1000, 2, 3, 7, 50};
Following is an example to assign a single element of the array −
balance[4]=50;
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.
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 −
int salary = balance[9];
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 −
C program to read and display array
elements
1. #include<stdio.h> 9. scanf("%d", &arr[i]);
2. int main() { 10. }
3. int i, arr[50], num; 11. //Printing of all elements of array
4. printf("\nEnter no of elements :"); 12. for (i = 0; i < num; i++) {
5. scanf("%d", &num); 13. printf("\narr[%d] = %d", i, arr[i]);
6. //Reading values into Array 14. }
7. printf("\nEnter the values :"); 15. return (0);
8. for (i = 0; i < num; i++) { 16. }
Two-dimensional Array
A two-dimensional array is essentially 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 considered a table with x number of
rows and y number of columns. A two-dimensional array a, which contains
three rows and four columns can be shown as follows −
In the given
example, we
take an integer
array arr, and
loop over the
elements of this
array using
While loop.
Matrix computation 18. printf("Enter b%d%d: ", i + 1, j + 1);
19. scanf("%f", &b[i][j]);
20. }
1. #include <stdio.h> 21. // adding corresponding elements of two
2. int main() arrays
3. { 22. for (int i = 0; i < 2; ++i)
4. float a[2][2], b[2][2], result[2][2]; 23. for (int j = 0; j < 2; ++j)
5. // Taking input using nested for loop 24. {
6. printf("Enter elements of 1st matrix\n"); 25. result[i][j] = a[i][j] + b[i][j];
7. for (int i = 0; i < 2; ++i) 26. }
8. for (int j = 0; j < 2; ++j) 27. // Displaying the sum
9. { 28. printf("\nSum Of Matrix:");
10. printf("Enter a%d%d: ", i + 1, j + 1); 29. for (int i = 0; i < 2; ++i)
11. scanf("%f", &a[i][j]); 30. for (int j = 0; j < 2; ++j)
12. } 31. {
13. // Taking input using nested for loop 32. printf("%.1f\t", result[i][j]);
14. printf("Enter elements of 2nd matrix\n"); 33. if (j == 1)
15. for (int i = 0; i < 2; ++i)
34. printf("\n");
16. for (int j = 0; j < 2; ++j)
35. }
17. {
36. return 0;
37. }