Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Arrays. Introduction Arrays –Structures of related data items –Static entity - same size throughout program A few types –C-like, pointer-based arrays.
 2003 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 192 Lecture 10 Winter 2003 December 22-23, 2003 Dr. Shafay Shamail.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Using Arrays in Abstract Data Types.
 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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 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
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
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.
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.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
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.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
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.
chap8 Chapter 8 Arrays (reference: Deitel ’ s chap 6)
1 Arrays as Lists with examples. 2 Review Arrays –Structures of related data items of the same type –Data items (called array elements) are stored at.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
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.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
C Lecture Notes 1 Arrays (...cont.). C Lecture Notes 2 6.6Sorting Arrays Sorting data –Important computing application –Virtually every organization must.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
CSC 113: C OMPUTER P ROGRAMMING (T HEORY = 03, L AB = 01) Computer Science Department Bahria University, Islamabad.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Searching Arrays Linear search Binary search small arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
C++ Programming Lecture 15 Arrays – Part II
آرايه ها اصول كامپيوتر 1.
C++ Programming Lecture 15 Arrays – Part II
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Arrays as Lists with examples.
Arrays Outline Introduction Arrays Declaring Arrays
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays (2) (Chapter 6) ECET 264.
4.1 Introduction Arrays A few types Structures of related data items
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Chapter 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays.
Chapter 3 Arrays Dr. A. PHILIP AROKIADOSS Assistant Professor
Vectors.
4.9 Multiple-Subscripted Arrays
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study

Dale Roberts Sorting Arrays Sorting data 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: pass 1: pass 2: Small elements "bubble" to the top

Dale Roberts Searching Arrays: Linear Search vs Binary Search Search an array for a key value Linear search Simple Compare each element of array with key value Useful for small and unsorted arrays Binary search For sorted arrays Compares middle element with key If equal, match found If key < middle, looks in first half of array If key > middle, looks in last half Repeat Very fast; at most n steps, where 2 n > number of elements 30 element array takes at most 5 steps 2 5 > 30 so at most 5 steps

Dale Roberts Case Study: Computing Mean, Median and Mode Using Arrays Mean – average Median – number in middle of sorted list Example: 1, 2, 3, 4, 5  3 is the median Mode – number that occurs most often Example: 1, 1, 1, 2, 3, 3, 4, 5  1 is the mode 1/* Fig. 6.16: fig06_16.c 2 This program introduces the topic of survey data analysis. 3 It computes the mean, median, and mode of the data */ 4#include 5#define SIZE void mean( const int [] ); 8void median( int [] ); 9void mode( int [], const int [] ) ; 10void bubbleSort( int [] ); 11void printArray( const int [] ); 12 13int main() 14{ 15 int frequency[ 10 ] = { 0 }; Function prototypes

Dale Roberts 33 34void mean( const int answer[] ) 35{ 36 int j, total = 0; printf( "%s\n%s\n%s\n", "********", " Mean", "********" ); for ( j = 0; j <= SIZE - 1; j++ ) 41 total += answer[ j ]; printf( "The mean is the average value of the data\n" 44 "items. The mean is equal to the total of\n" 45 "all the data items divided by the number\n" 46 "of data items ( %d ). The mean value for\n" 47 "this run is: %d / %d = %.4f\n\n", 48 SIZE, total, SIZE, ( double ) total / SIZE ); 49} 16 int response[ SIZE ] = 17 { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9, 18 7, 8, 9, 5, 9, 8, 7, 8, 7, 8, 19 6, 7, 8, 9, 3, 9, 8, 7, 8, 7, 20 7, 8, 9, 8, 9, 8, 9, 7, 8, 9, 21 6, 7, 8, 7, 8, 7, 9, 8, 9, 2, 22 7, 8, 9, 8, 9, 8, 9, 7, 5, 3, 23 5, 6, 7, 2, 5, 3, 9, 4, 6, 4, 24 7, 8, 9, 6, 8, 7, 8, 9, 7, 8, 25 7, 4, 4, 2, 5, 3, 8, 7, 5, 6, 26 4, 5, 6, 1, 6, 5, 7, 8, 7 }; mean( response ); 29 median( response ); 30 mode( frequency, response ); 31 return 0; 32} Define function mean Initialize array Call functions mean, median, and mode

Dale Roberts Define function median Sort Array Print middle element Define function mode Increase frequency[] depending on response[] 65} 66 67void mode( int freq[], const int answer[] ) 68{ 69 int rating, j, h, largest = 0, modeValue = 0; printf( "\n%s\n%s\n%s\n", 72 "********", " Mode", "********" ); for ( rating = 1; rating <= 9; rating++ ) 75 freq[ rating ] = 0; for ( j = 0; j <= SIZE - 1; j++ ) 78 ++freq[ answer[ j ] ]; void median( int answer[] ) 52{ 53 printf( "\n%s\n%s\n%s\n%s", 54 "********", " Median", "********", 55 "The unsorted array of responses is" ); printArray( answer ); 58 bubbleSort( answer ); 59 printf( "\n\nThe sorted array is" ); 60 printArray( answer ); 61 printf( "\n\nThe median is element %d of\n" 62 "the sorted %d element array.\n" 63 "For this run the median is %d\n\n", 64 SIZE / 2, SIZE, answer[ SIZE / 2 ] ); Notice how the subscript in freq[] is the value of an element in response[] (answer[]) Is this computation of the median element correct? Notice that SIZE must be odd for this to be correct. Is SIZE odd?

Dale Roberts 95 printf( "\n" ); 96 } printf( "The mode is the most frequent value.\n" 99 "For this run the mode is %d which occurred" 100 " %d times.\n", modeValue, largest ); 101} printf( "%s%11s%19s\n\n%54s\n%54s\n\n", 81 "Response", "Frequency", "Histogram", 82 " ", " " ); for ( rating = 1; rating <= 9; rating++ ) { 85 printf( "%8d%11d ", rating, freq[ rating ] ); if ( freq[ rating ] > largest ) { 88 largest = freq[ rating ]; 89 modeValue = rating; 90 } for ( h = 1; h <= freq[ rating ]; h++ ) 93 printf( "*" ); 94 Print stars depending on value of frequency[] Does this code implement the definition of mode that you learned in math class?

Dale Roberts printf( "%2d", a[ j ] ); 128 } 129 } 103 void bubbleSort( int a[] ) 104 { 105 int pass, j, hold; for ( pass = 1; pass <= SIZE - 1; pass++ ) for ( j = 0; j <= SIZE - 2; j++ ) if ( a[ j ] > a[ j + 1 ] ) { 112 hold = a[ j ]; 113 a[ j ] = a[ j + 1 ]; 114 a[ j + 1 ] = hold; 115 } 116 } void printArray( const int a[] ) 119 { 120 int j; for ( j = 0; j <= SIZE - 1; j++ ) { if ( j % 20 == 0 ) 125 printf( "\n" ); Bubble sort: if elements out of order, swap them. Define bubbleSort Define printArray Bubble sort can make the smallest values “sink” by scanning top to bottom using.

Dale Roberts Program Output ******** Mean ******** The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items (99). The mean value for this run is: 681 / 99 = ******** Median ******** The unsorted array of responses is The sorted array is The median is element 49 of the sorted 99 element array. For this run the median is 7 ******** Mode ******** Response Frequency Histogram * 2 3 *** 3 4 **** 4 5 ***** 5 8 ******** 6 9 ********* 7 23 *********************** 8 27 *************************** 9 19 ******************* The mode is the most frequent value. For this run the mode is 8 which occurred 27 times.