Q)what is an array.
explain decleration and initilization of 1D
array in C
An array is a fundamental data structure that stores the multiple
values of similar datatype under a single variable nameThe
elements are stored in contiguous memory locations.
Declaring a 1D Array in C
Declaration tells the compiler the name of the array, the type of data it will
hold, and its size
Syntax
The general syntax for declaring a 1D array in C is:
dataType arrayName[arraySize];
dataType:The type of elements the array will store (e.g., int, char,
float).
ArrayName :Array name shoulad be valid identifier.
ArraySize :It specifies the number of elements the array can hold.
Example of Declaration
To declare an array named scores that can hold 5 integer values:
int scores[5];
• This creates an array named scores capable of storing 5 integers.
• The valid indices for this array are 0, 1, 2, 3, and 4.
Initialization of one-dimensional array
Assigning the required values to an array elements before processing is
called initialization.
Syntax:
data type array_name[expression]={v1,v2,v3…,vn};
Where
datatype can be char,int,float,double
array name is the valid identifier
size is the number of elements in array
v1,v2,v3……..vn are values to be assigned.
The various ways of initializing arrays are as follows:
1. Compile-Time Initialization (during declaration):
a)Full Initialization: All elements are assigned specific
values at the time of declaration.
int numbers[5] = {10, 20, 30, 40, 50};
In this example,numbers[0]is initialized to 10 numbers[1]to 20, and
so on.
b)Partial Initialization:If fewer values are provided than
the array size, the remaining elements are automatically
initialized to 0.
int values[5] = {1, 2}; // values becomes {1, 2, 0, 0, 0}
c)Initialization Without Specifying Size:The compiler
automatically determines the array size based on the
number of provided initial values.
int marks[] = {75, 80, 85}; // marks becomes an array of size 3
d)Zero Initialization:All elements can be initialized to 0
by providing a single 0 in the initializer list.
int data[5] = {0}; // All elements of data are set to 0
2. Run-Time Initialization (after declaration):
• Using Loops:Values can be assigned to array elements
during program execution using loops, often by taking input
from the user.
int arr[3];
for (int i = 0; i < 3; i++) {
printf("Enter value for arr[%d]: ", i);
scanf("%d", &arr[i]);
}
Q)How can we declare and initialize 2D arrays? Explain with
examples.
An array consisting of two subscripts is known as two-
dimensional array. In two dimensional arrays the array is divided
into rows and columns.
Declaration:-
Syntax: data_type array_name[row_size][column_size];
Ex:- int arr[3][3];
where first index value shows the number of the rows and second index
value shows the number of the columns in the array.
Initializing two-dimensional arrays:
Like the one-dimensional arrays, two-dimensional arrays may be
initialized by following their declaration with a list of initial
values enclosed in braces.
Ex: int a[2][3]={0,0,0,1,1,1};
initializes the elements of the first row to zero and the second row
to one. The initialization is done row by row.
The above statement can also be written as
int a[2][3] = {{ 0,0,0},{1,1,1}};
by surrounding the elements of each row by braces.
We can also initialize a two-dimensional array in the form of a matrix as
shown below
int a[2][3]={
{0,0,0},
{1,1,1}
};
When the array is completely initialized with all values, explicitly we need
not specify the size of the first dimension.
Ex: int a[][3]={
{0,2,3},
{2,1,2} };
If the values are missing in an initializer, they are automatically set to zero.
Ex: int a[2][3]={
{1,1}, {2}
};
Will initialize the first two elements of the first row to one, the first
element of the second row to two and all other elements to zero.
Q)Define multi-dimensional arrays? How to declare multi-
dimensional arrays?
Multidimensional arrays are often known as array of the arrays. In
multidimensional arrays the array is divided into rows and columns,
Syntax:
data_type array_name[size1][size2][size3]------[sizeN];
Where data_type is the type of elements,array_name is the
identifier, and size1 through sizeN represent the size of each
[Link] example, a 2D array (matrix) would be int
matrix[3][4];for 3 rows and 4 columns.
Initialization Methods:
a)Initialization at Declaration with Nested Braces.
Values can be assigned directly when the array is declared
using nested curly braces, reflecting the array's
dimensions.
int matrix[2][3] = {
{1, 2, 3}, // First row
{4, 5, 6} // Second row
};
For a 3D array:
int arr3D [2][2][3] = {
{ {1, 2, 3}, {4, 5, 6} }, // First "layer"
{ {7, 8, 9}, {10, 11, 12} } // Second "layer"
};
b)Initialization at Declaration without Inner Braces
(Partial or Full):
If inner braces are omitted, the values are assigned in row-
major [Link] fewer values are provided than the array can
hold, the remaining elements are initialized to zero.
int matrix[2][3] = {1, 2, 3, 4, 5, 6}; // Initializes all elements
int partial_matrix[2][3] = {1, 2, 3}; // Initializes first row, rest are zeros
Q) Write the difference between break and continue