Introduction to Data Structures CMPE231 Spring 2012 Assoc. Prof. Alexander Chefranov 1.

Slides:



Advertisements
Similar presentations
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Advertisements

Lecture # 02 07/02/2013Dr. Muhammad Umair 1. 07/02/2013Dr. Muhammad Umair 2  Numeric  Integer Numbers  0,10,15,4563 etc.  Fractional Number  10.5,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Representing Information as Bit Patterns
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
Introduction to Computing CPSC 203 January 24, 2006 Heejin Lim Chapter 1 Chapter 2 (part of)
Binary Number Systems.
ES 84 Numerical Methods for Engineers, Mindanao State University- Iligan Institute of Technology Prof. Gevelyn B. Itao.
Binary Representation and Computer Arithmetic
Chapter 5 Data representation.
The Binary Number System
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Simple Data Type Representation and conversion of numbers
Binary Representation. Binary Representation for Numbers Assume 4-bit numbers 5 as an integer  as an integer  How? 5.0 as a real number  How?
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Input & Output: Console
Computer Science 111 Fundamentals of Programming I Number Systems.
Higher Computing Computer Systems S. McCrossan 1 Higher Grade Computing Studies 1. Data Representation Data Representation – Why do we use binary? simplicity,
Shawlands Academy Higher Computing Data Representation.
Number Systems Spring Semester 2013Programming and Data Structure1.
Representation of data types. Introduction We are going to look at representing cardinal numbers, integers and real numbers in decimal, binary, octal.
Constants in C A Presentation On Department of Computer & Information Technology, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Computer Science Engineering B.E.(4 th sem) c omputer system organization Topic-Floating and decimal arithmetic S ubmitted to– Prof. Shweta Agrawal Submitted.
The Teacher CP4 Binary and all that… CP4 Revision.
Chapter 2: The Logic of Compound Statements 2.5 Application: Number Systems and Circuits for Addition 1 Counting in binary is just like counting in decimal.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
The Teacher CP4 Binary and all that… CP4 Revision.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
PROCESSING Types. Objectives Be able to convert between binary and decimal numbers. Be able to declare and initialize character variables and constants.
Floating Point in Binary 1.Place Value Chart:
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Floating Point Binary A2 Computing OCR Module 2509.
CS 160 Lecture 4 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Lecture 1 Data Structures Shafay Shamail September 05, 2006.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Bits, Data types, and Operations: Chapter 2 COMP 2610 Dr. James Money COMP
 Computers are 2-state devices › Pulse – No pulse › On – Off  Represented by › 1 – 0  BINARY.
Software Design and Development Storing Data Computing Science.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Starter Using two’s Complement form convert the following from Denary to Binary using 8 bits. Answer on mini whiteboard Using two’s.
Introduction to Numerical Analysis I
Department of Computer Science Georgia State University
Fundamentals of Computer Science
ECE 2110: Introduction to Digital Systems
Introduction To Computer Science
CSCI 198: Lecture 4: Data Representation
Chapter 6: Data Types Lectures # 10.
CSCI 161: Lecture 4: Data Representation
EPSII 59:006 Spring 2004.
Binary, Decimal and Hexadecimal Numbers
Data Structures Mohammed Thajeel To the second year students
1.1 Real Numbers & Number Operations
BCD = Binary Coded Decimal
Introduction to Abstract Data Types
Computer Organization
Numbers representations
Storing Integers and Fractions
Representation of real numbers
COMS 161 Introduction to Computing
1.6) Storing Integer: 1.7) storing fraction:
Presentation transcript:

Introduction to Data Structures CMPE231 Spring 2012 Assoc. Prof. Alexander Chefranov 1

Information and Meaning Concept of information similar to the concept of point, line, and plane in geometry is not defined formally It is possible to talk about the length of a line We can measure quantities of information The basic unit of information is the bit 2

Binary and Decimal Numbers Binary number system Ones complement notation Twos complement notation Binary coded decimal 3

Real Numbers Floating-point notation Mantissa Base Exponent 4

Data Type Integer Real number Character string Memory, Address, Value (Contents) Int x,y; Float a,b; 5

Character Strings Byte First byte size Null terminating strings 6

Abstract Data Types /*value definition*/ Abstract typedef RATIONAL; Condition Rational[1]!=0; /*operator definition*/ Abstract RATIONAL makerational(a,b) Int a,b; Precondition b!=0; Postcondition makerational[0]==a; makerational[1]=b; Abstract RATIONAL add(a,b) /*written a+b */ RATIONAL a,b; Postcondition add[1]==a[0]*b[0]; Add[0]==a[0]*b[1]+a[1]*b[0]; Abstract RATIONAL mult(a,b) /*written a*b */ RATIONAL a,b; Postcondition mult[0]==a[0]*b[0]; mult[1]==a[1]*b[1]; Abstract equal(a,b)/*written a==b */ RATIONAL a,b; Postcondition equal==(a[0]*b[1]==a[1]*b[0]); 7

Sequences as Value Definitions S= Abstract typedef > stp1; Abstract typedef stp2; Abstract typedef > stp3; 8

ADT for Varying-length Character Strings Abstract typedef > STRING; Abstract length(s) STRING s; Postcondition length==lens(s); Abstract STRING concat(s1,s2) STRING s1,s2; Postcondition concat==s1+s2; Abstract STRING substr(s1,i,j) STRING s1; Int I,j; Precondition 0<=i<len(s1); 0<=j<len(s2); Postcondition substr==sub(s1,I,j); Abstract pos(s1,s2) STRING s1,s2; Postcondition /*lastpos=len(s1)=len(s2) */ ((pos==-1)&&(for(i=0;i sub(s1,I,len(s2))))) || (pos>=0)&&(pos sub(s1,I,len(s2))))) 9

Data Types in C Int, float, char, double Short int, long int, unsigned Pointers Int *pi; Float *pf; Char *pc; Pi=(int *)pf; Int x; Pi=&x; x=*pi; *(pi+1) *pi+1 10

Parameters By value, by reference 1 x=5; 2 printf(“%d\n”,x); 3 funct(x); 4 printf(“%d\n”,x);.. 5 void funct(int y){ 6 ++y; 7 printf(“%d\n”,y); 8 } 11

Parameters (cont) 1 x=5; 2 printf(“%d\n”,x); 3 funct(&x); 4 printf(“%d\n”,x); 5 void funct(int *py){ 6 ++(*py); 7 printf(“%d\n”,*py); 8 } 12

Arrays in C One-dimensional array Int a[100]; Basic operations: extracting and storing Lower bound, Upper bound #define NUMELTS 100 Int a[NUMELTS]; For(int i=0;i<NUMELTS;a[i++]=0); 13

The Array as an ADT Abstract typedef > ARRTYPE(ub,eltype); Condition type(ub)==int; Abstract eltype extract(a,i) /* written a[i] */ ARRTYPE(ub, Eltype) a; Int I; Precondition 0<=i<ub; Postcondition extract==a i ; Abstract store(a,I,elt) /* written a[i]=elt */ ARRTYPE(ub,eltype) a; Int I; Eltype elt; Precondition 0<=i<ub; Postcondition a[i]==elt; 14

Using One-Dimensional Arrays #define NUMELTS 100 Void main(){ Int num[NUMELTS]; Int I; Int total; Float avg; Float diff; Total=0; For(i=0;i<NUMELTS;i++){ scanf(“%d”,num[i]); total+=num[i]; } Avg=(float)total/NUMELTS; Printf(“\nnumber difference”); For(i=0;i<NUMELTS;i++){ diff=num[i]-avg; printf(“\n %d %f”, num[i], diff); } Printf(“\n average= %f”,avg); } 15

Implementing One-Dimensional Arrays Int b[100]; Base address B[i] in base(b)+i*esize In C an array variable is a pointer variable Int *b does not reserve 100 elements B+I; *(b+i) Varying-sized element array: reserve a contiguous set of memory locations each of which holds an address of an element 16

Arrays as Parameters Float avg(float a[], int size){ int I; Float sum; sum=0; for(i=0;i<size;i++) sum+=a[i]; return (sum/size); } #define ARANGE 100 Float a[ARANGE];.. Avg(a, ARANGE); Array is passed by reference saving space and time 17

Character Strings in C #define STRSIZE 80 Char string[STRSIZE]; Int Strlen(char string[]){ int I; for(i=0;string[i]!=‘\0’;i++); return I; } Int strpos(char s1[],char s2[]){ int len1, len2, I,j1,j2; len1=strlen(s1); len2=strlen(s2); for(i=0;i+len2<=len1;i++) for(j1=i,j2=0;j2<=len2&&s1[j1]==s2[j2];j1++,j2++) if(j2==len2) return I; return -1; } 18

Character Strings in C (cont) Void strcat(char s1[], char s2[]){ int I,j; for(i=0;s1[i]!=‘\0’;i++); for(j=0;s2[j]!=‘\0’;s1[i++]=s2[j++]; } Void substr(char s1[],int I, int j, char s2[]){ int k,n; for(k=I,m=0;m<j;s2[m++]=s1[k++]; s2[m]=‘\0’; } 19

Two-Dimensional Arrays Int a[3][5]; Range r1 of the 1 st dimension (rows) is 3 Range r2 of the 2 nd dimension (columns) is 5 A[1][3]=5; Base(ar)+(i1*r2+i2)*esize 5 20

Multi-Dimensional Arrays Int b[3][2][4]; Base(ar)+esize*(i1*r2*..*rn+i2*r3*..*rn+..+i(n- 1)*rn+in) B[0][0][0] B[0][0][1] B[0][0][2] B[0][0][3] B[0][1][0] B[0][1][1] B[0][1][2] B[0][1][3] 21

Structures in C Struct{ char first[10]; char midinit; char last[20]; } sname, ename; Typedef struct{ char first[10]; char midinit; char last[20]; } NAMETYPE; NAMETYPE sname, ename; struct nametype{ char first[10]; char midinit; char last[20]; } Struct nametype sname, ename; Printf(“%s”, sname.first); Ename.midinit=‘N’; For(i=0;i<20;i++) sname.last[i]=ename.last[i]; 22

Unions #define LIFE 1 #define AUTO 2 #define HOME 3 Struct addr{ char street[50]; char city[10]; char state[3]; char zip[6]; } Struct date{ int month; int day; int year; } Struct policy{ int polnumber; Char name[30]; struct addr address; int amount; Float premium; Int kind; /*LIFE,AUTO,HOME*/ Union{ struct{ char beneficiary[30]; struct day birthday; } life; struct{ int autodeduct; Char license[10]; char state[3]; char model[15]; int year} auto; Struct{ int homededuct; int yearbuilt; } home; }policyinfo; } Struct policy p; If(p.kind==LIFE) printf(“\n%s %2d %2d %4d”, p.policyinfo.life.beneficiary, p.policyinfo.birthday.month, p.policyinfo.life.birthday.day, p. policyinfo.life.birthday.year); Struct policy a[100]; For(i=0;i<100;i++) if(a[i].kind==LIFE).. 23

Structure Parameters Int writename(struct nametype *name){ int count,I; count=0; printf(“\n”); for(i=0;(i first[i]!=‘\0’);i++){ printf(“%c”,name->first[i]); count++; } printf(“%c”,’ ‘); count++; if(name->midinit!=‘ ‘){ printf(“%c%s”, name->midinit,”. “); count+=3; } for(i=0;(i last[i])!=‘\0’);i++){ printf(“%c”,name->last[i]); count++; } return count; } 24

Allocation of Storage and Scope of Variables Automatic variables (declared within a function) Can be referenced only throughout entire block unless the variable identifier is redeclared within an internal block External variables (declared outside of any function) The scope lasts from the point at which it is declared until the end of its containing source file 25

Allocation of Storage and Scope of Variables File1 #define MAXSTUDENTS.. int grades[MAXSTUDENTS]; End of file1 File2 extern int grades[]; Float average(){.. } End file2 Static variables Register variables Uninitialized external and static variables are initialized to 0, whereas uninitialized automatic and register variables have undefined values 26

Source file1.c 1 int x,y,z; 2 void func1(){ 3 int a,b; 4 x=1; y=2;z=3;a=1;b=2; 5 printf(“%d %d %d %d %d\n”,x,y,z,a,b); 6} 7 void func2(){ 8 int a=5; 9 printf(“%d %d %d %d\n”,x,y,z,a); 10} End of file1.c 27

file2.c 11 #include 12 extern int x,y,z; 13 void main(){ 14 func1(); 15 printf(“%d %d %d\n”,x,y,z); 16 func2(); 17 func3(); 18 func3(); 19 func4(); 20 printf(“%d %d %d\n”,x,y,z); 21} 22 Void func3(){ 23 static int b; 24 y++; b++ 25 printf(“%d %d %d %d\n”,x,y,z,b); 26} 27 void func4(){ 28 int x=10,y=20,z=30; 29 printf(“%d %d %d\n”,x,y,z); 30} 28

Dynamic Memory Allocation Void *calloc(size_t nobj, size_t size) Returns a pointer to space for an array of nobj objects, each of size size, or NULL if the request cannot be satisfied. The space is initialized to zero bytes Void *malloc(size_t size) Returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized. 29