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

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

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 1011 Lab 4 Formatting and Flow Control Windchill Temps

CpSc 1111 Lab 9 2-D Arrays

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

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

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 5 Formatting and Flow Control

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

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

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

Lab 1 Introduction to UNIX and C

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

2. Numbers In, Numbers Out

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

Program Organization and Comments

3. A Periodic Alarm: intdate.c & sigsend.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

CS 211 Programming Practicum Fall 2018

2. Numbers In, Numbers Out

Lab 1 Introduction to UNIX and C

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

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

CMSC 201 Fall 2018 Lab 04 While Loops

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

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

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

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

CS3157: Advanced Programming. Outline

Repetition Structures

C Functions. 5.2 Program Modules in C

The C standard library

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

Pointer Casts and Data Accesses

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

Use of scanf. scanf("%d", &number);

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Compiling and Running a C Program in Unix

ECE15: Lab #2. Problem 1. University of California San Diego

CpSc 101, Fall 2015 Lab7: Image File Creation

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

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

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

Lab 2: Structured Program Development in C

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Computer Science & Engineering 150A Problem Solving Using Computers

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

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

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

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

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

C: How to Program. Week /Mar/05

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Lab 4: Shell Scripting

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Chapter 4: Making Decisions

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

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

Chapter 4: Making Decisions

Other Loop Options EXAMPLE

LECTURE 5 Control Structures Part 2

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

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

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

Exercise 1 Using Boolean variables, incorporating JavaScript code into your HTML webpage and using the document object

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Remaining Enhanced Labs

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

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

WARM UP LESSONS BARE BASICS

Fundamentals of Programming Session 8

Lab 4: Bash Scripting

Getting started with C++ (Part 2)

DECISION CONTROL AND LOOPING STATEMENTS

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

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

A) Central Processing Unit (CPU = Arithmetic-Logic Unit (ALU) + Control Unit (CU))

Programming Assignment #4 Arrays and Pointers

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

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

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

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

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

COMP s1 Lecture 1

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

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

Introduction: The Unix shell and C programming

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 1 Getting Started Structured Programming 1

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

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

C introduction: part 1

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

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Transcription:

CpSc Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input 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) the pow() function from the math library a loop that will allow the user to go again 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? if (n < 2) printf( Hello ); printf( Tigers!!\n ); What does it print if the value of n is 2?

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 >= 3) && (age <= 9)) 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 ); The math.h Library Another library provided by C is the math.h library. This library provides math functions that you can use, such as pow(), sqrt(), abs(), cos(), sin(), tan(), log(), among many others. Pages 485-492 of the Programming in C book list many of the functions in this library. You can also learn about the math library functions by typing man at the Unix command prompt, a space, the function name, and then pressing Enter. For example, to learn about sqrt you would enter: man sqrt To get some experience using the math library, for this lab assignment, you will be using the pow() function to cube a number. One last thing for you to know - you will need to compile your program with an option (l for library, m for math), for example: gcc -Wall lab6.c lm Redirecting Input In lab 3, you learned that you can redirect output to a file by typing something like this: > output.txt You can also redirect input from a file as well. If your input file is called following: input.txt, then you would type the < input.txt You can even combine these and do both on the same line: < input.txt > output.txt 3

Lab Assignment Part I: Write a program called lab5.c which will prompt the user to enter a single letter or number on the keyboard. If the user enters a lower case letter, the program will print out the same letter as a capital letter. If the user enters a capital letter, the program will print out the same letter in lower case. If the user enters a number, the program will print out a message saying if it is an even number or an odd number. Also, the program will print to the user what that number cubed is. If the user enters anything that is not a letter or a number, the program will print out a message indicating that they entered an invalid character. Your output should look similar to the following, which shows 5 separate runs: Enter a letter or number from the keyboard. d The upper case of that letter is: D. Enter a letter or number from the keyboard. R The lower case of that letter is: r. Enter a letter or number from the keyboard. 6 You entered an even number. 6 cubed is 26. Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Enter a letter or number from the keyboard. $ Invalid input. Try again. Part II: Once you get that working, make the program loop continually, asking the user if they want to go again, using a for yes and a 0 for no. If they enter a, then they will be prompted to enter a single letter or number on the keyboard again, displaying the results explained above. If they enter a 0, then the program will quit. With this loop added, your output should look similar to the following: Enter a letter or number from the keyboard. d The upper case of that letter is: D. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. R The lower case of that letter is: r. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. 6 You entered an even number. 6 cubed is 26. Would you like to go again? Enter for yes, 0 for no. Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Would you like to go again? Enter for yes, 0 for no. 0 4

Once you are certain that your program works with the addition of the loop, add some code that will check for invalid input for going again. For example: Enter a letter or number from the keyboard. 3 You entered an odd number. 3 cubed is 27. Would you like to go again? Enter for yes, 0 for no. 5 Invalid input. Enter a to go again, or a 0 to quit: 2 Invalid input. Enter a to go again, or a 0 to quit: 0 Only add a check to see if they entered a number other than or 0 to go again you don t need to check for other characters. Part III: Create a file called input.txt and in that file, put the inputs to the program that would otherwise have been entered from the keyboard. Using the first example output from Part II above, your input file would contain the following: d R 6 3 0 Run your program again, redirecting the input, like the following (you should see the same results as above): < input.txt Then try the following: < input.txt > output.txt Type ls to verify that the file output.txt was created. Then type the following to view the contents of that file: cat output.txt Hint #: You will need to refer to an ASCII table for this program. Remember that characters are really a type of integer, and you can refer to a character by its corresponding integer value. Hint #2: With your scanf statements asking the user to enter a character or number, for the format strings, inside the quotes put a space in front of the %. For example, use %c instead of %c. 5

Reminder About Formatting and Comments The top of your file should have a header comment, which should contain: o Your name o Course and semester 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 Turn In Work Show your ta that you completed the assignment. Then submit your lab5.c file to the handin page. Grading Rubric For this lab, points will be based on the following: Functionality 80 (correct use of conditionals: 60; loop to go again: 20) Formatting 0 Use of pow() function 5 Catches invalid input 5 (any number not a or 0; don t worry about catching other invalid input, such as a character) 6