gcc o driver std=c99 -Wall driver.c everynth.c

Similar documents
gcc o driver std=c99 -Wall driver.c bigmesa.c

Pointer Casts and Data Accesses

Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer.

You will provide an implementation for a test driver and for a C function that satisfies the conditions stated in the header comment:

CS 1044 Program 6 Summer I dimension ??????

Given that much information about two such rectangles, it is possible to determine whether they intersect.

For storage efficiency, longitude and latitude values are often represented in DMS format. For McBryde Hall:

A rectangle in the xy-plane, whose sides are parallel to the coordinate axes can be fully specified by giving the coordinates of two opposite corners:

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

struct _Rational { int64_t Top; // numerator int64_t Bottom; // denominator }; typedef struct _Rational Rational;

A Capacity: 10 Usage: 4 Data:

Accessing Data in Memory

Pointer Accesses to Memory and Bitwise Manipulation

CS 2604 Minor Project 1 DRAFT Fall 2000

Creating a String Data Type in C

CS 2604 Minor Project 1 Summer 2000

For this assignment, you will implement a collection of C functions to support a classic data encoding scheme.

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Pointer Accesses to Memory and Bitwise Manipulation

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

Decision Logic: if, if else, switch, Boolean conditions and variables

Here is a C function that will print a selected block of bytes from such a memory block, using an array-based view of the necessary logic:

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so!

CS 2704 Project 1 Spring 2001

Pointer Accesses to Memory and Bitwise Manipulation

Both parts center on the concept of a "mesa", and make use of the following data type:

The assignment requires solving a matrix access problem using only pointers to access the array elements, and introduces the use of struct data types.

Simple C Dynamic Data Structure

CS 1044 Program 2 Spring 2002

PR quadtree. public class prquadtree< T extends Compare2D<? super T> > {

CS 1044 Project 1 Fall 2011

File Navigation and Text Parsing in Java

Invoice Program with Arrays and Structures

CS 2604 Minor Project 3 Movie Recommender System Fall Braveheart Braveheart. The Patriot

CS 2604 Minor Project 3 DRAFT Summer 2000

CS 1044 Project 2 Spring 2003

Stream Model of I/O. Basic I/O in C

EM108 Software Development for Engineers

CS 2704 Project 3 Spring 2000

Fundamental Concepts: array of structures, string objects, searching and sorting. Static Inventory Maintenance Program

Standard File Pointers

File Navigation and Text Parsing in Java

CS2304 Spring 2014 Project 3

IO = Input & Output 2

CpSc 1111 Lab 5 Formatting and Flow Control

File I/O. Arash Rafiey. November 7, 2017

CS 3114 Data Structures and Algorithms DRAFT Minor Project 3: PR Quadtree

The Program Specification:

ECE264 Spring 2013 Exam 1, February 14, 2013

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS 1044 Project 5 Fall 2009

CpSc 1111 Lab 9 2-D Arrays

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CS 2605 Lab 10 Spring 2008

CS ) PROGRAMMING ASSIGNMENT 11:00 PM 11:00 PM

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CS 2505 Computer Organization I

Do not start the test until instructed to do so!

Chapter 10: File Input / Output

CS 2505 Computer Organization I

CpSc 1111 Lab 4 Formatting and Flow Control

3. A Periodic Alarm: intdate.c & sigsend.c

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

System Software Experiment 1 Lecture 7

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

printf( Please enter another number: ); scanf( %d, &num2);

Curator System Student Guide January 2004

a f b e c d Figure 1 Figure 2 Figure 3

CSCI 4963/6963 Large-Scale Programming and Testing Homework 1 (document version 1.0) Regular Expressions and Pattern Matching in C

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Multiple-Key Indexing

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-32 assembly code is produced:

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

are all acceptable. With the right compiler flags, Java/C++ style comments are also acceptable.

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CS 2704 Project 2: Elevator Simulation Fall 1999

CDA3101 Project 1: MIPS Assembler Due 2/5. I. Purpose

COMP1917: 15 File IO

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #47. File Handling

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

C mini reference. 5 Binary numbers 12

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

int32_t Buffer[BUFFSZ] = {-1, -1, -1, 1, -1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, -1, -1, -1, -1, -1}; int32_t* A = &Buffer[5];

This paper was presented at DVCon-Europe in November It received the conference Best Paper award based on audience voting.

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

The basic nature of string-handling in C causes some problems with input of strings.

CSE 374 Midterm Exam 11/2/15. Name Id #

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS : Programming for Non-majors, Fall 2018 Programming Project #2: Census Due by 10:20am Wednesday September

A Crash Course in C. Steven Reeves

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

READ THIS NOW! Do not start the test until instructed to do so!

C FILE Type. Basic I/O in C. Accessing a stream requires a pointer of type FILE.

M.CS201 Programming language

Transcription:

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