Question
1) Write the code to dynamically allocate ONE integer variable using malloc (memory allocation) and have it pointed to by a pointer (of type int
1) Write the code to dynamically allocate ONE integer variable using malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Give yourself an accomplishment point. 2) Write the code to dynamically allocate an integer array of length 5 using calloc (contiguous allocation) and have it pointed to by a pointer named arr. Write the code to fill arr with 5 integers in a for loop, using normal array notation assigning the elements of the array. In a second for loop print out the values in arr (again using normal array notation). Then free the elements in arr returning the dynamically allocated memory to the heap. Give yourself an accomplishment point. Is the pointer itself, arr, dynamically allocated or is it a normal "static" variable like any other variable declared in a function (eg int x; )? Give yourself another accomplishment point if you can answer this correctly. 3) Modify your code to dynamically allocate an integer array of length 40 using calloc (contiguous allocation), having it again pointed to by the pointer named arr. Write the code to put 5 (yes, still 5) integers into the first 5 elements of arr using a for loop, and again using normal array notation to assign the first 5 elements of the array. In a second for loop print out the 5 values in arr (again using normal array notation). Then free all 40 elements in arr returning the dynamically allocated memory to the heap. Note that you can allocate a very large array and need not use all of it; or, as in the previous step you can allocate exactly the right number of dynamically allocated array elements to fit your data. Give yourself an accomplishment point. 4) Create a data file with the first number being an integer . The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many. Give yourself a minor accomplishment point. 5) Next write the code to use calloc to dynamically allocate how_many integers and assign the returned memory to the variable named arr, already in your code. (Is this allocating the exact amount needed or an excessive amount of dynamic memory? Give yourself a minor accomplishment point for the correct answer.) Then write a for loop to input the remainder of the data into the arr. Following that loop code, write another for loop to print the data in arr (generally you should do not do input and output in the same loop when using arrays - keep your programs "modular"). As usual, ensure there is some labelling along with what is printed so you can identify it on the output. Give yourself an accomplishment point.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started