Representation of data types

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Representation of Data Types Comparing double and int data types Using integers are faster and more precise round-off errors when using doubles The range.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Lecture 2 Introduction to C Programming
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 ICS103 Programming in C Lecture 12: Arrays I. 2 Outline Motivation for One-dimensional Arrays What is a One-dimensional Array? Declaring One-dimensional.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Computer Science 210 Computer Organization Introduction to C.
Simple Data Type Representation and conversion of numbers
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 MT258 Computer Programming and Problem Solving Tutorial 03.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS /1433 – Term 1.
Lecture2.
Variables, Operators, and Expressions
Arithmetic Expressions
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Computer Science 210 Computer Organization
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Chapter 2 - Introduction to C Programming
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 6 Floating Point
Chapter 2 - Introduction to C Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Data Type.
Arithmetic Operator Operation Example + addition x + y
Computer Science 210 Computer Organization
Lexical Elements, Operators, and the C Cystem
Data Type.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Simple Data Types and Function Calls
Chapter 2 - Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Lecture4.
A function with one argument
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 - Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Introduction to Programming
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Lecture3.
Summary of what we learned yesterday
Chapter 2 - Introduction to C Programming
Data Type.
DATA TYPES There are four basic data types associated with variables:
ICS103 Programming in C Lecture 12: Arrays I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Computer Science II CS132/601* Lecture #C-1.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Representation of data types Integers (int) are represented as a signed binary number. Ex. (5)10 = (0101)2 (-5)10 = (?)2 Take the 1-complement of 0101, That is 1010, and add 1 which gives 1011. 1011 is the representation of -5 The left-most bit is named the sign bit. Normally an integer is stored in a memory ”cell” that contains 16 bits. I.e. INT_MAX is 215 = 32767 and INT_MIN is -215 = -32768 TDBA66, VT-03 Lecture Ch 3

Representation of double real number = mantissa*2exponent Mantissa stored in finite number of bits and its value is between 0.5 and 1.0 or between -1.0 and -0.5 Round-off error and accuracy The minimum range of double is approximately 10-37 to 1037 Check DBL_MIN and DBL_MAX in file float.h See Appendix B in text book TDBA66, VT-03 Lecture Ch 3

Finite arithmetic can fail Cancellation error: adding values of very different magnitudes might not give the expected result (out-shift) Another cancellation error can occur when two similar values are subtracted (loosing precision) Arithmetic underflow and overflow: underflow when the value of an expression (in absolute value) is less than DBL_MIN overflow when a value exceeds DBL_MAX TDBA66, VT-03 Lecture Ch 3

peppar:~/c/Book-Figs> cat fig3_02V2.c /* Modification of program in Fig. 3.2 * Find implementation's ranges for positive numeric data */ #include <stdio.h> #include <limits.h> /* definition of INT_MAX */ #include <float.h> /* definitions of DBL_MIN, DBL_MAX */ #include <math.h> /* definition of pow() */ int main(void) { int max_number; printf("Range of integer values of type int: %d . . %d\n", INT_MIN, INT_MAX); /* Compute 2 raised to 63 and compare */ max_number = pow(2,63)-1; printf("\n2 raised to 63 minus 1 equals %d\n", max_number); printf("Range of positive values of type double: %e . . %e\n", DBL_MIN, DBL_MAX); return (0); } TDBA66, VT-03 Lecture Ch 3

Note! –lm makes the mathematical library to be loaded Compile, link and load Note! –lm makes the mathematical library to be loaded gcc -o fig3_02V2 fig3_02V2.c –lm peppar:~/c/Book-Figs> ./fig3_02V2 Range of integer values of type int: -2147483648 . . 2147483647 2 raised to 63 minus 1 equals 2147483647 Range of positive values of type double: 2.225074e-308 . . 1.797693e+308 peppar:~/c/Book-Figs> Output from program fig3_02V2.c TDBA66, VT-03 Lecture Ch 3

Evaluation of arithmetic expressions Integer arithmetic Ex.1: 7/5*5 equals 1*5 (division) Ex.2: 7%5*5 equals 2*5 (reminder) The same for variables of int type. • Real arithmetic Ex.3: 8.0/5.0 equals 1.6 (as expected) Mixed mode 8.0/5 also equals 1.6 TDBA66, VT-03 Lecture Ch 3

Mixed type assignment Ex.1: double x, y; int tal; y=tal/5+6; /* expression of type int */ But the value of y is stored as a double Ex.2: tal=x*y-4; /* expression is of type double*/ But the value of tal is stored as an int (the integral part of the expression) TDBA66, VT-03 Lecture Ch 3

Casting A value of an expression can be explictly cast to another data type Ex.3: y=(double)tal/5+6; /* prevent from integer division */ Note the difference when writing Ex.4: y=(double)(tal/5+6); Here expression is evaluated using integer divide and the whole expression is cast to double TDBA66, VT-03 Lecture Ch 3

Figure 3.7 Using Casts to Prevent Integer Division /* Computes a test average*/ #include <stdio.h> int main(void) { int total_score, num_students; double average; printf("Enter sum of students' scores> "); scanf("%d", &total_score); printf("Enter number of students> "); scanf("%d", &num_students); average = (double)total_score / (double)num_students; printf("Average score is %.2f\n", average); return (0); } Enter sum of students' scores> 1822 Enter number of students> 25 Average score is 72.88 TDBA66, VT-03 Lecture Ch 3

Priority of operators See Appendix C in text book If the same priority the evaluation goes from left to right Ex.1: (a+b)/c is evaluated as ((a+b)/c) Ex.2: a+b/c is evaluated as (a+(b/c)) Ex.3: a*b/z is evaluated as ((a*b)/z) Ex.4: y-b/x-a is evaluated as ((y-(b/x))-a) Ex.5: (y-b)/(x-a) is eval. as ((y-b)/(x-a)) TDBA66, VT-03 Lecture Ch 3

Predefined math functions See page 98 in text book Ex. write program to simulate one toss with an ordinary die ON THE WHITEBOARD TDBA66, VT-03 Lecture Ch 3

return 0; printf("\n\n"); } printf("%3d", two_sum); two_sum = die_1+ die_2; die_2= (int) floor(rand()/(double)RAND_MAX*6.0)+1; die_1= (int) floor(rand()/(double)RAND_MAX*6.0)+1; putchar('\n'); if (i % 20 == 0) for (i = 0; i < n; ++i) { scanf("%d", &n); "How many throws? "); "Simulate throwing two dice a number of times.", printf("\n%s\n%s", srand(time(NULL)); /* nitialize the random number generator */ int i, n, die_1, die_2, two_sum; { int main(void) #include <time.h> #include <math.h> #include <stdlib.h> #include <stdio.h> peppar:~/c/Ckod> cat slumpa.c TDBA66, VT-03 Lecture Ch 3

Simple user-written functions Functions without parameters (neither input nor output) See Fig. 3.15 TDBA66, VT-03 Lecture Ch 3

printf("The square root of the second number is %.2f\n", second_sqrt); second_sqrt = sqrt(second); scanf("%lf", &second); printf("Enter a second number> "); /* Get second number and display its square root. */ printf("The square root of the number is %.2f\n", first_sqrt); first_sqrt = sqrt(first); scanf("%lf", &first); printf("Enter a number> "); /* Get a number and display its square root. */ instruct(); /*************Call of function instruct **************/ /* Display instructions. */ sum_sqrt; /* output - square root of sum */ second_sqrt, /* output - square root of second input */ first_sqrt, /* output - square root of first input value */ double first, second, /* input - two data values */ { main(void) int void instruct(void); /* Displays user instructions */ #include <math.h> /* definition of sqrt */ #include <stdio.h> /* definitions of printf, scanf */ /* Performs three square root computations */ Figure 3.15 Program with a User-Defined Function TDBA66, VT-03 Lecture Ch 3

/* Display the square root of the sum of the two numbers. */ Fig. 3.15 cont. /* Display the square root of the sum of the two numbers. */ sum_sqrt = sqrt(first + second); printf( "The square root of the sum of the two numbers is %.2f\n", sum_sqrt); return (0); } /* Displays user instructions*/ void instruct(void) { printf("This program demonstrates the use of the \n"); printf("math library function sqrt (square root).\n"); printf("You will be asked to enter two numbers --\n"); printf("the program will display the square root of \n"); printf("each number and the square root of their sum.\n\n"); TDBA66, VT-03 Lecture Ch 3