Arrays -II. Arrays. Outline ECE Cal Poly Pomona Electrical & Computer Engineering. Introduction

Podobne dokumenty
OpenPoland.net API Documentation

Camspot 4.4 Camspot 4.5

deep learning for NLP (5 lectures)

Installation of EuroCert software for qualified electronic signature

Hard-Margin Support Vector Machines

EXAMPLES OF CABRI GEOMETRE II APPLICATION IN GEOMETRIC SCIENTIFIC RESEARCH

DO MONTAŻU POTRZEBNE SĄ DWIE OSOBY! INSTALLATION REQUIRES TWO PEOPLE!

HAPPY ANIMALS L01 HAPPY ANIMALS L03 HAPPY ANIMALS L05 HAPPY ANIMALS L07

HAPPY ANIMALS L02 HAPPY ANIMALS L04 HAPPY ANIMALS L06 HAPPY ANIMALS L08

Zakopane, plan miasta: Skala ok. 1: = City map (Polish Edition)


POLITYKA PRYWATNOŚCI / PRIVACY POLICY

Helena Boguta, klasa 8W, rok szkolny 2018/2019

Zarządzanie sieciami telekomunikacyjnymi

Machine Learning for Data Science (CS4786) Lecture 11. Spectral Embedding + Clustering

DO MONTAŻU POTRZEBNE SĄ DWIE OSOBY! INSTALLATION REQUIRES TWO PEOPLE!

Instrukcja obsługi User s manual

HAPPY K04 INSTRUKCJA MONTAŻU ASSEMBLY INSTRUCTIONS DO MONTAŻU POTRZEBNE SĄ DWIE OSOBY! INSTALLATION REQUIRES TWO PEOPLE! W5 W6 G1 T2 U1 U2 TZ1

Previously on CSCI 4622

Ukryte funkcjonalności w oprogramowaniu i urządzeniach elektronicznych. mgr inż. Paweł Koszut

Analysis of Movie Profitability STAT 469 IN CLASS ANALYSIS #2

archivist: Managing Data Analysis Results

ZGŁOSZENIE WSPÓLNEGO POLSKO -. PROJEKTU NA LATA: APPLICATION FOR A JOINT POLISH -... PROJECT FOR THE YEARS:.

Knovel Math: Jakość produktu

LEARNING AGREEMENT FOR STUDIES

Rev Źródło:

Weronika Mysliwiec, klasa 8W, rok szkolny 2018/2019

MS Visual Studio 2005 Team Suite - Performance Tool


Rozpoznawanie twarzy metodą PCA Michał Bereta 1. Testowanie statystycznej istotności różnic między jakością klasyfikatorów

DO MONTAŻU POTRZEBNE SĄ DWIE OSOBY! INSTALLATION REQUIRES TWO PEOPLE!

Programowanie Komputerów

11.1 Obsługa błędów i wyjątków Polecenia try, throw, catch CLI C++, klasa Exception... 9

This is CS 50. Harvard College s Introduction to Computer Science I \ DAVID J. MALAN 99 WEEK 2

LED PAR 56 7*10W RGBW 4in1 SLIM

Kolekcje. object that groups multiple elements into a single unit

Ćwiczenia 2 IBM DB2 Data Studio

Stargard Szczecinski i okolice (Polish Edition)

Testy jednostkowe - zastosowanie oprogramowania JUNIT 4.0 Zofia Kruczkiewicz

USB firmware changing guide. Zmiana oprogramowania za przy użyciu połączenia USB. Changelog / Lista Zmian

Machine Learning for Data Science (CS4786) Lecture11. Random Projections & Canonical Correlation Analysis

Office 365 Midsize Business

Machine Learning for Data Science (CS4786) Lecture 24. Differential Privacy and Re-useable Holdout

Pobieranie argumentów wiersza polecenia

Zmiany techniczne wprowadzone w wersji Comarch ERP Altum

Sieciowa komunikacja procesów - XDR i RPC

Marzec: food, advertising, shopping and services, verb patterns, adjectives and prepositions, complaints - writing

Linear Classification and Logistic Regression. Pascal Fua IC-CVLab


Volcano MC-GM4 OPTICAL MOUSE USER S MANUAL MODECOM

Wojewodztwo Koszalinskie: Obiekty i walory krajoznawcze (Inwentaryzacja krajoznawcza Polski) (Polish Edition)

Has the heat wave frequency or intensity changed in Poland since 1950?

Volcano MC-GMX4 OPTICAL MOUSE USER S MANUAL MODECOM

Program szkolenia: Fundamenty testowania

PROGRAM. Partnerskie Projekty Szkół Program sektorowy Programu Uczenie się przez całe życie. Tytuł projektu: My dream will change the world

RS868v3 module configuration

Plan bazy: Kod zakładający bazę danych: DROP TABLE noclegi CASCADE; CREATE TABLE noclegi( id_noclegu SERIAL NOT NULL,

Emilka szuka swojej gwiazdy / Emily Climbs (Emily, #2)

Powyższe wyrażenie alokuje 200 lub 400 w zależności od rozmiaru int w danym systemie. Wskaźnik wskazuje na adres pierwszego bajtu pamięci.


ZGŁOSZENIE WSPÓLNEGO POLSKO -. PROJEKTU NA LATA: APPLICATION FOR A JOINT POLISH -... PROJECT FOR THE YEARS:.

Shared memory and messages. Functions. process 0. process 1. program 0. program 0. data 0. data 1. program 1. data 0. data 1.

I.Wojnicki, Tech.Inter.

Hippo Boombox MM209N CD. Instrukcja obsługi User s Manual

Programowanie obiektowe w VB cz 2

PROCESORY ARM TRUDNO ZNALEŹĆ PROCESORY O TAK LICZNYCH, ORYGINALNYCH, NOWYCH, POMYSŁOWYCH ROZWIĄZANIACH!

Steeple #3: Gödel s Silver Blaze Theorem. Selmer Bringsjord Are Humans Rational? Dec RPI Troy NY USA

Pielgrzymka do Ojczyzny: Przemowienia i homilie Ojca Swietego Jana Pawla II (Jan Pawel II-- pierwszy Polak na Stolicy Piotrowej) (Polish Edition)


BLACKLIGHT SPOT 400W F

miniature, low-voltage lighting system MIKRUS S

Revenue Maximization. Sept. 25, 2018

TYLKO DO UŻYTKU WŁASNEGO! PERSONAL USE ONLY!

ARNOLD. EDUKACJA KULTURYSTY (POLSKA WERSJA JEZYKOWA) BY DOUGLAS KENT HALL

General Certificate of Education Ordinary Level ADDITIONAL MATHEMATICS 4037/12

Zarządzanie sieciami komputerowymi - wprowadzenie

WYŁĄCZNIK CZASOWY OUTDOOR TIMER

Modulacja i kodowanie. Labolatorium. Kodowanie źródłowe Kod Huffman a

Modelowanie numeryczne w fizyce atmosfery Ćwiczenia 3

Presented by. Dr. Morten Middelfart, CTO

Hurtownia Świętego Mikołaja projekt bazy danych

EXAMPLES of file operations using the library <stdio.h>

MAGNESY KATALOG d e s i g n p r o d u c e d e l i v e r

ITIL 4 Certification

y = The Chain Rule Show all work. No calculator unless otherwise stated. If asked to Explain your answer, write in complete sentences.

PROGRAMOWANIE SYSTEMÓW CZASU RZECZYWISTEGO

Karpacz, plan miasta 1:10 000: Panorama Karkonoszy, mapa szlakow turystycznych (Polish Edition)

Systemy wbudowane. Poziomy abstrakcji projektowania systemów HW/SW. Wykład 9: SystemC modelowanie na różnych poziomach abstrakcji

tum.de/fall2018/ in2357

Nazwa projektu: Kreatywni i innowacyjni uczniowie konkurencyjni na rynku pracy

Pierwszy program. else1 <html> <body> <script type="text/javascript"> var d = new Date()

Strona główna > Produkty > Systemy regulacji > System regulacji EASYLAB - LABCONTROL > Program konfiguracyjny > Typ EasyConnect.

SQL 4 Structured Query Lenguage

DB2 XML w relacyjnych bazach danych wstęp do wprowadzenia. Kuba Pochrybniak

EGZAMIN MATURALNY Z JĘZYKA ANGIELSKIEGO POZIOM ROZSZERZONY CZĘŚĆ I 8 MAJA Godzina rozpoczęcia: 14:00. Czas pracy: 120 minut

Programowanie. Sylwester Arabas. prowadzący ćwiczenia: Magdalena Kuich, Krzysztof Piasecki, Łukasz Dulny. Wydział Fizyki Uniwersytetu Warszawskiego

XML. 6.6 XPath. XPath is a syntax used for selecting parts of an XML document

TTIC 31210: Advanced Natural Language Processing. Kevin Gimpel Spring Lecture 9: Inference in Structured Prediction

TECHNICAL CATALOGUE WHITEHEART MALLEABLE CAST IRON FITTINGS EE

Transkrypt:

ECE 114-9 Arrays -II Dr. Z. Aliyazicioglu Electrical & Computer Engineering Electrical & Computer Engineering 1 Outline Introduction Arrays Declaring and Allocation Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Searching Arrays Multidimensional Arrays 2 1

Passing Arrays to Functions To pass array to function, specify the name without any brackets in call line To pass array hourlytemperatures declared as int hourlytemperatures gc[] = new int gc[ 24 ]; then the function call would be ModifyArray (hourlytemperatures); passes array hourlytemperatures (by reference) to function ModifyArray Every array object knows its own size, don t need to pass the size Value of name of array is address of the first element Function knows where the array is stored» Modifies original memory locations Individual array elements pass by call-by-value pass subscripted name (i.e., myarray[ 3 ]) to function 3 Passing Arrays to Functions Function prototype: void ModifyArray( int b gc[] ); Indicates that ModifyArray expect to receive an integer array in parameter b. Arrays are passes by reference A function can also return an array by defining as int FunctionName (parameter-list) gc[]; The gc [ ] indicates that function returns an integer array. optional int FunctionName (parameter-list) []; 4 2

Example #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype void ModifyArray ( int gc[] ); void ModifyElement ( int ); int _tmain() int array1 gc[] = 1, 2, 3, 4, 5; Console::WriteLine( S"Effects of passing entire array call-by-reference: " S"\n\nThe values of the original array are:" ); for ( int i = 0; i < array1 ->Length; i++ ) Console::Write ( S" 0", array1[ i].tostring() ); ModifyArray(array1); //array is passed by reference Console::WriteLine( S"\nModified array's values:" ); for ( int i = 0; i < array1 ->Length; i++ ) Console::Write ( S" 0", array1[ i].tostring() ); 5 Example(cont) Console::Write ( S" \n\neffects of passing array elements" S"pass-by-value:\n\narray1[3] before" ); Console::Write ( S" ModifyElement : 0", array1[ 3].ToString() ); ModifyElement( array1[ 3 ] ); Console::WriteLine ( S"\narray [3] after ModifyElement : 0", array1[ 3].ToString() ); return 0; // Function modify by array it receives void ModifyArray( int array2 gc[ ]) for ( int j = 0; j < array2 -> Length; j++ ) array2 [ j ] *= 2; 6 3

Example (cont) // Function, local copy of array element a[ 3 ] passed from main. void ModifyElement( int element ) Console::WriteLine ( S"\nValue recived in ModifyElement : 0", element.tostring() ); element *=2; Console::WriteLine ( S" Value calculated in ModifyElement : 0", element.tostring() ); 7 Example Output 8 4

Sorting Array Sorting data Important computing application Virtually every organization must sort some data» Massive amounts must be sorted Ascending or descending order 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 these steps for every element 9 Example Using Arrays Example: a[4]=3,4,2,6,1 Original: 3 4 2 6 1 a[0] a[1] a[2] a[3] a[4] Pass 1: 3 4 2 6 1 3 2 4 6 1 3 2 4 1 6 Pass 2: 3 2 4 1 6 2 3 4 1 6 2 3 1 4 6 Pass 3: 2 3 1 4 6 2 1 3 4 6 Pass 4: 2 1 3 4 6 1 2 3 4 6 Small elements "bubble" to the top To swap a[i] with a[i+1] if ( a[i] > a[i+1]) hold=a[i]; a[i]=a[i+1]; a[i+1]=hold; 10 5

9 5 7 22 a[0] a[1] a[2] a[3] Example for (pass = 1; pass < 3; pass ++) for ( i = 0; i < 3-1; i++) if(a[i] > a[i+1]) temp = a[i]; a[i]=a[i+1]; a[i+1]=temp; 11 Example #include "stdafx.h" #using <mscorlib.dll> using namespace System; void BubleSort ( int gc[] ); void Swap ( int gc[], int ); int _tmain() int a gc[] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37; Console::WriteLine( S"Data items in original order " ); for ( int i = 0; i < a -> Length; i++ ) Console::Write ( S" 0", a[ i].tostring() ); BubleSort( a ); //Sort element of array Console::WriteLine( S"\nData item in ascending order" ); for ( int i = 0; i < a -> Length; i++ ) Console::Write ( S" 0", a[ i].tostring() ); Console::WriteLine ( ); return 0; 12 6

Example (cont) // Function BubleSort void BubleSort( int b gc[ ]) for ( int pass = 1; pass < b -> Length; pass++ ) for ( int i = 0; i < b -> Length -1; i++ ) if (b[i] > b[i+1]) Swap(b,i); // Function Swap void Swap( int c gc[], int first ) int hold; hold = c[ first ]; c[ first ]=c[ first + 1 ]; c[ first+1 ]=hold; 13 Different sorting idea for (i=0;i<n-1;i=++) for ( j=i+1;j<n;j++) if(a[i] > a[j]) temp = a[i]; a[i]=a[j]; a[j]=temp; 2 15 9 22 a[0] a[1] a[2] a[3] temp = a[1] a[1]=a[2] a[2]=temp 14 7

Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search Compare each element of array with key value Useful for small and unsorted arrays Binary search Can only be used on sorted arrays Compares middle element with key» If equal, match found» If key < middle, repeat search through the first half of the array» If key > middle, repeat search through the last half of the array Very fast; at most n steps, where 2 n > # of elements» 30 element array takes at most 5 steps 2 5 > 30 15 #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype int LinearSearch( int gc[], int); Example of Linear search int _tmain() int a gc[] = 2, 6, 4, 8, 10, 12, 14, 16, 18, 20, 22,24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50; Console::Write( S"Please enter the search key :" ); int searchkey = Int32::Parse( Console::ReadLine() ); int elementindex = LinearSearch (a, searchkey); if (elementindex!= -1) Console::WriteLine ( S" Found value in element 0", elementindex.tostring() ); else Console::WriteLine ( S" Value not found"); return 0; 16 8

Example (cont) // Function LienarSearch int LinearSearch( int array gc[], int key) for ( int n = 0; n < array -> Length; n++ ) if (array [n] == key) return n; return -1; 17 Example of binary search: Min=0 a[14]=0, 2,4,6,8,10,12,14,16,18,20,22,24,26,28 15 elements Search key =20 mid=(min+max)/2=7 If (20 ==a[mid]), If (20 > a[mid]) 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 Max=14 min max if (20 >a[mid]) min 16 18 20 22 24 26 28 16 18 20 20 max mid=(min+max)/2=(8+14)/2=11 18 9

Example of binary search #include "stdafx.h" #using <mscorlib.dll> using namespace System; //Function prototype int BinarySearch( int gc[], y); void ShowOutput( int gc[], int, int, int); int _tmain() int a gc[] = 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22,24, 26, 28; Console::Write( S"Please enter the search key :" ); int searchkey = Int32::Parse( Console::ReadLine() ); Console::WriteLine ( S"\nPortion of array searched"); int element = BinarySearch (a, searchkey); if (element!= -1) Console::WriteLine ( S" Found value in element 0", element.tostring() ); else Console::WriteLine ( S" Value not found"); return 0; 19 Example Cont. // Function BinarySearch int BinarySearch( int array gc[], int key) int low = 0; //low index int high = array ->Length -1; //high index int middle; //middel index while ( low <= high) middle=(low + high) / 2; ShowOutput (array, low, middle, high); if (key == array[middle]) return middle; else if (key < array[ middle ]) high=middle-1; else low = middle + 1; return -1; 20 10

Example cont. //Function ShowOutput void ShowOutput (int array gc[], int low, int mid, int high) for (int i=0; i <array ->Length; i++) if (i < low i > high) Console :: Write ( S" "); else if (i == mid) Console::Write( S"0* ", array[i].tostring("00") ); else Console::Write( S"0 ", array[i].tostring("00") ); Console::WriteLine(); 21 Output 22 11