Introduction
C language provides four basic data types: int, char, float, and double. These basic data types are useful but limited in handling large amounts of data. As programs grow more complex, managing data becomes challenging, leading to longer variable names for uniqueness and difficulty in focusing on correct coding.
Arrays offer a solution by allowing multiple data items to be declared and accessed with a single identifier, simplifying data management. For example, processing a list of numbers, marks, or enrollment numbers can be cumbersome with individual variables. Using arrays simplifies this process.
An array is a collection of similar data elements stored in adjacent memory locations, referred to by a single array name. In C, arrays must be declared and defined before use, specifying the array name, element type, and size. For example, to store marks of 100 students, you can define an array as int ar[100];
instead of using 100 individual variables.
Each element in an array is accessed using a subscript, and arrays allow efficient memory usage and easier data management. This unit explains the use of arrays, their types, and how to declare and initialize them with examples.
Objectives
After going through this unit you will be able to:
Key Features of Arrays:
Array Declaration
Syntax: data-type array_name [constant-size]; where, Data-type: The type of elements stored and Constant-size: The number of elements.
Examples:
Restrictions:
Size Specification
Use symbolic constants for array sizes for easier modifications. Example:
#define SIZE 50
int arr[SIZE];
Array Initialization in C
Arrays can be initialized at the time of declaration with values enclosed in braces and separated by commas.
Syntax. data-type array-name[size] = {val1, val2, ..., valn}; where,
val1: Value for the first element, val2: Value for the second element, valn: Value for the nth element.
Examples:
Character Array Initialization:
Character arrays are used for strings in C, automatically including a null character \0
at the end.
Examples:
char thing[4] = "TIN"; // Equivalent to {'T', 'I', 'N', '\0'}
char thing[] = "TIN"; // Automatically includes '\0'
A subscript is used to refer to individual elements in an array, ranging from 0 to SIZE-1.
Example:
char country[] = "India";
int stud[] = {1, 2, 3, 4, 5};
Array Refrences:
Element no. | Subscript | country Array | value | stud Array | value |
1 | 0 | country[0] | 'I' | stud[0] | 1 |
2 | 1 | country[1] | 'n' | stud[1] | 2 |
3 | 2 | country[2] | 'd' | stud[2] | 3 |
4 | 3 | country[3] | 'i' | stud[3] | 4 |
5 | 4 | country[4] | 'a' | stud[4] | 5 |
Subscripts provide a way to access and manipulate individual elements within an array.
In certain applications, arrays need initial values and can be defined globally or as static local arrays. The provided example demonstrates summing marks from two subjects for three students and calculating their averages.
Example:
/* Program to display the average marks of 3 students */
#include <stdio.h>
#define SIZE 3
main() {
int i;
float stud_marks1[SIZE], stud_marks2[SIZE], total_marks[SIZE], avg[SIZE];
// Input marks for subject 1
printf("\n Enter the marks in subject-1 out of 50 marks: \n");
for(i = 0; i < SIZE; i++) {
printf("Student no. =%d Enter the marks= ", i+1);
scanf("%f", &stud_marks1[i]);
}
// Input marks for subject 2
printf("\n Enter the marks in subject-2 out of 50 marks \n");
for(i = 0; i < SIZE; i++) {
printf("Student no. =%d Please enter the marks= ", i+1);
scanf("%f", &stud_marks2[i]);
}
// Calculate and display averages
for(i = 0; i < SIZE; i++) {
total_marks[i] = stud_marks1[i] + stud_marks2[i];
avg[i] = total_marks[i] / 2;
printf("Student no.=%d, Average= %f\n", i+1, avg[i]);
}
}
O/p:
Enter the marks in subject-1 out of 50 marks:
Student no. = 1 Enter the marks= 23
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 42
Enter the marks in subject-2 out of 50 marks:
Student no. = 1 Enter the marks= 31
Student no. = 2 Enter the marks= 35
Student no. = 3 Enter the marks= 40
Student no. = 1 Average= 27.000000
Student no. = 2 Average= 35.000000
Student no. = 3 Average= 41.000000
Multi-dimensional arrays are arrays with more than one dimension, used to represent data in a grid or table format.
Syntax:
datatype array_name[size1][size2]...[sizeN];
where datatype is the type of elements (e.g., int, double), and size1, size2, ..., sizeN are the sizes of each dimension.
Example:
An 8x8 grid can be represented using a two-dimensional array, similar to algebraic notation in chess.
Note: Arrays can have multiple dimensions, but higher dimensions increase memory usage. For instance, a 20x20x20x20 array of double-precision numbers uses about 1.28 MB.
Two-Dimensional Arrays
Two-dimensional arrays use two indices, e.g., array[rows][columns]. The upper left element is array[0][0], the next is array[0][1], etc. Multi-dimensional arrays are stored linearly in memory, with the last index varying fastest.
Syntax for a two-dimensional array: datatype array_name[size1][size2];. Example for an 8x8 integer array: int chessboard[8][8];
Example:
int table[2][3] = { {1, 2, 3}, {4, 5, 6} };
Results in:
table[0][0] = 1;
table[0][1] = 2;
table[0][2] = 3;
table[1][0] = 4;
table[1][1] = 0;
table[1][2] = 0;
No Bounds Checking: C does not check array bounds, so accessing out-of-bounds elements can cause errors. It is the programmer's responsibility to ensure valid indices are used.
John Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
ReplyJohn Doe
5 min agoLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Reply