Question: You are to write a C program for fourier that repeatedly reads input from a file and processes it. Each input consists of an integer,

You are to write a C program for fourier that repeatedly reads input from a file and processes it. Each input consists of an integer, two endpoints specifying a substring of the integer, and a flag. We select the specified substring and print it out in hex and as an unsigned, 1s complement, or 2s complement integer, depending on the flag ('u', '1', or '2'). For example, an input of 1234abcd 8 4 u asks to find bits 8:4 of 1234abcd, which turn out to be the 5 bits 11100, so we print hex 1c and 28 (the interpretation of 11100 as a 5-bit unsigned integer). We would have printed -3 for a flag of '1' and -4 for a flag of '2'. You'll write the program by extending the skeleton Lab3_skel.c and you can test using the data in Lab3_data.txt

1. [4 points] The file to read from should be specified on the command line as the second word. E.g., ./a.out somefile.txt If the filename is not specified, use Lab3_data.txt as the default file. Say what file youre opening (possibly the default) and fopen the file.

2. [3 points] Make sure fopen succeeded; if it returns NULL, the input file couldnt be opened. In that case, print a message saying so and quit the program using return 1 . (Returning a non-zero value is the standard way to indicate that a program had an error on Unix-like systems.)

3. [2 points] Use fscanf to read the value to process as a 32-bit hexadecimal integer. while fscanf succeeds and the value to process is nonzero

4. [4 points] fscanf the right and left endpoints a type character.

5. [5 points] . If the endpoints or type are bad, complain (Should have 0 left right 31 for the endpoints and u, 1, or 2 for the type.)

6. [7 points] . Otherwise, . . Select the n-bit substring of the integer specified by the endpoints (where n is the length of the selection; you only need to handle n 31).

7a. [3 points] . . Print the substring as a hexadecimal value and as an n-bit

7b. [3 points] unsigned integer,

7c. [8 points] or 1's or 2's complement integer (as specified by the type).

8. [3 points] Read the next hexadecimal integer and continue the loop. Note your program shouldnt go into an infinite loop if any fscanf fails to read successfully (because of end-of-file or badly-formatted data).

9. [3 points] Once youve hit the end of the input, use fclose to close the input file. If fclose returns 0, the close succeeded; say youve closed the file and exit the program normally (return 0). If fclose failed, print an error message saying so and exit with an error (return 1).

10. [5 points] Comment and indent your program to make it readable and understandable. Also, The general structure of your program should be reasonable. This includes using conditional and loop statements well and avoiding repetitive code.

Here is the skeleton code:

// This program should process data contained in a file; the // filename is specified as a command-line argument // (argv[1]) or defaults to Lab3_data.txt. // // Here is pseudocode for the program // Read a hexadecimal integer (as a 32-bit integer) // while the integer is nonzero // Read two integers specifying right and left endpoints // for a 32-bit integer. // Read a type character u, 1, or 2. // Select the n-bit substring of the integer specified // by the endpoints (where n is the length of the // selection). // Print the substring as a hexadecimal value and as // an n-bit unsigned, 1's complement, or 2's // complement integer (as specified by the type). // Read the next hexadecimal integer // // Example of input: 1234abcd 8 4 u // Since the last 12 bits of 1234abcd are 1011 110 1101, we // calculate 11100 as the bitstring and print out hex 1c, // which is 28 as a 5-bit unsigned integer. (In 5-bit 1's // complement, 1c = -3; in 2's complement, 1c = -3.) // // For each request, you should verify that the endpoints // are legal for a 32-bit integer, that the right endpoint // is the left endpoint, and the type is u, 1, or 2. (If // not, generate an error message and go on to the next set // of data.) // // Note: You can assume that the selection is at most 31 // bits long. (5 points extra credit if you allow 32-bit // selections -- it's fairly tricky.)

// General instructions (for the skeleton): // Replace all STUB code with actual code. You don't have // to use this skeleton, with the warning that this code // (yours and the skeleton's) are fair game to appear on // exams.

#include #include // if you need string library

int main (int argc, char *argv[]) { printf("CS 350 Lab 3 for %s ", "*** STUB ***");

// If argv[1] exists, set filename to it, otherwise set // filename to "Lab3_data.txt". // char *filename; filename = "Lab3_data.txt"; // *** STUB ***

// Open the file and say what file it was. If opening // failed, say so and return with error code 1. // FILE *in_file; in_file = fopen(filename, "r"); // NULL if the open failed // *** STUB ***

// Read the next hex integer value to process. We // initialize the value read in to zero, so that if the // fscanf fails due to bad input or end of file, we'll // stop the loop. // int val_to_inspect = 0; fscanf(in_file,"%x", &val_to_inspect);

// Loop until the value to inxpect is zero. // while (val_to_inspect != 0) { printf("%x", val_to_inspect);

// Read the endpoints and selection type. We initialize // to bad values in case the fscanf fails. // int right_end = -1, left_end = -1; char type = ' ';

// fscanf(in_file, ...); *** STUB ***

// If the endpoints are bad, complain // // *** STUB ***

// If type type is bad, complain // // *** STUB ***

// Otherwise process this input // else { // Print the endpoints // *** STUB ***

// Get the selected bits: Calculate the length // of the selection, calculate a mask of all // 1's, and use logical and to select the bits. // Print the selection in hex. // // Hint: The mask of n 1's equals 2^n - 1. // // *** STUB ***

// For type u, print the selection value as an // unsigned integer (using format %u) // // Example // if (type == 'u') { // printf(" = %u (unsigned) ", selection); // }

// For types 1 or 2, calculate the value of the // selection as a 1's or 2's complement integer // and print the result. Note we skip leading // 0's, so the length of the selection is key. // // Hint: In n bits, 011...11 = 2^n-1 is the // largest positive 1's or 2's complement // integer. If bitstring b begins with a 1, // then b (unsigned) - 2^n is b as an n-bit 2's // complement value. // // *** STUB *** // }

// Read the next value to inspect and continue the loop // val_to_inspect = 0; // *** STUB *** }

printf(" Program quitting "); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!