Structures of structures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

STRUCTURES OF STRUCTURES PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on
  • Presentation posted in: General

STRUCTURES OF STRUCTURES. Group 7 -Jorge Alejandro Chacon -Assiel Conde -Daniel Granda -Christian Tennant. -How structures work and what they are. -Their purpose in a program. How structures work and what they are.

Download Presentation

STRUCTURES OF 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Structures of structures

STRUCTURES OF STRUCTURES

Group 7

-Jorge Alejandro Chacon

-Assiel Conde

-Daniel Granda

-Christian Tennant

-How structures work and what they are.

-Their purpose in a program.


How structures work and what they are

How structures work and what they are.

  • The role of a structure in C programing is to group variables of different types into a single name.

  • A structure can be defined as a new defined type, that way we can extend the types available to us.

  • A structure can use other structures, arrays and pointers as members of itself.

  • When a structure is defined within a structure, the structure inside is called a nested structure. This is the technique we will be using.


Calculating the area of a rectangle program using structures

Calculating the area of a rectangle program using structures

  • First imagine a rectangle located in the first quadrant of a 2D plane.

  • We will only plot 2 points that will calculate the length and width.


Purpose of the structures in our program

Purpose of the structures in our program

  • Using a structure we group the variables “x and y” which will be our coordinates to be used.

  • Using a nested structure we point the location where the (x,y) points input in the first structure will be plotted in our rectangle. In this case we pick the top left and bottom right.

  • Then we calculate the width and the length which later can be use to calculate the are by A= Length*width.


Bubble diagram

Bubble Diagram

Gathers x and y coordinates

Computes area

Creates structure, calling x and y as integers

Call needed libraries

Calls Main()

Displays the area of the rectangle using given coordinates


Block diagram

Include Libraries

Calculate and print area

area = width * length;

printf("The area is %ld units.", area);

Block Diagram

Define Variables

int length, width

long area

Calculate length and width

width = mybox->bottomrt->x - mybox->topleft->x; length = mybox->bottomrt->y - mybox->topleft->y;

Define structures for coord and rectangle

structcoord{ int x; int y; }; struct rectangle{ structcoord *topleft; structcoord *bottomrt;

Store inputs for top left x and y and bottom right x and y.

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

Start main function


Highlighted parts in our code

Highlighted parts in our code

  • In blue we define our variables and function.

  • In orange we create our first structure “coord” and we group the members “ integers x,y”. Also, we use a nested structure of “struct coord” into “struct rectangle” to point where our points (x,y) will be plotted.

  • In red we gather the information input by the user and we use pointer notation (->)to indicate where the information will be stored.


Highlighted parts in our code1

Highlighted parts in our code

In blue we define our function to calculate the width and length.

In red we define our function to calculate the area and print the result.


Sample output

Sample Output


Structures of structures

From our last example, we can gather that structures can easily complicate simple tasks.

The example could have been drastically simplified in 16 lines vs. a whopping 46, achieving the same exact result.


Simpler technique

Simpler Technique

#include <stdio.h>

#include <stdlib.h>

int main()

{

int top_left_x, top_left_y, bottom_right_x, bottom_right_y;

printf("Enter top left X coordinate value: ");

scanf("%d", &top_left_x);

printf("Enter top left Y coordinate value: ");

scanf("%d", &top_left_y);

printf("Enter bottom right X coordinate value: ");

scanf("%d", &bottom_right_x);

printf("Enter bottom Y coordinate value: ");

scanf("%d", &bottom_right_y);

int length = bottom_right_x - top_left_x;

int width = top_left_y - bottom_right_y;

int area = length * width;

printf("The total area of the rectangle is: %d\n", area);

return0;

}


Sample output1

Sample Output


Second example

Second Example

One appropriate use for structures is databases. They provide an effective way to gather related information, for ex.: student data for financial aid.

Our next example gathers data from a student via nested structures.

The student will input in the data field the following: name, panther id, gender, etc. Through the use of functions, our program will display all the collected information


Bubble diagram1

Call needed libraries

Bubble Diagram

Creates structure, calling variables for employee information

Calls Main()

Displays stored employee information

Gathers Employee Information

Stores information


Block diagram1

Include Libraries

Block Diagram

Define MAX

#define MAX 1000

Display input

Display(student, n)

Define structures for date and school

structdate{intday; int month; int year; };

struct school{ char name[20]; long intpanther_id; char sex[5]; int age; structdate dob;};

Store input

CollectInformation(employee, n);

Define CollectInformation and Display functions

void CollectInformation(school student[MAX], int n)

void Display(school student[MAX], int n)

Start main function


Second example1

Second Example

struct date{

// members of structure definition

int day;

int month;

int year;

};

struct school{

char name[20];

long int panther_id;

char sex[10];

int age;

// nested block definition

struct date dob;

};

  • Here we define our structures.

  • The structure date will include the members of type int day, month and year.

  • In the structure school we’ll include the char array name and will declare it to be 20 characters long. A longint for the panther ID, an int for the age and a third structure for the date of birth.


Highlighted parts in our code2

Highlighted parts in our code

  • Next, we’ll declare the object inside our structure student to have a predefined MAX amount of characters (#define MAX 1000).

  • An integer n is declared to gather the number of students entered. Also we create functions with the purpose of collecting and displaying the data from the user.

  • The function CollectInformation gathers the input, stores it in the char array student and Display will reflect contents.

// structure object definition

school student[MAX];

// main function starts

int main(){

school student[MAX];

int n;

cout << "A program that collects student information";

cout << endl;

cout << "and displays the collected information";

cout << endl << endl;

cout << "How many students? : ";

cin >> n;

cout << endl;

// functions definition

void CollectInformation(school student[MAX], int n);

void Display(school student[MAX], int n);

// functions calling

CollectInformation(student, n);

Display(student, n);

cin.get();

return 0;

}


Highlighted parts in our code3

Highlighted parts in our code

// collecting information form the user

void CollectInformation(school student[MAX], int n){

cout << "Enter the following information:";

cout << endl << endl;

for (int i=1; i<=n; i++){

cout << "Enter information for student no. : " << i;

cout << endl;

cout << "\nName : "; cin >> student[i].name;

cout << "PID : "; cin >> student[i].panther_id;

cout << "Sex : "; cin >> student[i].sex;

cout << "Age : "; cin >> student[i].age;

cout << "Date of Birth :" << endl;

cout << "Day : "; cin >> student[i].dob.day;

cout << "Month : "; cin >> student[i].dob.month;

cout << "Year : "; cin >> student[i].dob.year;

cout << endl;

}

}

  • An integer i is looped for as many times as the amount of students entered.

  • Each parameter information is stored in the student array declared before with the information specified (i.e.: age, gender, etc.).


Highlighted parts in our code4

Highlighted parts in our code

void Display(school student[MAX], int n){

cout << "Student entered information : ";

cout << endl;

cout << "\nName PID Sex Age Date of Birth" << endl;

for (int i=1; i<=n; i++){

cout << student[i].name << "\t";

cout << student[i].panther_id << "\t";

cout << student[i].sex; cout << "\t";

cout << student[i].age; cout << "\t";

cout << student[i].dob.day << ".";

cout << student[i].dob.month << ".";

cout << student[i].dob.year ;

cout << endl;

}

}

  • For the display function, all the information gathered before will be printed on the screen with the aforementioned parameters (i.e.: sex, age, etc.)


Sample output2

Sample Output


Lessons learned

Lessons Learned

  • Nested structures can simplify complex code when in need to collect several pieces information that are related to each other.

  • One needs to be careful when creating structures since it can easily clump the main code and make it unrecognizable.

  • Structures have good usage in storing data, such as data in a database since different data types can be stored in them such as int, char, long, etc.


  • Login