// Pantea Aria // malloc and array example #include #include // Function prototypes int *allocate_marks(int size); void print_marks(int marks[], int size); int main(void) { int size; printf("Enter number of marks: "); scanf("%d", &size); // TODO 1: // Call allocate_marks and store the returned pointer int *marks = allocate_marks(size); // TODO 2: // Check if marks is NOT NULL if (marks == NULL) { return 1; } // If it is valid: // - Call print_marks print_marks(marks, size); // - Free the allocated memory // free(marks); return 0; } // This function should: // - Allocate memory for 'size' integers // - Check if malloc returned NULL // - Initialise each element with some value // - Return the pointer int *allocate_marks(int size) { // TODO 3: // Allocate memory using malloc int *p = malloc(size * sizeof(int)); // TODO 4: // Check if memory allocation failed // If so, print "Out of memory" and return if (p == NULL) { printf("Out of memory\n"); return NULL; } // TODO 5: // Use a loop to initialise each element // Example idea: p[i] = i * 2; int i = 0; while (i < size) { p[i] = i * 2; //or any other value you want i++; } // TODO 6: // Return the pointer return p; } // This function should print all elements void print_marks(int marks[], int size) { // TODO 7: // Use a loop to print each element on a new line for (int i = 0; i < size; i++) { printf("%d ", marks[i]); } printf("\n"); }