0% found this document useful (0 votes)
42 views23 pages

Data Types

The document outlines various data types including primitive, string, enumeration, array, record, union, and pointer/reference types. It provides examples of how these types are implemented and used in different programming languages like C++, Java, Fortran, COBOL and Ada. The document also discusses operations on data types and problems related to pointers.

Uploaded by

SamirReghouache
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views23 pages

Data Types

The document outlines various data types including primitive, string, enumeration, array, record, union, and pointer/reference types. It provides examples of how these types are implemented and used in different programming languages like C++, Java, Fortran, COBOL and Ada. The document also discusses operations on data types and problems related to pointers.

Uploaded by

SamirReghouache
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

PRIMITIVE DATA TYPES ASSOCIATIVE ARRAYS

-Integer RECORD TYPES


-Floating Point UNION TYPES
-Decimal POINTER/REFERENCE TYPES
-Boolean -Fundamental Operations
-Character -Problems
STRINGS -Memory Leak
-Character Array -Dangling Pointer
-Class -C++
-String Length -Java
-Static -Solutions to Pointer Problems
-Limited Dynamic -Tombstone
-Dynamic -Heap Management
ENUMERATION TYPES -Reference Counter
- C++ -Garbage Collection
- Fortran
- Java
SUBRANGE TYPES
ARRAYS
-Indexing
-Flavors
-Static LECTURE OUTLINE FOR:
-Fixed Stack Dynamic
-Stack Dynamic CHAPTER 6
-Fixed Heap-Dynamic
-Heap Dynamic
DATA TYPES
-Initalization
-Operations (APL)
-Rectangular/Jagged
-Implementation
-Single Dimensional
-Multi Dimensional
C++ Weak Typing to Display Integer

int main(void)
{
int theInt = 42;
char* theBytes = &theInt;

cout << “int is: “ << theInt << endl;

cout << “byte values: “


<< ((int) (unsigned char)) theBytes[0] << “ “
<< ((int) (unsigned char)) theBytes[1] << “ “
<< ((int) (unsigned char)) theBytes[2] << “ “
<< ((int) (unsigned char)) theBytes[3] << “ “
<< endl;
}
C++ Weak Typing to Display Integer
Hexadecimal
int main(void)
{
int theInt = 42;
char* theBytes = &theInt;

cout << “int is: “ << theInt << endl;

cout << “byte values: “


<< hex << ((int) (unsigned char)) theBytes[0] << “ “
<< hex << ((int) (unsigned char)) theBytes[1] << “ “
<< hex << ((int) (unsigned char)) theBytes[2] << “ “
<< hex << ((int) (unsigned char)) theBytes[3] << “ “
<< endl;
}
C++ Program to Write/Read Integer
Using Text Files
int main(void) int main(void)
{ {
int theInt = 12345678; int theInt;
ofstream out; ifstream in;

out.open(“temp.txt”); in.open(“temp.txt”);
out << theInt << endl; in >> theInt;
out.close(); in.close();
.
} .
.
}
C++ Program to Write/Read Integer
Using Binary Files
int main(void) int main(void)
{ {
int theInt = 12345678; int theInt;
ofstream out; ifstream in;

out.open(“temp.bin”, ios::binary); in.open(“temp.bin”, ios::binary);


out << theInt << endl; in >> theInt;
out.close(); in.close();
.
} .
.
}
Interest Calculation Using Floating Point Data Type

#include <iostream>
using namespace std;

int main(void)
{
// Credit card balance
double balance = 10.10;
double interest = 0.1;

// Formatting
cout.precision(30);
cout << showpoint;

// Output
cout << "Balance is:\t " << balance << endl;
cout << "Interest is:\t " << interest << endl;
cout << "New balance is:\t " << (balance * (1 + interest)) << endl;

}
Interest Calculation Using Decimal Data Type
VC++ .NET
#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

int _tmain()
{
// Credit card balance
Decimal balance = 10.10;
Decimal interest = 0.1;

// Output
Console::WriteLine("Balance is:\t {0}",balance.ToString("F30"));
Console::WriteLine("Interest is:\t {0}",interest.ToString("F30"));
Console::WriteLine("New Balance is:\t {0}",(balance * (1 + interest)).ToString("F30"));

return 0;
}
String Concatenation Problem
String* s = new String();
s = s.Concat(s,new String( “<html>”));
s = s.Concat(s,new String( “<body>”));
s = s.Concat(s,new String( “<ul>”));
s = s.Concat(s,new String( “<li> Item One”));
s = s.Concat(s,new String( “<li> Item Two”));
...
s = s.Concat(s,new String( “</ul>”));
s = s.Concat(s,new String( “</body>”));
s = s.Concat(s,new String( “</html>”));
ASCII Code Page
Latin-1 1252 Code Page
UNICODE LAYOUT Basic Plane

Figure 1: Figure 1: Unicode encoding layout for the BMP (Plane 0)


Enumeration Types (C++ Example
enum day {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

// Set day of week


day d = Mon;

switch (d)
{
case Mon: cout << “More sleep!” << endl; break;
case Tue: cout << “Close to the hump!” << endl; break;
case Wed: cout << “Hump day!” << endl; break;
case Thu: cout << “Over the hump!” << endl; break;
case Fri: cout << “Yipee! “ << endl; break;
case Sat: cout << “Sweet weekend.” << endl; break;
case Sun: cout << “Rats, almost Monday.” << endl; break;
}

// Set day of week


int d = 0;

switch (d)
{
case 0: cout << “More sleep!” << endl; break;
case 1: cout << “Close to the hump!” << endl; break;
case 2: cout << “Hump day!” << endl; break;
case 3: cout << “Over the hump!” << endl; break;
case 4: cout << “Yipee! “ << endl; break;
case 5: cout << “Sweet weekend.” << endl; break;
case 6: cout << “Rats, almost Monday.” << endl; break;
}
Enumeration Types (Java
Example)
public final class Day {
public static final Day MON = new Day();
public static final Day TUE = new Day();
public static final Day WED = new Day();
public static final Day THU = new Day();
public static final Day FRI = new Day();
public static final Day SAT = new Day();
public static final Day SUN = new Day();

private Day() {
// Empty private constructor ensures the only objects of
// this type are the enumerated elements declared above.
}
}
Program
in
Disk

Virtual Memory
Computing Address of Element In
Multidimensional Array
#!/usr/bin/env perl

#
# Welcome to Perl!
#
# To run this program type:
#
# perl AssociativeArrayExample.pl
#
# If the program works... then you've installed
# perl correctly!
#

print "Initializing associative array...\n";

%salaries = ("Gary" => 75000, "Perry" => 57000,


"Mary" => 55750, "Cedric" => 47850);

print "Perry's salary is: $salaries{'Perry'}\n"; Perl Program Demonstrating


# Iterate and print the key - value pairs
print "Dumping the associative array: \n";
foreach my $key (keys %salaries) {
Associative Arrays
print " value of $key is $salaries{$key}\n";
}

print "Deleting Gary from associative array: \n";


delete $salaries{"Gary"};

print "Checking for the existance of Gary in array: ";


if (exists $salaries{"Gary"})
{
print "EXISTS!\n";
}
else
{
print "DOES NOT EXIST!\n";
}

print "Dumping the associative array again: \n";


foreach my $key (keys %salaries) {
print " value of $key is $salaries{$key}\n";
}

print "Emptying array: \n";


%salaries = ();

print "Dumping the associative array again: \n";


foreach my $key (keys %salaries) {
print " value of $key is $salaries{$key}\n";
}
COBOL RECORD EXAMPLES

01 EMPLOYEE-RECORD. 01 OUTPUT-RECORD.
02 EMPLOYEE-NAME. 02 EMPLOYEE-NAME.
05 FIRST PICTURE IS X(20). 05 FIRST PICTURE IS X(20).
05 MIDDLE PICTURE IS X(20). 05 MIDDLE PICTURE IS X(20).
05 LAST PICTURE IS X(20). 05 LAST PICTURE IS X(20).
02 HOURLY-RATE PICTURE IS 99V99. 02 EMPLOYEE-NUMBER PICTURE IS 9(10).
02 EMPLOYEE-NUMBER PICTURE IS 9(10). 02 GROSS-PAY PICTURE IS 999V999.
02 NET-PAY PICTURE IS 999V999.
o Numerals 01, 02, 05 indicate hierarchical structure of
record
o PICTURE – indicates formatting for output
o X(20) – 20 alphanumeric characters
o 99V99 – 4 decimal digits with “.” in middle
o 9(10) – 10 decimal digits
Ada RECORD EXAMPLES

type Employee_Name_Type is record


First : String (1..20);
Middle: String (1..20);
Last: String (1..20);
end record;

type Employee_Record_Type is record


Employee_Name: Employee_Name_Type;
Hourly_Rate: Float;
end record;

Employee_Record: Employee_Record_Type;
#include <iostream>
C++ UNION TYPES
using namespace std; //introduces namespace std
int main( void )
{
typedef union _GenericInput
{
OUTPUT:
bool
char
theBool;
theChar;
Enter a character: a
int theInt;
double theDouble; Enter a double: 10.2
} GenericInput;
66 66 66 66 66 66 24 40
GenericInput input0;
GenericInput input1; As boolean x[66]
cout << "Enter a character: ";
cin >> input0.theChar;
As character [f]
cout << "Enter a double: ";
As integer x[66666666]
cin >> input1.theDouble;
As double [10.2]
// You should not be able to assign these two variables
// because they hold different types (char and double) Press any key to continue
// but the “free union” capability in C,C++ allows this
// DANGEROUS!!!
input0 = input1;

char *byteArray = (char *) &input1;


cout << hex << ((int) ((unsigned char) byteArray[0])) << " "
<< ((int) ((unsigned char) byteArray[1])) << " "
<< ((int) ((unsigned char) byteArray[2])) << " "
<< ((int) ((unsigned char) byteArray[3])) << " "
<< ((int) ((unsigned char) byteArray[4])) << " "
<< ((int) ((unsigned char) byteArray[5])) << " "
<< ((int) ((unsigned char) byteArray[6])) << " "
<< ((int) ((unsigned char) byteArray[7])) << endl;

cout << "As boolean x[" << input0.theBool << "]" << endl;
cout << "As character [" << input0.theChar << "]" << endl;
cout << "As integer x[" << input0.theInt << "]" << endl;
cout << "As double [" << input0.theDouble << "]" << endl;

return 0;
}
Ada UNION TYPES

type Shape is (Circle, Triangle, Rectangle);


type Colors is (Red, Green, Blue);
type Figure (Form : Shape) is
record
Filled : Boolean;
Color : Colors;
case Form is
when Circle =>
Diameter : Float;
when Triangle =>
Left_Side : Integer;
Right_Side : Integer;
Angle : Float;
when Rectangle =>
Side_1 : Integer;
Side_2 : Integer;
end case;
end record;

You might also like