Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi everyone, I have a C programming problem, I really need help on this one. Answers are better with explanations ,Thanks. This exercise needs you

Hi everyone, I have a C programming problem, I really need help on this one.

Answers are better with explanations ,Thanks.

This exercise needs you to write code to create some small, simple text files. The exercise is designed to use the fopen, fprintf, and fclose functions.

The Task:

image text in transcribed

The Answer:

the final version of vec6D.c

Below are all the related files:

Main6C.c:

#include  #include "vec6C.h" #define ELEMENT_COUNT(a) (sizeof(a) / sizeof(a[0])) #define NAME1 "arr1-precise.txt" #define NAME2 "arr1-fancy.txt" #define NAME3 "arr2-precise.txt" #define NAME4 "arr2-fancy.txt" #define NAME5 "no/such/folder/precise.txt" #define NAME6 "no/such/folder/fancy.txt" int main (void) { int status; vec_t arr1[ ] = { {-1.0, 0.0, 0.5}, {1.1, 2.2, -3.3}, {0.01, -0.005, 0.5}, {0.75, 0.76, -10.25} }; // REMARK: It's extremely unlikely that fclose will fail when // a program is quickly writing tiny text files. Don't expect to // see the "Error closing" message. status = save_precisely(NAME1, arr1, ELEMENT_COUNT(arr1)); if (status == SUCCESS) printf("Save to " NAME1 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME1 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME1 ". "); status = save_fancily(NAME2, arr1, ELEMENT_COUNT(arr1)); if (status == SUCCESS) printf("Save to " NAME2 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME2 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME2 ". "); vec_t arr2[ ] = { {-9.0, 0.0, 0.5}, {1.1, 2.2, -3.3}, {0.01, -0.005, 0.5}, {0.75, 0.76, -10.25}, {100, 200, 0.0000001} }; status = save_precisely(NAME3, arr2, ELEMENT_COUNT(arr2)); if (status == SUCCESS) printf("Save to " NAME3 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME3 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME3 ". "); status = save_fancily(NAME4, arr2, ELEMENT_COUNT(arr2)); if (status == SUCCESS) printf("Save to " NAME4 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME4 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME4 ". "); // The code between here and the end of main is designed to make // sure save_precisely and save_fancily do the right thing when it's // not possible to open an output file. status = save_precisely(NAME5, arr1, ELEMENT_COUNT(arr1)); if (status == SUCCESS) printf("Save to " NAME5 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME5 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME5 ". "); status = save_fancily(NAME6, arr1, ELEMENT_COUNT(arr1)); if (status == SUCCESS) printf("Save to " NAME6 " was successful. "); else if (status == OPEN_FAILED) printf("Could not open " NAME6 " for output. "); else if (status == CLOSE_FAILED) printf("Error closing " NAME6 ". "); return 0; }

vec6C.c:

#include  #include "vec6C.h" int save_precisely(const char *filename, const vec_t *a, int n) { // Get rid of the next line in your solution. printf("save_precisely was called and is doing nothing. "); return SUCCESS; } int save_fancily(const char *filename, const vec_t *a, int n) { // Get rid of the next line in your solution. printf("save_fancily was called and is doing nothing. "); return SUCCESS; }

vec6C.h:

struct vec { double x; double y; double z; }; typedef struct vec vec_t; #define SUCCESS 0 #define OPEN_FAILED 1 #define CLOSE_FAILED 2 int save_precisely(const char *filename, const vec_t *a, int n); // REQUIRES // filename points to the start of a string. // n > 0 and elements a[0], a[1], ... a[n-1] exist. // PROMISES // An attempt is made to to save the array contents to the text file // named by filename in the "precise" format described in the // instructions for this exercise. // Return values are: // SUCCESS if the file was opened, written to and closed successfully; // OPEN_FAILED if the file could not be opened; // CLOSE_FAILED if error was detected when the file was closed. int save_fancily(const char *filename, const vec_t *a, int n); // REQUIRES // filename points to the start of a string. // n > 0 and elements a[0], a[1], ... a[n-1] exist. // PROMISES // An attempt is made to to save the array contents to the text file // named by filename in the "fancy" format described in the // instructions for this exercise. // Return values are: // SUCCESS if the file was opened, written to and closed successfully; // OPEN_FAILED if the file could not be opened; // CLOSE_FAILED if error was detected when the file was closed. 

Replace the code in the definitions of save-precisely and save fancily with code that will make the functions do what is specified in their function interface comments. In the "precise" format, the number of elements is given in the first line of output, and double values are printed in scientific notation with 20 digits after the decimal place. (The % conversion for that in printf and fprintf is %. 20e.) For example, if an array of vectors is set up as vec-t arr [ ] = { 1-1.0, 0.0, 0.5, 0.01,-0.005, 0.5, 10.75, 0.76, -10.25) then the corresponding output file should look like this 4 -1.00000000000000000000e+00 0.00000000000000000000e+00 5.00000000000000000000e-01 .10000000000000008882e+00 2.20000000000000017764e+00-3.29999999999999982236e+00 .00000000000000002082e-02-5.00000000000000010408e-03 5.00000000000000000000e-01 7.50000000000000000000e-01 7.60000000000000008882e-01-1.02500000000000000000e+01 (By the way, some of the output shows that some numbers that can be represented exactly in base ten can't be represented exactly in the base two system used for the double type on most computers.) The "fancy" format doesn't print the number of array elements, but it does put an element index on each line. It uses parentheses and commas to group vector components together, and uses % .4f to print each double. Here is the "fancy" output file for the earlier example array element 0: (-1.0000, 0.0000, 0.5000) element 1: (1.1000, 2.2000, -3.3000) element 2: (0.0100, -0.0050, 0.5000) element 3: (0.7500, 0.7600, -10.2500) Replace the code in the definitions of save-precisely and save fancily with code that will make the functions do what is specified in their function interface comments. In the "precise" format, the number of elements is given in the first line of output, and double values are printed in scientific notation with 20 digits after the decimal place. (The % conversion for that in printf and fprintf is %. 20e.) For example, if an array of vectors is set up as vec-t arr [ ] = { 1-1.0, 0.0, 0.5, 0.01,-0.005, 0.5, 10.75, 0.76, -10.25) then the corresponding output file should look like this 4 -1.00000000000000000000e+00 0.00000000000000000000e+00 5.00000000000000000000e-01 .10000000000000008882e+00 2.20000000000000017764e+00-3.29999999999999982236e+00 .00000000000000002082e-02-5.00000000000000010408e-03 5.00000000000000000000e-01 7.50000000000000000000e-01 7.60000000000000008882e-01-1.02500000000000000000e+01 (By the way, some of the output shows that some numbers that can be represented exactly in base ten can't be represented exactly in the base two system used for the double type on most computers.) The "fancy" format doesn't print the number of array elements, but it does put an element index on each line. It uses parentheses and commas to group vector components together, and uses % .4f to print each double. Here is the "fancy" output file for the earlier example array element 0: (-1.0000, 0.0000, 0.5000) element 1: (1.1000, 2.2000, -3.3000) element 2: (0.0100, -0.0050, 0.5000) element 3: (0.7500, 0.7600, -10.2500)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions