ZyBooks 2.6-2.8 02/12/16. Exam I 2/19/16 Review next week.

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Computer Programming w/ Eng. Applications
Return values.
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Basic Elements of C++ Chapter 2.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
CS0004: Introduction to Programming Variables – Strings.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Introduction to Programming
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Math Library Functions
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture2.
Arithmetic Expressions
Arithmetic Expressions
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 3 Assignment and Interactive Input.
Exam 1 Review.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions, Part 2 of 2 Topics Functions That Return a Value
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Chapter 2 Overview of C.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Iterative Constructs Review
Lecture2.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Data Type.
Chapter 2.
Compiled and ready to run Memory Stack /*
Functions Declarations CSCI 230
Data Type.
A First Book of ANSI C Fourth Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
Data Type.
EECE.2160 ECE Application Programming
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

ZyBooks /12/16

Exam I 2/19/16 Review next week.

Objectives Use constant variables in programs. Use functions from the math.h library to do special calculations. Use casting to change a value's type.

Constant Variables 2.6 An initialized variable whose value cannot change is called a constant variable. const

#include /* Estimates distance of lightning based on seconds between lightning and thunder */ int main(void) { const double SPEED_OF_SOUND = ; // Miles/hour (sea level) const double SECONDS_PER_HOUR = ; // Secs/hour double secondsBetween = 0.0; double timeInHours = 0.0; double distInMiles = 0.0; printf("Enter seconds between lightning and thunder:\n"); scanf("%lf", &secondsBetween); timeInHours = secondsBetween / SECONDS_PER_HOUR; distInMiles = SPEED_OF_SOUND * timeInHours; printf("Lightning strike was approximately\n"); printf("%lf", distInMiles); printf(" miles away.\n"); return 0; }

Constant Variables Permanent Values in Program. Make sure they don't change. Document by giving them names. Easier to modify if value changes. Write a program to convert ounces to grams. Use a constant variable for the conversion factor of 28.35

Math Functions 2.7

Side of Bandana Problem I am manufacturing bandanas. Given the amount of cloth I can afford for each bandana, I want to know the measure of the side of that square. Write a program to find out the measure of the side of the square bandana.

Algorithm 1. Input a Output s

Program bandana.c

Built-in Functions Call Syntax: fname(arg1, arg2,… argn)‏ Example: s = sqrt(area); Include #include Must use -lm option when compiling gcc -lm prog.c

Some Functions in cmath

Absolute Value of Integers

More Functions in cmath Examples log(88.5) --> exp(4.483) --> 88.5

Find Age Difference Write a program to find the difference in age between two quarterbacks on a team.

Algorithm 1.Input qb1 and qb2 2.difference = |qb1 – qb2| 3.Output difference  Write the program today in lab.  Started in qb.c

Participation 1 Write a program to find the 4 th root of a number input by the user. fourthRoot.c

Type Conversions 2.8

Implicit Type Conversion Type converted across assignment operator double temperature; temperature = 70; //int to double int years; years = 2.6; //double to int

Explicit Type Conversion Directly change type in C code. Use the type name as an operator to change the type. int carbon = 6; double mass = (double) carbon; mass = mass * 2.0; double length = 7.5; int measure = (int)length;

Explicit Type Conversion Another example int families = 8; int children = 18; double ave_size; ave_size = children/families; ave_size = (double)18/(double)8; ave_size = double(children)/ double(families);

Questions 1. Why is it better to use constants instead of constant literals such as ? 2. What option must be used when compiling a program with math functions? 3. How could I write e 3 in C? 4. I have an double variable, x. How can I convert it to an int?

Participation 2 Write a program to input the number of cars and the number of households in a neighborhood. Input them as ints. Output the average number of cars per household as a double.