Unformatted text preview:

COMS 1003: Introduction to Computer Programming in CArrays, String, and Command Line ArgumentsSeptember 22nd 2005Outline●Relate to Readings●Arrays–Data Collections for multiple objects of the same type●Strings–sequences of characters–arrays of characters–string literals and string constants–string operations (string.h)●Command line argument processingReadings●PCP: Chapters –2 & 3–5●TCPL: Chapters–2–1.2Simple Data Collections●Store a student grade:–int locasto_grade = 0;●What if we have 70 students?–int student0_grade = 0;–int student1_grade = 0;–int student2_grade = 0;–int student...–int student69_grade = 0;–whew!Arrays●Arrays are typed collections of the same data type●Arrays allow for grouping of data under one common name●Array elements are accessed by giving an offset, or index into the array. ●Offsets (or indexes) are always integer values–it doesn't make sense to say “give me the value at element number 5.34Array Organization●Arrays have a name–e.g., student_grades●Arrays have a length–not the number of elements in the array, but rather how much memory the array takes up–in C, this length is not stored with the array. You, the programmer, must keep track of it●Array elements must be consistently typed–all elements must be of the same typeAccessing Array Elements●Each element is at a unique position in the array–position is indicated by the subscript or index value–the value of the subscript or index is NOT the value of the element @ that index or positionint student_array[70];student_array[45] = 100;student_array[2] = 50;student_array[34] = 34;student_array[0] = 100;Declaring an Array●Very similar to declaring a single variable of that type●Just add brackets and a size//declare an integer variableint myinteger;//declare an int arrayint part_numbers[400];typenamesize (number of elements)Initializing an Array●There are several ways to initialize the data in an array. The most straightforward are to have an explicit loop or series of statements that initialize each element. This is tedious.●The second way is to provide a comma separated list of elements in braces, like so:int temperatures[] = {89,54,100,0,34,-40};//there is no need to specify a size, b/c//compiler does it for you.Accessing Array Elements II●Array elements can be accessed by writing the name of the array and the position of the index of the item in the array.●This expression is equivalent to the value of the array at that indexint data[2];data[0] = 400;data[1] = 3;printf(“value at data[1] is %d\n”,data[1]);Visualizing an Arrayfloat sample_values[6] = {0.0};sample_values[0] = -1.43;sample_values[5] = 23.47;//programming error!sample_values[6] = 7.1;0123456???-1.4323.470.00.00.00.0Array Notes●The previous slide shows us:–arrays start indexing from 0, not 1–thus, the array length has a maximum index of (length-1)●Also, note that C doesn't do bounds checking, so you can inadvertantly read or write past the end (or beginning) of an array and the language won't stop you. However, this is (99.99% of the time) an error or not what you wanted to do.Advanced Array Topics●There are other ways to access array elements–we'll cover one when we cover pointers–we'll cover the other when we talk about repetition control structures●Arrays can be nested; that is, you can have an array of arrays, or an array of array of arrays, or ...–the basic idea is that you just add more []'s per dimension–a two-dimensional array is an array of arrays or a tableMulti-dimensional Arrays//declare a two dimensional array of integersint num_students = 70;int num_grades = 7;int class_grades[num_students][num_grades];//also legal:int class_grades[70][7];//access an element by providing subscriptsclass_grades[45][6] = 3;//print the 6th student's 5th gradeprintf(“%d\n”,class_grades[5][4]);StringsWhat are Strings?●Strings are arbitrarily long sequences of characters●C keeps many things as simple as possible, so strings in C are not first class data objects–Rather, they are simply character arrays–This makes sense, but you have to keep some rules in mind when operating on strings●Just remember that a string is always an array of characters (and treat it as such) and you'll be fineCharacter Basics●Characters in C are 8-bit (1 byte) values that can sometimes be treated like small integers.●How many unique integer values can you specify with 8 bits?●In a program, you may represent a character like:char somechar = 'B';but numbers work equally well:char anotherchar = 66;Examples of Strings●You've seen some strings before–String literals: a sequence of characters in quotation marks inside the text or body of a program–For example:the “result is %d\n” is a string literal.●A character array is the other common way to refer to a stringprintf(“result is %d\n”, result);char studentname[30];String Gotcha's●In order to truly treat a character array as a string, you must make sure it is null-terminated–the last character in the array (or of the string if the string is smaller than the array length) must be a null character–the null character is written as '\0' (backslash zero)–recall the '\n' for newlines–the C compiler automatically null-terminates string literalsA simple example//declare and initialize 2 char arrayschar myname[] = {'M','i','k','e','\0'};char yourname[] = {'E','l','i','\0'};//change some of the values in one stringmyname[1] = 'a';myname[2] = 't';myname[3] = 't';//note that chars are really very small intsyourname[0] = 65;//what is yourname?Char Arrays as ... char arrays●Every string is a character array●Not every character array is a string–character arrays are just collections of characters–can hold any legal char value (1 byte or 8 bits of information)–interpretation depends on context–the data stored in a character array doesn't need to be treated like a string–nevertheless, you can still treat it like a string. C allows you to shoot yourself in the foot if you really want toString Operations●Many basic string operations are tedious to write●So these operations are provided as functions in the standard library●To use them, your program should #include <string.h>●Operations include:–strnlen (return the length of the string)–strncmp (compare two strings lexicographically)–strncpy (copy one string to another)String Properties●The length of a string is the


View Full Document

Columbia COMS 1003 - Arrays String

Download Arrays String
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays String and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays String 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?