Array: Part 2

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.


  1. #include<stdio.h>
  2. int main( )
  3. {
  4. int N, G; 
  5. printf("Enter the number of Rows \n");
  6. scanf("%d", &N);  
  7. printf("Enter the number of Column \n");
  8. scanf("%d", &G);

  9. int a[ N ][ G ]; //here array is of N rows and G columns 
  10. int i, j;

  11. //Using scanf( ) ....

  12. for(i=0; i<N; i++)
  13. {
  14.   for(j=0; j<G; j++)
  15.   scanf("%d", &a[ i ][ j ] );
  16. }

  17. //Directly entering the elements...

  18. //let N=3 and G=3
  19. a[ 3 ][ 3 ]={ {10, 20, 30}, {5, 15, 25} };

  20. }

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 ?
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int N; //for the size of an array
  5. printf("Enter the size of the array\n");
  6. scanf("%d",&N);
  7. int a[N];
  8. int i;
  9. printf("Enter the elements\n");
  10. for(i=0;i<N;i++)
  11. {
  12. scanf("%d",&a[i]);
  13. }
  14. modify(a, N); //a function for addition
  15. return 0;
  16. }

  17. void modify(int x[ ],int s)
  18. { //int x[] represents that the passing value is an array
  19. int i,sum=0;
  20. for(i=0;i<s;i++)
  21. {
  22. sum=sum+x[i];
  23. }
  24. printf("%d",sum);
  25. }
  • 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( ).




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...

Post a Comment

0 Comments