Module 5 Working with Arrays

Slides:



Advertisements
Similar presentations
One Dimensional Arrays
Advertisements

CS0007: Introduction to Computer Programming Array Algorithms.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
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.
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.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays. Arrays are objects that help us organize large amounts of information.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Searching Arrays Linear search Binary search small arrays
1-d Arrays.
Computer Science 210 Computer Organization
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
EKT120 : Computer Programming
Program to search an element of array using linear search.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Arrays and Records.
Computer Science 210 Computer Organization
Arrays … The Sequel Applications and Extensions
Arrays, Part 1 of 2 Topics Definition of a Data Structure
C Arrays.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
CNG 140 C Programming (Lecture set 8)
Chapter 8 Search and Sort
EKT150 : Computer Programming
Arrays November 8, 2017.
Arrays Outline Introduction Arrays Declaring Arrays
CISC181 Introduction to Computer Science Dr
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
EKT120: Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 7 Arrays PROGRAMMING IN ANSI C.
Functions with arrays.
Search,Sort,Recursion.
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 I Handling lists of data.
Functions.
Arrays.
Module 10 Operations on Bits
Module 14 Miscellaneous Topics
Module 6 Working with Functions
Module 9 Pointers.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays.
Presentation transcript:

Module 5 Working with Arrays

@UMBC Training Centers 2013 What is an array? Data structure a collection of elements Ordered first, second, third, … Homogenous all elements are the same type Fixed Size defined when your code compiles www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Declaring Arrays type name[ size ]; int grades[ 6 ]; Use named constant for array size #define MAX_GRADES 6 int grades[MAX_GRADES]; 1 2 3 4 5 ??? grades: www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Initializing Arrays int scores[5] = {99, 44, 22, 11, 10}; 1 2 3 4 99 44 22 11 10 scores: char word [ ] = {‘B’, ‘o’, ‘b’}; 1 2 ‘B’ ‘o’ word: double costs[5] = {5.45, 7.89, 1.01}; 1 2 3 4 5.45 7.89 1.01 costs: www.umbctraining.com @UMBC Training Centers 2013

Accessing Array Elements array_name[ index ] First element has index = 0 Max index for an array with N elements is N – 1 int cats[MAX_CATS] = {2, 4, 6, 8, 10, 12}; int nrCats = cats[4]; cats[5] = cats[5] – cats[0] + 6; printf(“ %d\n”, cats[1]); www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Out of Bounds int ages[4] = {12, 24, 36, 48}; ages 1 2 3 ??? 12 24 36 48 int myAge = ages[5]; ages[-2] = 14; Compiler does not check validity of index www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 For Loops and Arrays #define MAX_CATS 6 int i, cats[MAX_CATS]; for(i = 0; i < MAX_CATS; i++) { cats[i] = 0; } for(i = MAX_CATS-1; i >= 0; i--) { printf(“%d\n”, cats[i]); } // displays backwards www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 const Qualifier Specified that an initialized variable won’t change values const double pi = 3.14159; const char digits[ ] = { ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ }; www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Let’s take a look CB – arrays1 Note the use of #define for array sizes – if array size changes, just change #define in one place IDOM.. i = 0; i < size; i++ CB-Program7.2 (counting responses) Initialization with a for-loop Not using [0] to avoid i-1 coding CB-Program7.3 (calc and store Fibonacci) Discuss the rabbit story (text) No need to initialize entire array www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Exercises 30 mins From text, pg 118 #4 and 5 www.umbctraining.com @UMBC Training Centers 2013

Entering values into an array First need to declare the array Use a for loop to ask for values Enter value Still uses same scanf setup Use another for loop to Display values enter DOES NOT USE SAME printf!! www.umbctraining.com @UMBC Training Centers 2013

Entering values into an array int scores[10]; for(int i = 0; i < 10; i++) { printf("Please enter the score: "); scanf("%d", &scores[i]); } printf("\nThese are the scores you entered:\n "); printf("%d\n", scores[i]); www.umbctraining.com @UMBC Training Centers 2013

Partially Filled Arrays Capacity (size) determined by declaration Program must track number of actual elements int studentGrades[ 10 ]; int nrGrades = ; 4 3 1 2 1 2 3 4 5 6 7 8 9 34 22 99 88 www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Let’s take a look CB – PartialArrays www.umbctraining.com @UMBC Training Centers 2013

Fundamental Array Operations Search Does the array contain a specified element? Sort Arrange the array elements in order from low to high (or high to low) www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Linear Search Examine elements of the array starting at the first element until The target element is found – SUCCESS There are no more elements – FAILURE Use a while-loop with a compound conditional Check if target was found www.umbctraining.com @Copyright UMBC Training Centers 2012

Linear Search Progression Search for 39 1 2 3 4 5 6 7 8 9 20 12 87 44 39 6 45 22 68 77 www.umbctraining.com @Copyright UMBC Training Centers 2012

@Copyright UMBC Training Centers 2012 Exchange Sort Compare the first element with all other elements. If the other element is smaller, exchange it with the first element. The first element is now the smallest Compare the second element with all other elements (except the first). If the other element is smaller, exchange it with the second element. The second element is now the second smallest Compare the third element with all other elements (except the first and second). If the other element is smaller, exchange it with the third element. The second element is now the third smallest . . . www.umbctraining.com @Copyright UMBC Training Centers 2012

Exchange Sort Progression 34 -5 6 0 12 100 56 4 First Pass -5 34 6 0 12 100 56 4 Second Pass -5 6 34 0 12 100 56 4 -5 0 34 6 12 100 56 4 Third Pass -5 0 6 34 12 100 56 4 -5 0 4 34 12 100 56 6 More Passes…. -5 0 4 6 12 34 56 100 www.umbctraining.com @Copyright UMBC Training Centers 2012

@UMBC Training Centers 2013 Let’s take a look C::B – Exchange sort www.umbctraining.com @UMBC Training Centers 2013

@Copyright UMBC Training Centers 2012 Binary Search Fast search for specified target element Requires sorted array Algorithm Look at the middle element If the middle element is the target, stop If the target > middle element, look at the middle element of the upper “subarray” If the target < middle element, look at the middle element of the lower“subarray” Continue until found or all elements examined www.umbctraining.com @Copyright UMBC Training Centers 2012

@UMBC Training Centers 2013 Binary Search Videos https://www.youtube.com/watch?v=F1ec0Omt9z8&list=PLC7fNkE1QplZo_sAbUaJpyQGt2Ni7CRZM&index=8 https://www.youtube.com/watch?v=7lwau5tLUqA www.umbctraining.com @UMBC Training Centers 2013

Binary Search Progression Search For 68 1 2 3 4 5 6 7 8 9 5 12 16 23 36 44 45 56 68 77 www.umbctraining.com @Copyright UMBC Training Centers 2012

@UMBC Training Centers 2013 Let’s take a look C::B – BinarySearch www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 Exercises From the Text -- pg 117 #3 – Modify program 7.2 (do NOT use break) -- #4 – calc average of 10 floating point values- AM “hard code” values into an array of size 10 #5 – predict output, the code and verify - AM Ex1-HighestQuiz.docx Ex2-Sets.docx Ex3-SieveOfE.docs (see text #7) Ex7-PartialArrays Rewrite program7-4.c using a while-loop www.umbctraining.com @UMBC Training Centers 2013

@UMBC Training Centers 2013 If you have any comments about this video, please use the contact information in your registration materials to let us know. www.umbctraining.com @UMBC Training Centers 2013