INTRODUCTION TO ARRAY
- ADVANTAGE AND DISADVANTAGE
- DECLARATION OF ARRAY
- INITIALIZATION OF ARRAY
- 2-D ARRAY/INITIALIZATION
- PASSING ARRAY TO A FUNCTION
The C array comes in handy when you need to hold related
elements. For instance, we don't need to construct separate variables for the
marks in each topic if we need to keep a student's marks across six subjects.
Alternatively, we can create an array that can store the marks
for every subject at consecutive memory locations.
We can easily access the elements by utilizing the array. To
access the items of the array, a few lines of code are all that are needed.
Array has some properties also let’s understand
them
· Each entry in the array, for
instance, int = 4 bytes, has the same size and data type.
· Array elements are stored in
memory locations, with the first member being stored in the smallest position.
· Since we can find the
addresses of array items using a specified base address and data element size,
you can use the array items at random.
1.
Code Optimization: Use less code to access data more
effectively.
2.
A for loop may be used to rapidly and easily obtain array
elements, which makes traversal a joy.
3.
Simple sorting: A few lines of code are all that are needed
to sort the elements in an array.
4. We can randomly access any element in an array.
Disadvantage
Fixed-size: Whatever size we specify while defining the
table, we cannot exceed the limit. So it doesn't grow dynamically like
LinkedList, which we'll learn about later.
we can declare the array like
Initialization of array
The basic method to initialize or identify the element is indexing. We can initialize each index to a particular element.
Suppose:
As we had seen above marks are the name of the array and the size
of the array is 5 then:
Marks0 = 70; (initializing point)
Marks1 = 60;
Marks2 = 80;
Marks3 = 55;
Marks4 = 40;
70 |
60 |
80 |
55 |
40 |
Marks0 |
Marks1 |
Marks2 |
Marks3 |
Marks4 |
Let us understand this by a program
#include<stdio.h>
int main(){
Array declaration
with initialization
Additionally, we may set the array's size at
declaration time. Let's see how
int marks [5]={20,40,60,80,30}
In these circumstances, we can also initialize the
value straight away without specifying the array's size.
int marks []={20,40,60,80,30}
Let us
understand this a C program
Let us understand some basic simple programs in C having arrays
We are now doing sorting of an array having bubble sort method to sort the array in ascending order
Try to have
this program and comment your output.
Now we will try to print the largest as well as second largest element in the array
Try to have this program and comment on your output.
2-D (two
dimensional) Array
A
two-dimensional array can be defined as a collection of arrays. A matrix is
really just a collection of rows and columns, and that is how a 2D table is
organized. To create a data structure that is akin to a relational database, 2D
tables are created. It makes managing enormous volumes of data at once easier
and can be supplied to other activities as needed.
Let’s
understand the syntax first
Data_type
array_name[rows][columns]
Example
Int
marks[2][4];
Here int
shows the data type
Marks shows
the name of the array
2 show the
number of rows
4 shows the number of columns
Initialization
of 2-D Array
When declaring and initializing a 1D array
simultaneously, sizing is unnecessary. On the other hand, 2D arrays will not be
able to use this. The second dimension of the array needs to be specified.
An illustration of declaring and defining a two-dimensional array is provided
here:
Let us
understand this by an example
Int
arr[4][3]={{1,2,3},{4,5,6},{7,8,9},{6,4,5}};
Let us understand this by a program
let
us try to store elements in a matrix and print it
Return an array in C
let us understand how to pass an array in a function
The getarray() function is used in the preceding code snippet with an array of recently generated elements, arr[]. Basically, the getarray() method prints each element of the array arr[].
Output
Elements of
an array are 45 67 78 90
Passing an array to a function
Let
us understand the program
In the code
above, we passed an array as a pointer to the function. The printarray()
function prints the elements of an array.
Output
Elements of
an array are A, B, C, D, E
Returning pointer pointing to the array
In the
program above, the function getarray() returns the variable arr.
Although a
local variable is assigned a value, the memory space was illegally allocated by
the stack function. All variables are removed from the stack when main() is
called again. Given that this software replaces a deleted region in memory, it
is safe to say that it creates a segmentation fault.
Array
outside function (segmentation fault )
There are
three ways of returning arrays to a function
- Using dynamically
allocated array
- Using a static array
- Using structure
Let us figure out all three by a C program
Returning array by passing an array which is to be
returned as a parameter to the function.
Using static variable
Because structures are user-defined data types, they can include
collections of different kinds of objects. The next stage is to build code
that, when it runs, will produce an array using structure.
No comments:
Post a Comment