Basic Arrays. Contents. Chris Wild & Steven Zeil. May 28, Description 3

Hasonló dokumentumok
Mapping Sequencing Reads to a Reference Genome

Angol Középfokú Nyelvvizsgázók Bibliája: Nyelvtani összefoglalás, 30 kidolgozott szóbeli tétel, esszé és minta levelek + rendhagyó igék jelentéssel

Széchenyi István Egyetem

Construction of a cube given with its centre and a sideline

On The Number Of Slim Semimodular Lattices

Tudományos Ismeretterjesztő Társulat

Using the CW-Net in a user defined IP network

SQL/PSM kurzorok rész

ANGOL NYELV KÖZÉPSZINT SZÓBELI VIZSGA I. VIZSGÁZTATÓI PÉLDÁNY

Computer Architecture

ANGOL NYELVI SZINTFELMÉRŐ 2013 A CSOPORT. on of for from in by with up to at

USER MANUAL Guest user

EN United in diversity EN A8-0206/419. Amendment

Proxer 7 Manager szoftver felhasználói leírás

Lopocsi Istvánné MINTA DOLGOZATOK FELTÉTELES MONDATOK. (1 st, 2 nd, 3 rd CONDITIONAL) + ANSWER KEY PRESENT PERFECT + ANSWER KEY

Minta ANGOL NYELV KÖZÉPSZINT SZÓBELI VIZSGA II. Minta VIZSGÁZTATÓI PÉLDÁNY

ANGOL NYELV KÖZÉPSZINT SZÓBELI VIZSGA I. VIZSGÁZTATÓI PÉLDÁNY

Relative Clauses Alárendelő mellékmondat

(Asking for permission) (-hatok/-hetek?; Szabad ni? Lehet ni?) Az engedélykérés kifejezésére a következő segédigéket használhatjuk: vagy vagy vagy

Adatbázisok 1. Rekurzió a Datalogban és SQL-99

There is/are/were/was/will be

ANGOL NYELVI SZINTFELMÉRŐ 2012 A CSOPORT. to into after of about on for in at from

Pénzügyi algoritmusok

Phenotype. Genotype. It is like any other experiment! What is a bioinformatics experiment? Remember the Goal. Infectious Disease Paradigm

ANGOL NYELVI SZINTFELMÉRŐ 2011 B CSOPORT. for on off by to at from

Can/be able to. Using Can in Present, Past, and Future. A Can jelen, múlt és jövő idejű használata

Genome 373: Hidden Markov Models I. Doug Fowler

Please stay here. Peter asked me to stay there. He asked me if I could do it then. Can you do it now?

Travel Getting Around

MATEMATIKA ANGOL NYELVEN

3. MINTAFELADATSOR KÖZÉPSZINT. Az írásbeli vizsga időtartama: 30 perc. III. Hallott szöveg értése

Word and Polygon List for Obtuse Triangular Billiards II

Correlation & Linear Regression in SPSS

Create & validate a signature

Website review acci.hu

TestLine - Angol teszt Minta feladatsor

Utazás Szállás. Szállás - Keresés. Szállás - Foglalás. Útbaigazítás kérése. ... kiadó szoba?... a room to rent? szállásfajta.

Előszó.2. Starter exercises. 3. Exercises for kids.. 9. Our comic...17

KN-CP50. MANUAL (p. 2) Digital compass. ANLEITUNG (s. 4) Digitaler Kompass. GEBRUIKSAANWIJZING (p. 10) Digitaal kompas

Tudományos Ismeretterjesztő Társulat

Intézményi IKI Gazdasági Nyelvi Vizsga

A vitorlázás versenyszabályai a évekre angol-magyar nyelvű kiadásának változási és hibajegyzéke

Statistical Inference

ios alkalmazásfejlesztés Koltai Róbert

EXKLUZÍV AJÁNDÉKANYAGOD A Phrasal Verb hadsereg! 2. rész

Angol érettségi témakörök 12.KL, 13.KM, 12.F

Lexington Public Schools 146 Maple Street Lexington, Massachusetts 02420

T Á J É K O Z T A T Ó. A 1108INT számú nyomtatvány a webcímen a Letöltések Nyomtatványkitöltő programok fülön érhető el.

AZ ACM NEMZETKÖZI PROGRAMOZÓI VERSENYE

INDEXSTRUKTÚRÁK III.

STUDENT LOGBOOK. 1 week general practice course for the 6 th year medical students SEMMELWEIS EGYETEM. Name of the student:

16F628A megszakítás kezelése

1. Gyakorlat: Telepítés: Windows Server 2008 R2 Enterprise, Core, Windows 7

MATEMATIKA ANGOL NYELVEN

mondat ami nélkül ne indulj el külföldre

Contact us Toll free (800) fax (800)

PIACI HIRDETMÉNY / MARKET NOTICE

Tudok köszönni tegezve és önözve, és el tudok búcsúzni. I can greet people in formal and informal ways. I can also say goodbye to them.

Cluster Analysis. Potyó László

Eladni könnyedén? Oracle Sales Cloud. Horváth Tünde Principal Sales Consultant március 23.

Az Open Data jogi háttere. Dr. Telek Eszter

Ültetési és öntözési javaslatok. Planting and watering instructions

bab.la Cümle Kalıpları: İş Sipariş İngilizce-Macarca

bab.la Cümle Kalıpları: İş Sipariş Macarca-İngilizce

MATEMATIKA ANGOL NYELVEN

Bevezetés a kvantum-informatikába és kommunikációba 2015/2016 tavasz

Léptetőmotorok. Előnyök: Hátrányok:

Miskolci Egyetem Gazdaságtudományi Kar Üzleti Információgazdálkodási és Módszertani Intézet Factor Analysis

Performance Modeling of Intelligent Car Parking Systems

Utasítások. Üzembe helyezés

1. MINTAFELADATSOR KÖZÉPSZINT. Az írásbeli vizsga időtartama: 30 perc. III. Hallott szöveg értése

KELET-ÁZSIAI DUPLANÁDAS HANGSZEREK ÉS A HICHIRIKI HASZNÁLATA A 20. SZÁZADI ÉS A KORTÁRS ZENÉBEN

Budapest By Vince Kiado, Klösz György

Longman Exams Dictionary egynyelvű angol szótár nyelvvizsgára készülőknek

Tavaszi Sporttábor / Spring Sports Camp május (péntek vasárnap) May 2016 (Friday Sunday)

ANGOL NYELVI SZINTFELMÉRŐ 2014 A CSOPORT

RPC Remote Procedure Call Távoli eljárás hívás

Adatbázis-kezelés ODBC driverrel

(NGB_TA024_1) MÉRÉSI JEGYZŐKÖNYV

C# nyelv alapjai. Krizsán Zoltán 1. Objektumorientált programozás C# alapokon tananyag. Általános Informatikai Tanszék Miskolci Egyetem

Véges szavak általánosított részszó-bonyolultsága

3. MINTAFELADATSOR EMELT SZINT. Az írásbeli vizsga időtartama: 30 perc. III. Hallott szöveg értése

A modern e-learning lehetőségei a tűzoltók oktatásának fejlesztésében. Dicse Jenő üzletfejlesztési igazgató

MATEMATIKA ANGOL NYELVEN

KIEGÉSZÍTŽ FELADATOK. Készlet Bud. Kap. Pápa Sopr. Veszp. Kecsk Pécs Szomb Igény

Lesson 1 On the train

A teszt a következő diával indul! The test begins with the next slide!

It Could be Worse. tried megpróbált while miközben. terrifying. curtain függöny

Tudományos Ismeretterjesztő Társulat

Emelt szint SZÓBELI VIZSGA VIZSGÁZTATÓI PÉLDÁNY VIZSGÁZTATÓI. (A részfeladat tanulmányozására a vizsgázónak fél perc áll a rendelkezésére.

discosnp demo - Peterlongo Pierre 1 DISCOSNP++: Live demo

Minden amerikanisztika szakos vegye fel az az alábbi elıadásokat (a


Abigail Norfleet James, Ph.D.

JEROMOS A BARATOM PDF

boolean motoros_szelep_vegallas_el = true; boolean serial_adatok_kikuldese = true; // ************ Hőmérséklet érzékelők Dallasos!!!!

A évi fizikai Nobel-díj

Miskolci Egyetem Gazdaságtudományi Kar Üzleti Információgazdálkodási és Módszertani Intézet. Hypothesis Testing. Petra Petrovics.

MOZGÓKÉPKULTÚRA ÉS MÉDIAISMERET ANGOL NYELVEN

7. Bemutatom a barátomat, Pétert. 8. Hogy hívják Önt? 9. Nagyon örülök, hogy találkoztunk. 10. Nem értem Önt. 11. Kérem, beszéljen lassabban.

Átírás:

Chris Wild & Steven Zeil May 28, 2013 Contents 1 Description 3 1

2 Example 4 3 Tips 6 4 String Literals 7 4.1 Description...................................... 7 4.2 Example........................................ 11 4.3 Tips........................................... 12 5 Common Errors Using Arrays 13 CS333 2

1 Description Arrays are convenient for naming and using a collection of objects of the same type. For example, if you wanted to find the biggest of three integers you could name each integer individually, but if you wanted to store and find the biggest of a thousand integers, naming each integer is inconvenient to say the least. All the objects in an array must be of the same type (e.g. integers, floats, user defined objects, or even arrays themselves) An array has a fixed size which must be known at the time the array is defined. Each array has a unique name and each element in the array has a unique position. CS333 3

The position is designated by an integer expression called the array index. The positions in an array start at the integer 0 (the first element is at position 0) and goes to the size of the array 1. Thus an array with 3 elements has positions 0,1 and 2 To name an element in an array, use the array name followed by the position enclosed in square brackets ( [ and ] ) 2 Example int somearray [ 3 ] ; / / t h i s i s an array of three integers. somearray [ 0 ] = 567; / / s e t the f i r s t element in the array / / to the integer constant "567"; CS333 4

int someinteger = 4 ; / / t h i s i s NOT an array j u s t a plain / / integer with i n i t i a l value "4" somearray [ 1 ] = someinteger ; / / s e t s the second element in the / / array to the value " 4" / / At t h i s point the value of the third element of the / / array ( somearray [ 2 ] ) i s undefined float avector [ 1 0 0 ] ; / / a vector of 100 floating point / / numbers, indexed from 0 to 99 avector [ 9 9 ] = 3. 4 ; / / s e t s the value of the l a s t element to 3.4 char astring [ 2 0 ] ; / / an array of characters i s also known as a string astring [ 9 ] = z ; / / s e t s the 10th character to z CS333 5

3 Tips Counting from 0 instead of 1 is strange at first and is the cause of errors. Why count from 0? think about this - how many single digits numbers are there in the decimal system? Answer: 10, the digits 0 through 9. If you start counting at 1 the last number would be "10" which requires two digits (thus more memory) then if you started counting at 0 and went to 9 for the 10th digit 0 is the first digit, 9 is the 10th digit The size of an array must be a constant (so that the compiler can known how much memory to allocate), however it can be any (practical) value. It is recommended CS333 6

that you define an integer constant and use that to define your arrays. - This allows the size of the array to change easily by changing the value of the constant. You will see more examples of this later on which better motivate the recommendation. const int SIZE_OF_SOME_ARRAY = 3 ; int somearray [SIZE_OF_SOME_ARRAY ] ; / / d e f i n e s an array of three elements Arrays are prone to several limitations and can be the source of errors in logic if misused. For more details check here 4 String Literals 4.1 Description When we write a "string" inside quotes, it is called a string literal, e.g., CS333 7

" This i s a string l i t e r a l " Oddly enough, this is not a std::string. String literals are actually done as nullterminated character arrays. "character arrays", because they are simply arrays of char "null-terminated", meaning that the final character in the string is an ASCII NUL character (the char of value zero). This character is "invisible" when printed, so it does not affect the visible appearance of the string. But when our code finds that zero value in the array, it knows that it has reached the end of the string. Strings that have been stored into a null-terminated aray are variously referred to as char arrays and C-strings. (The latter refers to the fact that this style of storage for strings is inherited from C++ s "parent" language, C.) CS333 8

Sadly, lots of people (including programmers and textbook authors who should know better) get pretty lax on this point, and frequently use the term "string" ambiguously to refer to both std::string and C-strings. A string literal is of type "const char *". (We have not yet introduced the * types in C++. For now it s enough to know that char* is pretty much the same as char[] and the "const" means that you can look at the individual characters inside a string literal, but you aren t allowed to change them.) You can input and output C-strings using the standard insertion «and extraction» operators on cin and cout. When outputting a C-string, the insertion operator stops at the null termination character CS333 9

When inputting a C-string, the extraction operator stops at the first whitespace character (e.g. blank, tab, new line) To include a double quote in a string literal put a backslash character \ before the double quote. This is called escaping the character. Other escape sequences include: character desired escape sequence description " \" double quote \ \\ backslash null character \0 Used as null termination character tab \t tab character new line \n new line character CS333 10

4.2 Example char name[ 4 ] = "Pam" ; / / need 4 characters don t f o r g e t the / / null termination character implicit at / / the end of a s t r i n g l i t e r a l name[ 0 ] = S ; / / changes name to "Sam" cout << name; / / prints out the s t r i n g "Sam" char anothername [ 6 ] ; / / unused array anothername [ 0 ] = name[ 0 ] ; / / copies the character S to anothername anothername [ 1 ] = name[ 1 ] ; / / copies the character a to anothername anothername [ 2 ] = name[ 2 ] ; / / copies the character m to anothername anothername [ 3 ] = name[ 3 ] ; / / copies the character \ 0 ( the null / / termination character ) to anothername CS333 11

cout << anothername ; / / prints out the s t r i n g "Sam" cin >> name; / / reads character up to the f i r s t / / white space, puts in name and adds null termination characte / / / / NOTE: assumes user types l e s s than 4 characters otherwise / / will overflow the array and lead to possible programming errors. / / / / This i s one of the reasons to avoid character arrays and to use / / the " s t r i n g " c l a s s. 4.3 Tips As a general rule, use C-strings/character arrays when you want to type a specific string within your code and to use it without modification. When you want to change strings, modify them, or pull pieces out of them, use a std::string. Remember to allocate an extra space for the null character when defining char arrays that hold null terminated strings CS333 12

There is an older C string library ( <cstring> ) for operating on character arrays to perform common string manipulation operations Input of C-strings is tricky because the compiler and run time system does not check to see if there is enough room for the input. 5 Common Errors Using Arrays There are several cases where array variables behave differently than regular C++ variables. For instance: You cannot use an array in an assignment statement (you can use an array element though) CS333 13

You cannot return an array as the return value of a function (you can return a pointer to an array) The size of an array is not an inherent property of it. When you pass an array to a function, the function in general has no idea how long it is. This is why C-strings (which are char arrays) are typically null terminated. The null termination character is one way to know where the string ends. Null termination does not work for other kinds of arrays. "0" is a "real" value typically used for purposes other than signaling the end of the array. Because the compiler and C++ run-time system do not know the size of an array, it is possible to attempt to access an element which is not in the array (by using an incorrect index value.). CS333 14

Accessing elements outside the array can lead to unpredictable results and is a serious error in logic. When reading in an array, it is possible to overwrite memory that does not belong to the array. CS333 15

Example: Common Errors / / the following statements are not allowed and w i l l cause compiler e r r o r s int a [ 1 0 ], b [ 1 0 ] ; a = b ; / / cannot assign an entire array visual c++ gives l e f t operand not a a [ 3 ] = b [ 3 ] ; / / t h i s i s OK int [ ] myfunction ( ) ; / / cannot return an entire array int * myfunction ( ) ; / / t h i s i s OK CS333 16

Example: Errors in Reading Arrays / / Examples in reading input char somestring [ 1 1 ] ; / / holds up to 10 characters plus the null character cin >> somestring ; / / reads next bunch of non whitespace characters into so / / i n s e r t s null character are end / / assumes that t h i s will be 10 or l e s s characters cin. g e t l i n e ( somestring, 1 1 ) ; / / reads at most 10 characters or until / / end of line / / i n s e r t s null character at end / / i f the line contains 9 or l e s s characters, the new line / / character i s removed / / i f the l i n e contains more than 9 characters, the extra / / characters ( i f any ) and the newline are kept. CS333 cin. get ( somestring, 1 1 ) ; / / reads17at most 10 characters or until end of lin / / i n s e r t s null character at end