Thursday, 22 August 2013

Arrays in c

An array is derived data type which can store similar type of data in sequence memory location. Data may be primitive type i.e  int, char, float, double…
C Programming
C Programming

Example of array declaration:

int a[5];
char a[5];
float a[5];
long double a[5];
char * a[5];
int (a[])();
double ** a[5];

Array is useful when:

(a) We have to store large number of data of similar type.:
void main(){
    int a=1;
    int b=2;
    int c=5;
    int d=7;
    int e=8;
    int f=0;
    int g=11;
    int h=5;
    int i=90;
    int avg;
    avg=(a+b+c+d+e+f+g+h+i)/9;
    printf("%d",avg);
   }
If we  use array then the same program will written as;
void main(){
    int a[ ]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+a[i];
    }
printf("%d",avg/9);
}

Advantage of using array:

1. An array provides single name for many variables of same data type.
2. Array is used in data structure like. implementation of stack Queue, Tree etc.
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

0 comments:

Post a Comment