C Arrays (2) (Chapter 6) ECET 264.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Multiple-Subscripted Arrays Multiple subscripts –a[ i ][ j ] –Tables with rows and columns –Specify.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Arrays Dale Roberts, Lecturer
Searching Arrays Linear search Binary search small arrays
 2007 Pearson Education, Inc. All rights reserved. 1 C Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 2007 Pearson Education, Inc. All rights reserved C Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Methods Divide and conquer Programmer-declared methods Prepackaged methods – Java API Software reusability Debug-ability and maintainability AKA functions.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
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.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
CSCI 51 Introduction to Programming March 12, 2009.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
Sorting Sorting takes an unordered array and makes it an ordered one
C++ Programming Lecture 16 Arrays – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting II.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Searching Arrays Linear search Binary search small arrays
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Searching and Sorting Algorithms
Week 13: Searching and Sorting
Arrays 2.
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Introduction to Programming
Chapter 8 Arrays Objectives
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
C++ Programming Lecture 16 Arrays – Part III
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
MSIS 655 Advanced Business Applications Programming
6 C Arrays.
To refer to an element, specify
Chapter 8 Arrays Objectives
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
4.9 Multiple-Subscripted Arrays
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

C Arrays (2) (Chapter 6) ECET 264

Topics Array applications Total Average double average(const int setOfGrades[ ], int tests) Printing : void printArray(const int grades[][EXAMS], int pupils, int tests) Min int minimum(const int grades[][EXAMS], int pupils, int tests) Max int maximum(const int grades[][EXAMS], int pupils, int tests) Sorting Searching

Topics Total total = 0; for (j =0; j<SIZE; j++) { total += answer[j]; }

Topics Total total = 0; for (row=0;row < STUDENTS; row++) for (column=0; column < EXAMS ; column++) total += grades [row] [column];

Outline (1 of 2 ) fig06_21.c array1 is initialized with both rows full array2 and array3 are initialized only partially

Outline fig06_21.c (2 of 2 )

Outline fig06_22.c (1 of 6 ) Each row in the array corresponds to a single student’s set of grades

Outline fig06_22.c (2 of 6 ) average function is passed a row of the array

Outline fig06_22.c (3 of 6 )

Outline fig06_22.c (4 of 6 )

Outline fig06_22.c (5 of 6 )

Outline fig06_22.c (6 of 6 )

Sorting Arrays Sorting data Bubble sort (sinking sort) Example: Important computing application Virtually every organization must sort some data Bubble sort (sinking sort) Several passes through the array Successive pairs of elements are compared If increasing order (or identical ), no change If decreasing order, elements exchanged Repeat Example: original: 3 4 2 6 7 pass 1: 3 2 4 6 7 pass 2: 2 3 4 6 7 Small elements "bubble" to the top

Outline fig06_15.c (1 of 2 )

Outline If any two array elements are out of order, the function swaps them fig06_15.c (2 of 2 )

Function for Bubble sorting void bubbleSort (int a[ ]) (figure 6.16) /* function that sorts an array with bubble sort algorithm */ void bubbleSort( int a[] ) { int pass; /* pass counter */ int j; /* comparison counter */ int hold; /* temporary location used to swap elements */ /* loop to control number of passes */ for ( pass = 1; pass < SIZE; pass++ ) { /* loop to control number of comparisons per pass */ for ( j = 0; j < SIZE - 1; j++ ) { /* swap elements if out of order */ if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } /* end if */ } /* end inner for */ } /* end outer for */ } /* end function bubbleSort */

Searching Arrays Search an array for a key value Linear search (Figure 6.18) Simple Compare each element of array with key value Useful for small and unsorted arrays Binary search (Figure 6.19) For sorted arrays only Very fast

Outline fig06_18.c (1 of 3 )

Outline fig06_18.c (2 of 3 )

Outline Linear search algorithm searches through every element in the array until a match is found fig06_18.c (3 of 3 )