DATA TYPES and DATA STRUCTURES Class: Comp

DATA TYPES and DATA STRUCTURES Class: Comp

Ministry of Secondary Education Republic of Cameroon Progressive Comprehensive High School Peace – Work – Fatherland PCHS Mankon – Bamenda School Year: 2013/2014 Department of Computer Studies TOPIC: DATA TYPES AND DATA STRUCTURES Class: Comp. Sc. A/L By: DZEUGANG PLACIDE The primary purpose of most computer programs is to store and retrieve data rather than to perform calculations. There are many different ways to organize data for storage and retrieval, and each type of organization is well suited to solving certain kinds of problems and poorly suited to solving problems of other kinds. Consequently, the way in which a program's data is organized may have a profound effect on the program's running time and memory requirements. The finite set of values along with set of rules for different operations is called data type and the study of data organization is called data structures and is the primary focus of this topic. Learning objectives After studying this chapter, student should be able to: Give examples of data types and state how their represented in the memory. Appreciate and use concepts of data structure as array, pointer, structure, linked list, … Appreciate the advantages of abstract data types as a software development strategy. To develop significant facility with the Abstract Data Type like Stack, Queue, and binary tree from the client perspective. Implement Abstract Data Type used data structures Contents I. INTRODUCTION TO DATA TYPE ............................................................................................. 2 II. PRIMITIVE DATA TYPES ................................................................................................................... 2 III. COMPOSITE DATA TYPE .............................................................................................................. 4 IV. ABSTRACT DATA TYPES ...................................................................................................... 8 This topic and others are available on www.dzplacide.overblog.com in PDF format Topic: Computer System Architecture 2 By DZEUGANG Placide I. INTRODUCTION TO DATA TYPE A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called program. A program usually contains different types of data types (integer, float, character etc.) and need to store the values being used in the program. C language is rich of data types. A programmer has to employ proper data type as per his requirements. Data type exists in various types: - Primitive or primary, or atomic data type - Composite, or complex, or secondary data type - Abstract data type NB: The size a data type is compiler dependent. One can be using 2 bytes to represent an integer while another is using 4 bytes. In C, the size of a data type can be obtained using the function sizeof() defined in the library stdio.h. For instance, with the declaration int p, we have sizeof(p) = sizeof(int) = 4; II. PRIMITIVE DATA TYPES Primitive data types are standard predefined types that you can use as the basis for variables, record fields, or your own Data Item parts. Though exact names may vary, many of these types (like INT) are common to most programming languages. C and other procedural languages often refer to these types as "elementary items" because they are not based on any other type. Classic basic primitive types may include: Character (character, char); Integer (integer, int, short, long, byte) with a variety of precisions; Floating-point number (float, double, real, double precision); Fixed-point number (fixed) with a variety of precisions and a programmer-selected scale. Boolean, logical values true and false. Reference (also called a pointer or handle), a small value referring to another object's address in memory, possibly a much larger one. II.1 Boolean type The Boolean type represents the values: true and false. Although only two values are possible, they are rarely implemented as a single binary digit for efficiency reasons. Many programming languages like C do not have an explicit boolean type, instead interpreting (for instance) 0 as false and other values as true. This topic and others are available on www.dzplacide.overblog.com in PDF format Topic: Computer System Architecture 3 By DZEUGANG Placide II.2 Integer Data Types: Integers are whole numbers with a range of values, which are machine dependent. Generally an integer occupies 2 bytes memory space and its value range limited to -32768 to +32767(that is, -215 to +215-1). (A signed integer use one bit for storing sign and rest 15 bits for number.) To control the range of numbers and storage space, C has three classes of integer storage namely short int, int and long int. All three data types have signed and unsigned forms. A short int requires half the amount of storage than normal integer. Unlike signed integer, unsigned integers are always positive and use all the bits for the magnitude of the number. Therefore , the range of an unsigned integer will be from 0 to 65535. The long integers are used to declare a longer range of values and it occupies 4 bytes of storage space. Data Type Bytes Range Format short int or signed short int 2 -32768 to +32767 %d unsigned short int 2 0 to 65535 %u long int or signed long int or long 4 -2147483648 to +2147483647 %ld unsigned long int or unsigned long 4 0 to 4294967295 %Ld II.3 Floating Point Data Types: The floating point data type is used to store fractional numbers (real numbers) with 6 digits of precision. Floating point numbers are denoted in C by the keyword float. When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space. Data Type Bytes Range Format float 4 -3.4e 38 to +3.4e38 %f double 8 -1.7e 308 to +1.7e 308 %lf long double 12 -1.7e 4932 to +1.7e 4932 %Lf II.4 Character Data Type: The character data types are used to store the special character and alphabets. It consists of ASCII characters. It occupies one byte of memory. It can be signed and unsigned i.e they have the range of -128 to +127 and 0 to 255 respectively. The following table shows the different character data types. Data Type Bytes Range Format Signed char or char 1 -128 to 127 %c Unsigned char 1 0 to 255 %c This topic and others are available on www.dzplacide.overblog.com in PDF format Topic: Computer System Architecture 4 By DZEUGANG Placide III. COMPOSITE DATA TYPE In computer science, a composite data type is any data type which can be constructed in a program using its programming language's primitive data types and other composite types. The act of constructing a composite type is known as composition. III.1 Record A record also called a structure is a group of related data items stored in fields, each with its own name and datatype. Suppose you have various data about an employee such as name, salary, and hire date. These items are logically related but dissimilar in type. A record containing a field for each item lets you treat the data as a logical unit. Thus, records make it easier to organize and represent information. Such a declaration can be as follow: pseudocode In C Type employee = record struct person Name: string { Salary: number char name[50]; float salary; sex: character char sex; Endrecord }; The size of a structure depends on the data type of its each field. For instance, for example with the structure defined above, Sizeof (struct person) = 50 * sizeof(char) + sizeof(float) + sizeof(char) = 50*1 + 4 + 1 = 55 bytes III.2 Array data type An array is a sequenced collection of elements of the same data type with a single identifier name. Arrays can have multiple axes (more than one axis). Each axis is a dimension. Thus a single dimension array is also known as a list. A two dimension array is commonly known as a table (a spreadsheet like Excel is a two dimension array). We refer to the individual values as members (or elements) of the array. Programming languages implement the details of arrays differently. Because there is only one identifier name assigned to the array, we have operators that allow us to reference or access the individual members of an array. The operator commonly associated with referencing array members is the index operator. It is important to learn how to define an array and initialize its members. Declaration In pseudocode In C Age = array[1 to 5] of integer int age[5]; This topic and others are available on www.dzplacide.overblog.com in PDF format Topic: Computer System Architecture 5 By DZEUGANG Placide Age is an array of 5 integers. Notice that in C the index is not defined by the user but the first is always 0 Here, the size of array age is 5 times the size of int because there are 5 elements. Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on. Accessing array elements In pseudocode In C Age[2] ← 4 Age[2] = 4; Read(Age[1]) Scanf(“%d”, &Age[1]) C Programming Multidimensional Arrays C programming language allows creating arrays of arrays known as multidimensional arrays. For example: In pseudocode In C A=array[1to 2, 1 to 6] of real float A[2][6]; Here, A is an array of two dimension, which is an example of multidimensional array. This array has 2 rows and 6 columns In C the first element of the array is A[0][0], then the next A[0][1], A[0][2], … For better understanding of multidimensional arrays, array elements of above example can be thinked of as below: III.3 String A string is any finite sequence of characters (i.e., letters, numerals, symbols and punctuation marks).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us