Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Read the documentation for the functional forms given below and write a brief

ID: 3567672 • Letter: 1

Question

1) Read the documentation for the functional forms given below and write a brief description (2-3 sentences) for each one. Each description should discuss parameters, return values (if any), and where the function might be used. a) TF = strcmp(s1,s2) http://www.mathworks.com/help/matlab/ref/strcmp.html b) file ID = fopen(filename,permission) http://www.mathworks.com/help/matlab/ref/fopen.html c) r = rand http://www.nnathworks.com/help/matlab/ref/rand.html d) fprint(fileID,formatSpec,A1,...,An) http://www.mathworks.com/help/matlab/ref/fprint.html e) fclose (fileID) http://www.mathworks.com/help/matlab/ref/fclose.html

Explanation / Answer

The C library function int strcmp(const char *str1, const char *str2) compares the string pointed to bystr1 to the string pointed to by str2.

Declaration

Following is the declaration for strcmp() function.

Parameters

str1 -- This is the first string to be compared.

str2 -- This is the second string to be compared.

Return Value

This function returned values are as follows:

2)

The C library function FILE *fopen(const char *filename, const char *mode) opens the filenamepointed to by filename using the given mode.

if Return value if < 0 then it indicates str1 is less than str2

if Return value if > 0 then it indicates str2 is less than str1

if Return value if = 0 then it indicates str1 is equal to str2

Declaration

Following is the declaration for fopen() function.

Parameters

filename -- This is the C string containing the name of the file to be opened.

mode -- This is the C string containing a file access mode.It includes:

This function returns a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error.

3)   

The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX.

RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.

Declaration

Following is the declaration for rand() function.

Parameters

NA

Return Value

This function returns an integer value between 0 and RAND_MAX.

Return Value

4)

Description

The C library function int fprintf(FILE *stream, const char *format, ...) sends formatted output to a stream.

Declaration

Following is the declaration for fprintf() function.

Parameters

stream -- This is the pointer to a FILE object that identifies the stream.

format -- This is the C string that contains the text to be written to the stream.It can optionally contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. Format tags prototype is %[flags][width][.precision][length]specifier

Return Value

If successful, the total number of characters written is returned otherwise, a negative number is returned.

5)

Description

The C library function int fclose(FILE *stream) closes the stream. All buffers are flushed.

Declaration

Following is the declaration for fclose() function.

Parameters

stream -- This is the pointer to a FILE object that specifies the stream to be closed.

Return Value

This method returns zero if the stream is successfully closed.On failure, EOF is returned.

mode Description "r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is considered as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. "a+" Open a file for reading and appending.