Advertisement
Guest User

q5

a guest
Mar 26th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void printArr(int *, int);
  5.  
  6. int main()
  7. {
  8. int n, i;
  9. printf("Size of array: ");
  10. scanf("%d", &n);
  11. if(n<0)
  12. {
  13. printf("Memory allocation failed");
  14. exit(0);
  15. }
  16.  
  17.  
  18. int *my_array;
  19. my_array = (int*) malloc(n * sizeof(int)); //dynamic allocation of memory
  20. if(my_array == NULL)
  21. {
  22. printf("Error! memory not allocated."); //if memory not allocated terminate the program
  23. exit(0);
  24. }
  25. *my_array = 1;
  26. for(i =1; i<n; i++)
  27. *(my_array+i) = i*100;
  28.  
  29. printArr(my_array, n);
  30.  
  31. free(my_array);
  32. return 0;
  33. }
  34.  
  35. void printArr(int * arr, int n){
  36. int i;
  37. for(i=0;i<n;i++)
  38. {
  39. printf("%d\n",arr[i]); //printing first n elements of array
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement