Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
LIST PROCESSING.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
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.
Structures Often we want to be able to manipulate logical entities as a whole For example, complex numbers, dates, student records, etc Each of these must.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Structures Spring 2013Programming and Data Structure1.
Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 11: Structure and Union 1.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Structures. Structures (1) u Structures are C’s way of grouping collections of data into a single manageable unit. –This is also the fundamental element.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
STRUCTURES. Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under.
 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.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Computer Programming for Engineers
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
Programming Principles II Lecture Notes 3.1 Void Functions Andreas Savva.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CS1010 Programming Methodology
CS SUMMER LECTURE 1 by İlker Korkmaz.
Buy book Online -
LINKED LISTS.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
READING AND PRINTING MATRICES (with functions)
Presentation transcript:

Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C

Senem Kumova Metin Structures in C A structure is –a convenient way of grouping several pieces of related information together –a collection of variables under a single name Examples : real number && imaginary number  complex number ( 3+5i ) height && width && length  rectangular prism

Senem Kumova Metin Structures in C The variables in structures can be of different types, and each has a name which is used to select it from the structure Example : ID (integer) && Name (char array)  A Student record A structure is a new named type that may involve several different typed variables

Senem Kumova Metin Defining Structures 1 struct complex_number { int real_part; int imaginary_part; }; “complex_number” is the tag name “struct complex_number” is the new type

Senem Kumova Metin Defining Structures 2 /* DEFINITION OF RECTANGULAR PRISM */ struct rectangular_prism {int height; int width; int length;}; // name of new type?? /* DEFINITION OF STUDENT RECORD */ struct student_record { int ID; char name[100]; };

Senem Kumova Metin Structures : Creating objects struct complex_number { int real_part; int imaginary_part;}; // Below the definition of structure you can create // objects from it  “s1” and “s2” struct complex_number s1; struct complex_number s2; s2 real_part imaginary_part s1 real_part imaginary_part

Senem Kumova Metin Structures : Creating objects struct rectangular_prism {int height; int width; int length;}; // Below the definition of structure you can create // objects from it  “obj” struct rectangular_prism obj; obj height lengthwidth

Senem Kumova Metin Structures : Creating static arrays of objects struct student_record { int ID; char name[100]; }; // Creates an array of student records  “group” struct student_record group[4]; group IDname IDname IDname IDname group[0]  group[1]  group[2]  group[3] 

Senem Kumova Metin Structures : Creating dynamic arrays of objects struct rec { int ID; char name[100]; }; // Creating a dynamic array // of student records  “group” struct rec * group; // DECLARES POINTER group = ( struct rec * ) malloc ( sizeof (struct rec) * 4 ); size of objects in array number of objects in array (array size)

Senem Kumova Metin Structures : Accessing members of structures struct rectangular_prism {int height; int width; int length;}; // Create an object from the structure defined above  “obj” struct rectangular_prism obj; // Members of the objects can be accessed by putting a dot // following the object name obj.height=10; obj.width=15; obj.length=40; obj height=10 length=40width=15

Senem Kumova Metin Structures : Accessing members of structures struct rectangular_prism {int height; int width; int length;}; struct rectangular_prism obj; // Create a pointer to point object “obj” struct rectangular_prism *p = &obj; (*p).height=10 // or obj.height or p->height=10 p->width=15; p->length=40; obj == *p height=10 length=40width=15

Senem Kumova Metin Structures : Accessing members of structures // Defines structure struct student_record { int ID; char name[100]; }; // Creates an array of student records  “group” struct student_record group[2]; group[0].ID=200710; strcpy(group[0].name, “doddy”); group[1].ID=200711; strcpy(group[1].name,group[0].name); group ID=200710name= “doddy” ID=200711name =?? group[0]  group[1] 

Senem Kumova Metin Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s1; struct record s2; struct record s3; Declaration 2 : struct record { int ID; char * name; char grade; } s1, s2; struct record s3;

Senem Kumova Metin Structures : DECLARATION ALTERNATIVES Declaration 1 : struct record { int ID; char * name; char grade; }; struct record s1; struct record s2; Declaration 3 : struct { int ID; char * name; char grade; } s1, s2; /* no tag name */ /* no permission to declare other variables of this type */

Senem Kumova Metin Structures : DECLARATION ALTERNATIVES Declaration 4 : struct record { int ID; char * name; char grade; }; typedef struct record rec; rec s1; struct record s2; Declaration 5 : /* high degree of modularity and portability */ typedef struct { int ID; char * name; char grade; } rec; rec s1; rec s2;

Senem Kumova Metin Initialization of Structure Objects 1.struct names { char name[10]; int length; int weigth;} man[3]= { “Tom”, 180, 65, “George”, 170, 68, “Bob”, 190, 100 }; 2.struct names woman[2]={{“Mary”, 170, 55}, {“Sue”, 160,67}}; 3.struct names your; your. name=“Jane”; your.length=160; your.weigth=50;

Senem Kumova Metin Structures in Structures #include struct physical_info { int length; int weigth; } ; struct record { int salary; int working_hour; struct physical_info man; } ; main() {struct record s1; s1.salary=10000; s1.working_hour= 6; s1.man.length=180; s1.man.weigth=78; }

Senem Kumova Metin Exercise 1 on Structures Declare a structure to represent fractions Create 2 objects of fractions Ask user to fill the objects Calculate and print out the multiplication value  Fraction x / y  x over y (3/7, 8/5)  Need 2 numbers as numerator and denominator  User has to give 2 numbers for each object  Multiplication rule  a/b * c/d = (a*c) / (b*d)

Senem Kumova Metin Exercise 1 on Structures struct fraction {int n; // an integer to represent numerator int d; // an integer to represent denominator }; main() { struct fraction obj1, obj2; // Create input objects struct fraction result; // Create an object to store the result of // multiplication printf(“please enter values for fractions”); scanf(“%d%d”, &obj1.n, &obj1.d); scanf(“%d%d”, &obj2.n, &obj2.d); result.n = obj1.n * obj2.n; result.d = obj1. d * obj2. d; printf(“result is %d / %d”, result.n, result.d); }

Senem Kumova Metin Exercise 2 on Structures Declare a structure for a customer record (name, surname, phone number) Create 5 customer objects Ask user to fill the objects Print out the information of all customers as in the following output customer 1 : Gabriel Gray customer 2 : Claire Bennet customer 3 : Hiro Nakamura customer 4 : Nathan Petrelli customer 5 : Niki Sanders

Senem Kumova Metin Exercise 2 on Structures struct customer {char name[100]; char surname[100]; int phone; }; main() { struct customer heroes[5]; // Create input objects int i; printf("please fill customer records"); for(i=0;i<5;i++) scanf("%s%s%d", heroes[i].name, heroes[i].surname, &heroes[i].phone ); for(i=0;i<5;i++) printf(“customer %d : %s%s%d\n", i+1, heroes[i].name, heroes[i].surname, heroes[i].phone); }

Senem Kumova Metin Exercise 3 on Structures Declare a structure for complex numbers (real and imaginary part) Create 1 dynamic object (use pointers) Ask user to fill the object Print out the complex number as given below output Please give the values for complex number : 5 6 Complex number : 5 + 6i

Senem Kumova Metin Exercise 3 on Structures struct complex {int real; int imaginary; }; main() { struct complex * p; // Declare pointer for object // Memory allocation for object p=(struct complex *) malloc(sizeof(struct complex)); printf( " Please give the values for complex number : " ); scanf( "%d%d", &(p->real), & (p->imaginary) ); printf( "Complex number : %d + %d i", p->real, p->imaginary ); }

Senem Kumova Metin Structures as Function Arguments Call by value Call by reference

Senem Kumova Metin // A function to add two integers int add(int a, int b) { int result= a+b; return result;} struct complex { int r; int i; }; // A function to add two complex numbers struct complex add ( struct complex a, struct complex b ) { struct complex result; result.r=a.r+b.r; result.i=a.i+b.i; return result;} Exercise 1 : Define a function to add two complex numbers

Senem Kumova Metin // A function to print an integers void print(int a) { printf(“%d \n”,a);} struct complex { int r; int i; }; // A function to print a complex number struct complex print ( struct complex a) { printf(“%d + i %d \n”, a.r,a.i);} Exercise 1 : Define a function to print a complex number

Senem Kumova Metin Exercise 1 on Call by Value : Define main() // Structure definition struct complex { int r; int i; } ; // function prototypes struct complex add ( struct complex a, struct complex b ); void print ( struct complex a); main() { struct complex e1={2,3}; struct complex e2 ={1,2}; struct complex e3; print(e1); print(e2); e3=add(e1, e2); print(e3);}

Senem Kumova Metin Exercise 2 on Call by Value struct fraction {int n; // an integer to represent numerator int d; // an integer to represent denominator }; struct fraction sum(struct fraction x, struct fraction y) { struct fraction result; result.d=x.d*y.d; result.n=x.n*y.d+y.n*x.d; return result; } struct fraction divide(struct fraction x, struct fraction y) { struct fraction result; result.d=x.d*y.n; result.n=x.n*y.d return result; }

Senem Kumova Metin Exercise 3 on Call by Value struct rectangular_prism {int height; int width; int length;}; ……. volume(……) {…….} …… area (……) {…….}

Senem Kumova Metin Exercise 3 on Call by Value struct rectangular_prism {int height; int width; int length;}; int volume(struct rectangular_prism x) {return x.height*x.width*x.length; } int area (struct rectangular_prism x) { return 2*x.width*x.lenght + 2*x.lenght*x.height + 2*x.width*x.lenght; }