Solved by verified expert:HW 08This assignment is about writing to a file, as covered in class, and about writing large amounts of data in columns.The opening and closing part should be easy. You can look up the fopen and fprintf commands online or use the “man” command on prclab. Writing columns will take some trial and error. I recommend that you use printf(), or fprintf(stdout, to print it to a console until you have it working, and then switch to using fprintf() where the file pointer comes from fopen() to print the same thing, only this time to a file.Here is the description: CS125_Asmt08_fileWrite.pdf. It includes main() and the function prototypes.
152894_14443006_cs125_asmt08_filewrite.pdf

Unformatted Attachment Preview

Writing to a File
 Create an array of 100 doubles with values between 10 and 75. Then write all 100 values to a
file called “data1.txt”, in tabular form, ten values across in each row. Include in your file, a
comment at the front, that says “Start of data” and one at the end that says, “End of data”.
 Refill the array with 13 doubles with values between 130.2 and 130.7. Write all 13 values to
a file called “data2.txt”. Include in “data2.txt” the same comment lines at the beginning and
end of the data.
Since you don’t seed the random numbers, your output on prclab should exactly match the
following:
% cat data1.txt
Start of data
64.61 35.63 60.90 61.90 69.26 22.84 31.79 59.93 28.06 46.01
41.03 50.88 33.71 43.37 71.89 69.55 51.32 56.62 19.20 49.45
11.06 25.79 18.92 62.27 20.18 36.06 18.44 17.07 74.93 24.19
43.34 64.54 49.82 29.24 51.44 44.08 42.08 73.23 29.01 60.14
44.24 60.04 36.01 67.95 28.42 32.91 62.50 69.74 14.53 71.71
44.19 15.59 22.49 53.11 67.87 32.68 14.17 11.30 39.75 14.10
25.49 73.09 68.64 65.31 27.33 45.08 34.39 59.42 43.31 53.40
44.55 12.55 38.45 70.57 70.50 56.86 28.48 58.00 51.60 33.01
54.71 20.79 38.61 67.20 63.90 31.47 24.88 68.07 32.77 54.63
72.17 48.26 52.72 65.81 38.57 70.06 35.90 62.96 54.47 69.21
End of data
% cat data2.txt
Start of data
130.44 130.31 130.68 130.66 130.27 130.64 130.52 130.42 130.51 130.34
130.59 130.35 130.42
End of data
%
Use the following prototypes and main(). Do not change the prototypes or main(). Leave the
main() at the top of your file as the first function defined in the file.
#include
#include
int fillRandom(double array[], int length,
double lowerLimit, double upperLimit);
void writeDataToFile(const char* fileName, const double arrray[], int length);
void writeArray(FILE* fp, const double array[], int length);
int main(void) {
const int ARRAY_SIZE = 100;
double array[ARRAY_SIZE];
int arrayLength = 0;
arrayLength = fillRandom(array, ARRAY_SIZE, 10, 75);
writeDataToFile( “data1.txt”, array, arrayLength);
arrayLength = fillRandom(array, 13, 130.2, 130.7);
writeDataToFile( “data2.txt”, array, arrayLength);
return EXIT_SUCCESS;
}
What to turn in:
For this assignment you must submit 4 files, in the following order:
1. The typescript file crated with the following 6 commands (you may rename your .c file
and the a.out executable is you wish):
a. cat –n hw8.c
b. gcc hw8.c
c. a.out
d. cat data1.txt
e. cat data2.txt
2. Your source code file (so we can check the indenting which otherwise gets messed up by
cat –n and Canvas.
3. The data1.txt file.
4. The data2.txt file.
Details:
For this homework, you must implement the three functions shown as prototypes.
1. Note that the keyword const is being used in the function prototypes and in main(). The
const keyword tells the compiler that the variable to which it is applied cannot be changed
once it has been initialized – making it effectively a constant. In the function arguments
where it is applied to a pointer, it means that the pointer cannot be used to change the values
to which it refers. In this case, it means that the function does not change the values in the
array or string.
2. This version of fillRandom() should be similar to or the same as the one you wrote for the
last assignment. You can use a separate randomBetween() function, or, if you wish, you can
put the code for creating a random double directly in the fillRandom() function.
3. The writeDataToFile() function is responsible for opening and closing the file with
fopen() and fclose(), and putting the two comment lines at the beginning and at the end.
In between, it calls writeArray() to write the data in tabular form. Do not put any code that
actually prints data in the writeDataToFile() – that’s what writeArray() is for.
4. The writeArray() function poses the biggest challenge. You must use the %f format
specifier to make each number occupy the same amount of space. Note that there are 10
columns and that the entire line does not exceed 80 characters. You must break the printing
of numbers with a newline character between every 10th and 11th number to produce the 10
column table. Part of this challenge is to not end up with too many or too few newlines at the
beginning or end of the data. There must be no blank lines left between the data and the
comments that are printed in the writeDataToFile function.
 When you think about how to achieve the table affect, remember that you can either print
the newline right after you print the last number in a row, or right before you print the
first number in a row. You can count your position in a row with a separate variable, or
you can use the mod (%) operator to spot the indexes ending in 9 or 0, depending on
which strategy you choose. Whatever strategy you choose, you will have to be careful
about the very beginning (index 0) or the very end (potentially the end of a row, with no
additional data for a next line). The data sets are designed to pose both challenges.
 You may not use fprintf() to print the Start and End comments in writeDataToFile.
Instead, you must use the fputs() function. For adding the newline character in
writeArray(), use fputc(). The fprintf() function is feature rich, but inefficient.
The fputs() and fputc() functions are far more efficient for outputting strings and
characters when you don’t have values to convert. To learn about these other functions,
just type “man fputs” in the UNIX shell. (Remember that to get out the man page
display, you just type q – for quit.) UNIX uses the name “stream” to refer to a sequence
of characters coming in or going out. That’s why you see the name stream, instead of fp.
But it’s the same thing.

Purchase answer to see full
attachment