dynimcally allocated array 2

 #include <stdio.h>  
 #include <stdlib.h>  
   
 void main()  
 {  
    double *ary;   //will be your dynamic 1d array  
    int num;       //your array element  
   
    //input your number of array element  
    printf("num: ");  
    scanf("%d", &num);  
   
    //syntax: number of block address times byte needed (sizeof) for double variable  
    ary = malloc(num * sizeof(double));     
   
    //fill the array (can be changed to any number) and display them  
    for(int i=0; i<num; i++)  
    {  
       ary[i] = i * i * 2.8;  
       printf("%.2f\n", ary[i]);  
    }  
   
    //must free the memory allocation to not make memory leak  
    if(ary) free(ary);  
 }  
   

No comments