In: Computer Science
Create a c program that takes 1 parameter, a number
using that number, dynamically allocate a memory so you store that number of integers
write integers in order starting from 1 until you fill all that memory
print the address and values of the first and the last integer stored in the memory
C Program:
#include <stdio.h>
#include <stdlib.h>
//Function prototypes
void createArray(int n);
//Main function
int main()
{
int n;
//Reading a number from user
printf("Enter a number: ");
scanf("%d", &n);
//Calling function
createArray(n);
return 0;
}
//Function that allocates and writes numbers
void createArray(int n)
{
int i; //Looping Variable
//Dynamically allocating memory
int *arr;
arr = (int *)malloc(sizeof(int)*n);
//Filling array with numbers
for(i=0; i<n; i++)
{
//Storing values
*(arr+i) = i+1;
}
//Printing address and values of the first and the last integer
stored in the memory
printf("\nFirst Integer: %d, stored at %u\n", *(arr+0),
(arr+0));
printf("\nLast Integer: %d, stored at %u\n", *(arr+n-1),
(arr+n-1));
}
________________________________________________________________________________________
Sample Run: