Array in C programming Class 11 and 12
Array
An array is a collection of elements of the same data type, stored in contiguous memory locations, and can be referenced by an index. Arrays allow you to store multiple values in a single variable and then access these values by referencing the array variable and the corresponding index.
The syntax for declaring an array in C is as follows:
data_type array_name[array_size];
where data_type is the type of data to be stored in the array (e.g. int, float, char, etc.), array_name is the name of the array, and array_size is the number of elements that the array can hold. For example:
int arr[5];
declares an integer array named arr of size 5.
To access the elements of an array, you use the array name followed by the index of the element in square brackets. The first element has an index of 0, the second element has an index of 1, and so on. For example:
arr[0] = 1;
arr[1] = 2;
In C programming language, arrays can be of different types:
1.One-dimensional arrays: This is the simplest form of an array, which stores elements in a single row.
2.Multi-dimensional arrays: Multi-dimensional arrays, also known as 2D, 3D or n- dimensional arrays, can store elements in multiple rows and columns.
3.Jagged arrays: Jagged arrays are arrays of arrays, where each row can have a different number of elements.
4.Character arrays: Character arrays are used to store strings in C, where each element of the array is a character.
5.Array of pointers: Array of pointers is an array where each element of the array is a pointer to another array.
6.Array of structures: An array of structures is an array where each element of the array is a structure.
7.Array of unions: An array of unions is an array where each element of the array can store different data types, but only one data type can be stored at a time.
1.One-dimensional arrays: One-dimensional arrays are the simplest form of arrays and store elements in a single row. You can think of it as a list of values. The syntax to declare a one-dimensional array is as follows:
data_type array_name[array_size];
where data_type is the type of data to be stored in the array, array_name is the name of the array, and array_size is the number of elements the array can hold. For example:
int arr[5];
declares an integer array named arr of size 5.
2.Multi-dimensional arrays: Multi-dimensional arrays, also known as 2D or 3D arrays, can store elements in multiple rows and columns. In other words, they can be thought of as a table of values, where each cell of the table is an element of the array. The syntax to declare a two-dimensional array is as follows:
data_type array_name[row_size][column_size];
where data_type is the type of data to be stored in the array, array_name is the name of the array, row_size is the number of rows the array can hold, and column_size is the number of columns the array can hold.
For example:
int arr[3][3];
declares a two-dimensional integer array named arr of size 3x3.
3. Character arrays: Character arrays are used to store strings in C, where each element of the array is a character. A string in C is a null-terminated character array, which means the last element of the array is always a null character ('\0'). The syntax to declare a character array is as follows:
char array_name[array_size];
where array_name is the name of the array, and array_size is the number of characters the array can hold (including the null character).
For example:
char str[10];
declares a character array named str of size 10.
WAP to input 4 elements and displays them in a 2x2 matrix: #include <stdio.h>
int main() {
int arr[2][2];
int i, j;
printf("Enter the elements of the 2x2 matrix:\n"); for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
scanf("%d", &arr[i][j]); }
}
printf("The 2x2 matrix is:\n"); for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", arr[i][j]); }
printf("\n"); }
return 0; }
A program in C that asks the user to input a name and displays it:
#include <stdio.h>
int main() {
char name[20];
printf("Enter your name: "); scanf("%s", name);
printf("Hello, %s!\n", name);
return 0; }
Write a program in C that asks the user to input the names of 12 months and displays them:
#include <stdio.h> int main() {
char months[12][10];
int i;
printf("Enter the names of the 12 months:\n"); for (i = 0; i < 12; i++) {
scanf("%s", months[i]); }
printf("The names of the 12 months are:\n"); for (i = 0; i < 12; i++) {
printf("%d. %s\n", i + 1, months[i]); }
return 0; }
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...
