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.