// this program asks the user to enter integer values // into an array, and prints out the array. #include #include // creates an array of integers that has the specified length, // and asks the user to enter a value for each element // in the array. int * read_int_array(int length) { // create an array of ints. The number of elements // in the array is specified by the value of length // (which was specified by the user) int * numbers = (int *) malloc(sizeof(int) * length); // ask the user to enter a value for each element in the array. int i; for (i = 0; i < length; i++) { printf("please enter a value for element %d:\n", i); scanf("%d", &numbers[i]); } return numbers; } // prints the elements of the array n. See bubblesort1 // for an alternative version of this function. // length is the number of elements in the array n. void print_int_array(int * a, int length) { int i; for (i = 0; i < length; i++) { printf("a[%d] = %d\n", i, a[i]); } } int main() { int length; printf("how many numbers do you want to enter?\n"); scanf("%d", &length); int * numbers = read_int_array(length); print_int_array(numbers, length); // for every array that we create with malloc, we must // destroy with free, when we don't need the array anymore. free(numbers); }