{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

201Spring08-L6 - CMPSC 201C Spring 2008 Lecture 6 Data Type...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 6 January 28, 2008 Data Type Conversions The data type of a value may be converted to another data type either automatically by the computer or explicitly by the programmer. As stated earlier, when an integer and a floating point number are part of an operation, e.g. 4 * 5.0, then the integer is automatically converted to a floating point number when the operator is performed. This is called data type coercion. If the value is stored in a variable, the variable still contains the original data type. Type Casting The programmer may explicitly specify the conversion of the data type for the purposes of an operation. Three methods: static_cast<data type>(variable or expression) data type (variable or expression) (data type) variable or expression The latter two are considered to be C-style Type casting a variable will not change what is stored in a variable Examples Given the declarations double alpha = 4.0, beta = 6.0; int x = 5, y = 10; what would happen in the following expressions? alpha/y x /beta x/y static_cast<double>(x)/y x/double(y) double(x/y) Reminders At this point there are only two methods to change what is stored in a variable: the assignment operator, alpha = x*b; the input statement, cin>>x; In converting a double value to an integer value, the decimal portion is truncated (cut off). If you want the double value to be rounded, add 0.5 prior to the conversion. Questions ??? Predefined Functions Functions are segments of code that can be used many times throughout a program. They may be written by the programmer, however there are also some predefined functions. For example, C++ does not have a exponent operator, instead there is a pow function. xy pow(x,y) requires the inclusion of another library file #include<cmath> See table 2.5 on page 59 for more functions Using Functions If you want to use a function, you need to determine what library file contains it and include that library file. You also need to know what value(s) the function requires to perform its task and the data type of these value(s). These values are called argument(s) or actual parameter(s). You should also be aware of the data type of the result. abs(x) fabs(y) Caution Trigonometric functions such as sin, cos, and tan expect the angle to be expressed in radians, not degrees. Review: What type of error would occur if the programmer forgot to convert the angle from degrees to radians before sending it to the function? Programming Errors Syntax errors code does not follow the grammar rules of language. Found by the compiler. Logic errors results are not what was expected due to incorrect implementation. Use test data for which you know what the results should be. Run-time errors program causes the computer to perform an illegal operation and terminate abnormally. Divide by 0 or square root of negative number. Questions ??? Programming Style How does the program look? Unimportant to computer, but helps others to understand your program. Consistency Comments Summary comments at the beginning of your program. Descriptive comments explaining what sections of your code does. Programming Style Names Descriptive Distinguish between variable, constant, function. Examples: radius, PI, calculateArea Indentation (must for this course!) Used to indicate relationship. All statements belonging to a specific block of code (bodies of ifs, loops, functions, ect.) should be indented. Flow of Control There are 4 types of control structures that affect the "flow of control" of the program, i.e. in what order the statements are executed. Sequence - statements are executed in order they are written Branched/Selection (decision) - some statements may be executed only when a specific condition is met. Loops (repetition) - statements are repeated Functions- a group of statements are executed at various times in the program Review Boolean Data Type Variables of bool data type store the values of true and false. bool isnegative = true; True and false is represented in the computer as 0 and 1. 0 is false and 1 is true A bool variable may be assigned any integer value, however it will be converted to true (1) if it is non-zero. Relational (Boolean) Expressions Relational (boolean) expressions are expressions that evaluate to true or false. They may be comprised of a bool variable or constant; or a combination of variables and constants using relational and/or logical operators. Relational expressions are often used to create conditions for both selection and repetition control structures. Relational Operators Used to compare, or relate, two values (variables or constants) == equal to (do not confuse with =) != not equal to > greater than < less than >= greater than or equal to <= less than or equal to If used with char variables the ASCII equivalents are compared. Items to note Be very cautious about == and = operators! Relational operators have lower precedence than arithmetic operators, therefore the arithmetic operations will be performed before the relational operations. x + 3 > y *5 may use parentheses to avoid confusion. (x + 3) > (y * 5) Relational operators may be used with string variables, but not with C-strings (character arrays) Questions ????? ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern