COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling

Similar documents
Standard File Pointers

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

CpSc 1111 Lab 9 2-D Arrays

LESSON 4. The DATA TYPE char

Welcome! COMP s1. Programming Fundamentals

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CpSc 1111 Lab 4 Formatting and Flow Control

9/10/2016. Time for Some Detailed Examples. ECE 120: Introduction to Computing. Let s See How This Loop Works. One Statement/Step at a Time

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

بسم اهلل الرمحن الرحيم

Intermediate Programming, Spring 2017*

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

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

COMP Assignment 1

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

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

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)

Lab 1 Introduction to UNIX and C

Fundamentals of Programming. Lecture 11: C Characters and Strings

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Lab 3 : Sort program

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

COMP-202: Foundations of Programming

CS4023 Week04 Lab Exercise

Basic I/O. COSC Software Tools. Streams. Standard I/O. Standard I/O. Formatted Output

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

CpSc 1111 Lab 5 Formatting and Flow Control

CSC209. Software Tools and Systems Programming.

Topic 6: A Quick Intro To C

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

Project #1: Tracing, System Calls, and Processes

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

Warm-up sheet: Programming in C

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

ASSIGNMENT TWO: PHONE BOOK

Fundamentals of Programming Session 4

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Display Input and Output (I/O)

Binghamton University. CS-220 Spring Includes & Streams

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

today cs3157-fall2002-sklar-lect05 1

Advanced C Programming Topics

Lab #2 Physics 91SI Spring 2013

Lab 03 - x86-64: atoi

CS240: Programming in C. Lecture 2: Overview

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

C PROGRAMMING. Characters and Strings File Processing Exercise

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Python lab session 1

COSC2031 Software Tools Introduction to C

Writing to and reading from files

Chapter 9 Strings. With this array declaration: char s[10];

Lab # 2. For today s lab:

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Project 1 (document version 1.3) Process Simulation Framework

Algorithms, Data Structures, and Problem Solving

Introduction to C An overview of the programming language C, syntax, data types and input/output

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

SWEN-250 Personal SE. Introduction to C

COMP 202 Java in one week

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

CSC209. Software Tools and Systems Programming.

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Computers and Computation. The Modern Computer. The Operating System. The Operating System

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

C Libraries. Bart Childs Complementary to the text(s)

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

CS Programming In C

CS3157: Advanced Programming. Outline

CS143 Handout 05 Summer 2011 June 22, 2011 Programming Project 1: Lexical Analysis

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Spring CS Homework 3 p. 1. CS Homework 3

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CS354 gdb Tutorial Written by Chris Feilbach

ACM SOUTH PACIFIC DIVISIONAL PROGRAMMING CONTEST

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

C-LANGUAGE CURRICULAM

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Variables and literals

8. Characters, Strings and Files

UNIT IV-2. The I/O library functions can be classified into two broad categories:

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

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

11 Console Input/Output

Chapter 8 - Characters and Strings

Course Outline Introduction to C-Programming

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Assignment 1. CSI Programming Practice, Fall 2011

Creating a Shell or Command Interperter Program CSCI411 Lab

Probably the best way to start learning a programming language is with a program. So here is our first program:

Multiple Choice Questions ( 1 mark)

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Transcription:

COMP26120 Academic Session: 2018-19 Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling Duration: 1 lab session For this lab exercise you should do all your work in your COMP26120/ex2 directory. Important: If you are new to C then you should read the supporting material on how to compile and run C programs first before lab session. Otherwise you will find this lab difficult. Also Important: After submitting your code log in to https://marking.cs.manchester.ac.uk to check that you are passing all of the tests. If you are not then you can resubmit. Learning Objectives At the end of this lab you should be able to: 1. Compile and link C programs (using make) and then run them. 2. Explain some major differences and similarities between C and Java. 3. Find out about different (basic) features of the C language by looking in man pages and other supporting tools. 4. Create and use strings and arrays in C Introduction This lab consists of two parts. The first part explores basic functionality of C programs and focuses on input and output. The second part introduces you to C strings (which are fundamentally different from Java strings) and how to deal with command line arguments in C. For your understanding it would be better to get half way through both than to finish one and not start the other. Through both parts we will also explore the idea of error handling in C programs. Part 1 - C Input/Output In this part you will produce a file called part1.c (which you will submit). You will modify this file as you go along so it would be a good idea to keep a working back-up each time you progress to a new step. 1

Step 1A Write a C program to read characters one-by-one from standard input (you can use ctrl-d to terminate the input), convert all upper-case characters to lower-case and all lower-case characters to upper-case, and write the result to standard output e.g.: Hello World! would become: hello world! You should also count how many characters you have read, and how many of those you have converted in each direction, and output the totals at the end in exactly the following format: Read 13 characters in total, 8 converted to upper-case, 2 to lower-case remember a newline at the end of the line. It is important that you use this exact format as the online marking system will expect it. But note that you can see the result of these tests after you submit and resubmit if the tests are failing there is no good reason to have failing tests as you can check them yourselves. As always, it might help to sketch the psuedocode for the algorithm first and then think about what functionality you need to implement it. It might be informative to reflect at this point how you would have done this in Java. Hints: You will need to use getchar and putchar for the individual characters, as well as printf for the final character counts. You may want to use some of the functions in ctype.h, such as tolower and isupper (e.g. use man ctype.h, look at supporting material for what header files are). Step 1B Edit your program so that the input is read from a file opened from within your program. For the time being, you can use a fixed file-name such as input. Remember to check that the file is correctly opened. Test this by running the program when there is no input file available. If opening the file fails write something relevant to stderr - we will return to how to handle errors in C properly later. Hint: Refer to man fopen and SalaryAnalysis.c. Step 1C Edit your program so that the updated input file is written to a file, using a fixed file-name such as output. The summary of what was read and converted should still be output to stdout and not to the output file. Remember to check that the file is correctly opened. Test this by running the program when a file of that name exists, but is write protected (e.g. chmod u-w output) 2

Step 1D Edit your program so that, instead of using fixed filenames such as input and output, both filenames are read from standard input when the program is run (i.e. use scanf or similar; I don t want you to use command-line parameters). Part2 - Strings and Program Parameters In this part we want you to start thinking more about how you write your code (note that there are marks for well-formatted and commented code throughout the course) in particular by writing tests and performing proper error handling. You will need to create a suitable set of tests for each problem, which both check that your program works correctly given sensible data, and that it detects any possible errors, including silly or missing inputs. In part b we will discuss how you should handle errors properly. a. Length of Program Parameters Write a C program part2a.c that, given any number of program parameters (command-line parameters), calculates the length of each one, and writes the longest to standard output. In the case of a tie-break the first parameter should be output. Before you begin, create some interesting tests for your program in your makefile 1, so it is easy to run them after each change. For example, you could add something like this (leave a blank line before the test2a line, and each of the following command-lines should start with a tab): 1 test2a : 2./ part2a # no parameters 3./ part2a " only one parameter " 4./ part2a " biggest parameter " "at" " start " 5./ part2a " biggest " " parameter " " at" " end " " very very very big parameter " 6./ part2a " answer " " somewhere " "in" " the " " middle " 7./ part2a " two " " strings " " the " " same " " length " "ha ha!" and then run it using make test2a. Note that make expects an exit code of 0 (you did remember to return 0 didn t you?) Hints: There are many functions that act on strings in string.h You should use one of these to find the length of a string. But what should we do if there are no inputs? It is a matter of debate whether this is an error or not but that should be part of the specification. For now print the line 1 Error : expected some command l i n e parameters and we will consider a better way to handle errors in the next part. b. Temperature Conversion Write a C program part2b.c that will convert temperatures between Celsius and Fahrenheit, and is controlled by program parameters. Your program should accept two parameters in the following way: part2b -f number converts the number from Fahrenheit to Celsius 1 If you re not sure what a makefile is then go back and read the supporting material. 3

part2b -c number converts the number from Celsius to Fahrenheit where number is any floating point number. e.g. 1 > part2b -f 50.0 2 10.00 C = 50.00 F 3 4 > part2b -c 10 5 10.00 C = 50.00 F We suggest you achieve the above by first writing the two functions float c2f (float c) which takes a temperature in Celsius and converts it to Fahrenheit (f = 9*c/5 + 32) float f2c (float f) which takes a temperature in Fahrenheit and converts it to Celsius Your program should handle erroneous input, such as the wrong flag or the wrong number of parameters or temperatures below absolute zero (-273.15 C). In C the standard way to handle errors is to return a non-zero exit status that can be interpreted to understand the error. Throughout this part you must use the following exit statuses. Status Description 1 The wrong number of parameters have been supplied 2 Failed to supply either -f or -c as the first option 3 Malformed number in second parameter 4 Temperatures below absolute zero Note that you should use the lowest numbered exit status that applies e.g. -x should return 1 rather than 2. Create some interesting tests for your program in your makefile, so it is easy to run them after each change. Note that these tests will be checked when you are marked. To test that the correct exit codes are produced you can use something like 1. / part2b && e x i t 1 [ $$? eq 1 ] #no parameters in your makefile (can you work out what this is doing?). Note: In C99, the compiler will issue a warning if a function is called before its declaration. Hint: Use sscanf to convert a string into a floating point number. You should check the return result to ensure that you have found a valid floating point number. Again, so that the automated tests work you must format your output as shown above. Importantly you must: Print numbers to two decimal places Include a single space on either side of = Use the degree symbol as specified in man iso 8859-1 (part of the ISO/IEC 8859 series of ASCII-based standard character encodings). 4

Submission Run submit in your COMP26120/ex2 directory. Three files are expected: part1.c, part2a.c, and part2b.c. Remember, once you have submitted go to https://marking.cs.manchester.ac.uk and login to see a report that will tell you whether you have passed all of the tests. If you haven t then you can resubmit. Marking Scheme The marks are awarded according to the following marking rubric. Similar marking rubrics will be used throughout this course. The marking scheme is formed of a sequence of questions where the answer to each question is associated with a particular mark. These are the questions that your markers will be asking themselves during marking. 5

Is Part 1 complete? All steps have been completed and work as expected (i.e. the tests pass), including (2) error handling in the handling of files All steps have been completed but some may have small errors or miss special (1.5) cases An attempt has been made to complete at least two of the steps (0.5) No attempt made (0) Is Part 2 complete? Both steps have been completed and work as expected (i.e. the tests pass), including error handling (2) Both steps have been completed but there is a small mistake (1.5) Both parts have been attempted, with one part complete, and everything completed works as expected (i.e. the tests pass) (1.5) Both parts have been attempted and what has been completed mostly works but (1) there are a few mistakes A non-trivial attempt has been made at one of the parts (0.5) No attempt made (0) Does Part 2 have good tests? A reasonable number (at least 5) of tests have been written for each part providing (2) good coverage of the functionality. The tests can be run automatically. Some tests have been written for each part but they miss important cases or (1.25) cannot be run automatically An attempt has been made at writing some tests but they are inadequate (0.5) There are no tests (0) Does the student understand their code? The student can explain how the code works, including what the various library (2) functions do and how the compilation process works The student can answer most but not all questions about how the code (etc) (1.25) works The student can answer some but not most of the questions about how the code (0.5) (etc) works The student does not understand their code (0) Is the overall quality of all code good? Code is well formatted and commented with appropriate variable names (2) Code is mostly well formatted and commented but there are some missing comments (1.5) or poor variable names There is at least one comment (0.5) There are no comments (0) The code is not readable (0) 6