CpSc 1111 Lab 5 Formatting and Flow Control

Similar documents
CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

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

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

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

CpSc 1111 Lab 9 2-D Arrays

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

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

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

CpSc 101, Fall 2015 Lab7: Image File Creation

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

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

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

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

Lecture 3. More About C

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

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

Programming Style Guide v1.1

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Informatica e Sistemi in Tempo Reale

Add Subtract Multiply Divide

Fundamentals of Programming

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

Formatting & Style Examples

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

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

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

Programming for Electrical and Computer Engineers. Loops

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

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

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]

Programming in C++ PART 2

Variables and literals

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

Fundamental of Programming (C)

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Introduction. C provides two styles of flow control:

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

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

Lecture 03 Bits, Bytes and Data Types

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

Approximately a Final Exam CPSC 206

Variables and Functions. ROBOTC Software

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

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

Differentiate Between Keywords and Identifiers

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

Software Lesson 1 Outline

Lab 2: Structured Program Development in C

Module 4: Decision-making and forming loops

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

Lecture 10. Daily Puzzle

Continued from previous lecture

The Design of C: A Rational Reconstruction (cont.)

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

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

Computer Programming : C++

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

Chapter 3: Arrays and More C Functionality

Chapter 3 Structured Program Development

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

Computer Programming: Skills & Concepts (CP) Variables and ints

CS102. Lecture 7. xkcd. xkcd. palindramas

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSE 303 Lecture 8. Intro to C programming

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

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

Lecture 10: Recursive Functions. Computer System and programming in C 1

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

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

Lecture 6. Statements

Binghamton University. CS-220 Spring Includes & Streams

EL2310 Scientific Programming

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

The Compilation Process

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

The Design of C: A Rational Reconstruction: Part 2

Library Functions. General Questions

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

3/13/2012. ESc101: Introduction to Computers and Programming Languages

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

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

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

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

FALL 2017 CSCI 304 LAB1 (Due on Sep-19, 11:59:59pm)

Transcription:

CpSc 1111 Lab 5 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user execute a basic block iteratively using loops to produce an isosceles triangle using the character entered by the user as well as a value for the height to be entered by the user Background Information The following will review the three different types of loops that you learned about in lecture. Loops Remember lab 1 where you were supposed to print Hello World! 20 times? What if you were supposed to print that message 1000 times? Would you really copy and paste it 1000 times in your program? Sometimes it is necessary to repetitively execute a statement, or block of statements, while some condition remains true, or until some condition becomes false. Because of all the possible formulations of conditions, it is the case that there are multiple correct ways to construct such iterations. And, alas, there are even more incorrect ways to construct them. There are three different iteration constructs in C: 1. while loop 2. do-while loop 3. for loop 1. while Loop The while loop allows us to write a segment of code that repeats while some condition remains true. Structure: // loop index variable used in condition declared and // initialized somewhere above the while loop while (condition) { one-or-more statements; loop_expression; // expression that changes the condition, // updates the loop index variable 1

Important Notes: The statement or statements controlled by the while loop are called the body of the loop and are enclosed by curly braces. It is necessary that the body of the loop modify the value of the condition. Why? Proper use of indentation is critical for human readability. But, indentation is completely irrelevant to the C compiler. Print the integer values between 10 and 1 in decreasing order. (Note: This is not a complete C program; it needs to include stdio.h and it needs a proper main() function header. Also, it is not indented properly see how hard it is to read and make sense of the code?) int value; value = 10; fprintf(stdout, " Table Of Values \n"); while (value > 0) { fprintf (stdout, "%3d\n", value); value = value - 1; In the above code, we only want the heading to be printed one time, so the first fprintf() is placed outside the loop. Example of while loop (with proper formatting and good use of comments): // addition.c // 08/31/15 // Print the sum of positive integers provided by the user. #include <stdio.h> int main(void) { int input; // to hold user input int sum = 0; // sum of all input values // prompt user and get input printf("enter a positive integer, or a negative integer when done: "); scanf("%d", &input); // loop to add each input to sum, and get another integer from user while (input >= 0) { sum = sum + input; printf("enter a positive integer, or a negative integer when done: "); scanf("%d", &input); // display the sum printf("the sum is %d\n", sum); return 0; Notice that we prompt the user for the first input before the loop. Inside the loop, we do the math and prompt the user for the next number. 2

2. do-while Loop The do-while loop allows us to write a segment of code that also repeats while some condition remains true. Structure: // loop index variable used in condition declared and // initialized somewhere above do-while loop do { one-or-more statements; loop_expression; // expression that changes the condition, or updates // the loop index variable while (condition); Important Notes: The above loop is guaranteed to execute the statement(s) at least once. Do you see why? As with the while() loop, it is necessary that the body of the loop modify the value of the condition so that it is not an infinite loop. Don t forget the semi-colon after the while condition. The other loops do not have a semi-colon at the end of the loop. 3. for Loop The third loop structure is the for loop. Structure: // loop index variable declared somewhere above for loop for (init_expression; loop_condition; loop_expression) { one-or-more statements; Important Notes: The statement or statements controlled by the for loop are called the body of the loop and are enclosed in curly braces. The init_expression gives the starting value for the control variable (loop index variable) being used by the loop. The control variable needs to be declared somewhere above the for loop, probably at the top of the program where the other variables are declared. int i; for (i = 0; loop_condition; loop_expression) The loop_condition will be evaluated before each iteration of the loop; if it evaluates to true, an iteration of the loop body will occur; if it evaluates to false, no code inside the body of the loop will execute. int i; for (i = 0; i <= 3; loop_expression) o In this code above, as long as the value of i is less than or equal to 3, the statements inside the body of the loop will be executed. 3

The loop_expression will change the value of the loop control variable so that a stopping point may be reached in the loop_condition. int i; for (i = 0; i <= 3; i++) o In the code above, i will increment by 1 each time immediately after an iteration of the loop, allowing for the loop_condition to eventually evaluate to false (assuming a proper condition exists), thereby ending the loop. (i++ is the same as i = i + 1 and the same as i += 1 and with loops, the same as ++i ) for (value = 10; value > 0; value--) { fprintf(stdout, "%3d\n", value); Note: value-- is the same as value = value 1 Also, the starting value for the control variable called value in this case is 10, and it decrements by 1 after each iteration of the loop. The loop will continue to iterate as long as the value of the control variable is greater than 0. So, how many iterations will occur? Review of scanf() and characters By now, you know to use scanf() or fscanf() to get input from the user. For example, if you were asking the user to enter an integer, and storing their input into a variable called input, your code might look like the following: int input; fprintf(stdout, Enter an integer: ); fscanf(stdin, %d, &input); If the user were to enter a character, your code would change this way: char inputchar; fprintf(stdout, Enter a character: ); fscanf(stdin, %c, &inputchar); Remember that characters are char type, which is really a specialized type of int. If the user entered a lower case t as their character which was stored in the variable called inputchar, to print the value of that variable, you would use %c as the format string, just like above in the fscanf(), such as: fprintf(stdout, %c, inputchar); Which would print the letter t to the screen. If you instead typed the following, what would be printed? fprintf(stdout, %d, inputchar); If you refer to an ASCII table, such as this one: http://www.asciitable.com, you will see that the decimal (integer) value for the lower case t is 116. You will also see that the octal value of that letter is 164, and the hexadecimal value is 74. That same ASCII table is also in section 3.7 of the the Chapter 3 notes posted on Canvas, as well as a direct link on the Chapter 3 page on Canvas. 4

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. One exception to the rule of using meaningful names for variables is to use i or j or n or some other single letter as a loop index variable name; this is a common, acceptable practice. 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 Create a file called lab5.c. In it, provide a C program that will do the following: 1. Prompt the user to enter a character. Note: Use fprintf() and fscanf () functions throughout your program. 2. Then prompt the user to enter an integer for the height of an isosceles triangle. This will be the same value for the base. 3. Use loops to draw an isosceles triangle made of the character that the user entered, and using the height entered by the user. NOTE: When printing the character, print it with a space immediately after the character so the characters are evenly spaced throughout the triangle. For example, with no spaces after the character, your shape will print like this: A AA AAA AAAA AAAAA Whereas, with a space after each character, your shape will print like this (which is how it should be): A A A A A A A A A A A A A A A 4. Print the value for the area of the triangle with that height (base would be the same as height). 5. Sample outputs are shown on next page. 5

SAMPLE OUTPUT #1 Enter a character: A Let's draw an isosceles triangle with your character A. Enter a value for the height of the triangle: 5 (Base will be the same as height.) A A A A A A A A A A A A A A A The area of your triangle is 12.50 SAMPLE OUTPUT #2 Enter a character: % Let's draw an isosceles triangle with your character %. Enter a value for the height of the triangle: 9 (Base will be the same as height.) % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % The area of your triangle is 40.50 6

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 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 (correct output on various test cases) 75 Style, Formatting, & Commenting 10 Use of loops for triangle 10 Use of fprintf() and fscanf() 5 Penalty for not following this lab s instructions (incorrect I/O, etc.) -5 if warnings when compile, and other point deductions decided by grader 7