Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016
Announcements Homework #3 (due next week) Homework #4 (exam review) posted (due after spring break, but don’t wait!) Midterm in Lab next week Open book Open notes Selective web resources Bring your lab kit!
Midterm Through today’s lecture Chapters 1 – 6, 9 Bitwise arithmetic (&, |, ^, >, ~) binary to decimal conversion decimal to binary conversions Boolean logic (T vs F)
What is an Array? So far we've dealt with scalar variables contain just one value Arrays are collections of data of the same type that occupy contiguous memory locations Individual values in the collection are called elements of the array Need to declare before use: #include int main() { int i=0; double test[4] = {0}; test[0]=95.5; test[1]=74.0; test[2]=88.5; test[3]=91.0; for(i=0; i<4; i++) { printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; } Format (1D array) type array_name [num_elements]; Ex. Array of 4 doubles named, 'test'
What is an array? - 2 int nums [10]; 10 element array of integers Element no. 3 is accessed by: nums [2] because indexing begins at 0
Accessing Array Elements Individual elements of an array are accessed by their index number ** Important Note** Array indexing starts at zero (take note of this, it is easy to forget!) char test[4]; the first element is test [0] the fourth element is test [3] #include int main() { int i=0; doube test[4]={0}; test[0]=95.5; test[1]=74.0; test[2]=82.75; test[3]=91.5; for(i=0; i<4; i++) { printf("test[%d]=%lf",i,test[i]); 0x%p\n",&test[i]); } return 0; } Be careful! The C compiler may not stop you from indexing beyond the boundary of your array. (What could happen?) What is test[4]? array_practice2.c
Initializing Array Elements Use braces to enclose the elements Separate elements by commas Can set number of elements in declaration: Explicit: int nums[5] Implicit: int imp[ ] = {1, 2}; /* array_practice3.c */ #include int main() { int i=0; int nums[5]={0,1,2,3,4}; for(i=0; i<5; i++) { printf("nums[%d]=%d",i,nums[i]); 0x%p\n",&nums[i]); } return 0; }
Initializing Array Elements For long, non-zero initializations, for loop is usually more efficient to program Note the use of a variable index /* array_practice3.c */ #include const int BIG_INDEX = 5000; int main() { int i=0; int nums[BIG_INDEX]={0}; for(i=0; i < BIG_INDEX; i++) { nums[i] = 34; } return 0; }
Strings Strings are encoded arrays of characters designed to hold language Recall that all characters are encoded as numeric values Most modern systems use a variant of ASCII or Unicode We’ll assume ASCII
ASCII Table
Strings By convention, strings in C are a null terminated array of characters There is no intrinsic string datatype Null equals character value 0 i.e. char z = 0; z has a null character written \0 Declaration: char string[] = “Hello world”; Stored: Hello World\0 (ie 12 characters) All string handling expects this
Working with strings Much like working with pointers and arrays Many helper routines in string.h strcmp – tests strings for equivalence strcat – concatenates two strings strstr – locates a string within a string Etc. char string[] = “Hello World”; for( int i = 0; string[i] != 0 ; ++i ) { if( string[i] >= ‘A’ && string[i] <= ‘Z’ ) { string[i] = string[i] + (97 – 65); }
Formatted Input (1) We use two commands to properly obtain input from the keyboard fgets & sscanf fgets places data collected form the keyboard into a buffer stored as a character string up to and including a carriage return (when the user presses return or enter) fgets( buffer, sizeof(buffer), stdin );
Formatted Input (2) sscanf splits the input into variables Space delimited Uses conversion metasymbols like printf sscanf( buffer, “%d %f”, &x, &y); Note the ambersands (&) & is commonly required for input & is not required for strings sscanf( buffer, “%s %s”, &city, &state);
Formatted Input (3) #include #define BUFF_SIZE 100 //const int BUFF_SIZE = 100; int main( void ) { float x = 0; float y = 0; char buffer[BUFF_SIZE] = { 0 }; printf( "Enter two floats(x y): "); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f %f", &x, &y );
Formatted Input (4) #include #define BUFF_SIZE 100 #define NUM_SIZE 100 int main( void ) { float x[NUM_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NUM_SIZE; ++i ) { printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f", &x[i]); }
Formatted Input (5) #include #define BUFF_SIZE 100 #define NAME_SIZE 100 int main( void ) { char name[NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; printf("Enter name:” ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", name); // note, no &
Formatted Input (5) #include #define BUFF_SIZE 100 #define NAME_SIZE 100 #define NAME_QTY 5 int main( void ) { char names[NAME_QTY][NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NAME_QTY; ++i ) { printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", names[i]); }
References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3 rd ed., Springer, New York, p Visited 23OCT