ETE 132 Lecture 7 By Munirul Haque.

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
C Language.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
CSE202: Lecture 12The Ohio State University1 Function Calling.
Structures Spring 2013Programming and Data Structure1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
CS 261 – Data Structures Introduction to C Programming.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Objective: Students will be able to: Declare and use variables Input integers.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
LESSON 3-2 ANGLES AND PARALLEL LINES. Concept Example 1 Use Corresponding Angles Postulate A. In the figure, m  11 = 51. Find m  15.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Lecture 7: Variable Scope B Burlingame March 16, 2016.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Computer Programming II
C++ Lesson 1.
Andrew(amwallis) Classes!
Basic concepts of C++ Presented by Prof. Satyajit De
Parallel Arrays? ANSI-C.
‘C’ Programming Structures and Commands
User-Written Functions
Chapter 6 - Functions modular programming general function format
Chapter 8 Arrays, Strings and Pointers
Variables Mr. Crone.
CS1010 Programming Methodology
Introduction to the C programming language
C Programming Tutorial – Part I
Quiz 11/15/16 – C functions, arrays and strings
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
C Short Overview Lembit Jürimägi.
Visit for more Learning Resources
Buy book Online -
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Operator Overloading
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Local Variables, Global Variables and Variable Scope
A function with one argument
Writing Functions.
Further C Programming Variable types Loops Conditional statements
Pointers Pointers point to memory locations
Using Objects (continued)
CS150 Introduction to Computer Science 1
Programming Languages and Paradigms
ETE 132 Lecture 6 By Munirul Haque.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Programming in C Pointer Basics.
C Structures and Memory Allocation
CSCE 206 Lab Structured Programming in C
ITM 352 Functions.
CPS125.
Structures, Unions, and Enumerations
Scope Rules.
Presentation transcript:

ETE 132 Lecture 7 By Munirul Haque

Topics Defining Your Own Types Structures

Structures The structure mechanism allows us to aggregate variables of different types struct definitions are usually placed outside of functions so that they are in scope throughout the file, as in the following example:

Structures struct card_struct { int number; char suit; }; /* note the semicolon after the definition! */ void some_function() { struct card_struct a, b; a.number = 3; a.suit = ’D’; b = a; }

Structures The “.” in a.num is the “structure member operator”, which connects the structure name and the member name. A “member” is a variable in a structure. Assignment (=) works just as you would expect, as if there were a separate assignment for each structure member.

More on structures The reason a semicolon follows the struct definition is that the definition is a statement. Also, you can declare a variable of that struct type using basically the same syntax: struct card_struct { int number; char suit; } my_card;

Example: points in the plane #include <math.h> struct point { double x; double y; }; double distance( struct point p1, struct point p2 ) { double dx, dy, dist; dx = p1.x - p2.x; dy = p1.y - p2.y; dist = sqrt((dx * dx) + (dy * dy)); return( dist ); } void main() { /* here’s a convenient notation for structure initialization: */ struct point a, b; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; printf( "Distance: %f\n", distance( a, b )); }

What happens here? struct employee { char name[50]; int age; }; void main() { struct employee tom1, tom2; strcpy( tom1.name, "Thomas Wolfe" ); tom1.age = 104; tom2 = tom1; tom2.name[0] = ’G’; printf( "Name: %s", tom1.name ); }

A Comparison Function There’s no standard way to compare structures. You can’t try tom1 == tom2 in the previous example, or tom1 < tom2. You can always write comparison code, if you need to: int compare_employees( struct employee e1, struct employee e2 ) { return (e1.age == e2.age) && (strcmp(e1.name,e2.name) == 0); }

Structure member as Parameter void sum(double p1_x,double p1_y,double p2_x,double p2_y ) { struct point psum; psum.x = p1_x + p2_x; psum.y = p1_y + p2_y; printf(“%lf_%lf\n”, psum.x, psum.y); } void main() { struct point a, b, c; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; printf(“%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y); sum( a.x, a.y, b.x, b.y); 10_5 3.5_4.5_6.5_0.5

Structure as Parameter Structures work seamlessly with functions. A structure is a type, so it can be the type of a function parameter (as here), or a return type: point sum( struct point p1, struct point p2 ) { struct point psum; psum.x = p1.x + p2.x; psum.y = p1.y + p2.y; return psum; } void main() { struct point a, b, c; a.x = 3.5; a.y = 4.5; b.x = 6.5; b.y = 0.5; c = sum( a, b); printf(“%lf_%lf_%lf_%lf_%lf_%lf\n”, a.x, a.y, b.x, b.y, c.x, c.y); 3.5_4.5_6.5_0.5_10_5

Array of Structure You can also define an array of structures: struct point point_list[100]; for(i=0;i<100;i++) { /* initialize point i */ point_list[i].x = (i+1)*2; point_list[i].y = (i+1)*3; } printf(“Point %d: %lf_%lf\n”, (i+1), point_list[i].x, point_list[i].y); Point 1: 2_3 Point 2: 4_6 Point 3: 6_9 … Point 100: 200_300 12

Problem Write a program to take input from console, Name (within 20 characters), Address (50 char), Telephone No (max. 9 digit), and Gender (single char) of 15 Students. Print those students information in reverse order (started with Gender, ended with Name for every student)