Question

Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

  1. Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or 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.
  2. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc 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. 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; )?
  3. Modify your code to dynamically allocate an integer array of length 40 using calloc or malloc, 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.
  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.
  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?). Then write a for loop to input the remainder of the data into the array 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.
  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 all is fine, if you didn't read it again slowly, repeatedly if necessary.

  7. 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). Ensure this is working properly.
  8. 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);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
void array_out(int fred[],int size){
   int i;
   for(i=0;i<size;i++){
       printf("%d ",fred[i]);
   }
   printf("\n");
}
int count_negs(int arr[],int size){
   int cnt=0,i;
   for(i=0;i<size;i++){
       if(arr[i]<0){
           cnt++;
       }
   }
   return cnt;
}
int count_zeros(int arr[],int size){
   int cnt=0,i;
   for(i=0;i<size;i++){
       if(arr[i]==0){
           cnt++;
       }
   }
   return cnt;
}
int main()
{
   int *ptr_1=(int*)calloc(1,sizeof(int));
   *ptr_1=7;
   printf("%d\n",*ptr_1);
  
   printf("Array with length 5\n");
   int *arr = (int*)malloc(5 * sizeof(int));
   int i;
   for(i=1;i<=5;i++){
       arr[i]=i;
   }
   for(i=1;i<=5;i++){
       printf("%d ",arr[i]);
   }
   printf("\n");
   free(arr);
   printf("Array with length 40\n");
   arr = (int*)malloc(40 * sizeof(int));
   for(i=1;i<=5;i++){
       arr[i]=i;
   }
   for(i=1;i<=5;i++){
       printf("%d ",arr[i]);
   }
   printf("\n");
   free(arr);
   FILE *file=fopen("file.txt" , "r");
   if(file == NULL) {
       perror("Error opening file");
       return(-1);
   }
   int how_many;
   i=0;
   fscanf (file, "%d", &how_many);
   arr = (int*)malloc(how_many * sizeof(int));
   while (!feof (file))
    {
      fscanf (file, "%d", &arr[i]);
      i++;    
    }
   fclose(file);
   printf("From file\n");
   for(i=0;i<how_many;i++){
       printf("%d ",arr[i]);
   }
   printf("\n");
   printf("Array out function\n");
   //array_out(arr,how_many);
   int zero=count_zeros(arr,how_many);
   int neg=count_negs(arr,how_many);
   printf("Number of zeros are: %d\n",zero);
   printf("Number of negative are: %d\n",neg);
  
}

Add a comment
Know the answer?
Add Answer to:
Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming

    Using C, 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.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Given an array of integer, please complete a function multiply(). The function accepts the first memory...

    Given an array of integer, please complete a function multiply(). The function accepts the first memory address of an array and the size of an array as parameters and returns the multiplication of all the elements. In order to get a full score, the function must use a loop to iterate through each element of an array. Pseudo code example:   multiply([ 1, 2, 3], 3) → 6   multiply([1, 1, 4 ,2], 4) → 8   multiply([7, 0, 0, 7, 0, 7],...

  • Homework Question Write a void function called transformArray that takes two parameters - a reference to...

    Homework Question Write a void function called transformArray that takes two parameters - a reference to a pointer to a dynamically allocated array of ints, and the size of that array.  The pointer is passed by referencebecause you want to change the value of the pointer. The function should dynamically allocate an array that is twice as long, filled with the values from the original array followed by each of those values times 2. For example, if the array that was...

  • WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with...

    WRITE THE NECESSARY C++ STATEMENTS 1. (10 points) Allocate memory dynamically for an int array with 100 elements. Next, assign the value of 1 to all the elements of the array and, finally, delete the array.

  • must be done in C You will implement a function that matches one of the prototypes...

    must be done in C You will implement a function that matches one of the prototypes below. int maxArray(int size, int* arr); int maxArray(int size, int arr]); The first parameter is the number of elements in the array. The second parameter is an address to the beginning of an int array. In the body of the function, use a looping structure to identify and return the maximum (largest number) of the array. NOTE: The maximum of the same number, say...

  • c++: Write a program to dynamically allocate memory for a C-style string on the heap for...

    c++: Write a program to dynamically allocate memory for a C-style string on the heap for up to 256 characters: Inside the main function: Allow the user to enter a word Pass the word to the function fun Write the function void fun(char *a) to: Print the word in Pig Latin. You can assume the word is all lowercase. The function should not alter the original array.

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • Malloc function For the prelab assignment and the lab next week use malloc function to allocate...

    Malloc function For the prelab assignment and the lab next week use malloc function to allocate space (to store the string) instead of creating fixed size character array. malloc function allows user to allocate memory (instead of compiler doing it by default) and this gives more control to the user and efficient allocation of the memory space. Example int *ptr ptr=malloc(sizeof(int)*10); In the example above integer pointer ptr is allocated a space of 10 blocks this is same as creating...

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT