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.
6) Write a function named array_out which will have a first parameter which is an integer array with name arr_param, and an integer variable size as the second parameter. The function should use a for loop to output the first size elements in the passed array. In your main function, call array_out, passing it arr and how_many, which are the the name of the array in the main function and the number of elements in that array. In the main: array_out( arr, how_many); The called function header: void array_out( int arr_param[ ], size) { As usual, instead of int arr[ ], any valid variable name could be used (it's just a placeholder name); for example: void array_out( int fred[ ], size) { Of course, for the remainder of the called function, array_out, you will have to use either arr_param or fred. [ Important aside: The first parameter is passed only the STARTING address of the array. This is just a SINGLE address. That is all that ever gets passed when you are "passing an array". Note that the parameter int arr_param[ ] only wants a single address passed to it; fortunately, when the compiler sees an array name, such as arr which you are using as the first argument in the call statement in the main, it interprets that array name as the first address of the array. You can absolutely consider the following to be true: arr is the same as &arr[0]. So, in the calling function (eg the main function), the array name arr is used as the argument which is being sent to the parameter arr_param. This is the same whether that first address is being sent to int arr_param[ ] or to int fred[ ]. Both are dummy names for a location in the memory area belonging to the called function. That location can hold a single address, and nothing else. In fact, although we commonly say "an array is passed" that is not correct... only the FIRST ADDRESS of a "passed array" actually gets passed. (Caution about what is passed: It is NOT the first element! It is the ADDRESS of the first element!). If you give it a moment's though, you will see that the called function array_out now knows WHERE in the main function's memory the arr is located. And it can work with that array, arr, IN THE MAIN FUNCTION. By the way, the size of that array, in the main function, is NOT known to the function from that first parameter - the first argument just passes where that array is living (in the main function) to the first parameter. This is why when arrays are "passed" (again the whole array is NOT passed), you must also pass along the size of the array. That is, the amount of the array (maybe all of it, maybe not) which the called function will use must also be provided to the function. If you followed and understood this aside, give yourself an accomplishment point. If you didn't read it again slowly, repeatedly if necessary, until you can earn that accomplishment point.] Continuing on with this step, now that the array "is passed", simply print it out in the function using a for loop controlled by size, and normal array notation in the printf statement: printf (... , arr[i]); or printf(..., fred[i]); Remember, when dealing with arrays, start you loop counting at 0, not 1, as in: for (i = 0; i < size; i++) or for (i = 0; i < size; ++i) Ensure this is working properly and give yourself a big accomplishment point.
7) Comment out the call to array_out and add two calls in the main function: to a function named count_zeros and to a function named count_negs. Instead of printing the "passed" array as with array_out in the above step, the first will return the number of array elements which are zero, and the second will return the number of array elements which are negative values. The header lines for these functions would look like: int count_zeros ( int arr[ ], int size ); int count_negs ( int arr[ ], int size ); The main function should print ou
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