Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Introduction to C Programming
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Structures Spring 2013Programming and Data Structure1.
C Pointers Systems Programming. Systems Programming: Pointers 2 Systems Programming: 2 PointersPointers  Pointers and Addresses  Pointers  Using Pointers.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Learners Support Publications Classes and Objects.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Structures Combining data types into a logical groupings.
1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Programming Fundamentals1 Chapter 6 FUNCTIONS AND POINTERS.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Programming Fundamentals Enumerations and Functions.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
User-Written Functions
CS1010 Programming Methodology
Structures.
Functions and Structured Programming
EPSII 59:006 Spring 2004.
Buy book Online -
Scope, Parameter Passing, Storage Specifiers
Functions I Creating a programming with small logical units of code.
Classes and Objects.
Functions I Creating a programming with small logical units of code.
CPS125.
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures

Structure Basics A structure is a collection of data values, called data members, that form a single unit. But the individual parts of the data do not have to be the same type. Structures are often called aggregate data type, because they allow multiple data values to be collected into a single data type. Data members are individual data values within a structure. A structure provides a means of grouping variables under a single name for easier handling and identification. Unlike arrays, the data members can be of different types.

Structure Definition struct name { variable declaration;. }; the keyword struct announces that this is a structure definition

Continue.. struct hurricane structure name/structure tag { char name[10]; int year, category; }; After a structure has been defined, structure variables can be defined using declaration statements. Members can be declared to be of any valid C data type the tag node may now be used just like predefined types int, char, etc Data members

Declaring Structure Variables The statements to define structure appear before the main function They are often stored in the header file. Define a variable of this structure by the following statement struct hurricane h1; ? ? ? h1 name year category

Example struct node { int data; node *link; }; the above defines a structure type the name of the structure type (node) is called the structure tag the identifiers declared inside the braces are called member names. Members can be declared to be of any valid C data type the tag node may now be used just like predefined types int, char, etc

Declaring Structure Variables struct node n1; struct node *head; The structure n1 is a collection of smaller variables called member variables. The member variables are accessed using the dot (.) operator; head =&n1; n1.data = 2; n1.link = NULL; 2

Initializing Structures Using a declaration statements: struct hurricane h1= {“Camille”,1969,5}; “Camille” h1 name year category The declaration statement allocates memory for the three data members, with their initial values.

Another way to initialize.. Using program statements: h1.name = “Camille”; h1.year = 1969; h1.category = 5;

Initializing Structures struct Rect { double x; double y; double width; double height; }; struct Rect r1 = {0,0,5,10};

Practice! Write the C statements necessary to output the value of the structure r1 defined in the previous slide.

Input and Output We can use scanf and fscanf statements to read values into the data members of a structure. printf and fprintf to print the values. The structure member operator must be used to specify an individual data member Example: –printf(“Hurricane: %s”,h1.name); –printf(“Year: %d, Category: %d”,h1.year,h1.category)

Example:code #include /* Define structure to represent a hurricane */ struct hurricane { char name[10]; int year, category; }; int main (void) { /* Declare variables */ struct hurricane h1; /* Initialization */ //struct hurricane h1 = {"Camille",1969,5}; another way to initialize strcpy(h1.name,"Camille"); //h1.name = "Camille"; h1.year = 1969; h1.category = 5; printf("%s, %d, %d \n", h1.name, h1.year, h1.category); return 0; }

Scope of Structure Member variables are local to the structure. Member names are not known outside the structure.

Assignment operator Assignment operator is defined for structure of the same type. (Compiler looks at tag, not composition.)

Example #include struct Car { char model[10], color[10]; double price; int year; }; int main() { struct Car Car1, Car2; Car1.year = 99; Car1.price = ; strcpy(Car1.color,"red"); strcpy(Car1.model,"Contour"); /* Copy all data from Car1 to Car2. */ Car2 = Car1; /* print the data*/ printf("%d, %lf, %s, %s \n", Car1.year,Car1.price, Car1.color, Car1.model); printf("%d, %lf, %s, %s \n", Car2.year,Car2.price, Car2.color, Car2.model); return 0; }

Arrays of Structures One benefit of having structure type in C is that we can create arrays of structures. With that, we can solve problems much more quickly and easily.

Arrays of Structures Arrays of structures may be declared in the same way as other C data types. struct Car Ford[20]; Ford[0] references first structure of Ford array. Ford[0].model = “Taurus”; Struct Car { char model [10]; };

Example struct hurricane { char name[10]; int year, category; }; …… struct hurricane h[25]; ??? ??? ……… ??? h[0] h[24] …… h[1]

Continue.. To access an individual data members : specify the array name, a subscript, and the data member name. Example: h[0].name = “Camille”; h[0].year = 1969; h[0].category = 5;

Output To print the first array –print_hurricane(h[0]); –output: Hurricane: Camille Year: 1969, Category: 5

Structures as Arguments to Functions Entire structures can be passed as arguments to functions. When a structure is passed as an argument to a function, it is a call-by-value reference. When a function referenced is made, each data member of the actual parameter is passed to the function and is used as the value of the corresponding data member of the formal parameters –Changes made to the formal parameters do not change the argument.

Continue A pointer to a structure may also be passed as an argument to a function. –Changes made to the formal parameters also change the argument.

Call by Value Example struct simple { int ival; double dval; }; int main(void) { void fun1(struct simple s); //function prototype struct simple s1 = {10, 1.5}; //structure variable fun1(s1);//call function fun1 printf(“%i %lf”, s1.ival, s1.dval ); return 0; } void fun1(struct simple s)output?___________ { s.ival++; s.dval++; }

To see the difference… #include struct simple { int ival; double dval; }; int main(void) { void fun1(struct simple s); //function prototype struct simple s1 = {10, 1.5}; //structure variable fun1(s1); //call function fun1 printf("in the main program:\n"); printf("%i %lf\n", s1.ival, s1.dval ); return 0; } void fun1(struct simple s) { s.ival++; s.dval++; printf("within the function:\n"); printf("%i %lf\n",s.ival, s.dval ); }

#include struct employee //define structure name employee { int idnum; double payrate; double hours; }; double calcNet(struct employee); //funct prototype int main() { struct employee emp={6782,8.93,40.5}; double netPay; netPay=calcNet(emp); //pass copy of the values in variable emp of structure employee printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay); return 0; } double calcNet(struct employee temp) //funct header { return (temp.payrate * temp.hours); }

Pointers to Structures When using pointers to access structure members, the arrow operator is used. Example: struct node { int data; struct node *link; }; struct node *nptr, n; nptr = &n; nptr->data = 2;//(*nptr).data nptr->link = NULL;

Pointers to Structures struct node { int data; struct node *link; }; int main(void) { struct node *nptr, n={2, NULL};//declare structure variable void fun1(struct node*);//function prototype nptr = &n;//pointer to structures fun1(nptr);//call function fun1 printf(“%i”, n.data ); return 0; } Output?___________________ void fun1(struct node* n) { n->data++; }

Source Code #include struct data { int aa; double bb; }; void main(void) { struct data engin; struct data *medic; engin.aa=8; engin.bb=12.5; medic=&engin; (*medic).aa = 20; medic->bb =15.7; printf("%d %lf\n",engin.aa,engin.bb); } Structure Definition Declaring a pointer to structure ( a variable that can store the address of the beginning of a data structure) Assigning an address to the pointer Using pointer to access the structure variable’s members. Two methods are shown. Both accomplish the same task. Printing the engin member, using pointer will modify the members of engin

#include struct employee //define structure name employee { int idnum; double payrate; double hours; }; double calcNet(struct employee *temp); //funct prototype int main() { struct employee emp={6782,8.93,40.5}; double netPay; netPay=calcNet(&emp); //pass address of emp of structure employee printf("the net pay of the employee %d is $ %6.2f",emp.idnum,netPay); return 0; } double calcNet(struct employee *temp) //funct header { return (temp->payrate * temp->hours); // or return (((*temp).payrate) * ((*temp).hours)); }