echo '' ;
C Programming Language

C – Array

It is very difficult to define more variables’ names when using 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. This tutorial about “C Array”

Array 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
    1. One Dimensional Array
    2. Two Dimensional Array and
    3.  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 :

Synatx:  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’;

Array initialization with Zero:

When an automatic array is partially initialized, the remaining elements are initialized to 0.

Example-1:

#include<stdio.h>
int main()
{
    int awe[2];
    printf("%d, %d, %d, %d, %d, %d, %d, %d\n", awe[0], awe[1], awe[2], awe[3], awe[4], awe[5], awe[6], awe[7]);
    return 0;
}

/* Output:     0, 0, 0, 0, -467742907, 32525, 0, 0 */

Example-2:

#include<stdio.h>
int main()
{
    int awe[3]={1,2,3};
    printf("%d, %d, %d, %d, %d, %d, %d, %d\n", awe[0], awe[1], awe[2], awe[3], awe[4], awe[5], awe[6], awe[7]);
    return 0;
}

/* Output:     1, 2, 3, 0, 0, -2036674747, 32587, 0 */

Accessing the Array Element

#include<stdio.h>

int main()
{
    int arr_aew[2]={10,2};
    printf("%d\n", 1[arr_aew]);
    return 0;
}

/* Output: 2 */

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

/* * C Program to Find the Number of Elements in an Array */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
    int array[] = {15, 50, 34, 20, 10, 79, 100};
    int n; 
    n = sizeof(array);
    printf("Size of the given array is %d\n", n/sizeof(int));
    return 0;
}

Method 2 :

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


Find the size of array

#include <stdio.h>
int main()
{
  int size,i;
  printf("Enter the size of the array list");
 // scanf("%d",&size);
  int Arr[5]={88,97,65,43,77};
  size = 5;
   
  printf("\nBefore Array value\t:\t");
  for(i=0;i<size;i++)
    printf("%d\t",Arr[size]);
  /*
  for(i=0;i<size;i++)
  {
    if(Arr[size]>Arr[size+1])
      
  }
  */
  getchar();
  return 0;
}

Sum of the arrays 10 element

#include<stdio.h>
#include<conia.h>
int main()
{
	int num_array[10];
	int i,sum=0;
	int *ptr;
	printf("\n Enter 10 Elements")
	for(i=0; i<=10; i++)
		scanf("%d", num_array[i]);
	ptr = num_array;
	for(i=0; i<=10; i++)
	{
		sum = sum + *ptr;
		*ptr++;
	}
	printf("\n Sum of the arrays's 10 element : %d", sum);
	return 0;
}

NEXT

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

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

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading