1 / 36

Saturday, February 3, 2007

Saturday, February 3, 2007. “Programmer - an organism that turns coffee into software.” - Anonymous. struct for representing roll_no , name and marks for student. Pointers to arrays of structures struct Rect{ int x; int y; int width; int height; }; int main(void){ int size;

base
Télécharger la présentation

Saturday, February 3, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Saturday, February 3, 2007 “Programmer - an organism that turns coffee into software.” - Anonymous

  2. struct for representing roll_no, name and marks for student

  3. Pointers to arrays of structures struct Rect{ int x; int y; int width; int height; }; int main(void){ int size; cin>>size; Rect* rectArray=new Rect[size]; rectArray[0].x=10; rectArray[0].y=20; rectArray[0].width=50; rectArray[0].height=100; // and so-on return 0; }

  4. const int size=5; Rect rect[size]={ {10,10, 20,30}, {15,15, 25,40}, {8, 10, 80, 50}, {60, 0, 50, 50}, {0, 20, 60, 10} }; int i; for (i=0; i<size; i++){ cout<<rect[i].x<<" "<<rect[i].y<<" " <<rect[i].width<<" "<<rect[i].height<<endl; }

  5. numerator 7 f1 22 denominator fraction * struct fraction { int numerator; int denominator; }; // Don't forget the semicolon! fraction* f1=new fraction; Expression Type f1 fraction* *f1 fraction (*f1).numerator int //parenthesis important f1->numerator int

  6. struct Location{ char *address; int zipcode; }; struct Course{ char title[20]; int num_students; Location loc; }; int i; Course courses[10]; for (i=0; i<10; i++){ //______________? } Course* coursePtr; coursePtr=new Course; //_________________? cout<<___________________<<endl; return 0; } How to set num_students to some value in both cases?

  7. struct Location{ char *address; int zipcode; }; struct Course{ char title[20]; int num_students; Location loc; }; int i; Course courses[10]; for (i=0; i<10; i++){ courses[i].num_students=60; } Course* coursePtr; coursePtr=new Course; coursePtr->num_students=35; cout<<coursePtr->num_students<<endl; return 0; }

  8. Structures within Structures struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr address; float wage; } ; emp worker; How to set zip code to 12345?

  9. Structures within Structures struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr address; float wage; } worker; worker.address.zip = 12345;

  10. Structures within Structures struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr *address; float wage; }; emp worker; How to set zip code to 12345?

  11. Structures within Structures struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr *address; float wage; }; int main(void){ emp worker; worker.address=new addr; worker.address->zip=12345; cout<<worker.address->zip; return 0; }

  12. What if we had declared a pointer to emp? emp * worker;

  13. struct Location{ char *address; int zipcode; }; struct Course{ char title[20]; int num_students; Location loc; }; SELF TEST: Fill out all members of the nested struct with values

  14. int main(){ Course cs192; strcpy(cs192.title, "Problem Solving"); cs192.num_students=65; cs192.loc.zipcode=54890; cs192.loc.address = new char[20]; strcpy(cs192.loc.address, "LUMS"); cout<<cs192.loc.address<<endl; ... } SELF TEST

  15. struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; }; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan fan; };

  16. Turbine steamTurbine; steamTurbine.location=new char[20]; strcpy(steamTurbine.location, "Tarbela Dam"); steamTurbine.rating=15000; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan fan; }; struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; };

  17. Turbine steamTurbine; steamTurbine.location=new char[20]; strcpy(steamTurbine.location, "Tarbela Dam"); steamTurbine.rating=15000; steamTurbine.motor.manufacturer=new char[20]; strcpy(steamTurbine.motor.manufacturer, "Mitsubishi"); steamTurbine.motor.num_phases=3; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan fan; }; struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; };

  18. Turbine steamTurbine; steamTurbine.location=new char[20]; strcpy(steamTurbine.location, "Tarbela Dam"); steamTurbine.rating=15000; steamTurbine.motor.manufacturer=new char[20]; strcpy(steamTurbine.motor.manufacturer, "Mitsubishi"); steamTurbine.motor.num_phases=3; steamTurbine.motor.fan.blade_length=100; steamTurbine.motor.fan.temp_rating=300; //etc... cout<<steamTurbine.motor.manufacturer<<endl; cout<<steamTurbine.motor.fan.temp_rating<<endl; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan fan; }; struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; };

  19. What should change here? Turbine steamTurbine; steamTurbine.location=new char[20]; strcpy(steamTurbine.location, "Tarbela Dam"); steamTurbine.rating=15000; steamTurbine.motor.manufacturer=new char[20]; strcpy(steamTurbine.motor.manufacturer, "Mitsubishi"); steamTurbine.motor.num_phases=3; steamTurbine.motor.fan.blade_length=100; steamTurbine.motor.fan.temp_rating=300; //etc... cout<<steamTurbine.motor.manufacturer<<endl; cout<<steamTurbine.motor.fan.temp_rating<<endl; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan *fan; }; struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; };

  20. Turbine steamTurbine; steamTurbine.location=new char[20]; strcpy(steamTurbine.location, "Tarbela Dam"); steamTurbine.rating=15000; steamTurbine.motor.manufacturer=new char[20]; strcpy(steamTurbine.motor.manufacturer, "Mitsubishi"); steamTurbine.motor.num_phases=3; steamTurbine.motor.fan = new Fan; steamTurbine.motor.fan->blade_length=100; steamTurbine.motor.fan->temp_rating=300; //etc... cout<<steamTurbine.motor.manufacturer<<endl; cout<<steamTurbine.motor.fan->temp_rating<<endl; struct Fan{ char *manufacturer; double blade_length; double temp_rating; double pressure_rating; }; struct Motor{ char * manufacturer; double watts; double speed; int num_phases; Fan *fan; }; struct Turbine{ char *location; long rating; int num_cylinders; double weight; Motor motor; };

  21. Structures (Inventory Example) const int SIZE = 100; struct inv_type { char item[40]; // name of item double cost; // cost double retail; // retail price int on_hand; // amount on hand int lead_time; // number of days before resupply }; inv_type invtry[SIZE]; READ THE BOOK!

  22. int main() { char choice; init_list(); for(;;) { choice = menu(); switch(choice) { case 'e': enter(); break; case 'd': display(); break; case 'u': update(); break; case 'q': return 0; } } }

  23. Structures (Inventory Example) // Initialize the inv_type_info array. void init_list() { int t; // a zero length name signifies empty for(t=0; t<SIZE; t++) *(invtry[t].item) = '\0'; }

  24. Structures (Inventory Example) // Get a menu selection. int menu(){ char ch; cout << '\n'; do { cout << "(E)nter\n"; cout << "(D)isplay\n"; cout << "(U)pdate\n"; cout << "(Q)uit\n\n"; cout << "choose one: "; cin >> ch; } while(!strchr("eduq", tolower(ch))); return tolower(ch); } #include <string.h> strchr locates character in string 

  25. Structures (Inventory Example) // Enter items into the list. void enter(){ int i; // find the first free structure for(i=0; i<SIZE; i++) if(!*(invtry[i].item)) break; // i will equal SIZE if the list is full if(i==SIZE) { cout << "List full.\n"; return; } input(i); }

  26. Structures (Inventory Example) // Input the information. void input(int i){ char str[80]; // enter the information cout << "Item: "; cin >> invtry[i].item; cout << "Cost: "; cin >> invtry[i].cost; cout << "Retail price: "; cin >> invtry[i].retail; cout << "On hand: "; cin >> invtry[i].on_hand; cout << "Lead time to resupply (in days): "; cin >> invtry[i].lead_time; }

  27. Structures (Inventory Example) // Modify an existing item. void update(){ int i; char name[80]; cout << "Enter item: "; cin >> name; for(i=0; i<SIZE; i++) if(!strcmp(name, invtry[i].item)) break; if(i==SIZE) { cout << "Item not found.\n"; return; } cout << "Enter new information.\n"; input(i); }

  28. Structures (Inventory Example) // Display the list. void display(){ int t; for(t=0; t<SIZE; t++) { if(*(invtry[t].item)) { cout << invtry[t].item << '\n'; cout << "Cost: $" << invtry[t].cost; cout << "\nRetail: $"; cout << invtry[t].retail << '\n'; cout << "On hand: "<< invtry[t].on_hand; cout << "\nResupply time: "; cout<<invtry[t].lead_time<< " days\n\n"; } } }

  29. Arrays VS. linked lists? int scores[100]; The disadvantages of arrays are... • The size of the array is fixed — 100 elements in this case. Most often this size is specified at compile time with a simple declaration such as in the example above. • With a little extra effort, the size of the array can be deferred until the array is created at runtime, but after that it remains fixed.

  30. Arrays VS. linked lists? • Because of this, the most convenient thing for programmers to do is to allocate arrays which seem "large enough" (e.g. the 100 in the scores example) • Although convenient, this strategy has two disadvantages: (a) most of the time there are just 20 or 30 elements in the array and 70% of the space in the array really is wasted. (b) If the program ever needs to process more than 100 scores, the code breaks.

  31. Arrays VS. linked lists? • Inserting new elements at the front of array is potentially expensive because existing elements need to be shifted over to make room. • Merging, Deleting ... • Example of queues

  32. LINKED LISTS • Linked lists store collections of data • A linked list is a list constructed using pointers • A linked list is not fixed in size but can grow and shrink while your program is running.

  33. What Linked Lists Look Like? • An array allocates memory for all its elements lumped together as one block of memory. • In contrast, a linked list allocates space for each element separately in its own block of memory called a "linked list element" or "node". • The list gets is overall structure by using pointers to connect all its nodes together like the links in a chain.

  34. What Linked Lists Look Like? • Each node contains two fields: a "data" field to store whatever element type the list holds and a "next" field which is a pointer used to link one node to the next node. • Example … • The Empty List — NULL

  35. Linked Lists struct node { int data; node* next; };

  36. Linked Lists - easy example first . . . second->next = third; third->data = 3; third->next = NULL; return head; } /*Build the list {1, 2, 3}*/ node* BuildOneTwoThree() { node* head = NULL; node* second = NULL; node* third = NULL; head = new node; second = new node; third = new node; head->data = 1; head->next = second; second->data = 2;

More Related