An array is a collection of data items, in which all of the data items have the same datatype, accessed using a common name. A 1-D array is like a list. A 2-D array is like a table. An array is used to store a lot of data's at a time.
How to ENTER values in an 2-D Array ?
Either we can enter the elements in an array by using "scanf( )" or we can directly enter them in the program.
- #include<stdio.h>
- int main( )
- {
- int N, G;
- printf("Enter the number of Rows \n");
- scanf("%d", &N);
- printf("Enter the number of Column \n");
- scanf("%d", &G);
- int a[ N ][ G ]; //here array is of N rows and G columns
- int i, j;
- //Using scanf( ) ....
- for(i=0; i<N; i++)
- {
- for(j=0; j<G; j++)
- scanf("%d", &a[ i ][ j ] );
- }
- //Directly entering the elements...
- //let N=3 and G=3
- a[ 3 ][ 3 ]={ {10, 20, 30}, {5, 15, 25} };
- }
So, here are the two ways to enter the values of element in 2-D Array
NOTE:
int a[ ][ ] ={{2, 3, 4, 5},{1, 6, 7, 8}}; ❌
It is an Error.
int a[4][ ]={{2, 3, 4, 5},{1, 6, 7, 8}}; ❌
It is an Error.
int a[ ][ 4] ={{2, 3, 4, 5},{1, 6, 7, 8}}; ✔️
It is not an Error.
When you are entering elements in the 2-D array then it is compulsory to give the number of columns on the time of declaration of the array, if the value of the rows is not given even then it is not an error.
How to pass an Array into the function ?
To understand this let us take an example:-
@ Write a program to find the sum of the elements of an array using functions ?
- #include<stdio.h>
- int main()
- {
- int N; //for the size of an array
- printf("Enter the size of the array\n");
- scanf("%d",&N);
- int a[N];
- int i;
- printf("Enter the elements\n");
- for(i=0;i<N;i++)
- {
- scanf("%d",&a[i]);
- }
- modify(a, N); //a function for addition
- return 0;
- }
- void modify(int x[ ],int s)
- { //int x[] represents that the passing value is an array
- int i,sum=0;
- for(i=0;i<s;i++)
- {
- sum=sum+x[i];
- }
- printf("%d",sum);
- }
- Here in this program we have passed the arguments (a, N) in the modify function.
- Here 'a' contains the base address of the array so we have passed the base address in the function. And in array x[ ] we don't need to specify its size.
- Here 'N' is passed as 's' in the function too store the number of elements so that we can use a perfect loop.
Applications of Arrays:
Array are used to implement mathematical vectors and matrices, as well as other kinds of rectangular tables. Arrays are used to implement other data structures, such as lists, heaps, hash tables, deques, queues, stacks, strings, and VLists.
To get the codes of different programs related to Arrays, please click on Array_Programs.
To read our post on Array (Part-1) please click on Arr1.
To read our post on Function, please click on FUN( ).
To read our post on Function, please click on FUN( ).
For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...
0 Comments