Question

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 user to enter an integerN between 1 and 100 (inclusive) . o If the value is less than one, then set N to 1 o If greater than 100, then setNto 100 Dynamically create an array of sizeNthat contains integer values. o You may use either the calloc or malloc function. Using a loop, assign a random integer value (from -100 to +100) to each element in the array o Utilize the srand and rand functions. After the array has been initialized, use another loop to write the array values to the file o Each line in the output file should be one element from the array. Free the allocated array, close the file, and exit the program. Run and test your program. Look at the generated output file to verify that it works. Note: It is also possible to useC99s variable length array (VLA) feature to create an array whose size is determined at run-time. However, the maximum VLA size is limited by the amount of stack memory available, which is usually much less than the heap memorvused by calloc or malloc. Sample Run Enter the desired size of the dynamic array: 5 Contents of the generated output file data.txt (your results will vary): 59 34 -69 -62

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>


void main()
{
FILE *fp;
int *arr, n, i;

/* Open an output file in write mode */
fp = fopen("data.txt", "w");

/* If file handle 'fp' is NULL, it will quit the program */
if( fp == NULL )
{
printf("File can not openes\n");
exit(1);
}

/* Taking input the value of 'N' from the user */
printf("Enter the desired size of the dynamic array:");
scanf("%d", &n);

if(n < 1)
n = 1; /* when 'n' is less than 1 */
else
if(n > 100)
n = 100; /* when 'n' is greater than 100 */

/* Dynamically creating an array of size 'n' */
arr = (int*) malloc(n * sizeof(int));

/* Assign a random integer value from -100 to +100 */
for (i = 0; i < n; i++)
*(arr+i) = (rand() % 201) - 100;

/* Writing the array values to the file */
for (i = 0; i < n; i++)
fprintf(fp,"%d\n", *(arr+i));

/* free allocated array */
free(arr);

/* Close the file */
fclose(fp);
}

After executing the program the content of "data.txt" file:

-59
76
3
69
-26
-54
-79
-88
-72
43

Add a comment
Know the answer?
Add Answer to:
Can anyone help me with my C hw? Exercise 3 You will write a new program...
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
  • Write a program in either C++ or Java meeting these requirements Description: In this program, we...

    Write a program in either C++ or Java meeting these requirements Description: In this program, we assume that our data comes in dynamically. We need to maintain our data in a heap, after every insertion and deletion. We also need to handle the underlying array dynamically. whenever we detect an overflow in our array, we will create a new array with size doubling the previous size. Requirements: 1. (Dynamic Data) when we generate the data, we simulate dynamic data. We...

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

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...

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

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first numbe...

    Here is the Prompt for problem 1: Write a C++ program that reads in a test file. The first number in the file will be an integer, and will indicate the amount of decimal numbers to follow. Once you have the read the numbers from the file into an array then compute the following properties on the set of numbers: the sum, the average (mean), the variance, the standard deviation, and the median. Don’t try to do the entire program...

  • Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

    Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt. Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code. File format: apple banana pear . . . Hint: Program will need to utilize double pointers. More Hints: 1. Assume no word is bigger that 50...

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