1 / 15

Structures within Structures

Structures within Structures. Group 5 Javeed Ali Javier Oliu James Wrabel Richard Alves Peter Gomez. Intro to Structures. Why would your own structure data type be useful to you? Flexibility Shorter program length/More efficient programming Increased program legibility

sai
Download Presentation

Structures within Structures

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. Structures within Structures Group 5 Javeed Ali Javier Oliu James Wrabel Richard Alves Peter Gomez

  2. Intro to Structures • Why would your own structure data type be useful to you? • Flexibility • Shorter program length/More efficient programming • Increased program legibility • Allows for the combination of several different data types • What is a structure within a structure? • With almost limitless uses a structure within a structure creates a world designed by the programmer that will follow set guidelines and rules. • Some examples: • Billiard balls on a pool table – The balls can only travel within the confines of the table while still maintaining their own individual characteristics, i.e. color, number. • Car on a racetrack – The car can only travel along the track and is subject to different properties within the track, i.e. gravel, grass, water, turns; the car also has properties of its own, i.e. speed, weight, color, etc.

  3. Structured Flow Chart Part 1: We begin our flow chart by declaring our structure types, names, and variables.

  4. Flow Chart Part 2: Using our structure data types we define object ‘mybox’ and create coordinates.

  5. Flow Chart Part 3: Using printf and scanf we prompt the user to input coordinates for ‘mybox’.

  6. Flow Chart Part 4: Finally we declare and define width, length to solve for area.

  7. Outline/Description Block 1: #include <stdio.h> intlength, width; long area; structcoord { int x; inty; }; We start off the program with our standard library function #include <stdio.h> . We then declare local integer variables length , and width as well as variable area of type long to use later in our program in order to find area. We define our first structure named ‘coord’ using a structure declaration with two elements x, y of type int. The semicolon at the end of our structure declaration is necessary in order to compile our structure.

  8. Outline/Description Block 2: structrectangle { structcoord *topleft; structcoord *bottomrt; }; In this block we define our second structure named ‘rectangle’. This structure uses variables ‘topleft’ and ‘bottomrt’ to point to elements x and y in the first structure ‘coord’. The reason behind this is to assign a topleft and bottomrt location to both x and y elements in our first structure ‘coord’. Hence the title, structures within structures. We use the asterik in this structure declaration to declare both topleft and bottomrtas pointers of type structcoord. A semicolon is still necessary for our second structure declaration.

  9. Outline/Description In our main function we start by creating our object ‘mybox’ of type rectangle: rectangle *mybox = new rectangle; We use the newdynamic memory management operator to allocate the exact amount of space needed to create our structure object at execution time. This helps to save on our processing time as well as space. Following our object declaration and definition we do the same for elements topleft and bottomrt: mybox->topleft = new coord; mybox->bottomrt = new coord; After creating and defining elements within our object we use several printf and scanf functions to input the coordinates for our object. In this case we use the ‘->’ operator to point to our intended elements and assign dimensions to our object. Block 3: int main(void) { /*create the object*/ rectangle *mybox = new rectangle; mybox->topleft = new coord; mybox->bottomrt = new coord; /*Input the coordinates */ printf("\nEnter the top left x coordinate: "); scanf("%d", &mybox->topleft->x); printf("\nEnter the top left y coordinate: "); scanf("%d", &mybox->topleft->y); printf("\nEnter the bottom right x coordinate: "); scanf("%d", &mybox->bottomrt->x); printf("\nEnter the bottom right y coordinate: "); scanf("%d", &mybox->bottomrt->y); …..

  10. Outline/Description Block 4: /* Calculate the length and width */ width = mybox->bottomrt->x - mybox->topleft->x; length = mybox->bottomrt->y - mybox->topleft->y; /* Calculate and display the area */ area = width * length; printf("The area is %ld units.", area); return 0; } END PROGRAM After we read-in our values for topleft, bottomrt in x and y we use our dimensions to find the area of our object. Using our previously defined integer variables width and length we subtract our two x components and two y components to find our width and length values. Allowing us to multiply our width and length and find total area. We use a printf function to display our area of type %ld or long and return 0 upon program completion.

  11. Output Window Running our program we entered our top left coordinate as (0,2) and our bottom right as (2,0) finding our area to be equal to 4.

  12. Similar Example int main(void) { name *group = new name; group->james = new grade; group->peter = new grade; printf("\nEnter the test1 grade for James: "); scanf("%d", &group->james->test1); printf("\nEnter the test2 grade for James: "); scanf("%d", &group->james->test2); printf("\nEnter the test3 grade for James: "); scanf("%d", &group->james->test3); printf("\nEnter the test4 grade for James: "); scanf("%d", &group->james->test4); printf("\n---------------------------------------\n"); printf("\nEnter the test1 grade for Peter: "); scanf("%d", &group->peter->test1); printf("\nEnter the test2 grade for Peter: "); scanf("%d", &group->peter->test2); printf("\nEnter the test3 grade for Peter: "); scanf("%d", &group->peter->test3); printf("\nEnter the test4 grade for Peter: "); scanf("%d", &group->peter->test4); expectedGradeJ = (group->james->test1 + group->james->test2 + group->james->test3 + group->james->test4) / 4; expectedGradeP = (group->peter->test1 + group->peter->test2 + group->peter->test3 + group->james->test4) / 4; printf("\n---------------------------------------\n"); printf("\nThe expected average grade for James is: %d", expectedGradeJ); printf("\nThe expected average grade for Peter is: %d", expectedGradeP); return 0; } #include <stdio.h> intexpectedGradeJ, expectedGradeP; struct grade { int test1; int test2; int test3; int test4; }; struct name { struct grade *james; struct grade *peter; }; …..

  13. Example Output This example output shows our program which reads the first 4 test grades for James and Peter and averages the total.

  14. Lessons Learned Some key points about structures: • Structure - a data type that aggregates a fixed set of labeled objects, possibly of different types, into a single object. • Structures are used to join simple variables like integers, floating points, arrays, etc. into complex structures. A structure type also allots sub categories for extra information i.e. eye color, height, gender. • Structures can contain many different elements within it. • Structures can be created inside of other structures to help make programming simpler. • After declaring a structure you have to place a semicolon at the end in order for it to compile. • You can define a function inside a structure which is called a member function. • Defining a structure within a structure or a nested structure that can be used to shorten program length.

  15. Questions?

More Related