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

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

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

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

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:

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

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

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

Pointer Casts and Data Accesses

CS 2604 Minor Project 1 Summer 2000

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

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

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

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 2604 Minor Project 1 DRAFT Fall 2000

Creating a String Data Type in C

CS 1044 Program 2 Spring 2002

CS 1044 Project 1 Fall 2011

The Program Specification:

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

CS 1044 Project 2 Spring 2003

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

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.

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

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

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 2704 Project 1 Spring 2001

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

A Capacity: 10 Usage: 4 Data:

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

Simple C Dynamic Data Structure

File Navigation and Text Parsing in Java

CS 1044 Project 5 Fall 2009

CS 2604 Minor Project 3 DRAFT Summer 2000

Project 1: How to Make One Dollar

File Navigation and Text Parsing in Java

CS 2704 Project 3 Spring 2000

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

Invoice Program with Arrays and Structures

Pointer Accesses to Memory and Bitwise Manipulation

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

CS2304 Spring 2014 Project 3

ECE264 Fall 2013 Exam 3, November 20, 2013

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

Pointer Accesses to Memory and Bitwise Manipulation

IO = Input & Output 2

// 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.

Bristol Institute of Technology

CS 2704 Project 2: Elevator Simulation Fall 1999

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

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

Project 2: Shell with History1

Accessing Data in Memory

Data Structures and Algorithms

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

CSE 303 Midterm Exam

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

CS 2505 Fall 2018 Data Lab: Data and Bitwise Operations Assigned: November 1 Due: Friday November 30, 23:59 Ends: Friday November 30, 23:59

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Pointer Accesses to Memory and Bitwise Manipulation

High Performance Computing MPI and C-Language Seminars 2009

ECE264 Summer 2013 Exam 1, June 20, 2013

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

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

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

CS3114 (Fall 2013) PROGRAMMING ASSIGNMENT #2 Due Tuesday, October 11:00 PM for 100 points Due Monday, October 11:00 PM for 10 point bonus

3. When you process a largest recent earthquake query, you should print out:

Recitation: C Review. TA s 20 Feb 2017

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

CS 322 Operating Systems Programming Assignment 4 Writing a memory manager Due: April 5, 11:30 PM

Problem Set 1: Unix Commands 1

CS 111X - Fall Test 1

ECE264 Spring 2013 Exam 1, February 14, 2013

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

CS 2505 Computer Organization I

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

CS 2505 Fall 2013 Data Lab: Manipulating Bits Assigned: November 20 Due: Friday December 13, 11:59PM Ends: Friday December 13, 11:59PM

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Exercise Session 2 Systems Programming and Computer Architecture

CSE 333 Midterm Exam July 24, Name UW ID#

Approximately a Test II CPSC 206

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

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

CSE 303: Concepts and Tools for Software Development

COP4342 UNIX Tools Assignment #3: A Simple Unix Shell. Instructor: Dr. Robert Van Engelen Teaching Assistant: Imran Chowdhury Spring 2018

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

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

CSE 333 Lecture 7 - final C details

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

PROGRAMMAZIONE I A.A. 2017/2018

CS 6353 Compiler Construction Project Assignments

1. We have a code sequence that is potentially executed twice.

Homework 2 Answers. Due Date: Monday, April 29, 2002, at 11:59PM Points: 100

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

CS 1713 Introduction to Programming II

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

Transcription:

Decision-making in C Squeezing Digits out of an Integer Assignment For part of this assignment, you will use very basic C techniques to implement a C function to remove from a given nonnegative integer all the even (or odd) digits, and return the resulting integer. For example: squeeze even digits from 1961316882: 19131 squeeze odd digits from 1205714786: 20486 squeeze odd digits from 7911359: 0 You will provide an implementation for a C function that performs this calculation: /** Pre: N is initialized * Action is EVEN or ODD * Returns: integer obtained by removing all EVEN (or all ODD) digits * from N; 0 if no digits of N remain * * Restrictions: * - uses only its parameters and local automatic variables * - does not make any use of character variables or arrays * - does not read any input or write any output */ unsigned int SqzInteger(unsigned int N, enum Process_Choice Action); For the rest of the assignment, you will finish the implementation of the short C program that is supplied later in this specification (and on the course website). The completed program provides a means of testing your implementation of the function SqzInteger() described above, and is what you will submit to the Curator for grading. The supplied program provides some code and comments indicating how it can be completed. The supplied program will compile, but you must add code in the indicated places in order for it to work correctly. The restrictions stated in the header comment for the function are not enforced by the autograding system, but they will be checked by a TA after the project is closed. Violating the restrictions will result in a score of 0. The completed program will read test data from an input file, that will be named on the command line when the program is executed. Here is a sample file: odd 92488844 even 137557 odd 135791593 even 86188243 even 46010890 odd 398402673 even 91560820 odd 65724 Each line will contain a string ("even" or "odd"), followed by one or more spaces, followed by a nonnegative integer. There is no limit on the number of lines of test data that may occur in the file, so the program that reads the file must be designed so that it works correctly by detecting the end of the input data. The input file is guaranteed to follow the specified format, so you don't have to check for errors of that sort. This is a purely individual assignment! 1

The program will write results to a file, that will be named on the command line when the program is run. Here is a sample output file that corresponds to the input file shown above: 92488844(no_odds) 2488844 137557(no_evens) 137557 135791593(no_odds) 0 86188243(no_evens) 13 46010890(no_evens) 19 398402673(no_odds) 84026 91560820(no_evens) 915 65724(no_odds) 624 For each input line, there is one output line, displaying the given nonnegative integer, followed by a string inside parentheses indicating how the integer was "squeezed", followed by one or more spaces, followed by the "squeezed" integer value returned by your implementation of the function SqzInteger(). The values should be neatly aligned, as shown above. You might take advantage of the fact that an unsigned int in C won't have more than 10 digits in its base-10 representation. You should test your solution thoroughly; there is no guarantee that the supplied test data covers all cases. You may assume that the supplied data will be logically correct. You should not make any other assumptions about the test data values. Make useful comments in your implementation of SqzInteger(). The same general guidelines for commenting that you have been taught in your Java courses should provide sufficient guidance. Testing You should begin by downloading the posted C source file, SqzInteger.c, from the course website, and editing it to satisfy the requirements given above. To compile the program, you should use the following command: CentOS> gcc o SqzInteger std=c99 Wall SqzInteger.c Once you've eliminated compile-time errors (and ideally warnings as well), you should download test files from the course website. Say the first input file is named DataIn01.txt; then you could execute the program by using the following command: CentOS>./SqzInteger DataIn01.txt DataOut.txt Next, you should compare the output file your program writes to the corresponding posted output file. If there are differences, analyze the reason for them and fix your code. Repeat as necessary. You should not submit your solution to the Curator until you can correctly process all the posted test files. Even then, the posted data files are not guaranteed to include all the possible logical cases, so you should consider writing additional test files. It is perfectly OK to share test data files with other students, including on the course Forum board. What to Submit Submit your completed version of SqzInteger.c, after making changes and testing. Your submission will be compiled, tested and graded according to the formatting of your output and how many cases your solution handles correctly. You will be allowed a limited number of submissions for this assignment (at least 10). Use them wisely. Test your program thoroughly before submitting it. Make sure that your program produces correct results for every test case you can think of. If you do not get a perfect score, analyze the problem carefully and test your fix with the test data shown in the Curator grade report, before submitting again. The highest score you achieve will be counted. This is a purely individual assignment! 2

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/ 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 authorized source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. - I have not designed this program in such a way as to defeat or interfere with the normal operation of the Curator System. <Student Name> Failure to include this pledge in a submission may result in a score of zero being assigned. This is a purely individual assignment! 3

Supplied code: The following code is supplied on the course website. You should use this file as your starting point. Feel free to modify the design used here, but your solution must conform to the requirements stated above, and the SqzInteger() function must retain the specified interface. The code contains numerous comments that explain what is being done (or should be done) when the program is executed. Since the comments also serve as a tutorial for some aspects of C coding, you should read them carefully. /** CS 2505 Summer 2015 * Supplied framework for "squeeze out digits" project. Your task is to * complete the supplied code to satisfy the posted specification for this * assignment. Comments at various places in the code below indicate where * you must complete the implementation. * * Student: <ENTER YOUR NAME HERE> * PID: <ENTER YOUR VT EMAIL PID HERE> */ #include <stdio.h> #include <stdlib.h> #include <string.h> We will expect the longest line in the input file to contain no more than 100 characters (which is generous in this case). const unsigned int MAX_LINE_LENGTH = 101; Rather than use a nondescriptive label, or none at all, we will use an enumerated type to make the logic of the code clearer: enum Process_Choice {EVEN, ODD; SqzInteger() removes all digits from N that do meet the specified Action (ODD or EVEN), and returns the resulting integer. If all the digits of N are removes, 0 is returned. unsigned int SqzInteger(unsigned int N, enum Process_Choice Action); int main(int argc, char* argv[]) { This program expects the user to supply the names of an input file and an output file on the command line. Therefore, it checks for that and exits with a diagnostic message if the number of command line tokens is incorrect: if ( argc!= 3 ) { printf("invocation: squeezeint inputfilename outputfilename\n"); return 1; Attempt to open the specified input file; exit with a diagnostic if it cannot be opened for reading: FILE* Input = fopen(argv[1], "r"); if ( Input == NULL ) { printf("could not open input file: %s\n", argv[1]); return 2; This is a purely individual assignment! 4

Attempt to open the specified output file; exit with a diagnostic if it cannot be opened for writing: FILE* Output = fopen(argv[2], "w"); if ( Output == NULL ) { printf("could not open output file: %s\n", argv[2]); fclose(input); return 3; Note that we use different exit values for different errors; that's good practice. char Line[MAX_LINE_LENGTH]; char actionflag[max_line_length]; unsigned int N = 0, squeezedn = 0; holds the current input line holds the current Action holds the integer to be "squeezed" holds the "squeezed" integer The loop uses the read-to-input-failure pattern. That is, we will attempt to read the next expected input from the file, and then validate that reading, before we attempt to process any data. In the loop test, we take advantage of the fact that fgets() will return NULL If it fails to read any input. That will force the loop to terminate when the end of the input file is reached. while ( fgets(line, MAX_LINE_LENGTH, Input)!= NULL ) { Try to read the actionflag from the input line that was just read into Line; sscanf() will return 0 if nothing was read. In that case, write a diagnostic message, skip the remainder of the loop body and look for another line of input. if ( sscanf(line, "%s", actionflag) /*?? TEST?? */ ) { printf("failed to read an actionflag value from %s\n", Line); continue; Try to read an integer value from the input line that was just read into Line. Here, we encounter one limitation of sscanf(); it does not maintain any knowledge of previous calls, so it resumes reading from the beginning of Line. We could sidestep that issue by reading both the actionflag and the integer in a single call to sscanf(); but that makes checking the results of the two read operations clumsy. Instead, we take advantage of a C format specifier feature; if we follow the '%' with an asterisk, a value is read and then discarded. In this case we reread the action flag value but don't save it. if ( sscanf(line, "%*s %d", &N) /*?? TEST?? */ ) { printf("failed to read an integer value from %s\n", Line); continue; Determine whether the actionflag indicates we should squeeze out the even digits, or the odd digits, or that there was something wrong with the actionflag. strcmp() compares two (C-style) character strings; it returns zero if the strings match, something negative if the first This is a purely individual assignment! 5

string precedes the second string, and something positive if the first string follows the second string. if ( strcmp(actionflag, "even") == 0 ) { squeezedn = 0; CALL THE FUNCTION!! WRITE RESULTS TO THE OUTPUT FILE!! else if ( strcmp(actionflag, "odd") == 0 ) { squeezedn = 0; CALL THE FUNCTION!! WRITE RESULTS TO THE OUTPUT FILE!! else { This should NEVER happen... fprintf(output, "Unrecognized action flag: %s\n", actionflag); It's important to explicitly close files when we are finished with them, because: - the OS needs to know the files are no longer in use - output written to a file is buffered and may not actually reach the file unless it is properly closed fclose(input); fclose(output); Traditionally, return 0 on successful termination. return 0; /** Pre: N is initialized * Action is EVEN or ODD * Returns: integer obtained by removing all EVEN (or all ODD) digits * from N; 0 if no digits of N remain * * Restrictions: * - uses only its parameters and local automatic variables * - does not make any use of character variables or arrays * - does not read any input or write any output */ unsigned int SqzInteger(unsigned int N, enum Process_Choice Action) { unsigned int Squeezed = 0; IMPLEMENT THIS!! return Squeezed; This is a purely individual assignment! 6