Data structures Dynamic data structures are data structures that grow and shrink as you need them to by allocating and deal locating memory from a place.

Slides:



Advertisements
Similar presentations
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Advertisements

Informática II Prof. Dr. Gustavo Patiño MJ
Dale/Weems/Headington
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
How Create a C++ Program. #include using namespace std; void main() { cout
CS150 Introduction to Computer Science 1
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Programming Introduction to C++.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
IXA 1234: C++ PROGRAMMING CHAPTER 2: PROGRAM STRUCTURE.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Module 4: Structures ITEI222 Advanced Programming.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C to C++ © Bruce M. Reynolds & Cliff Green, C++ Programming Certificate University of Washington Cliff Green.
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Fundamentals of Programming Most.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Variables, Identifiers, Assignments, Input/Output
What header file contains C++ file I/O instructions?
Variables Mr. Crone.
Computing Fundamentals
Programming Structures.
Dynamic Memory Allocation Reference Variables
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Pointers & Functions.
Variables, Identifiers, Assignments, Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Data Structures.
Programming Introduction to C++.
Using string type variables
Pointers & Functions.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Data structures Dynamic data structures are data structures that grow and shrink as you need them to by allocating and deal locating memory from a place called the heap. They are extremely important in C because they allow the programmer to exactly control memory consumption. Dynamic data structures allocate blocks of memory from the heap as required, and link those blocks together into some kind of data structure using pointers. When the data structure no longer needs a block of memory, it will return the block to the heap for reuse. This recycling makes very efficient use of memory.

STRACT IN C++

// example about structures #include using namespace std; struct movies_t { string title; int year; } mine, yours; void printmovie (movies_t movie); int main () { string mystr; mine.title = "2001 A Space Odyssey"; mine.year = 1968; cout << "Enter title: "; getline (cin,yours.title); cout << "Enter year: "; getline (cin,mystr); stringstream(mystr) >> yours.year; cout << "My favorite movie is:\n "; printmovie (mine); cout << "And yours is:\n "; printmovie (yours); return 0; } void printmovie (movies_t movie) { cout << movie.title; cout << " (" << movie.year << ")\n"; } Enter title: Alien Enter year: 1979 My favorite movie is: 2001 A Space Odyssey (1968) And yours is: Alien (1979)

#include using namespace std; int main() { cout << "Hello World!" << endl; return 0; }

// struct1.cpp struct PERSON { // Declare PERSON struct type int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON int main() { struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7; }

// struct2.cpp // compile with: /c struct POINT { // Declare POINT structure int x; // Define members x and y int y; } spot = { 20, 40 }; // Variable spot has // values x = 20, y = 40 struct POINT there; // Variable there has POINT type struct CELL { // Declare CELL bit field unsigned short character : 8; // ???????? unsigned short foreground : 3; // 00000??? unsigned short intensity : 1; // 0000? unsigned short background : 3; // 0??? unsigned short blink : 1; // ? } screen[25][80]; // Array of bit fields

// anonymous_structures.c #include struct phone { int areacode; long number; }; struct person { char name[30]; char gender; int age; int weight; struct phone; // Anonymous structure; no name needed } Jim; int main() { Jim.number = ; printf_s("%d\n", Jim.number); }

#include using namespace std; struct date { int month; int day; int year; }; struct person { date bday; int ID; }; void main() { date manufact; date expiration; person me; // above are 3 structure variables manufact.month = 10; manufact.day = 20; manufact.year = 2007; expiration = manufact; expiration.year = 2009; cout << " Manufacture \n"; cout << manufact.month<< "-" << manufact.day<<"- " <<manufact.year<<endl; cout << "Expiration \n"; cout << expiration.month<< "-" << expiration.day<<"-" <<expiration.year<<endl; cout<<endl<<endl<<endl<<endl<<endl<<endl<<"Next Program"<<endl<<endl; me.bday.month = 1; me.ID = 152; cout<<me.bday.month; }

#include void main() { char repeat='y'; while(repeat=='y') { int x,n,a=1,temp; cout >x; cout >n; cout =1) { a = a * x; n--; } cout >repeat; } } Output: Enter a number : 4 Enter the power : 5 5th power of 4 is equal to 1024 Do you want to do it again ? (y/n) n Press any key to continue

#include using namespace std; int main() { int x = 0; while(x < 10) { double y = sqrt((double)x); cout << "The square root of " << x << " is " << y << endl; x++; } system("pause"); return 0;

Temperature conversion from Fahrenheit to Centigrade #include int main() { float c,f; cout >f; c = 5 * (f - 32)/9; cout<<" The temperature in centigrade : "<<c<<endl; return 0; } Output: Enter temperature in Fahrenheit : 100 The temperature in centigrade : Press any key to continue

Program to reverse a 2 digit number #include void main() { int n; cout > n; if(n 99) cout<<"!!! Invalid Number !!!"<<endl; else { int n2, n3; n2 = n % 10; n3 = n / 10; cout << "You Entered : "<< n3<<n2<<endl; cout << "The reverse of that no is : " << n2<<n3<<endl; } } Output: Enter a 2 digit number : 61 You Entered : 61 The reverse of that no is : 16 Press any key to continue

program to show the greatest and smallest number from the given numbers #include int main() { float num1,num2,num3,num4,num5,g,s; cout >num1; cout >num2; cout >num3; cout >num4; cout >num5; g = num1; s = num1; if(g num2) s = num2; if(s>num3) s = num3; if(s>num4) s = num4; if(s>num5) s = num5; cout<<" The smallest number is "<<s<<endl; return 0; } Output: Enter the first number : 1.5 Enter the second number : 3 Enter the third number : 9 Enter the fourth number : 0.35 Enter the fifth number : 9.1 The greatest number is 9.1 The smallest number is 0.35 Press any key to continue

finding nth power of any number using c++ #include void main() { char repeat='y'; while(repeat=='y') { int x,n,a=1,temp; cout >x; cout >n; cout =1) { a = a * x; n--; } cout >repeat; } } Output: Enter a number : 4 Enter the power : 5 5th power of 4 is equal to 1024 Do you want to do it again ? (y/n) n Press any key to continue

Program to shows size of data types used in C++ #include int main() { int a; long b; short c; float d; double e; bool f; char g; cout<<"int size = " <<sizeof(a)<<endl; cout<<"long size = " <<sizeof(b)<<endl; cout<<"short size = " <<sizeof(c)<<endl; cout<<"float size = "<<sizeof(d)<<endl; cout<<"double size = "<<sizeof(e)<<endl; cout<<"bool size = "<<sizeof(f)<<endl; cout<<"char size = "<<sizeof(g)<<endl; return 0; } Output: int size = 4 long size = 4 short size = 2 float size = 4 double size = 8 bool size = 1 char size = 1