Create an array of 100 doubles with values between 10 and 75. Then write all 100 values to a file ca

  

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”. Since you don’t seed the random numbers, your output on prclab1 should exactly match the following:% cat data1.txtStart of data64.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.4511.06 25.79 18.92 62.27 20.18 36.06 18.44 17.07 74.93 24.1943.34 64.54 49.82 29.24 51.44 44.08 42.08 73.23 29.01 60.1444.24 60.04 36.01 67.95 28.42 32.91 62.50 69.74 14.53 71.7144.19 15.59 22.49 53.11 67.87 32.68 14.17 11.30 39.75 14.1025.49 73.09 68.64 65.31 27.33 45.08 34.39 59.42 43.31 53.4044.55 12.55 38.45 70.57 70.50 56.86 28.48 58.00 51.60 33.0154.71 20.79 38.61 67.20 63.90 31.47 24.88 68.07 32.77 54.6372.17 48.26 52.72 65.81 38.57 70.06 35.90 62.96 54.47 69.21End of dataRefill 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. Your output in the second file should look like the following:% cat data2.txtStart of data130.44 130.31 130.68 130.66 130.27 130.64 130.52 130.42 130.51 130.34130.59 130.35 130.42End of data%
cshw.pdf

template.txt

Don't use plagiarized sources. Get Your Custom Essay on
Create an array of 100 doubles with values between 10 and 75. Then write all 100 values to a file ca
Just from $13/Page
Order Essay

Unformatted Attachment Preview

Part 1: 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”.
Since you don’t seed the random numbers, your output on prclab1 should exactly match the
following:
% cat data1.txt
Start of data
64.61 35.63
41.03 50.88
11.06 25.79
43.34 64.54
44.24 60.04
44.19 15.59
25.49 73.09
44.55 12.55
54.71 20.79
72.17 48.26
End of data
60.90
33.71
18.92
49.82
36.01
22.49
68.64
38.45
38.61
52.72
61.90
43.37
62.27
29.24
67.95
53.11
65.31
70.57
67.20
65.81
69.26
71.89
20.18
51.44
28.42
67.87
27.33
70.50
63.90
38.57
22.84
69.55
36.06
44.08
32.91
32.68
45.08
56.86
31.47
70.06
31.79
51.32
18.44
42.08
62.50
14.17
34.39
28.48
24.88
35.90
59.93
56.62
17.07
73.23
69.74
11.30
59.42
58.00
68.07
62.96
28.06
19.20
74.93
29.01
14.53
39.75
43.31
51.60
32.77
54.47
46.01
49.45
24.19
60.14
71.71
14.10
53.40
33.01
54.63
69.21

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.
Your output in the second file should look like the following:
% 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().
int fillRandom(double array[], int length, double lowerLimit, double upperLimit);
void writeToFile(const char* fileName, const double arrray[], int length);
void writeArray(FILE* fp, const double array[], int length);
#define ARRAY_SIZE 100
void part1(void) {
double array[ARRAY_SIZE];
int arrayLength = fillRandom(array, ARRAY_SIZE, 10, 75);
writeToFile( “data1.txt”, array, arrayLength);
arrayLength = fillRandom(array, 13, 130.2, 130.7);
writeToFile( “data2.txt”, array, arrayLength);
}
int main(void) {
part1();
return EXIT_SUCCESS;
}
Details for part I:
For this homework, you must implement the three functions shown as prototypes.
1. Notice 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. Within the function, it acts like a constant. More significantly, if
the argument to which it is applied is a pointer, it means that the pointer cannot be used to
change the values to which it refers. When passing an array or string as an argument to a
function, it means that the function will not change any values in that 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 writeToFile() 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, for writing the numbers, it calls writeArray(). It is the job of writeArray to write
data from an array in the form of a table with rows and columns. Do not put any code to print
numbers in the writeToFile() function – that’s what writeArray() is for.
In writeArray(), 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 for the writeArray()
function to not print too many or too few newlines, either at the beginning or at the end of the
data. In other words, don’t omit the newline at the end of “Start of datan” or add one to the
beginning of “End of datan” to compensate for what happens in writeArray().



You may not use fprintf() to print the Start and End comments in writeDataToFile.
Instead, use fputs(). For printing 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 at runtime. To learn about these other functions, just type “man 3 fputs” in the
console. (Remember, to get out a man page display, just type q – for quit.) Also 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.
When you think about how to achieve the table affect, remember that you can either print the
newline for a new column 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 column position in a row with a separate
variable, or you can use a mod ten (% 10) operation to spot 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 two data sets are designed to pose both challenges.
You can test your writeArray function in the console by calling it with stdout as the first
argument, instead of the value of an open FILE* connection. This causes it to print data to
the console. (In it defines stdout as the FILE* connection to the console.)
Part 2: Reading from a File
Once you have part 1 working, you must now read the data back in again, and display it in the
console. To do this, you must define two additional functions, readFromFile() and
readArray().
The readFromFile() function is similar to the writeToFile() function, except that this time
rather than printing to a file it will be reading from a file. This means it needs two features not
found in the earlier writeToFile().
1. If the fopen fails, it must call perror() and then just return 0.
2. It must return the number of values that were read. (How else can you know the new
length of data in the array?)
3. The code in the readToFile() function does not read or write anything to or from the
file. It just calls readArray if the file was successfully opened. Then it must close the
file before returning the number of values read.
The readArray() function must have the following features.
1. It must ignore any file contents other than a number that appears before the beginning of
the first number in a section of data (in this case, that Start of data comment).
2. It must read the values from the file into an array, one value per array element. If there is
enough space in the array, it must read all of the elements up to the end of the file, or
until it encounters non-numerical content in the file. (Relax! That’s what fscanf() does.
You just have to get the details right.)
3. It must not read more values than the array size that is given as its 3rd argument.
Here is the starting code for the second part:
int readFromFile(const char* filename, double array[], int size);
int readArray(FILE* fp, double array[], int maxSize);
#define BIG_BUFFER_SZ 1024
#define SMALL_BUFFER_SZ 48
void part2(void) {
double buffer1[BIG_BUFFER_SZ];
double buffer2[BIG_BUFFER_SZ];
double buffer3[SMALL_BUFFER_SZ];
int buffer1Len = 0;
int buffer2Len = 0;
int buffer3Len = 0;
fputs(“data1.txtn”, stdout);
buffer1Len = readDataFromFile(“data1.txt”,
writeArray(stdout, buffer1, buffer1Len);
fputs(“data2.txtn”, stdout);
buffer2Len = readDataFromFile(“data2.txt”,
writeArray(stdout, buffer2, buffer2Len);
fputs(“data3.txtn”, stdout);
buffer3Len = readDataFromFile(“data3.txt”,
writeArray(stdout, buffer3, buffer3Len);
fputs(“data1.txtn”, stdout);
buffer3Len = readDataFromFile(“data1.txt”,
writeArray(stdout, buffer3, buffer3Len);
}
buffer1, BIG_BUFFER_SZ);
buffer2, BIG_BUFFER_SZ);
buffer3, SMALL_BUFFER_SZ);
buffer3, SMALL_BUFFER_SZ);
int main(void) {
part1();
part2();
return EXIT_SUCCESS;
}
Your output must match exactly the output shown below.
% a.out
data1.txt
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
data2.txt
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
data3.txt
data3.txt: No such file or directory
data1.txt
64.61 35.63
41.03 50.88
11.06 25.79
43.34 64.54
44.24 60.04
%
60.90
33.71
18.92
49.82
36.01
61.90
43.37
62.27
29.24
67.95
69.26
71.89
20.18
51.44
28.42
22.84
69.55
36.06
44.08
32.91
31.79
51.32
18.44
42.08
62.50
59.93
56.62
17.07
73.23
69.74
28.06
19.20
74.93
29.01
46.01
49.45
24.19
60.14
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
3. The data1.txt file.
4. The data2.txt file.
Upload all 4 files so we can check format and values, since they often get messed up by Canvas.
What you need to know for Part 2:

In the writeToFile(), you opened the file for writing. The second argument for
fopen() was a mode string with “w” for writing. In this assignment, the mode string is
“r” for reading. Note that the argument is a string, and not a character (double quotes).

In the writeToFile(), if the file didn’t exist, a new one is created. When opening a file
for reading, if the file doesn’t exist, the fopen() function “fails” and returns a NULL
pointer. (NULL means value 0). You have to test the return value from fopen to see if it
is NULL (instead of a valid pointer to a FILE struct). If it is NULL, which indicates failure,
you must report the error and then simply return. Since no data is read, the function
returns 0.

To report errors in C, there is a special library function, called perror() (short for print
error) that prints a message associated with the most recent error to have occurred. If you
call it right after fopen fails, the failure of fopen will be the most recent error. The
perror function takes one argument for a string, and prints that string right before the
error message. The logical thing to use for that argument is the name of the file you were
trying to open. For more info, type “man 3 perror”.

Like fscanf (like fprintf), has a first argument that is a pointer to an open file
connection. In this case, open the file for reading. (Also like fprintf, fscanf can be
directed to use the console. For console input, the connection name is stdin. But in this
case, I don’t think that pointing readArray at stdin will be useful for testing.)

In the previous assignment, we used “%f” for writing the floating point value. The %f
descriptor works for writing either floats or doubles. For reading, however, you must be
specific, since otherwise the program has no way of knowing the size of the variable you
want it to populate. For scanf, %f means a float, while %lf (for Long Float) means a
double. Since you are reading into a double, you have to use %lf. (If you use %f, it only
fills the first 32 bits of the 64 bits in a double, and will probably show up as 0.0.)

When writing the table, we specified the field width and number of decimal places to
control the appearance of what went into the file. For reading, we don’t control that, so
don’t specify it. The format is whatever appears in the document. Fscanf just has to
read what is there and convert it to a double. Leave “%lf” by iteself, and don’t decorate
it with other details.

When reading the next number, fscanf() will skip over any “white space” where white
space is any space, tab, or newline characters in the stream. Once it reads a number, it
stops at the next white space, or any other non-digit character. Each call to scanf reads
one number, and leaves the position in the file at the end of that number (which is the
beginning of the white space before the next number). You don’t really have to worry
about it until it reaches the end (non-number text or end of file).

Like scanf(), fscanf() returns an integer, and that integer is the number of values that
were successfully read. Here we are only looking for one value per call, so if it
successfully read a value, it returns 1. If it fails to read a value because it hit something
other than a number, it returns 0. If it fails to read a value because it hit the end of the file
(EOF), it returns -1. So reaching the end of the numbers can return either a 0 or a -1. As
long as it returns a value greater than 0, we successfully read a number, and should repeat
to see if we can read another. If it is not greater than 0 (the test fails), then we are done.
That makes the basic loop test: while (fscanf(xx) > 0), where xx are the bits that
make it fill the next double in the array.

Each time we successfully read a number into the array, we must increment the array
index, which should be called i, so that the next value that we read will go into the next
location in the array. We start with i = 0; so that the first value we read goes into
array[0]. Then we increment, so the next value goes into array[1], and so on.

Like for scanf, that we used in earlier assignments, the last argument to fscanf is the
location of the variable that we want to fill (using &). Since the location that we want
filled is a location of an element in the array, we have to put the & in front of the array
location including its index: &(array[i]). Note the added parens, which is needed to
make sure that the [i] is applied before applying the &, rather than after.

If testing fscanf() for a successful read is the only loop test, then the loop will continue
until it has read all of the values in the file. But we may not have a long enough array to
hold all of the values in the file. The readArray() function takes a second argument,
which is the size of the array. The loop test must also test for having enough space left in
the array to read another number. In fact it must do that test first. We don’t want to put
the next value beyond the end of the array, and only then discover that there wasn’t
space. So the test must be i < size && fscanf(… If the i < size test fails, the second part won’t happen, since the && is already false. In the readArray() prototype, the size argument is called maxSize.  The array index (i) starts at 0 and is incremented each time a new number is successfully read. At the end of the loop (when it stops), i is the number of values read. When you come out of the loop, i will have been incremented exactly as many times as there were successful values read. That’s the number you want to return.  One last problem: what about getting rid of the non-number verbiage at the front of the file. If you remember, your file starts with that line: “Start of data”. Here, I will just give you the line that does that since it involves “regular expressions”, and unless you are taking discrete math, you won’t know what those are. The line is: fscanf(fp, "%*[^0-9]"); What the format string in that line says is: read (%) but ignore all (*) characters that fit the pattern given by this regular expression ([]), where the regular expression specifies anything that is not (^) a character in the range of ASCII characters for the digits 0 through 9 (0-9). This pattern consumes white space and non-number characters alike. So
it will consume everything up to the first digit character, regardless of how many lines
that might be.
#include
#include
/* Functions needed for part 1 of assignment 8 – writing to a file */
int fillRandom(double array[], int length,
double lowerLimit, double upperLimit);
void writeArray(FILE* fp, const double array[], int length);
void writeToFile(const char* fileName, const double array[], int length);
void part1(void);
/* Additional functions for part 2 of assignment 8 – reading from a file */
int readArray(FILE* fp, double array[], int maxCount);
int readFromFile(const char* filename, double array[], int size);
void part2(void);
int main(void) {
part1();
part2();
return EXIT_SUCCESS;
}
#define ARRAY_SIZE 100
void part1(void) {
double array[ARRAY_SIZE];
int arrayLength = 0;
arrayLength = fillRandom(array, ARRAY_SIZE, 10, 75);
writeToFile( “data1.txt”, array, arrayLength);
arrayLength = fillRandom(array, 13, 130.2, 130.7);
writeToFile( “data2.txt”, array, arrayLength);
}
#define BIG_BUFFER_SZ 1024
#define SMALL_BUFFER_SZ 48
void part2(void) {
double buffer1[BIG_BUFFER_SZ];
double buffer2[BIG_BUFFER_SZ];
double buffer3[SMALL_BUFFER_SZ];
int buffer1Len = 0;
int buffer2Len = 0;
int buffer3Len = 0;
fputs(“data1.txtn”, stdout);
buffer1Len = readFromFile(“data1.txt”, buffer1,
writeArray(stdout, buffer1, buffer1Len);
fputs(“data2.txtn”, stdout);
buffer2Len = readFromFile(“data2.txt”, buffer2,
writeArray(stdout, buffer2, buffer2Len);
fputs(“data3.txtn”, stdout);
buffer3Len = readFromFile(“data3.txt”, buffer3,
writeArray(stdout, buffer3, buffer3Len);
fputs(“data1.txtn”, stdout);
buffer3Len = readFromFile(“data1.txt”, buffer3,
writeArray(stdout, buffer3, buffer3Len);
}
/* implement your 5 functions here */
BIG_BUFFER_SZ);
BIG_BUFFER_SZ);
SMALL_BUFFER_SZ);
SMALL_BUFFER_SZ);

Purchase answer to see full
attachment

  
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more