CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Size: px
Start display at page:

Download "CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps"

Transcription

1 CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps Overview By the end of the lab, you will be able to: use fscanf() to accept inputs from the user and use fprint() for print statements to the user format some of the output using flag and width specifications in the fprintf() statement execute a basic block iteratively using loops to allow the user to continue entering values repeatedly until a given sentinel value is entered use a function (the pow()function) from the math.h library Background Information The following will review the three different types of loops that you learned about in lecture. Also, there is a section about formatting output. 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

2 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 (a priming read). Inside the loop, we do the math and prompt the user for the next number. 2

3 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

4 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? Formatting output You already know that to limit the precision of a floating-point value printed out to the screen to three decimal places, you might do something like the following: fprintf(stdout, %.3f, var1); There are other flags and modifiers that can be included in a print statement to format the output: %[flags][width][.precision][length]type where flags include: - left-justify + generate a plus sign for positive values # put a leading 0 (zero) on an octal value and 0x (zero-x) on a hex value 0 (zero, not the letter O ) pad a number with leading zeros and the width modifier: is the minimum number of characters to generate numbers are padded with leading spaces unless the width has a leading zero flag specifying leading zeros if the number is larger than the number of digits specified, the number will be printed with the wider width For example, using the - as a flag, left justifies the values in a column that is width wide. fprintf(stdout, %-6d%-6d, 12, 43); fprintf(stdout, %-6d%-6d, 324, 72); produces the following output with the values in columns that are 6 characters wide and are left-justified:

5 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 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. 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 lab4.c lm 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: Create a file called lab4.c. In it, provide a C program that will do the following: 1. Prompt the user to enter a temperature in degrees Fahrenheit (an integer). NOTE: Use fprintf() and fscanf () functions throughout your program. 2. Using code from lab 2, print to the user the converted temperature in degrees Celsius (a floating-point value). Use a width modifier with the format specifier in the print statement to make it print in a column of size Test this much out (compile and run) and make sure it works before continuing with the next step. 4. Then prompt the user to enter a value for wind in mph (an integer). 5. Using the following formula, calculate the wind chill (a floating-point value) and print that result to the user: wind chill = T 35.75(V^0.16) T(V^0.16) where T is the temperature and V is the wind For the exponent values in the formula, use the pow() function from the math.h library. You ll have to #include the math.h library at the top of your file near where your other #include statement is. You can look up this function in the book, pages ; use the man pages, as explained above; or you can also use this web page as a reference for the pow() function: NOTE: Don t forget, when compiling a program that uses functions from the math library, you will have to also include the lm flag to link the math library. For example: gcc Wall lab4.c lm 5

6 6. Test this out (compile and run) and make sure it works before continuing with the next step. You can use the chart on this web page as a reference for the values: 7. Once your program works, go back and add the loop to your code so that the user can continue entering values (temperature and wind). Your program should quit when the user enters a -100 for the temperature (a sentinel value). 8. Once your program compiles, test it with several values to make sure it works. 9. Sample output is shown below: (between -50 to 50 degrees, enter -100 to quit): degrees F = degrees C Enter the wind in mph (between 3 to 110 mph): 35 The windchill for 20 degrees fahrenheit at 35 mph winds is (between -50 to 50 degrees, enter -100 to quit): degrees F = degrees C Enter the wind in mph (between 3 to 110 mph): 25 The windchill for 10 degrees fahrenheit at 25 mph winds is (between -50 to 50 degrees, enter -100 to quit): -5-5 degrees F = degrees C Enter the wind in mph (between 3 to 110 mph): 10 The windchill for -5 degrees fahrenheit at 10 mph winds is (between -50 to 50 degrees, enter -100 to quit): -100 [22:29:21] chochri@koala4: 6

7 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 lab4.c program using the handin page: 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) 50 Use of loop 20 Style, Formatting, & Commenting 10 Use of pow() function from the math.h library 10 Use of fprintf() and fscanf() 5 Use of width modifier to indent F to C temp by 6 spaces 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

CpSc 1111 Lab 5 Formatting and Flow Control

CpSc 1111 Lab 5 Formatting and Flow Control 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

More information

CpSc 1111 Lab 4 Formatting and Flow Control

CpSc 1111 Lab 4 Formatting and Flow Control CpSc 1111 Lab 4 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 and print out the ASCII decimal, octal, and hexadecimal

More information

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

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.

More information

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

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point? Overview For this lab, you will use: one or more of the conditional statements explained below scanf()

More information

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

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes 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

More information

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

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection Overview By the end of the lab, you will be able to: declare variables perform basic arithmetic operations on integer variables

More information

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

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input 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

More information

CpSc 1111 Lab 9 2-D Arrays

CpSc 1111 Lab 9 2-D Arrays CpSc 1111 Lab 9 2-D Arrays Overview This week, you will gain some experience with 2-dimensional arrays, using loops to do the following: initialize a 2-D array with data from an input file print out the

More information

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

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection Overview By the end of the lab, you will be able to: declare variables perform basic arithmetic operations on integer variables

More information

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

CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C CpSc 1111 Lab 1 Introduction to Unix Systems, Editors, and C Welcome! Welcome to your CpSc 111 lab! For each lab this semester, you will be provided a document like this to guide you. This material, as

More information

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering 2013/2014 Programming Fundamentals for Engineers Lab Lab Session # 1 Introduction to C Language ALQUDS University Department of Computer Engineering Objective: Our objective for today s lab session is

More information

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski) Class 2: Variables and Memory Variables A variable is a value that is stored in memory It can be numeric or a character C++ needs to be told what type it is before it can store it in memory It also needs

More information

Fundamentals of Programming Session 8

Fundamentals of Programming Session 8 Fundamentals of Programming Session 8 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

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

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) CP Lect 5 slide 1 Monday 2 October 2017 Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) Cristina Alexandru Monday 2 October 2017 Last Lecture Arithmetic Quadratic equation

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

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

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

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

بسم اهلل الرمحن الرحيم بسم اهلل الرمحن الرحيم Fundamentals of Programming C Session # 10 By: Saeed Haratian Fall 2015 Outlines Examples Using the for Statement switch Multiple-Selection Statement do while Repetition Statement

More information

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

1. The programming language C is more than 30 years old. True or False? (Circle your choice.) Name: Section: Grade: Answer these questions while viewing the assigned videos. Not sure of an answer? Ask your instructor to explain at the beginning of the next class session. You can then fill in your

More information

C introduction: part 1

C introduction: part 1 What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation

More information

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow Variables Integer Representation Variables are used to store a value. The value a variable holds may change over its lifetime. At any point in time a variable stores one value (except quantum computers!)

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

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

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable CS 201 Selection Structures (2) and Repetition Debzani Deb Multiple-Alternative Decision Form of Nested if Nested if statements can become quite complex. If there are more than three alternatives and indentation

More information

CpSc 101, Fall 2015 Lab7: Image File Creation

CpSc 101, Fall 2015 Lab7: Image File Creation CpSc 101, Fall 2015 Lab7: Image File Creation Goals Construct a C language program that will produce images of the flags of Poland, Netherland, and Italy. Image files Images (e.g. digital photos) consist

More information

Programming Style Guide v1.1

Programming Style Guide v1.1 Oregon State University Intro to programming Source Code Style Guide Modified from: OREGON INSTITUTE OF TECHNOLOGY COMPUTER SYSTEMS ENGINEERING TECHNOLOGY Modified by: Joseph Jess Programming Style Guide

More information

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

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

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

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program CMPT 102 Introduction to Scientific Computer Programming Input and Output Janice Regan, CMPT 102, Sept. 2006 0 Your first program /* My first C program */ /* make the computer print the string Hello world

More information

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

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code CS102: Standard I/O Our next topic is standard input and standard output in C. The adjective "standard" when applied to "input" or "output" could be interpreted to mean "default". Typically, standard output

More information

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Autumn 2015 Lecture 3, Simple C programming M. Eriksson (with contributions from A. Maki and

More information

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple

More information

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

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points Files to submit: 1. HW3.py This is a PAIR PROGRAMMING Assignment: Work with your partner! For pair

More information

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

Introduction to Programming in Turing. Input, Output, and Variables Introduction to Programming in Turing Input, Output, and Variables The IPO Model The most basic model for a computer system is the Input-Processing-Output (IPO) Model. In order to interact with the computer

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

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

CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM CS 356, Fall 2018 Data Lab (Part 1): Manipulating Bits Due: Wednesday, Sep. 5, 11:59PM 1 Introduction The purpose of this assignment is to become more familiar with bit-level representations of integers

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

Formatting & Style Examples

Formatting & Style Examples Formatting & Style Examples The code in the box on the right side is a program that shows an example of the desired formatting that is expected in this class. The boxes on the left side show variations

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 7: Introduction to C (pronobis@kth.se) Overview Overview Lecture 7: Introduction to C Wrap Up Basic Datatypes and printf Branching and Loops in C Constant values Wrap Up Lecture 7: Introduction

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

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

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Programming for Electrical and Computer Engineers. Loops

Programming for Electrical and Computer Engineers. Loops Programming for Electrical and Computer Engineers Loops Dr. D. J. Jackson Lecture 6-1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly

More information

Java Coding 3. Over & over again!

Java Coding 3. Over & over again! Java Coding 3 Over & over again! Repetition Java repetition statements while (condition) statement; do statement; while (condition); where for ( init; condition; update) statement; statement is any Java

More information

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Informatica e Sistemi in Tempo Reale

Informatica e Sistemi in Tempo Reale Informatica e Sistemi in Tempo Reale Introduction to C programming Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 5, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Introduction to the C language Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa February 29, 2012 G. Lipari (Scuola Superiore Sant Anna) The C language

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

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

Use of scanf. scanf(%d, &number); Use of scanf We have now discussed how to print out formatted information to the screen, but this isn't nearly as useful unless we can read in information from the user. (This is one way we can make a

More information

Condition Controlled Loops. Introduction to Programming - Python

Condition Controlled Loops. Introduction to Programming - Python + Condition Controlled Loops Introduction to Programming - Python + Repetition Structures n Programmers commonly find that they need to write code that performs the same task over and over again + Example:

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Variables and Functions. ROBOTC Software

Variables and Functions. ROBOTC Software Variables and Functions ROBOTC Software Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal numbers, and words Variable names follow the same

More information

WARM UP LESSONS BARE BASICS

WARM UP LESSONS BARE BASICS WARM UP LESSONS BARE BASICS CONTENTS Common primitive data types for variables... 2 About standard input / output... 2 More on standard output in C standard... 3 Practice Exercise... 6 About Math Expressions

More information

Control Statements: Part 1

Control Statements: Part 1 4 Let s all move one place on. Lewis Carroll Control Statements: Part 1 The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert Frost All

More information

numerical calculations, decisions, mathematical functions, usability, I/O, defensive programming, functions.

numerical calculations, decisions, mathematical functions, usability, I/O, defensive programming, functions. pracnique Wind Chill Factor SYNOPSIS This pracnique describes writing a program to calculate the wind chill factor. The program is then refined by adding defensive programming, and modularized to use functions.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014 Lesson 6A Loops By John B. Owen All rights reserved 2011, revised 2014 Topic List Objectives Loop structure 4 parts Three loop styles Example of a while loop Example of a do while loop Comparison while

More information

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

COMP26120 Academic Session: Lab Exercise 2: Input/Output; Strings and Program Parameters; Error Handling 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

More information

Chapter 3 Structured Program Development

Chapter 3 Structured Program Development 1 Chapter 3 Structured Program Development Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 3 - Structured Program Development Outline 3.1 Introduction

More information

(Refer Slide Time: 00:26)

(Refer Slide Time: 00:26) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute Technology, Madras Module 07 Lecture 07 Contents Repetitive statements

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++ Repetition Contents 1 Repetition 1.1 Introduction 1.2 Three Types of Program Control Chapter 5 Introduction 1.3 Two Types of Repetition 1.4 Three Structures for Looping in C++ 1.5 The while Control Structure

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

CS112 Lecture: Repetition Statements

CS112 Lecture: Repetition Statements CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general

More information

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture CPE 101 slides adapted from UW course Lecture (9): Iteration Overview Concepts this lecture Iteration - repetitive execution Loops and nested loops while statements for statements 2000 UW CSE H1-1 H1-2

More information

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

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018 CMPUT 201: Practical Programming Methodology Guohui Lin guohui@ualberta.ca Department of Computing Science University of Alberta September 2018 Lecture 1: Course Outline Agenda: Course calendar description

More information

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

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University Overview of C, Part 2 CSE 130: Introduction to Programming in C Stony Brook University Integer Arithmetic in C Addition, subtraction, and multiplication work as you would expect Division (/) returns the

More information

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions Lecture 02 Summary C/Java Syntax Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 1 2 By the end of this lecture, you will be able to identify the

More information

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

CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM CS 356, Fall 2018 Data Lab (Part 2): Manipulating Bits Due: Mon, Sep. 17, 11:59PM 1 Introduction This second part of the data lab continues on bit-level manipulations and 2 s complement arithmetic as well

More information

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python CONDITION CONTROLLED LOOPS Introduction to Programming - Python Generating Random Numbers Generating a random integer Sometimes you need your program to generate information that isn t available when you

More information

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

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock) C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 By the end of this lecture, you will be able to identify the

More information

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

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for C/Java Syntax 1 Lecture 02 Summary Keywords Variable Declarations Data Types Operators Statements if, switch, while, do-while, for Functions 2 1 By the end of this lecture, you will be able to identify

More information

Program Organization and Comments

Program Organization and Comments C / C++ PROGRAMMING Program Organization and Comments Copyright 2013 Dan McElroy Programming Organization The layout of a program should be fairly straight forward and simple. Although it may just look

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation. Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements

More information

CSE / ENGR 142 Programming I

CSE / ENGR 142 Programming I CSE / ENGR 142 Programming I Variables, Values, and Types Chapter 2 Overview Chapter 2: Read Sections 2.1-2.6, 2.8. Long chapter, short snippets on many topics Later chapters fill in detail Specifically:

More information

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input Loops / Repetition Statements Repetition s allow us to execute a multiple times Often they are referred to as loops C has three kinds of repetition s: the while loop the for loop the do loop The programmer

More information

Software Lesson 1 Outline

Software Lesson 1 Outline Software Lesson 1 Outline 1. Software Lesson 1 Outline 2. What is Software? A Program? Data? 3. What are Instructions? 4. What is a Programming Language? 5. What is Source Code? What is a Source File?

More information

Data Lab: Manipulating Bits

Data Lab: Manipulating Bits Data Lab: Manipulating Bits 1 Introduction The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You ll do this by solving a series

More information

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

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops. Chapter 6 Loops 1 Iteration Statements C s iteration statements are used to set up loops. A loop is a statement whose job is to repeatedly execute some other statement (the loop body). In C, every loop

More information

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

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 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 1 Introduction The purpose of this assignment is to become more

More information

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O) Overview (5) Topics Output: printf Input: scanf Basic format codes More on initializing variables 2000

More information

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

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

ECEn 424 Data Lab: Manipulating Bits

ECEn 424 Data Lab: Manipulating Bits ECEn 424 Data Lab: Manipulating Bits 1 Introduction The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You ll do this by solving

More information

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

CS 2505 Fall 2013 Data Lab: Manipulating Bits Assigned: November 20 Due: Friday December 13, 11:59PM Ends: Friday December 13, 11:59PM CS 2505 Fall 2013 Data Lab: Manipulating Bits Assigned: November 20 Due: Friday December 13, 11:59PM Ends: Friday December 13, 11:59PM 1 Introduction The purpose of this assignment is to become more familiar

More information

Lecture 10. Daily Puzzle

Lecture 10. Daily Puzzle Lecture 10 Daily Puzzle Imagine there is a ditch, 10 feet wide, which is far too wide to jump. Using only eight narrow planks, each no more than 9 feet long, construct a bridge across the ditch. Daily

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COP 2000 Introduction to Computer Programming Mid-Term Exam Review he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

More information

Data Lab: Manipulating Bits

Data Lab: Manipulating Bits Data Lab: Manipulating Bits 1 Introduction The purpose of this assignment is to become more familiar with bit-level representations of integers and floating point numbers. You ll do this by solving a series

More information

c. First convert to base-10 by expanding out the powers of 2:

c. First convert to base-10 by expanding out the powers of 2: EGR4 F 18 Assignment #1 Solution ------------------------------------------------------------------------------------------------------------------- Due Date: Monday (Section 10), September 10, Beginning

More information

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE

LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE Department of Software The University of Babylon LECTURE NOTES ON PROGRAMMING FUNDAMENTAL USING C++ LANGUAGE By Collage of Information Technology, University of Babylon, Iraq Samaher_hussein@yahoo.com

More information

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with

More information

Continued from previous lecture

Continued from previous lecture The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations

More information

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

Stream Model of I/O. Basic I/O in C Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object

More information

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

L o o p s. for(initializing expression; control expression; step expression) { one or more statements } L o o p s Objective #1: Explain the importance of loops in programs. In order to write a non trivial computer program, you almost always need to use one or more loops. Loops allow your program to repeat

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

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

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A. Chapter 5 While For 1 / 54 Computer Science & Engineering 150A Problem Solving Using Computers Lecture 05 - s Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 While For 2 / 54 5.1 Repetition

More information

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

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 5 1 More Loops Andrew Bennett while loops loops inside loops stopping loops 2 Before we begin introduce

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (yaseminb@kth.se) Overview Overview Roots of C Getting started with C Closer look at Hello World Programming Environment Discussion Basic Datatypes and printf Schedule Introduction to C - main part of

More information

Programming in C++ PART 2

Programming in C++ PART 2 Lecture 07-2 Programming in C++ PART 2 By Assistant Professor Dr. Ali Kattan 1 The while Loop and do..while loop In the previous lecture we studied the for Loop in C++. In this lecture we will cover iteration

More information

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

3. A Periodic Alarm: intdate.c & sigsend.c p6: Signal Handling 1. Logistics 1. This project must be done individually. It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such

More information