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.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x.
Advertisements

Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Chapter 3 Message in a Bottle Programming Loops in C.
Literals Why does this produce an error? float x = 2.5;
COMP 14 Introduction to Programming
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
The Quadratic Formula..
Sec 5.6 Quadratic Formula & Discriminant Quadratic Formula (Yes, it’s the one with the song!) If ax 2 + bx + c = 0 and a ≠ 0, then the solutions (roots)
Warm up – Solve by Taking Roots. Solving by the Quadratic Formula.
Simple Data Type Representation and conversion of numbers
Input & Output: Console
Computer Arithmetic Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Independent Representation.
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Goals: To solve quadratic equations by using the Quadratic Formula.
Lecture #5 Introduction to C++
CISC105 – General Computer Science Class 9 – 07/03/2006.
Fundamental Programming: Fundamental Programming Introduction to C++
3.8 Warm Up Write the function in vertex form (by completing the square) and identify the vertex. a. y = x² + 14x + 11 b. y = 2x² + 4x – 5 c. y = x² -
CS177 Week2: Recitation Primitive data types and Strings with code examples.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
SOLVING QUADRATIC EQUATIONS BY COMPLETING THE SQUARE BECAUSE GRAPHING IS SOMETIMES INACCURATE, ALGEBRA CAN BE USED TO FIND EXACT SOLUTIONS. ONE OF THOSE.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
The Quadratic Formula & Discriminant Essential question – How do you solve a quadratic equation using the Quadratic Formula?
Copyright © – Curt Hill Types What they do.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
The Quadratic Formula Students will be able to solve quadratic equations by using the quadratic formula.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Solving Quadratic Formula using the discriminant.
Chapter 4: Polynomial and Rational Functions. 4-2 Quadratic Equations For a quadratic equation in the form ax 2 + bx + c = 0 The quadratic Formula is.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Solving Quadratic Equations by Using the Quadratic Formula (9-5) Objective: Solve quadratic equations by using the Quadratic Formula. Use the discriminant.
3.4 Chapter 3 Quadratic Equations. x 2 = 49 Solve the following Quadratic equations: 2x 2 – 8 = 40.
Lesson 6.5: The Quadratic Formula and the Discriminant, pg. 313 Goals: To solve quadratic equations by using the Quadratic Formula. To use the discriminant.
2.2 Solving Quadratic Equations Algebraically Quadratic Equation: Equation written in the form ax 2 + bx + c = 0 ( where a ≠ 0). Zero Product Property:
1.2 Quadratic Equations. Quadratic Equation A quadratic equation is an equation equivalent to one of the form ax² + bx + c = 0 where a, b, and c are real.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2 Variables.
Data Types, Variables & Arithmetic
Tokens in C Keywords Identifiers Constants
EPSII 59:006 Spring 2004.
Fundamental Data Types
Warm up – Solve by Completing the Square
The Quadratic Formula.
Chapter 2 Variables.
Lectures on Numerical Methods
Review: Simplify.
C++ Programming Lecture 3 C++ Basics – Part I
Fundamental Data Types
Chapter 2 Variables.
Solve quadratic equations using the: QUADRATIC FORMULA
Variables and Constants
Presentation transcript:

Lecture Computer 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) exponent mantissa assumed decimal point sign

Lecture Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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 Computer 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