Array (Numeric Array) is the collection of the homogenous data types that is the similar type of data get stored in contiguous memory locations to allotted by the compiler while executing the array. An array can be accessed by its indices which are given by the compiler accordingly. If we declare the array like int a[10] then allocation takes place in the following ways :
An array of all types of data can be declared easily like fundamental data types or derived data types and user-defined data types.
Reason for using Arrays :
Arrays mean a group of homogenous or similar types of data is if we want to work on a large number of data of similar type then declaring of variables we must have to work hard to manage such quantity of data. like int a1,a2,a3,a4,a5,a6,a7,a8,a9,a19;
or we can declare it using int a[10]; statement
Types of Array :
Depending on the number of square brackets used, the array is one dimension, two Dimension, and multi-dimension types.
In 1d array the declaration should be int a[5];
In 2d array the declaration should be int a[3][4];
In multi D array the declaration should be int a[2][3][4];
How to declare and initialize the Array :
An array can be declared both statically and dynamically . for example :
Static initialization : int x[10];
int x[]={1,2,3,4,5,6,7,8,9,10} ;
In such a type of declaration, the array limit is automatically assigned by the compiler by counting the number of elements initialized.
Properties of Array :
- The array is a linear and homogenous data structure or we can say Array is the fixed-sized sequentially collection of the elements of similar data type
- Memory allocation is contiguous that is depending upon the type of data size of the next element automatically increased by the size of the type of data.
- It is also called a subscripted variable.
- index of the array starts from 0 that is the lower bound of the array is 0 and the upper bound is less than 1, for example, int a[10] it means the lower bound is 0 and the upper bound is 10-1=9.
- Array's name itself is a pointer.
- All the elements of the array share the same name but they differ from each other on the basis of the index value and value they initialized.
- Amount of the memory required to store the array depends upon the type of data and size that is the number of elementsTOTAL BYTES = SIZE OF(DATA TYPE) * SIZE OF ARRAY(INDEX)
- Once the array is created we cannot reallocate the memory or change the memory allocation
- The initial value of an uninitialized array is garbage as it belongs to the automatic storage class if no other prefix is added to it( like register extern or static).
Advantages of Using Arrays :
- Random access of the elements of the array is very easy as elements can be accessed using the index of the array.
- A large number of elements can be stored without declaring a large number of names of the variable.
- All operations like LIFO or FIFO traversal is easily presentable.
- Used to represent all types of lists
- other data structures like heap stack queues can be represented using the array
Limitations of Arrays
- Fixed memory blocks are created
- insertion and deletion are costly as the insertion need to reshuffle elements and deletion also need the same.
Applications of Arrays
- Contiguous memory allocation
- indexed storage
- Grouping of similar variables
- mathematical operation on a matrix
- Row major representation: The row of the elements of the array can be printed first and then the second row for example
int a[][]={1,2,3,
4,5,6
7,8,9};
row-major representation {1,2,3,
4,5,6
7,8,9}
Calculating the address of the elements of the array (1 d array )
the formula is Address of a[i]=B+W*(i-LB) where
i=index of the element whose address to be found
B=base address
W=Storage size of a single element or size of type of data
LB=Lower Bound of the array
Calculating the address of the elements of the array (2d array) (Row major)
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element stored in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
N = Number of columns given in the matrix. - Column major representation : The column elements of the array printed first like
{1,4,7
2,5,8
3,6,9}
Calculating the address of the elements of the array(2d array)(Column major)
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element stored in any array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero),
M = Number of rows given in the matrix.
Functions in C:
Advantages of Functions :
- Since a function is a small piece of code capable of performing any action thus by calling it again and again we can easily avoid the rewriting of the code .
- A large problem can be divided into samll modules called the function
- basics of the modular programming enhance the testing practices
- Function call may cause to move the control of program in another direction by keeping the address of its line of code into the stack pointer then return back after executing the function starts from same point may cause little bit overhead in the program
- inline functions may cause addition of the function code depending upon certain conditions to the main function while executing the program which may cause expansion of program at the runtime.
- Function may cause the program divion of work.
Syntax of the Function :
- return type: A function only return single value of any data type or it may return the address of any data type.
- Name of the function: name of the function is an identifier and it follow all the naming conventions that are followed by the variable names
- Arguments list : The argument of the function should be any type of the data ,address or pointers .for example :
<return type><ideniier>(<arguement list>);
Important Terms Regarding the Functions :
- Function Declaration: A function declaration statement is also known as the prototype of the function as it contains the function header without any code. The function header includes the: return type, function name, and arguments list as mentioned in the next para.
- Function call: The calling of function takes place according to the need as the calling must take place in the main function.
Types of the Functions :
Types of the Function On the basis of Arguments and Return Value
- No argument and No return value : Such functions have no argument and no retrning any value.like
#include<stdio.h>
void display();
void main()
{ printf("\nThis is the main function ");
display();
}
void display ()
{ printf("\nThis is the display fumction and must be called in the main function.");
}
During the execution of the program - Argument but no return value
- No argument but the return value
- Argument and return value
Formal Argument and Actual Argument
add(300,200); // function call
Local and global variables :
|
Local variables |
Global variables |
I t is generally declared inside a function body. · If it isn’t initialized at the time of declaration a garbage value is stored in it. · It is created when the function begins its execution. · It is lost when the function is terminated. · Data sharing is not possible since the local variable/data cannot be accessed outside the function. · Parameters need to be passed to local variables so that they can access the value in the function. · It is stored on a stack unless mentioned otherwise. · They can be accessed using statements inside the function where they are declared. · When the changes are made to the local variable in a function, the changes are not reflected in the other function. |
· It is declared outside the function. · If it isn’t initialized, the value of zero is stored in it as default. · It is created before the global execution of the program. · It is lost when the program terminates. · Data sharing is possible since multiple functions can access the global variable. · They are visible throughout the program, hence passing parameters is not required. · It can be accessed using any statement within the program. · It is stored on a specific location inside the program, which is decided by the compiler. · When changes are made to the global variable in one function, these changes are reflected in the other parts of the program as well. |
No comments:
Post a Comment