Download presentation
Presentation is loading. Please wait.
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
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 7 22 f1 fraction * numerator denominator
6
int i; Course courses[10]; for (i=0; i<10; i++){ //______________? } Course* coursePtr; coursePtr=new Course; //_________________? cout<<___________________<<endl; return 0; } struct Location{ char *address; int zipcode; }; struct Course{ char title[20]; int num_students; Location loc; }; How to set num_students to some value in both cases?
7
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 num_students<<endl; return 0; } struct Location{ char *address; int zipcode; }; struct Course{ char title[20]; int num_students; Location loc; };
8
struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr address; float wage; } ; emp worker; Structures within Structures How to set zip code to 12345?
9
struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr address; float wage; } worker; worker.address.zip = 12345; Structures within Structures
10
struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr *address; float wage; }; Structures within Structures How to set zip code to 12345? emp worker;
11
struct addr { char name[40]; char street[40]; char city[40]; int zip; }; struct emp { addr *address; float wage; }; Structures within Structures int main(void){ emp worker; worker.address=new addr; worker.address->zip=12345; cout 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 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; };
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
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; }; 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;
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 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
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]; Structures (Inventory Example) 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
// 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'; } Structures (Inventory Example)
24
// 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); } Structures (Inventory Example) #include strchr locates character in string
25
// 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); } Structures (Inventory Example)
26
// 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; } Structures (Inventory Example)
27
// 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); } Structures (Inventory Example)
28
// 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"; } Structures (Inventory Example)
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... /*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; second->next = third; third->data = 3; third->next = NULL; return head; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.