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

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

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

CpSc 1111 Lab 9 2-D Arrays

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

CpSc 1111 Lab 5 Formatting and Flow Control

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

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

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C

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

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

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

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

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Topic 6: A Quick Intro To C

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

Pointer Casts and Data Accesses

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

CS 211 Programming Practicum Fall 2018

CpSc 101, Fall 2015 Lab7: Image File Creation

CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

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

C: How to Program. Week /Mar/05

Should you know scanf and printf?

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

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

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

CptS 360 (System Programming) Unit 1: Introduction to System Programming

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM

Lab 1 Introduction to UNIX and C

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

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

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 142 Style Guide Grading and Details

Fundamentals of Programming Session 8

CS16 Exam #1 7/17/ Minutes 100 Points total

Reserved Words and Identifiers

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

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

Fundamentals of Programming Session 4

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

EL2310 Scientific Programming

Chapter 2 - Introduction to C Programming

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

Introduction to Programming in Turing. Input, Output, and Variables

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

2 Input and Output The input of your program is any file with text. The output of your program will be a description of the strings that the program r

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

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

BRANCHING if-else statements

CMSC 201 Fall 2018 Lab 04 While Loops

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

Lab 1 Introduction to UNIX and C

Repetition Structures

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Software Practice 1 Basic Grammar

CS Programming In C

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

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

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

LECTURE 5 Control Structures Part 2

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Conditional Statement

EE 109 Lab 8a Conversion Experience

CSE 374 Programming Concepts & Tools

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CSE 5A Introduction to Programming I (C) Homework 4

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

today cs3157-fall2002-sklar-lect05 1

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

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

Due Date: Two Program Demonstrations (Testing and Debugging): End of Lab

Lecture 03 Bits, Bytes and Data Types

Program Organization and Comments

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

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

Chapter 1 & 2 Introduction to C Language

Binghamton University. CS-220 Spring Includes & Streams

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

CS15100 Lab 7: File compression

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

Tips from the experts: How to waste a lot of time on this assignment

Transcription:

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes Overview For this lab, you will use: one or more of the conditional statements explained below scanf() or fscanf() to read in integer values entered by the user (from standard input) a loop that will allow the user to go again ASCII table redirection to get input from a file, as well as send output to a file Background Information All programming languages provide constructs to execute a section of code conditionally. In this week s lab, you will implement decision making with the use of one or more of the following conditional statements. if Statements The simplest conditional statement is the if statement. It is used when we want the computer to maybe execute some code based on the truth value of some condition. If the condition is true, the code in the body of the if statement will execute; if it is false, the body of the if statement will be skipped and execution will continue with the code immediately after the if statement. Structure: if (condition) Example of an if statement: if (n < 2) printf( Hello\n ); The above code prints a message only if the value of n is less than two. Otherwise, it does nothing. What does the following code print if the value of n is 1? if (n < 2) printf( Hello ); printf( Tigers!!\n ); What does it print if the value of n is 2? 1

if-else Statements The previous if statement included code that executes when a condition is true. If we want certain code to execute when a condition is true and other code that executes when a condition is false, we use if-else statements. Exactly one of the two possible branches will be taken with an if-else statement. Structure: if (condition) else Example: if ((age >= 13) && (age <= 19)) printf( You are a teenager.\n ); else printf( You are not a teenager.\n ); if-else-if Statements When there are more than two options, instead of nesting if-else statements, C provides us with the if-else-if construct. Structure: if (condition) else if (condition) else if (condition) else Note: The last else is optional and works well whenever there is a fall through case when all other conditions are false. If there is no fall through option needed, then it may make sense to not have that last else at all. 2

Example: if ( (day > 2) && (day <= 6) ) printf( weekday\n ); else if (day == 7) printf( Saturday\n ); else printf( Sunday\n ); Dangling else Problem The compiler associates an else- part with the closest if. The following code illustrates this point. What does it print? (Remember that the compiler ignores formatting.) int n = 5; printf( hello\n ); if (n < 4) if (n > 0) printf( good\n ); else printf( bye\n ); Redirecting Input In lab 3, you learned that you can redirect output to a file by typing something like this:./a.out > output.txt You can also redirect input from a file as well. If your input file is called input.txt, then you would type the following:./a.out < input.txt You can even combine these and do both on the same line:./a.out < input.txt > output.txt ASCII Code The data type for a single character is char. Each character is represented in 1 byte (8 bits) by a specific binary value. On most computers today, that value is defined by the American Standard Code for Information Interchange, or ASCII. The following link will bring up an ASCII table: http://www.asciitable.com/. If you look at the chart starting with the lowest characters, you will find numbers, then upper case letters, and then lower case letters. Special characters are sprinkled around these ranges. The type char in C is really a type of integer. For example, if you had the following line of code in C: printf( %c, 115); the lower case letter s would be printed to the screen because 115 is the decimal value representing that character. 115 corresponds to the binary number 1110011 which, padded out to 8 bits, would be 01110011. In other words, when we press the s key on the keyboard, the processor gets the binary number 01110011 which to us, in decimal, is 115. What does the following print statement print? printf( %d, k ); 3

Lab Assignment Reminder About Style, Formatting, and Commenting Requirements The top of your file should have a header comment, which should contain: o Your name o Date o Lab section o Lab number o Brief description about what the program does o Any other helpful information that you think would be good to have. Variables should be declared at the top of the main function, and should have meaningful names. Always indent your code in a readable way. Some formatting examples may be found here: https://people.cs.clemson.edu/~chochri/assignments/formatting_examples.pdf Don t forget to use the Wall flag when compiling, for example: gcc Wall lab5.c Part I: Write a program called lab5.c which will prompt the user to enter a single letter or number (i.e., a character) on the keyboard; use fscanf() to get the input. NOTE: use %c instead of %c for the format string in your fscanf where there is a space in front of the % inside the quotes. Then print to the user the decimal (integer) representation of that character. Use the ASCII table to confirm that it is correct. Compile and run to verify this much works before continuing. Then calculate 2 times that integer value, showing that result. Compile and run after this step. Use that product as a hypothetical wind speed value and show what category hurricane it would be. Using a conditional statement, incorporate the values from the following table to show the result to the user: Compile again and run to make sure it works before continuing. Add a loop asking the user if they would like to go again, using a 1 for yes and a 0 for no. See sample output on the next page for an example of what your program should look like when run. 4

Enter a character: # The decimal representation of your character # is 35. Would (2 * # ) be a hurricane?? (2 * # ) = 70 Wind speed of 70 is not strong enough to be a hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 1 Enter a character: T The decimal representation of your character T is 84. Would (2 * T ) be a hurricane?? (2 * T ) = 168 Wind speed of 168 is a Category 5 hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 1 Enter a character: 3 The decimal representation of your character 3 is 51. Would (2 * 3 ) be a hurricane?? (2 * 3 ) = 102 Wind speed of 102 is a Category 2 hurricane. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Enter 1 to go again or 0 to quit: 0 Part II: Create a file called input.txt that contains the input values for testing your program. Make sure to test all the possible conditions of your if-statement. Then, run your program using your input file by typing:./a.out < input.txt and verify the output. You do not have to change anything in your code - your fscanf() statement will still be using stdin as the first argument. When you redirect the input from a file, it acts as if the values are still being entered on the keyboard by hand. Once you verify your program works, type the following to redirect the output as well:./a.out < input.txt > output.txt Then, as you did in lab 3, use the cat command to view the contents of your output file, clearing the screen first: clear cat output.txt 5

Turn In Work 1. Before turning in your assignment, make sure you have followed all of the instructions stated in this assignment and any additional instructions given by your lab instructor(s). Always test, test, and retest that your program compiles and runs successfully on our Unix machines before submitting it. 2. Show your TA that you completed the assignment. Then submit your lab5.c program, and your input.txt & output.txt files using the handin page: http://handin.cs.clemson.edu. Don t forget to always check on the handin page that your submission worked. You can go to your bucket to see what is there. Grading Rubric If your program does not compile on our Unix machines or your assignment was not submitted on time, then you will receive a grade of zero for this assignment. Otherwise, points for this lab assignment will be earned based on the following criteria: Functionality 60 Formatting 10 Use of conditional statement 10 Use of loop to go again 10 Inclusion of input and output files 10 Penalty for not following this lab s instructions (incorrect I/O, etc.) -5 if warnings when compile, and other point deductions decided by grader 6