Download presentation
Presentation is loading. Please wait.
Published byGerald Rodgers Modified over 8 years ago
1
ZyBooks 2.6-2.8 02/12/16
2
Exam I 2/19/16 Review next week.
3
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.
4
Constant Variables 2.6 An initialized variable whose value cannot change is called a constant variable. const
5
#include /* Estimates distance of lightning based on seconds between lightning and thunder */ int main(void) { const double SPEED_OF_SOUND = 761.207; // Miles/hour (sea level) const double SECONDS_PER_HOUR = 3600.0; // 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; }
6
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
7
Math Functions 2.7
8
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.
9
Algorithm 1. Input a 2. 3. Output s
10
Program bandana.c
11
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
12
Some Functions in cmath
14
Absolute Value of Integers
15
More Functions in cmath Examples log(88.5) --> 4.483 exp(4.483) --> 88.5
16
Find Age Difference Write a program to find the difference in age between two quarterbacks on a team.
17
Algorithm 1.Input qb1 and qb2 2.difference = |qb1 – qb2| 3.Output difference Write the program today in lab. Started in qb.c
18
Participation 1 Write a program to find the 4 th root of a number input by the user. fourthRoot.c
19
Type Conversions 2.8
20
Implicit Type Conversion Type converted across assignment operator double temperature; temperature = 70; //int to double int years; years = 2.6; //double to int
21
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;
22
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);
23
Questions 1. Why is it better to use constants instead of constant literals such as 3.14159? 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?
24
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.