C – Array

Array

It is very difficult to define more variables name when use more data.  So can create a array to solve this difficult(single variable name and more data). If the index of the array size is exceeded, the program will crash. But the modern compilers will take care of this kind of errors. We cannot initialize the size of array dynamically. Constant expression is required.

  • Rules
    • only store the same data types in array structures
    • Arrays are always passed by reference
    • Array list should be end expression with semicolon  ; . Otherwise it will show the error.
  • What is Base address of array??
    •  Eg :  a[10] = {0,1,2,3,4,5,6,7,8,9};     // The starting address of the array is called as the base address of the array. Consider the address of the array starts from 101. Here the variable 0(Zero) address is the base address and the base address of the array is 101.
  • When we pass an array as a function argument, the base address of the array will be passed.
  • Array three types
    • One Dimensional Array
    • Two Dimensional Array and
    •  Multi Dimensional Array

 

One Dimensional Array

  • When declare one index value is called One dimensional array
  • Syntax:  data_type array_name [ size];
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size  – array size
  • Syntax Declaration:
    • int a[100];  //Data type- int (2Byte), array_name – ‘a’, size – 100, Memory Occupies- 200 byte (100×2).
    • float s [50];  //Data type- float(4byte), array_name – ‘s’, size – 50, Memory Occupies- 200 byte (50×4).
    • char name[15];  // Data type- char(1byte), array_name – “name”, size – 15, Memory Occupies- 15 byte (15×1).
  • Declaration :  int a[100];
  • Reading : for(i=0; i<100; i++) scanf("%d", &a[i]);
  • Writing :  for(i=0; i<100; i++) printf("%d", a[i]);
  • Memory Representation :
  • Array initialization Syntax :  data_type array_name [size] = {list_of_values}; 
    • Examples
      • static int age [3] = {10,20,30};  //its means – a[0]=10; a[1]=20; a[3]=30;
      • static float salary[] = {1000,2200,3300};  //salary[0]=1000; salary[2]=3300;
      • static char a[10] = "ArunEworld";  //its means – a[0]=’A’; a[1]=’r’; like… a[9]=’d’;
  • Example Programs : Check this below examples list in this Links
    • C program to Find Smallest & Largest Number of N numbers in Array.c
    • C program to Ascending Order of N numbers in Array.c
    • C program to Descending Order of N numbers in Array.c
    • C program to fine Largest Number and Position of N numbers in Array.c
    • C program to Find Mean and Standard Deviation in Array Element.c

Two Dimensional Array

  • When declare two index value is called two dimensional array
  • Two dimensional arrays can consider as a table. size_1 is row size and size_2 is column size.
  • Syntax:  data_type array_name [ size_1] [size_2];
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size_1 , size_2 – two dimensional array size
  • Syntax Declaration:
    • int a[100] [20];  //Data type- int (2Byte), array_name – ‘a’, size_1(row) – 100,size_2(column)-20, Memory Occupies- 200 byte (100×2).
  • Declaration :  int a[10][15];
  • Reading : for(i=0; i<10; i++) for(j=0; j<15; j++) scanf("%d", &a[i][j]); 
  • Writing : for(i=0; i<10; i++) for(j=0; j<15; j++) printf("%d", a[i][j]); 
  • Memory Representation :
  • Array initialization Syntax :  data_type array_name [size_1] [size_2] = {v1, v2,.....vn}; 
    • Examples
      • static int mark[3][2] = {60,70,80, 35,90,18};  //its means – a[0][0]=60; a[0][1]=70; a[0][3]=30; a[1][0]=35; a[1][1]=90; a[1][2]=18;
      • static mark[3][2] = {{50},{0},{35}; //a[0][0]=50; a[0][1]=50; a[0][3]=0; a[1][0]=0; a[1][1]=35; a[1][2]=35;
  • Example Programs
    • Two_dimensional_array_read_row_column.c
    • Two_dimensional_array_print_row_column.c
    • Matrix_transpose_using_two_dimensional_array.c
    • Diagonal_elements_using_two_dimensional_array.c
    • Sum_of_diagonal_elements_using_two_dimensional_array.c
    • Sum_of_all_matrix_elements_using_two_dimensional_array.c
    • Add_a_given_matrix_elements_using_two_dimensional_array.c
    • Multiply_matrix_elements_using_two_dimensional_array.c
    • Largest_and_smallest_elements_using_two_dimensional_array.c

Multi Dimensional Array

  • In C, can use 3 or more dimension called as multi dimensional array
  • Multi Dimension limit is based on the compiler
  • Syntax: data_type array_name [size-1][size_2]….[size_n]; 
    • data_type  – What kind of array data type
    • array_name  – name of the array
    • size  – array size
  • Example : 
    •   int a[10][10][10];  //Data type- int (2Byte), array_name – ‘a’, size – 2000(10x10x10x2), Memory Occupies- 2000bytes

 

Array disadvantage:

  • One disadvantage of using arrays to store data is that arrays are static structures and therefore cannot be easily extended or reduced to fit the data set.
  • Arrays are also expensive to maintain new insertions and deletions

 



 

Exercise

C Program to Find the Number of Elements in an Array

Method 1:

if you know then data type of the array then easily can find out the array elements one if its initialized using sifeof(variable or array name) predefined function.

 

 

 

Method 2 :

if you don’t know then data type of the array then bit difficult to find out the array elements.

 

 

 



 

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: