Question:What is and why array in c? 

Answer An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array. Example of array declaration:
int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];
Array is useful when: (a) We have to store large number of data of similar type. If we have large number of similar kind of variable then it is very difficult to remember name of all variables and write the program. For example: //PROCESS ONE
int main(){
    int ax=1;
    int b=2;
    int cg=5;
    int dff=7;
    int am=8;
    int raja=0;
    int rani=11;
    int xxx=5;
    int yyy=90;
    int p;
    int q;
    int r;
    int avg;
    avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
    printf("%d",avg);
    return 0;
}
If we will use array then above program can be written as: //PROCESS TWO
int main(){
    int arr[]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+arr[i];
    }
printf("%d",avg/12);
return 0;
}
Question: Write a C program to find out average of 200 integer number using process one and two. (b) We want to store large number of data in continuous memory location. Array always stores data in continuous memory location. What will be output when you will execute the following program?
int main(){
int arr[]={0,10,20,30,40};
    char *ptr=arr;
    arr=arr+2;
    printf("%d",*arr);
    return 0;
}
Advantage of using array: 1. An array provides singe name .So it easy to remember the name of all element of an array. 2. Array name gives base address of an array .So with the help increment operator we can visit one by one all the element of an array. 3. Array has many application data structure. Array of pointers in c: Array whose content is address of another variable is known as array pointers.  For example:
int main(){
float a=0.0f,b=1.0f,c=2.0f;
float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;
}
 

+ Report
Total Preview: 1026
What is and why array in c?
Copyright © 2024. Powered by Intellect Software Ltd