C Programming The Basics This assignment consists of two parts. The first part focuses on implementing logical decisions and integer computations in C, using a C function, and also introduces some examples of I/O in C and simple separate compilation. The second part focuses on dealing with formatted input and writing a complete C program. Q1. For this question, you will implement the following C function: /** * Forms an integer value by taking the Nth digits of Value, starting * at the low digit, and concatenating those digits. * * Examples: * everynth(12345, 1) = 12345 * everynth(12345, 2) = 135 * everynth(12345, 3) = 25 * everynth(12345, 4) = 15 * everynth(12345, 5) = 5 * * Pre: Value and N have been initialized. * N > 0. * Post: * Returns: integer value formed by concatenating the specified * digits of Value; if N = 0, the return value is undefined * * Restrictions: * You may use any integer operations supported in C. You may also * use any selection (if, if..else, etc) or iteration (for, while) * constructs. * You may not use an array, nor may you perform any I/O operations. */ uint64_t everynth(uint64_t Value, uint8_t N); Begin by downloading the posted shell files: driver.c everynth.h everynth.c sample test driver; modify as you like C header file for compiling with test driver --- do not modify! C source file for implementation of everynth() function You can compile these files by using the following command in a Linux shell: gcc o driver std=c99 -Wall driver.c everynth.c Using this command with the supplied files will yield an executable named driver. You can execute the testing code by using the following command in a Linux shell:./driver (If you've added the current directory to your Linux path, you can omit the "./" from the command.) Although the given code will compile correctly, the resulting program will not satisfy the requirements of the assignment, since the supplied implementation of the required function merely returns a hard-wired value regardless of the values of the parameters. So, you must correctly complete the implementations of the functions. Of course, as you work on your solution for the function, you may encounter compiler error messages. You may, and should, modify the supplied testing code since it's not exactly thorough. But, if your solutions don't compile with the supplied testing code, they won't compile with the real testing code either. This is a purely individual assignment! 1
It's not likely, but you may need to add include directives to your.c file, as needed for any C Standard Library features you use. You may write secondary "helper" functions if you like; if so, those must be defined and declared within one of the supplied source files. Take note of and conform to the restrictions in the header comments above. Here are some samples of test data and correct results: Value N Result 0 4 0 831 3 1 3725447465556560270 2 3247656620 247380727 2 27877 9215 4 5 16007473260259540923 3 6730903 100005 3 5 1045864930834290 3 154040 10403828 3 38 100000119 8 19 Do not consider the test data above to be comprehensive. What to Submit You will submit your modified version of the file everynth.c to the Curator, via the collection point HW04Q1. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ Q2. For this question, you will implement a C program that reads an input file containing some highly-formatted data values, performs some rather simple calculations, and writes out a report conforming to some strict formatting requirements. More specifically, consider the following line of input data: 9040-12345 83:97:63:88:72:91:79:56 The first value is an ID field, consisting of a 4-digit integer and a 5-digit integer, separated by a single hyphen character. That is followed by some whitespace, and then a sequence of up to 8 nonnegative integers, separated by colon characters. Your program would parse this line, compute the average of the scores, and write the following line to an output file named Results.txt. For the input line above, your program should write the following output: 904012345: 78.62 on 8 assignments The output consists of the ID, without the hyphen, a colon, some whitespace, the average of the given scores with two digits after the decimal point, and then some text stating how many assignments were included. This is a purely individual assignment! 2
The input file your program will read is named ScoreData.txt. There is no specified limit on the number of data lines you'll have to process. Each line will conform to the description given above, except that there may be fewer than 8 scores in some cases. Here's a sample input file and the corresponding output your program should write: 9040-12345 83:97:63:88:72:91:79:56 9040-38532 56:63:71:55:72:81:: 9030-73014 79:93:89:97:85:91:88:95 9040-00132 ::::::: If any scores are missing, there will still be the expected number of colon characters. Moreover, there won't be any missing scores "in the middle". That is, if a score is missing, so are all the following ones. Your program must deal correctly with any number of scores, from 0 to 8. The output (shown below) shows what your program should write in the event of missing scores: 904012345: 78.62 on 8 assignments 904038532: 66.33 on 6 assignments 903073014: 89.62 on 8 assignments 904000132: no score data was read Aside from the possibility of missing scores, you don't need to account for any syntactic issues. The input file will always conform to the description given here. There is some coding restrictions: Your implementation must all be in a single file. We will explore separate compilation thoroughly on later assignments. You may not use arrays in your implementation; that is an annoyance, if you already understand how to use arrays, but it gives you the opportunity to write a very simple solution using other approaches. What to Submit You will submit your solution in a single.c file to the Curator, via the collection point HW04Q2. You may use any valid Linux file name. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ This is a purely individual assignment! 3
Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an allowed source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. <Student Name> Failure to include this pledge in a submission will result in the submission being disallowed during code review. This is a purely individual assignment! 4
Some Hints on Managing I/O Suppose we want to read an input file like this: 17 * 42 + 19-23 * 5 + 81... 113 * 12 + 5 Each line of the file is supposed to contain three integers, formatted in a specific way: integer * integer + integer We don't know in advance how many lines of input will be found, so we need to implement logic that will terminate reading gracefully when we reach the end of the input. But, suppose that some line in the input file doesn't follow the specified format? We would also want our logic to behave sensibly in that case as well. Here's a first attempt: int main() { int x, y, z; FILE* fp = fopen("input.txt", "r"); if ( fp == NULL ) { printf("error opening file input.txt.\n"); return 1; fopen() returns NULL if the file could not be opened while ( fscanf(fp, "%d * %d + %d", &x, &y, &z) == 3 ) { fscanf() returns the number of values that were read and assigned to variables printf("%5d * %5d + %5d = %6d\n", x, y, z, x * y + z); if (!feof(fp) ) { printf("failed to read to end of file.\n"); fclose(fp); return 0; feof() returns true iff the EOF flag has beens set for the file The format string corresponds to our expectations about the formatting of the input we are going to read. This program would deal correctly with an input file that did conform to the format we described above, halting automatically when we try to read after the last line of input. It would also halt if we encountered any sort of failure trying to read input before the end of the file (since fscanf() would then return something less than 3). But, if there's an input failure in the middle of the file, the program will not process the remainder of the file. That's not good enough, but how can we fix this? The problem is that if an input failure occurs in the middle of a line, that's where reading will end. To recover we need to be sure we start reading again at the beginning of the next line (if there is one). The idiomatic way to deal with this in C is to read the file line by line, and parse the lines as they are read. In order to do this, we will introduce a few new C features, a bit before their time: This is a purely individual assignment! 5
#include <stdio.h> #define MAXLENGTH 100 int main() { char linebuffer[maxlength]; int x, y, z; FILE* fp = fopen("input.txt", "r"); if ( fp == NULL ) { printf("error opening file input.txt.\n"); return 1; We use an array of chars to hold the current line; we've assumed a reasonable maximum line length fgets() reads an entire line, including the trailing newline, and puts it into the array while ( fgets(linebuffer, MAXLENGTH, fp)!= NULL ) { sscanf() is like fscanf() except that it reads from a character array instead of a file if ( sscanf(linebuffer, "%d * %d + %d", &x, &y, &z) == 3 ) printf("%5d * %5d + %5d = %6d\n", x, y, z, x * y + z); if (!feof(fp) ) { printf("failed to read to end of file.\n"); fclose(fp); return 0; The program above will continue processing lines, even if a "bad" line is found. As it stands, bad lines are simply discarded, but it would be simple to change this to do something with the bad lines as well. We will cover the details of arrays in C and how character strings are handled in C shortly. For now, this example should be sufficient. This is a purely individual assignment! 6