Question

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:

  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
Request Professional Answer

Request Answer!

We need at least 9 more requests to produce the answer.

1 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Using C programming
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    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. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • 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...

  • 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...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • 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],...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  •      program will enter data into two single dimension arrays (do not st...

         program will enter data into two single dimension arrays (do not store duplicate values in arrays)      program will find the union and intersection of the two arrays using one function      program will find the symmetric difference of two arrays      program will display the union, intersection, and symmetric difference   */     short* input_data(short size);   // function to dynamically allocate and array and enter data into the array void display_data(short *data, short size); // function to display data in an array void get_union_intersection(short...

  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

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