Advanced Data types and Sorting

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Introduction to C Programming
Introduction to C Programming
Chapter 7: Arrays In this chapter, you will learn about
Spring Semester 2013 Lecture 5
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.
Programming and Data Structure
Structure.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Sort the given string, without using string handling functions.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Introduction to C Programming CE Lecture 10 Data Structures typedef and struct.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 8 Arrays and Strings
Programming Logic and Design Fourth Edition, Comprehensive
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Chapter 9: Advanced Array Concepts
1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 2006 Pearson Education, Inc. All rights reserved Arrays.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Understanding Structures tMyn1 Understanding Structures In order to describe virtually anything in the real world, you need to define several values that.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
Learners Support Publications Classes and Objects.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Structures Combining data types into a logical groupings.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
+ 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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Structured Programming Approach Module VIII - Additional C Data Types Structures Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Programming Fundamentals Enumerations and Functions.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
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.
Consultation Hours. Mubashir: – Tuesday from 12:30 to 1:30 with ease of Students. Zohaib – Wedneday b/w 9:30 -10:30 Location: TA Room (next to HOD Office)
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Classes and Objects.
To refer to an element, specify
Presentation transcript:

Advanced Data types and Sorting Session 11

Objectives - 1 Explain structures and their use Define structures Declare structure variables Explain how structure elements are accessed Explain how structures are initialized Explain how assignment statements are used with structures Explain how structures can be passed as arguments to functions Use arrays of structures Explain the initialization of structure arrays Elementary Programming with C/Session 11/ 2 of 23

Objectives - 2 Explain pointers to structures Explain how structure pointers can be passed as arguments to functions Explain the typedef keyword Explain array sorting with the Selection sort and Bubble sort methods Elementary Programming with C/Session 11/ 3 of 23

Structures } Structure A structure consists of a number of data items, which need not be of the same data type, grouped together The structure could hold as many of these items as desired 1 Variable I L U S O N Array I L U S O N B A C H 1 } Name of the book Author Edition Structure Elementary Programming with C/Session 11/ 4 of 23

Defining a Structure A structure definition forms a template for creating structure variables The variables in the structure are called structure elements or structure members Example: struct cat { char bk_name [25]; char author [20]; int edn; float price; }; Elementary Programming with C/Session 11/ 5 of 23

struct cat books1, books2; Declaring Structure Variables Once the structure has been defined, one or more variables of that type can be declared Example: struct cat books1; The statement sets aside enough memory to hold all items in the structure Other ways struct cat { char bk_name[25]; char author[20]; int edn; float price; } books1, books2; struct cat books1, books2; or struct cat books1; struct cat books2; Elementary Programming with C/Session 11/ 6 of 23

Accessing Structure Elements Structure elements are referenced through the use of the dot operator (.), also known as the membership operator Syntax: structure_name.element_name Example: scanf(“%s”, books1.bk_name); Elementary Programming with C/Session 11/ 7 of 23

Initializing Structures Like variables and arrays, structure variables can be initialized at the point of declaration struct employee { int no; char name [20]; }; Variables emp1 and emp2 of the type employee can be declared and initialized as: struct employee emp1 = {346, “Abraham”}; struct employee emp2 = {347, “John”}; Elementary Programming with C/Session 11/ 8 of 23

Assignment Statements Used with Structures-1 It is possible to assign the values of one structure variable to another variable of the same type using a simple assignment statement For example, if books 1 and books2 are structure variables of the same type, the following statement is valid books2 = books1; Elementary Programming with C/Session 11/ 9 of 23

Assignment Statements Used with Structures - 2 In cases where direct assignment is not possible, the in-built function memcpy() can be used Syntax: memcpy (char * destn, char &source, int nbytes); Example: memcpy (&books2, &books1, sizeof(struct cat)); Elementary Programming with C/Session 11/ 10 of 23

Structures within Structures It is possible to have one structure within another structure. A structure cannot be nested within itself struct issue { char borrower [20]; char dt_of_issue[8]; struct cat books; }issl; To access the elements of the structure the format will be similar to the one used with normal structures, issl.borrower To access elements of the structure cat, which is a part of another structure issue, issl.books.author Elementary Programming with C/Session 11/ 11 of 23

Passing Structures as Arguments A structure variable can be passed as an argument to a function This facility is used to pass groups of logically related data items together instead of passing them one by one The type of the argument should match the type of the parameter Elementary Programming with C/Session 11/ 12 of 23

Array of Structures A common use of structures is in arrays of structures A structure is first defined, and then an array variable of that type is declared Example: struct cat books[50]; To the access the variable author of the fourth element of the array books: books[4].author Elementary Programming with C/Session 11/ 13 of 23

Initialization of Structure Arrays Structure arrays are initialized by enclosing the list of values of its elements within a pair of braces Example: struct unit { char ch; int i; }; struct unit series [3] = { {‘a’, 100} {‘b’, 200} {‘c’, 300} Elementary Programming with C/Session 11/ 14 of 23

Pointers to Structures Structure pointers are declared by placing an asterisk(*) in front of the structure variable’s name The -> operator is used to access the elements of a structure using a pointer Example: struct cat *ptr_bk;   ptr_bk = &books; printf(“%s”, ptr_bk->author); Structure pointers passed as arguments to functions enable the function to modify the structure elements directly Elementary Programming with C/Session 11/ 15 of 23

The typedef keyword A new data type name can be defined by using the keyword typedef It does not create a new data type, but defines a new name for an existing type Syntax: typedef type name; Example: typedef float deci; typedef cannot be used with storage classes Elementary Programming with C/Session 11/ 16 of 23

Sorting Arrays Sorting involves arranging the array data in a specified order such as ascending or descending Data in an array is easier to search when the array is sorted There are two methods to sort arrays – Selection Sort and Bubble Sort In the selection sort method, the value present in each element is compared with the subsequent elements in the array to obtain the least/greatest value In bubble sort method, the comparisons begin from the bottom-most element and the smaller element bubbles up to the top Elementary Programming with C/Session 11/ 17 of 23

Bubble Sort-1 Elementary Programming with C/Session 11/ 18 of 23

Bubble Sort-2 Example #include <stdio.h> void main() {   void main() { int i, j, temp, arr_num[5] = { 23, 90, 9, 25, 16}; clrscr(); for(i=3;i>=0;i--) /* Tracks every pass */ for(j=4;j>=4-i;j--) /* Compares elements */ { if(arr_num[j]<arr_num[j-1]) { temp=arr_num[j]; arr_num[j]=arr_num[j-1]; arr_num[j-1]=temp; } } Contd….. Example Elementary Programming with C/Session 11/ 19 of 23

Bubble Sort-3 Example printf("\nThe sorted array"); for(i=0;i<5;i++) printf("\n%d", arr_num[i]);   getch(); } Example Elementary Programming with C/Session 11/ 20 of 23

Insertion Sort-1 Elementary Programming with C/Session 11/ 21 of 23

Insertion Sort-2 #include<stdio.h> void main() { int i, j, arr[5] = { 23, 90, 9, 25, 16 }; char flag; clrscr(); /*Loop to compare each element of the unsorted part of the array*/ for(i=1; i<5; i++) /*Loop for each element in the sorted part of the array*/ for(j=0, flag='n'; j<i && flag=='n'; j++) if(arr[j]>arr[i]) { /*Invoke the function to insert the number*/ insertnum(arr, i, j); flag='y'; } printf("\n\nThe sorted array\n"); for(i=0; i<5; i++) printf("%d\t", arr[i]); getch(); Elementary Programming with C/Session 11/ 22 of 23

Insertion Sort-3 insertnum(int arrnum[], int x, int y) { int temp; /*Store the number to be inserted*/ temp=arrnum[x]; /*Loop to push the sorted part of the array down from the position where the number has to inserted*/ for(;x>y; x--) arrnum[x]=arrnum[x-1]; /*Insert the number*/ arrnum[x]=temp; } Elementary Programming with C/Session 11/ 23 of 23