Q4.Why arrays need in programming?
When we have small number of objects or numbers we can use regular variables (v1, v2, v3,..), but if we want to store large number of instances or values, manage them using regular variables, it becomes difficult to do. The idea of an array is to represent multiple values of a single variable.
Q5. How can we calculate the size of an array in C?
The size of an array is calculated using the sizeof operator in C. First, we get the size of the entire array and divide it by the size of each element type.
Q6. What is the difference between Pointers and Arrays in C?
Following list gives the difference between Arrays and Pointers:
Pointer:
i. A pointer is a derived data type that can store the address of other variables.
ii. Pointers are allocated at run time.
iii. The pointer is a single variable.
iv. Dynamic in Nature
Array:
i. An array is a homogeneous collection of items of any type such as int, char, etc.
ii. Arrays are allocated at compile time.
iii. An array is a collection of variables of the same type.
iv. Static in Nature.
Q7. What is the use of array?
Following is the uses of array in C:
Grouping related data:
Arrays allows us to group related data items values of the same data type. For example, you can use an array to store a list of characters, integers or any other type of data.
Serial access or sequential access:
Arrays allow sequential access to elements using index values. This allows us to easily iterate through the elements of an array using loops such as while or for loops.
Efficient storage:
Arrays allocate a contiguous block of memory to store elements and make memory management more efficient. Array elements can be accessed directly by their index without searching or navigating through data structures.
Data manipulation:
Arrays provide an easy way to manage and process data sets. You can perform various operations on array elements, such as searching, sorting, filtering, and performing mathematical calculations.
Efficient Algorithms:
Many algorithms and data structures use arrays for their implementation. Arrays are the basic building blocks for data structures such as stacks, rows, and matrices. They also play an important role in searching and sorting algorithms.
Compact Representation:
Arrays provide compact and efficient use of memory for storing large amounts of data. They allow you to access and manage large data sets without consuming too much memory.
Q9.What are Types of Arrays in C?
There are following types array in C based on the number of dimensions.
1-D Array: One-dimensional Arrays
2-D, 3-D : Multi-dimensional Arrays
1-D Array: One-dimensional Arrays in C
Also known as a 1-D array or vector, a one-dimensional array in the C has only one dimension. These are represented by a row or column.
Syntax :
array_name [size];
2-D, 3-D : Multi-dimensional Arrays
They have more than one dimension, such as 2-D and 3-D arrays. There can be an ‘n’ number of dimensions in arrays, but they can increase complexity and occupy more space, so they are not used widely.
Two-Dimensional Arrays in C
They have two dimensions and can be visualized in rows and columns organized in a two-dimensional plane.
Syntax:
Name_of_array[s1] [s2];
Here,
S1 is the size of the first dimension.
S2 is the size of the second dimension.
3-D Arrays in C
A three-dimensional array has three dimensions and can be supposed of as a collection of two-dimensional arrays that form a third dimension.
Syntax:
Name_of_array [s1] [s2] [s3];