# Lecture 7 -- 1Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.

## Presentation on theme: "Lecture 7 -- 1Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything."— Presentation transcript:

Lecture 7 -- 1Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything else is the same as int l range of values: -32,768 through 32,767 l int uses a full word (32 bits) to represent a value. l range of values: -2,147,483,648 through 2,147,483,647 rVariables of type float use one word (32 bits) to represent a value instead of the two used by a double. l the extra bits are taken from the mantissa (number of digits) l A float has about 7 digits of accuracy l A double has about 14 digits of accuracy l Both can represent the same huge range (exponent) 00000011111010000000000000000000 exponent mantissa assumed decimal point sign

Lecture 7 -- 2Computer Science I - Martin Hardwick More types rAn unsigned integer does not use a bit for the sign. rAn unsigned integer can be twice the size of an integer but cannot be negative unsigned positive; rA character variable is used to store a single character char letter = A; Like float and short, char variable is not used much anymore. We will use it occasionally. rInstead we use String objects (much more on this later).

Lecture 7 -- 3Computer Science I - Martin Hardwick Types we use rInteger for ordinary numbers rDouble for floating points numbers rString for string data (more later) rBool for logical values (more later) rAnd in a few lectures we will begin to define our own data types. rThe types provided by the language are also known as the primitive types rThe types that we define are also known as defined types. rGood programming requires good data types

Lecture 7 -- 4Computer Science I - Martin Hardwick Converting between types rFor the primitive types conversion is usually implicit int x = 61; double y = 65.9; char c; x = y;// y is truncated to an integer i.e to 65 y = x;// y becomes 65.0 c = x;// c is the character with value 65 what is it? c = y;// what happens find out by running a program!

Lecture 7 -- 5Computer Science I - Martin Hardwick Converting between types with warning messages rThe compiler tries to be helpful so it gives a warning when values are converted in a way that could lead to a loss of data. l Most common example rounding a double into an integer rYou should get rid of unnecessary warnings so that they do not hide the real warnings. rTo eliminate the warning you can put a coercion into your program. The coercion is shown as the name of the required type in parenthesis. x = (int) y; c = (char) x; c = (char) y; y = (double) x;

Lecture 7 -- 6Computer Science I - Martin Hardwick Quadratic Formula -- No Roots rA quadratic equation may have no real roots. l this happens when the discriminator ( B 2 - 4AC ) is less than zero l cant take the square root of a negative number rIf you take the square root of a negative number, the program will generate an invalid result. l result contains #IND to mark it invalid rTherefore, the program must check this before trying to compute the roots. //Find roots of Ax 2 + Bx + C = 0 #include using namespace std; int main () { doubleA, B, C, x1, x2; doublesqrtOfDiscriminant; //get three inputs cout << "Enter the value for A: "; cin >> A; cout << "Enter the value for B: "; cin >> B; cout << "Enter the value for C: "; cin >> C;

Lecture 7 -- 7Computer Science I - Martin Hardwick Quadratic Formula -- Using The IF-ELSE Stmt //compute the solutions if ((B*B - 4*A*C) < 0.0) { cout << "No real roots" << endl; } else { sqrtOfDiscriminant = sqrt( B*B - 4*A*C ); x1 = (-B+sqrtOfDiscriminant) /(2*A); x2 = (-B-sqrtOfDiscriminant) /(2*A); //display the first root cout << "First solution: " << x1 << endl; rWhen there are two cases and one or the other must be true, then use an IF-ELSE statement. rIn this program the two cases are: l Case 1: discriminator less than 0 so no real roots l Case 2: discriminator greater than or equal to 0 so one or two real roots rThe condition in the IF-ELSE statement determines which case to execute.

Lecture 7 -- 8Computer Science I - Martin Hardwick IF - ELSE Statements rSyntax:if ( condition ) { statement1; statement2;... } else { statementa; statementb;... }; rMeaning: condition statement1; statement2;... truefalse statementa; statementb;... Handle two special cases where one or the other must apply.

Lecture 7 -- 9Computer Science I - Martin Hardwick Quadratic Formula (continued) // display second root // if different if (x1 != x2) { cout << "Second solution: " << x2 << endl; } return 0; } rMust still decide if there is one or two roots. l this is done with the same IF statement as before rNote that the IF statement is nested inside the else part of the IF-ELSE statement rThe program now correctly handles the three cases: l no real roots l one real root l two real roots

Lecture 7 -- 10Computer Science I - Martin Hardwick Code patterns rProgrammers try hard to be consistent in their programming style. rWe always use the same pattern for the same type of logic to make our programs easier to read and check. rAs a beginner you should copy the patterns used by more experienced programmers. rOur first pattern is the nested IF-THEN rWith each nest we indent the code by a tab to make the nest easier to see. if (A < B) { if (A < C) { } }

Lecture 7 -- 11Computer Science I - Martin Hardwick Another pattern - Cascading IF - ELSE Statements // Display letter grade for // a given test score. #include using namespace std; int main () { doublescore; //get input cout << "Enter test score: "; cin >> score; rSuppose that you want a program that reads a test score and converts it to a letter grade. l 90 scoreA l 80 score < 90B l 70 score < 80C l 60 score < 70D l score < 60F rThis requires more than two cases (i.e., five cases). l multiple cases can be done with nested IF-ELSE statements l you must be careful in how you order the cases

Lecture 7 -- 12Computer Science I - Martin Hardwick Cascading IF - ELSE Statements (continued) //display the letter grade if (score < 60) { cout << "It is F." << endl; } else if (score < 70) { cout << "It is D." << endl; } else if (score < 80) { cout << "It is C." << endl; } else if (score < 90) { cout << "It is B." << endl; } else { cout << "It is A." << endl; } return 0; } rEach case assumes the previous cases are not true. rIf multiple cases are true, only the first true case is executed. rThe last case is usually written with else rather than else if to serve as a default case. l catch situations where the other cases dont apply rEach case can have multiple statements to execute. l in this example there is only one statement in each case

Download ppt "Lecture 7 -- 1Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything."

Similar presentations