1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes.

Slides:



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

C Language.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
Introduction to C Programming
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 ICS103 Programming in C Lecture 10: Functions II.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
C Arrays. One-Dimensional Arrays Proper ways of declare an array in C: int hours[ 24]; double Temp [24]; int test_score [15] = { 77, 88, 99, 100, 87,
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures as Functions.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Prepared by MMD, Edited by MSY1 CHAPTER 4 ARRAY. Prepared by MMD, Edited by MSY2 Arrays  Introduction to arrays  Declaring arrays  Initializing arrays.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
1. 2 Introduction Structure Definitions and Declarations Initializing Structures Operations on Structures Members Structures as Functions Parameters Array.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
User-Written Functions
Computer Science 210 Computer Organization
CS1010 Programming Methodology
ICS103 Programming in C Lecture 10: Functions II
Module 4 Functions – function definition and function prototype.
Arrays in C.
Pointers.
Computer Science 210 Computer Organization
Programming in C Pointer Basics.
ICS103 Programming in C Lecture 10: Functions II
ICS103 Programming in C Lecture 10: Functions II
Files Chapter 8.
Presentation transcript:

1 Structures Etter Chapter 7 In engineering, we need to process or visualize data –Some of you have done Matlab simulations and visualizations Sometimes we work with a single piece of data –E.g. the temperature reading at this point in time. But usually we’d work with a large set of data. –E.g. the temperature reading every minute for the last 8 hours. –This works out to 480 pieces of data.

2 Structures Pages 327 to 332 In Lecture 8, we solved this problem by using arrays. BUT arrays can only store data of a single type! –E.g. you can’t store strings in this array: double mydata[1024];

3 Structures Fortunately C gives us a way of representing related information of different types, in a “structure.” E.g. hurricanes have names, intensities and categories. –Name: This is a string. –Intensity: Integer. –Category: Intger.

4 Structures struct hurricane { char name[10]; int year, category; }; “name”, “year” and “category” are called “members” of the structure “hurricane” This statement declares a new type! –IT DOES NOT DECLARE A NEW VARIABLE! This “;” is very important and often forgotten!

5 Self Help Questions 1.What are the differences between a “variable” and a “type”?

6 Answer 1.A “type” tells C what the variable is. E.g. “int” is a type. “double” is a type. After declaring our structure “hurricane”, “struct hurricane” is also a type! Types do not occupy memory and cannot store values. To declare a variable, we always use “type varname;”. So to declare an integer variable, we use “int xl;”. This tells C that “x1” is a variable of type “int”. Variables occupy memory and can be assigned values.

7 Structure Variables To declare a new variable of the “hurricane” type, we do: struct hurricane h1; ? ? ? h1 name year category

8 Structure Variables You can declare multiple variables of this type: struct hurricane h1, my_hurricane; ? ? ? h1 name year category ? ? ? my_hurricane name year category

9 Structure Variables You can also initialize the members of a structure variable. struct hurricane h1 = {“Camille”, 1969, 5}; Camille h1 name year category

10 Structure Variables Alternatively you can initialize the members using C statements: struct hurricane h1; h1.name = “Camille”; h1.year = 1969; h1.category = 5; Notice how we used the “.” operator to access members of a structure variable!

11 Structure Example We can use scanf or fscanf statements to write to structure variable members. We can use printf or fprintf to read them. We will now write a program that reads hurricane information from a file, and prints them out.

12 Structure Example #include #define FILENAME "storms2.txt" struct hurricane { char name[10]; int year, category; };

13 Structure Example int main(void) { /* Declare variables */ struct hurricane h1; FILE *storms; storms = fopen(FILENAME, "r"); if(storms == NULL) printf("Error opening data file.\n");

14 Structure Example else { while(fscanf(storms, "%s %d %d", h1.name, &h1.year, &h1.category) == 3) { printf("Hurricane: %s\n", h1.name); printf("Year: %d, Category: %d\n", h1.year, h1.category); } fclose(storms); } /* else */ return 0; }

15 Self Help Questions 1.In the fscanf statement: fscanf(storms, "%s %d %d", h1.name, &h1.year, &h1.category) - h1.year and h1.category are both passed to fscanf with “&”, but not h1.name. Why?

16 ANSWERS The “name” member of “hurricane” is defined as char name[10]; struct hurricane { char name[10]; int year, category; };

17 ANSWERS This makes “name” an array of 10 characters. In an array: –The name of the array is a pointer to the first item of the array. “fscanf” expects pointers to the variable arguments. E.g.: fscanf(fptr, “%d”, &a); –This is so that fscanf can return the values read using these variables. –So we use “&” to get the address of the variable.

18 ANSWERS Since “name” is already a pointer to the first item of the “name” array, there is no need to use “&”. We can just pass in “name” instead of “&name”. –Further question: What does &name give you?

19 Computations with Structures We use the “.” operator to access individual members of a structure variable: h1.name=“Hazel”; If we use the structure variable’s name without the “.” operator, then we are accessing the entire array.

20 Computations with Structures You can do assignments with structures: –Given two structures “h1” and “h2”. To make h2 equal to h1, just do: h2 = h1;

21 Computation with Structures E.g. struct hurricane h1 ={“Hazel”, 1954, 4}, h2; ……… h2 = h1; Hazel h1 name year category ? ? ? h2 name year category Initially:

22 Computation with Structures E.g. struct hurricane h1 ={“Hazel”, 1954, 4}, h2; ……… h2 = h1; Hazel h1 name year category Hazel h2 name year category After h2=h1:

23 Computation with Structures Other operations like “+”, “-”, “*”, “/”, “>”, “ =“, “==“ etc. can only be performed on individual structure variable members. E.g. h1.category += 1;/* Increment */

24 Computation Example We will modify our hurricane program to print only the Category 5 hurricanes. –Can just do this by: if(h1.category == 5) /* Print hurricane data */

25 Computation Example #include #define FILENAME "storms2.txt" struct hurricane { char name[10]; int year, category; };

26 Computation Example int main(void) { struct hurricane h1; FILE *storms; storms = fopen(FILENAME, "r"); if(storms == NULL) printf("Error opening data file.\n");

27 Computation Example else { printf("Category 5 hurricanes\n\n"); while(fscanf(storms, "%s %d %d", h1.name, &h1.year, &h1.category) == 3) if(h1.category == 5) printf("%s\n", h1.name); fclose(storms); } /* else */ }

28 Passing Structures to Functions Pages 332 to 334 Entire structures can be passed into functions. –Structures are passed by value: Members of the actual arguments are copied into corresponding members of the formal parameters.

29 Passing Structures to Functions #include struct hurricane { char name[10]; int year, category; }; void print_struct(struct hurricane my_h) { printf("INSIDE print_struct function\n"); printf("Contents of parameter my_h: %s, %d, %d\n", my_h.name, my_h.year, my_h.category); printf("Address of parameter my_h: %u\n\n", &my_h); } Formal parameter declaration for a structure.

30 Passing Structures to Functions int main(void) { struct hurricane h1 = {"Hazel", 1954, 4}; printf("\nContents of h1: %s, %d, %d\n", h1.name, h1.year, h1.category); printf("Address of h1: %u\n", &h1); printf("Passing h1 in as argument to print_struct\n\n"); print_struct(h1); } Declaration for struct variable h1. Structure variable is passed in just like any other variable.

31 Passing Structures by Reference. Ordinarily, structures are passed by value. –This means that you cannot modify the members of a structure from within a function. To be able to modify structure members, you must pass in a pointer to the argument, rather than the argument itself.

32 Passing Structures by Reference Self Help Question If: –int *ptr declares a pointer to int. –double *ptr declares a pointer to a double. –char *ptr declares a pointer to a char. How do you: –Declare a pointer to a struct hurricane ?

33 Passing Structures by Reference #include /* This program has a function that modifies a structure passed to it */ struct hurricane { char name[10]; int year, category; };

34 Passing Structures by Reference void modify_structure(struct hurricane *hur) { /* Modify the structure values */ strncpy(hur->name, "Grace", 10); hur->year=1972; hur->category = 5; } Pointer to a struct hurricane. When accessing structure members from a pointer, use “ -> ” instead of “.”.

35 Passing Structures by Reference int main(void) { struct hurricane h1={"Audrey", 1957, 4}; printf("Contents of h1: %s %d %d\n", h1.name, h1.year, h1.category); printf("\nCalling modify_structure to change the contents of h1.\n\n"); modify_structure(&h1); printf("Contents of h1: %s %d %d\n\n", h1.name, h1.year, h1.category); } Pass in the address of h1, instead of h1 itself.

36 Passing Structures by Reference One point to note: –When accessing members of a structure variable, we use the “.” operator. –E.g. struct hurricane my_hurricane; my_hurricane.year=1852;

37 Passing Structures by Reference One point to note: –When accessing members of a pointer to a structure variable, we use the “ -> ” operator. –E.g. struct hurricane *ptr = &my_hurricane; ptr->year=1952; –“ ptr->year ” is a shortcut for (*ptr).year ;

38 Returning Structures from Functions. A function can return whole structures. To declare a function that returns an int, we do: int myfun1() { … } To declare a function that returns a char, we do: char myfun2() { … }

39 Returning Structures from Functions. To call these functions: char c; int num; num = myfun1(); c = myfun2();

40 Returning Structures from Functions. Likewise, to declare a function that returns struct hurricane: struct hurrican myfun3(…) { … } To call myfun3: struct hurricane my_h; my_h = myfun3();

41 Example #include struct tsunami { int mo, da, yr, fatalities; double max_height; char location[20]; }; struct tsunami get_info(void);

42 Example struct tsunami get_info(void) { /* Declare variables */ struct tsunami t1; printf("Enter information for tsunami in the following order:\n"); printf("Enter month, day, year, number of deaths:\n"); scanf("%d %d %d %d", &t1.mo, &t1.da, &t1.yr, &t1.fatalities); printf("Enter location (<20 characters.):\n"); scanf("%s", t1.location); return t1; } t1 will temporarily store what the user keys in. t1 is returned here.

43 Example int main(void) { struct tsunami my_t; my_t = get_info(); printf("\nThe information you entered:\n\n"); printf("Date (day/month/year): %d/%d/%d\n", my_t.da, my_t.mo, my_t.yr); printf("Location: %s\n", my_t.location); printf("Fatalities: %d\n\n", my_t.fatalities); return 0; } We declare a variable of type struct tsunami, the exact same type as the function get_info. We call get_info just like any other function.

44 Arrays of Structures (Pages 334 to 336) Refresher: –To declare an array of 20 ints: int my_array1[20]; –To declare an array of 20 doubles: double my_array2[20]; –To declare an array of 20 chars: char my_array3[20]; Question: How do we declare an array of “struct hurricane”?

45 Array of Structures Assuming we have already declared “struct hurricane”, do declare an array of 20 “struct hurricane”: struct hurricane my_array4[20];

46 Array of Structures This creates 20 “struct hurricane” variables, each with: –name –year –category All values are un- initialized. ??? ??? ??? ??? ……… ??? nameyearcategory

47 Array of Structures Refresher: –To access individual ints in my_array1: myarray1[2] = 5; –To access individual doubles in my_array2: myarray2[5] = 3.1; –To access individual chars in my_array3: myarray3[2] = ‘c’; So: –How to we access individual struct variables?

48 Array of Structures Answer: my_array4[0].name = “Camille”; my_array4[0].year = 1969; my_array4[0].category = 5;

49 Example /* Program ch7_4.c */ #include #define FILENAME "storms2.txt" struct hurricane { char name[10]; int year, category; }; void print_hurricane(struct hurricane h);

50 Example int main(void) { int max_category=0, k=0, npts; struct hurricane h[100]; FILE *storms; storms = fopen(FILENAME, "r"); if(storms == NULL) printf("Error opening data file.\n"); else { printf("Hurricanes with Maximum Category:\n");

51 Example while(fscanf(storms, "%s %d %d", h[k].name, &h[k].year, &h[k].category) == 3) { if(h[k].category > max_category) max_category = h[k].category; k++; } /* while */ npts = k; for(k=0; k<npts; k++) if(h[k].category == max_category) print_hurricane(h[k]); fclose(storms); } /* else */ }

52 Example void print_hurricane(struct hurricane h) { printf("Hurricane: %s\n", h.name); printf("Year: %d, Category: %d\n", h.year, h.category); }

53 Case Study (Pages 336 to 340) Tsunamis are large destructive waves: –Caused by sudden movements in the sea floor. Wave speeds: –Shallow water: 125 mph. –Deep water: 400 mph. Heights: –30 to 200+ feet high.

54 Case Study Problem Statement: Print a report giving: –Maximum wave height for tsunamis in a data file. –Average wave height. –Location of all tsunamis with heights higher than the average.

55 Case Study Analysis: –Open file “waves2.txt” Tell the user if the file open fails. –Find largest maximum height. Set max to 0 first. Assume the tsunami array is called t: –If t[k].max_height > max, set max = t[k].max_height. –Find average Add t[k] to sum. Average = sum / # of data points

56 Case Study –Print locations with wave heights larger than the average: if t[k].max_height > average, print t[k].location. Note: Height data in file is in meters. Report is in feet: –1 m = 3.28 feet. –z m = z * 3.28 feet.

57 Case Study /* ch7_5.c */ #include #define FILENAME "waves1.txt" struct tsunami { int mo, da, yr, fatalities; double max_height; char location[20]; };

58 Case Study int main(void) { /* Declare variables */ int k=0, npts; double max=0, sum=0, ave; struct tsunami t[100]; FILE *waves; waves = fopen(FILENAME, "r"); if(waves == NULL) printf("Error opening data file.\n");

59 Case Study else { while(fscanf(waves, "%d %d %d %d %lf %s", &t[k].mo, &t[k].da, &t[k].yr,&t[k].fatalities, &t[k].max_height, t[k].location) == 6) { sum += t[k].max_height; if(t[k].max_height > max) max = t[k].max_height; k++; } /* while */

60 Case Study npts = k; ave = sum/npts; printf("Summary Information for Tsunamis\n"); printf("Maximum wave height (in feet): %.2f\n", max * 3.28); printf("Average wave height (in feet): %.2f\n", ave * 3.28);

61 Case Study printf("Tsunamis with greater than average heights:\n\n"); for(k=0; k<npts; k++) if(t[k].max_height > ave) printf("%s\n", t[k].location); fclose(waves); } /* else */ }

62 Summary In this lecture we learnt: –How to aggregate data together in structures. –How to pass structures to functions. –How to return structures in functions. –How to declare arrays of structures.