STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz.

Slides:



Advertisements
Similar presentations
Programming in C Chapter 10 Structures and Unions
Advertisements

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.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
C Language.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Senem Kumova Metin Introduction to Programming CS 115 Introduction to Computing PART I : Computer Basics PART II: Introduction to Computing/Programming.
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.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
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.
Lab 8 User Defined Function.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Senem Kumova Metin STRUCTURES CHAPTER 9 in A Book in C.
STACKS AND QUEUES. A LINKED LIST IMPLEMENTATION OF A QUEUE data next data next NULL data next cnt front rear queue node Queue: First-In-First-Out (FIFO)
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
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.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
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.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
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.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
EENG212 Algorithms and Data Structures
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Senem Kumova Metin STRUCTURES continues CHAPTER 9 in A Book in C.
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.
C Programming Lecture 11 : Structure. Data Type struct union enum typedef.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Computer Programming for Engineers
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Popping Items Off a Stack Using a Function Lesson xx
CS1010 Programming Methodology
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
CS SUMMER LECTURE 1 by İlker Korkmaz.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
LINKED LISTS.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
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.
Popping Items Off a Stack Lesson xx
EKT150 : Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
By C. Shing ITEC Dept Radford University
Programming Languages and Paradigms
Arrays.
Infix to postfix conversion
READING AND PRINTING MATRICES (with functions)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSCE 206 Lab Structured Programming in C
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 11 Classes.
Presentation transcript:

STRUCTURES (PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz

Review on “struct” concept  A structure has components, called members, which can be of various types.  The declaration of a structure captures the information needed to represent the related structure.  The keyword is struct

Structures (III) struct record { int ID; char * name; char grade; }; struct record s1; struct record s2; IDnamegrade s2 IDnamegrade s1

Structures : DECLARATION ALTERNATIVES (I) Declaration 1 : struct record { int ID; char name[20]; // or: char * name; char grade; }; struct record s1; // sample definitions struct record s2; struct record s3;

Exercise 1 on Structures (I)  Declare a structure to represent fractions  Create 2 variables of fractions  Ask user to fill the variables  Calculate and print out the multiplication result  Fraction  x / y (such as 3/7, 8/5...)  Need 2 numbers as numerator and denominator  User has to give 2 member values for each variable  Multiplication rule  a/b * c/d = (a*c) / (b*d)

Exercise 1 on Structures (II) struct fraction {int n; // an integer to represent numerator int d; // an integer to represent denominator }; main() { struct fraction obj1, obj2; // Create input variables struct fraction result; // Create a variable to store the result printf(“please enter the values for 2 members of each fraction structures”); 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); }

Exercise 2 on Structures (I)  Declare a structure to represent a “customer” (name, surname, phone number)  Create 5 customer variables  Ask user to fill the variables  Print out the information for 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

Exercise 2 on Structures (II) struct customer {char name[100]; char surname[100]; int phone; }; main() { struct customer heroes[5]; // Create input variables int i; printf("please fill the 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); }

Exercise 3 on Structures (I)  Declare a structure to represent the complex numbers (real and imaginary part)  Create a dynamic variable to point a complex number at run time (use calloc/malloc to allocate the memory dynamically)  Ask user to fill the variable  Sample output: Please give the real and the imaginary member values of the complex number : 3 5 Info of your complex number : 3 + 5i

Exercise 3 on Structures (II) struct complex_num {int real; int imaginary; }; main() { struct complex_num * p; /* Memory allocation for the complex number*/ p=(struct complex_num *) malloc(sizeof(struct complex_num)); 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 ); }

A general example for structures: stack  /*stack.h*/  #include  #define STACK_SIZE 4  typedef struct st { int data [STACK_SIZE]; int top;} stack;  /*stack.c*/  #include "stack.h"  void reset(stack * stk)  {  stk->top=-1;  }  void push( int c, stack * stk)  {  if( stk->top!= STACK_SIZE-1)  {  stk->top++;  stk->data[stk->top]=c;  }  else  printf("Stack is full!!\n");  }  int pop (stack * stk)  {  if(stk->top!=-1)  return (stk->data[stk->top--]);  else  {  printf("stack is empty");  return -1;  }  /*test.c*/  #include "stack.h"  main()  {  stack n;  int x;  reset(&n);  push(4,&n);  push(5,&n);  x= pop(&n);  printf("%d\n",x);  push(3,&n);  push(2,&n);  x= pop(&n);  printf("%d\n",x);  }