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

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

SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers

LAB 5 (June 15/20, 2017) Pointers and arrays Due: June 24 (Sat) 11:59 pm.

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

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

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

York University Faculty Science and Engineering Fall 2008

CpSc 1111 Lab 9 2-D Arrays

EECS2031 Software Tools

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

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

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

File IO and command line input CSE 2451

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

CpSc 1111 Lab 4 Formatting and Flow Control

Basic and Practice in Programming Lab 10

Input/Output Week 5:Lesson 16.1

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

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.

COMP s1 Lecture 1

CSC111 Computer Science II

Introduction: The Unix shell and C programming

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

Friday, February 10, Lab Notes

LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C

Program Organization and Comments

CpSc 1111 Lab 5 Formatting and Flow Control

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

Binghamton University. CS-211 Fall Input and Output. Streams and Stream IO

LESSON 4. The DATA TYPE char

Data Types. Data Types. Integer Types. Signed Integers

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

CSC 1107: Structured Programming

CSC 1107: Structured Programming

Worksheet 4 Basic Input functions and Mathematical Operators

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

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

Organization of a file

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

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

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

Standard File Pointers

Unit 4. Input/Output Functions

Input / Output Functions

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

Friday, February 3, Lab Notes. Functions in C Arrays as parameters Pass by value vs pass by reference Programs 1 and 2

Fundamentals of Programming. Lecture 11: C Characters and Strings

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

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Text Output and Input; Redirection

Binghamton University. CS-220 Spring Includes & Streams

Fundamentals of Programming Session 8

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

Simple Output and Input. see chapter 7

CSE 303 Lecture 8. Intro to C programming

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

Console Input and Output

Lab: Supplying Inputs to Programs

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

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

Introduction to Computing Lecture 03: Basic input / output operations

Exercise 1: Basic Tools

Creating, Compiling and Executing

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018

Overview (1A) Young Won Lim 9/25/17

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Introduction to Algorithms and Programming I Lab. Exercises #1 Solution

Lab 4: Tracery Recursion in C with Linked Lists

CSE / ENGR 142 Programming I

LAB 1 INTRODUCTION TO LINUX ENVIRONMENT AND C COMPILER

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #28. Functions: Examples 2

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

Fundamentals of Programming Session 4

Intermediate Programming, Spring 2017*

AMCAT Automata Coding Sample Questions And Answers

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CMPT 300. Operating Systems. Brief Intro to UNIX and C

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

Lecture 03 Bits, Bytes and Data Types

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

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

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

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

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

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

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Chapter 11 Introduction to Programming in C

Online Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1

Programming & Data Structure

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

Physics 306 Computing Lab 1: Hello, World!

Lecture 8: Structs & File I/O

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

In this introduction to UNIX, only the dumb terminal (also known as the command line ) interface is discussed.

Introduction to C Programming. What is a C program?

Software Development With Emacs: The Edit-Compile-Debug Cycle

File Handling. 21 July 2009 Programming and Data Structure 1

Transcription:

SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar) 1 Problem A 1.1 Specification Write an ANSI-C program that reads input from the Standard Input, and then outputs the reformatted versions of the input. 1.2 Implementation You can download the file.c and use it as a template. name your program lab1a.c use scanf to read input (from Standard Input), which are in the form of Month Day Year (i.e., three integers separated by white spaces). use printf to generate output in the format of Year/month/day and Year-monthday display the following prompt (leave a white space after the semi-colon) Enter month, day and year separated by blank: display output as follows (white space between each word): The input x xx xxxx is reformatted as x/xx/xxx and x-xx-xxx 1.3 Sample Inputs/Outputs: red 306 % gcc lab1a.c red 307 % a.out Enter month, day and year separated by blank: 5 20 2017 The input 5 20 2017 is reformatted as 2017/5/20 and 2017-5-20 red 308 % submit 2031 lab1 lab1a.c 2 Problem B0 Download the program lab1b0.c, compile it using gcc lab1b0.c Notice that the compilation process fails (why?), and no a.out is generated. Modify the program to make it compile. Note that you should not modify the existing code. That is, do not modify the code of main() and sum(), and also do not move the functions. Instead, add something to make it compile. Run the program to see the output. submit 2031 lab1 lab1b0.c 1

3 Problem B 3.1 Specification Improve program lab1b0, so that it can read two float numbers for the Standard Input, separated by two pound (#) signs, and then output the sum of the two float numbers. 3.2 implementation name your program lab1b.c use scanf to read inputs (from Standard Input), which are in the form of float1##float2 (i.e., two float numbers separated by two pound signs). use printf to generate output display the following prompt (leave a white space after the semi-colon): Enter two float numbers separated by ##: 3.3 Sample Inputs/Outputs: (notice that by default printf displays six digits after decimal points of a floating point number.) red 338 % gcc lab1b.c -o lab1b red 339 % lab1b Enter two float numbers separated by ##: 2.35##5.64 2.350000 + 5.640000 = 7.990000 red 340 % submit 2031 lab1 lab1b.c 4 Problem C 4.1 Specification Improve program lab1b, in such a way that it first prompts the user to enter an integer number, which indicates how many times the user wants to interact with the program. Then the program interacts with the user accordingly. 4.2 Implementation name your program lab1c.c use a loop (for or while) to interact (i.e., read input and generate output) n times, where n is entered by the user. 4.3 Sample Inputs/Outputs: (ONE blank line between each interaction/iteration): red 338 % gcc lab1c.c -o lab1c red 339 % lab1c Enter the number of interactions: 3 Enter two float numbers separated by ##: 2.35##5.64 2.350000 + 5.640000 = 7.990000 Enter two float numbers separated by ##: 1.1##2.2 1.100000 + 2.200000 = 3.300000 Enter two float numbers separated by ##: 2.5##6 2

2.500000 + 6.000000 = 8.500000 red 340 % submit 2031 lab1 lab1c.c 5. Problem D0 Download the provided program copy.c, which uses getchar and putchar to read user input from Standard input (keyboard) and write the input to Standard output (screen). (This program is also in the textbook and the lecture slides.) Play with the program and make sure you understand the program. In particular, observe a few things about getchar and putchar: getchar() returns EOF (which is a special negative number defined in C) when the end of file is reached. If it reads from a (redirected) text file, then the end of file is EOF; If it reads from Standard in (i.e., keyboard), then in Unix, ctrl D indicates EOF (in Windows, it is ctrl Z ) Although the program call putchar after every getchar, you will notice that the output is displayed only after a whole line is read in. This is correct and is related to the buffer used by the system. Specifically, instead of executing putchar for every getchar, the system buffer stores the chars that are read in, and executes putchar only after a new line \n character is read in. Instead of a char, function getchar returns an int, and putchar takes as argument an int. This will be explained in class. You don t need to submit anything for this question but doing this gets you prepared for the next question. 6. Problem D 6.1 Specification Modify the program copy.c, so that the program can count the number of blank characters (white spaces) in a line of characters. The program reads from the Standard input a line of characters and outputs the line to the standard output, and then (on the next line) the number of blank characters found in the line. The program continues to read and output until EOF (Ctr D from keyboard or end of redirected input file) is encountered. At the end, the program outputs the total number of blanks in the inputs. 6.2 Implementation name your program lab1d.c as already did in copy.c, we can use getchar and a loop to read a line of characters, which is terminated by the new line character \n. We will see more straightforward way to read line of input later. 3

6.3 Sample Inputs/Outputs (from Standard input - keyboard): red 308 % gcc lab1d.c o lab1d red 309 % lab1d blank count 0 blank count 2 blank count 5 ^D (press Ctrl and D) Total blank count 7 red 310 % 6.4 Sample Inputs/Outputs (use redirected input/output files): You can always redirect the Standard in (keyboard) from an input file using < You can always redirect the Standard out (screen) to an output file using > Using your favorite text editor, create (in current directory) a text file input.txt that contains red 309 % a.out < input.txt blank count 0 blank count 2 blank count 5 Total blank count 7 red 310 % red 311 % a.out < input.txt > output.txt If your program runs correctly, a new file output.txt should be generated (in the current directory). Use command ls or ls l to confirm this. Then use command cat or more to view the content of output.txt (If you don t know what is happening here, please review the CSE1020 lab tour posted on the course website). red 312 % ls -l red 313 % cat output.txt 4

Submit your program and the output file using submit 2031 lab1 lab1d.c output.txt Common Notes All submitted files should contain the following header: /*************************************** * EECS2031 Lab1 * * Filename: Name of file * * Author: Last name, first name * * Email: Your email address * * eecs_num: Your eecs number * ****************************************/ In addition, all programs should follow the following guidelines: Include the stdio.h library in the header of your.c files. Use /* */ to comment your program. You are not encouraged to use //. Assume that all inputs are valid (no error checking is required, unless asked to do so). You are also encouraged to Give a return type int for main(), and return 0 at the end of main() Specify parameters for main() function, as main(int argc, char *argv[]) 5