210 likes | 233 Views
Introduction to Computers and Programming. Class 5 Introduction to C Professor Avi Rosenfeld. Float Example (Program 2.2). %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum: 276.50. /* Float Example Program */ #include <stdio.h>
E N D
Introduction to Computers and Programming Class 5 Introduction to C Professor Avi Rosenfeld Introduction to Computers and Programming - Class 5
Float Example (Program 2.2) %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum: 276.50 /* Float Example Program */ #include <stdio.h> main () { float var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; printf ("Sum: %.2f", sum); }
Finding an Average • Problem #2: • 90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. • Integer division can result in data truncation (or loss of data) • Hence, avg is set to: 94, but the students real average is 94.25 • There are actually three ways to solve this problem.
Rules of Promotion • Promotion: when mixing ints and floats, everything is promoted to floats. • In our average example, there are three ways to force promotion, and get the right answer of 94.25: 1. change num to a float: float num = 4.0; float avg = (90 + 92 + 95 + 100) / num;
Casting • Forces one type into another • For example, fYourFloat = (float) iMyInteger ; • Will force the integer myInteger into a float, yourFloat • iMyInteger = (int) fYourFloat ; • Will work too, but you’ll lose the decimal portion • i.e. 1.9 forced into an integer will result in 1 Introduction to Computers and Programming - Class 5
Rules of Promotion (cont’d) 2. Use a Cast Operator int num = 4; float avg = ( 90 + 92 + 95 + 100 ) / (float) num; In this case, num is explicitly cast to a float. And, because we have one float, everything else is promoted. 3. Divide by 4.0 float avg = (90 + 92 + 95 + 100) / 4.0;
printf conversion specifications • The format control string (the first argument to printf) contains: • conversion specifiers (%d, %i, %f) • field widths • precisions • other info we will not look at
Averaging Program /* Implicit Type Casting Example*/ #include <stdio.h> int main () { int var1= 87, var2= 92, var3= 96; double ave; ave = (var1 + var2 + var3)/3.0; printf ("The average of %d, %d and %d is: %.2f\n", var1, var2, var3, ave); // could also have said float(var1+var2+var3)/3; // could also have said (var1+var2+var3)/3.0; return 0; } Introduction to Computers and Programming - Class 5
Using Type Casting • Rounding Numbers • Finding out ASCII values Introduction to Computers and Programming - Class 5
Field width • The exact size of the field in which data is to be printed is specified by a field width. • If the field width is larger than the data being printed, the data will normally be right- justified within that field. • Use a “-” sign before the number in field width to left-justify • If the field width is smaller than the data being printed, the field width is increased to print the data.
ASCII Values #include <stdio.h> int main () { char x = 'A'; printf("The ASCII value of %c is %d\n", x, x); return 0; } Introduction to Computers and Programming - Class 5
Rounding Numbers #include <stdio.h> int main () { float num; int round; printf("Please type a value to round\n"); scanf("%f", &num); round = int (num + 0.5); printf("%f rounded is %d\n", num, round ); return 0; } Introduction to Computers and Programming - Class 5
Field width • An integer representing the field width is inserted between the percent sign (%) and the conversion specifier • For example, %4d int iYourAnswer = 1 ; printf( “The answer %4d is correct!\n”, iYourAnswer ); Will print the following The answer 1 is correct! Introduction to Computers and Programming - Class 5
Precision • You can also specify the precision with which data is to be printed. • Precision has different meaning -- for different types. • integer: minimum number of digits to be printed • float: number of digits to appear after the decimal point
Logic • Is an expression that evaluates to true or false (Boolean Logic) • We’ll discuss it first in the if structure Introduction to Computers and Programming - Class 5
If Structure • A control structure using equality and relational operators to test conditions • Looks something like this: if (something is true) do this Introduction to Computers and Programming - Class 5
Equality and Relational Operators • Equality Operators == x == y x is equal to y != x != y x is not equal to y • Relational Operators > x > y x is greater than y < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Introduction to Computers and Programming - Class 5
Example Using If #include <stdio.h> void main() { int num1,num2; printf("Please enter 2 numbers\n"); scanf("%d%d", &num1, &num2); if(num1 == num2) /* what would if (num1 = num2 do? */ { printf("You typed %d and %d\n", num1, num2); printf("and yes, they are equal\n"); } } Introduction to Computers and Programming - Class 5
VERY COMMON ERROR! • Confusing the equality operator == with the assignment operator = • In our example this error would actually assign the value of y to x if ( x = y ) printf( “ x is equal to y!\n” ); • The condition would always be true!!! Introduction to Computers and Programming - Class 5
Equality v. Assignment • Given: if (grade = 100) printf (“Perfect Score!”); • This statement does the following: • Grade is assigned the value 100. • Because 100 is true (i.e. non-zero!), the condition is always true. • No matter what the student grade, it always says “Perfect Score!” Introduction to Computers and Programming - Class 5
Also note the ;’s! • Placing a semicolon ; immediately to the right of a condition in an if structure. • The if test would never be used – the printf would always print if ( x == y ) ; printf( “ x is equal to y!\n” ); • Again not what we want to do Introduction to Computers and Programming - Class 5