> 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)"> > 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)">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 STRACT IN C++

3

4

5

6

7

8 // 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)

9

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

11

12

13

14 // 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; }

15 // 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; // 00000000 ???????? unsigned short foreground : 3; // 00000??? 00000000 unsigned short intensity : 1; // 0000?000 00000000 unsigned short background : 3; // 0???0000 00000000 unsigned short blink : 1; // ?0000000 00000000 } screen[25][80]; // Array of bit fields

16 // 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 = 1234567; printf_s("%d\n", Jim.number); }

17 #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; }

18 #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

19 #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;

20 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 : 37.7778 Press any key to continue

21 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

22 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

23 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

24 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


Download ppt "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."

Similar presentations


Ads by Google