Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0.

Similar documents
Lecture 2: C Programming Basic

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

Question 2. [5 points] Given the following symbolic constant definition

BSM540 Basics of C Language

CC112 Structured Programming

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

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

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

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

Computer System and programming in C

As stated earlier, the declaration

WARM UP LESSONS BARE BASICS

EE C Program Elements

CMPE-013/L. Introduction to C Programming

CS102: Variables and Expressions

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Function I/O. Last Updated 10/30/18

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

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Structured programming

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

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

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

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Constants Lesson Outline

These are reserved words of the C language. For example int, float, if, else, for, while etc.

AMCAT Automata Coding Sample Questions And Answers

Computer Systems Programming. Practice Midterm. Name:

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

CSE / ENGR 142 Programming I

Arithmetic Expressions in C

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

ET156 Introduction to C Programming

BSM540 Basics of C Language

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

Lecture 3. Variables. Variables

Assignment 3: Binary numbers, data types and C Programs Due 11:59pm Monday 26th March 2018

Fundamentals of Programming

Unit 4. Input/Output Functions

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

DEPARTMENT OF COMPUTER ENGINEERING CMPE101: Foundation of Computer Engineering EXPERIMENT 3. Introduction to C Programming: Sequential code structure

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

Computer Programming CS F111

First of all, it is a variable, just like other variables you studied

COMP Assignment 1

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C Program Structures

Peer Instruction 1. Elementary Programming

SIMPLE I/O WITH PYTHON

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

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

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Beginning C Programming for Engineers

C introduction: part 1

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

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

Display Input and Output (I/O)

NWEN 241 Systems Programming Exercises (Set 1)

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

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

CS149: Elements of Computer Science. Fundamental C++ objects

Variables and Operators 2/20/01 Lecture #

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

Creating, Compiling and Executing

Structured Programming. Dr. Mohamed Khedr Lecture 4

Basic data types. Building blocks of computation

Lesson 3 Introduction to Programming in C

Programming in C++ 6. Floating point data types

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Programming Using C Homework 4

a. a * c - 10 = b. a % b + (a * d) + 7 =

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Formatted Output / User IO

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

Computer Programming : C++

C Language, Token, Keywords, Constant, variable

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Advanced C Programming Topics

The Math Class. Using various math class methods. Formatting the values.

At the end of this module, the student should be able to:

CSE 1320 INTERMEDIATE PROGRAMMING - OVERVIEW AND DATA TYPES

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

C++ Programming Applied to Robotics, Mark Aull Lesson 2: Intro to Visual Studio, debugger Intro to C++, variables, conditionals, loops, strings

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

Fundamentals of Programming Session 8

Input/Output Week 5:Lesson 16.1

Binghamton University. CS-211 Fall Data Conversion. software diversity in action

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

IT 1033: Fundamentals of Programming Data types & variables

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #3: Of Integers and Endians (pt. 2)

Lesson 2A Data. Data Types, Variables, Constants, Naming Rules, Limits. A Lesson in Java Programming

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Transcription:

Floating Point Math Submitted by Andy Lindsay on Thu, 03/21/2013-16:37 original source: http://learn.parallax.com/propeller-c-start-simple/floating-point-math Lesson edited to work with Dev-C++ IDE by Jeff La Favre 10/22/15 (Updated 2013-08-08 for SimpleIDE 0.9.4 and its Learn folder's Simple Libraries and Examples) [SimpleIDE is the IDE for use with the robot. This lesson is edited so that we can use Dev-C++ as the IDE, which does not require a robot J. La Favre] So far, these lessons have processed integers values, which encompass negative and positive counting values. C language also handles floating point values, which allow you to process numbers with a decimal point and one or more digits to the right, much as a calculator does. Since the number of digits to the left or right of the decimal point is flexible, the decimal point's position can float from one position to another as needed. Use a calculator and c = 2 π r to calculate the circumference of a circle with a radius of 1.0. Start Dev-C++. Open the File menu and select New. Then select Source File. Click the mouse in the text window of Dev-C++ and use the keyboard to enter the following text: #include <stdio.h> Open the File menu and select Save, which opens a Save As dialog box. In the dialog box, open the drop-down labeled Save as type and select c source files(*.c). In the file name slot enter this name for the file: floating point calculations At the top of the dialog box there is a Save in slot, which determines where the file will be saved. Make sure you know the location where you are saving your file so that you can find it later. Now click the Save button to save your program file. Copy the text in the box on the next page and paste it into the text window of Dev-C++ under the first line of text you have already entered. Alternatively, you can enter the text using the keyboard (enter exactly as given, without comments). 1

Click the Save button to save the code you just pasted or entered with keyboard. Now examine the program code, and try to predict what will be displayed when the program runs. Run the program by opening the Execute menu and selecting Compile and Run. If there are no errors in the program, a new program window will open. Compare the actual output to your predicted output. #define PI 3.14 //create a constant named PI and give it a value of 3.14, notice you don t use an = operator in this case int main() { //begin of code block for main() float r = 1.0; //create a variable named r of the floating point data type and assign a value of 1.0 to it float c = 2.0 * PI * r; //create a float type variable named c and store the answer of the calculation in it printf("circumference = %f \n", c); // in this case the following will be displayed on screen: circumference = 6.280000 } // end of code block for main() How it Works [This program knows that PI 3.1415926... because it is defined in the simpletools library. Ed. Note: this is true for the IDE for the robot, but not for Dev-C++, therefore we need to define PI as a constant, which is done with the line #define PI 3.14 ] Inside the main function, a floating point variable named r is initialized to 1.0 with float r = 1.0. After that, the circumference is calculated with c = 2.0 * PI * r. Then, printf("circumference = %f \n", c) displays the floating point value stored in c. Notice the new format placeholder: %f for a floating point value. 2

Did You Know? You can declare different variable types that can store different sizes and types of numbers. signed char -127 to 127 char 0 to 255 int -2,147,483,647 to 2,147,483,647 unsigned int long unsigned long float 0 to 4,294,967,295 same as int same as unsigned int approx: 3.4X10-38 to 3.4X10 38 with 6 digits of precision 3

Try This Let s try calculating the area of a circle with a = π r 2, which is PI * r * r. Use Save As in the File menu to save your program with the name More Floating Point (make sure to save it as a c source files(*.c) type.) Modify the main function by adding the two statements under the first printf. Compare the program result against your calculated result. Try changing the radius to 3 and repeat. #define PI 3.14 //create a constant named PI and give it a value of 3.14, notice you don t use an = operator in this case int main() { float r = 1.0; float c = 2.0 * PI * r; printf("circumference = %f \n", c); float a = PI * r * r; //create a float type variable named a and store the answer of the calculation in it printf("area = %f \n", a); } 4

Your Turn Expand More Floating Point.c to calculate the volume of a sphere using v = 4/3 π r 3. Keep in mind that you have to use 4.0/3.0 to get the floating point version of 4/3. You can also use pow(r, 3.0) to raise r to the third power. 5