Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.

Similar presentations


Presentation on theme: "Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016."— Presentation transcript:

1 Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016

2 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!

3 Midterm Through today’s lecture Chapters 1 – 6, 9 Bitwise arithmetic (&, |, ^, >, ~) binary to decimal conversion decimal to binary conversions Boolean logic (T vs F)

4 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]); printf(" @ 0x%p\n",&test[i]); } return 0; } Format (1D array)  type array_name [num_elements]; Ex. Array of 4 doubles named, 'test'

5 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

6 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]); printf(" @ 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

7 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]); printf(" @ 0x%p\n",&nums[i]); } return 0; }

8 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; }

9 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

10 ASCII Table

11 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

12 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. http://www.cplusplus.com/reference/cstring/ 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); }

13 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 );

14 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);

15 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 );

16 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]); }

17 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 &

18 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]); }

19 References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3 rd ed., Springer, New York, p. 327. http://www.cppreference.com/wiki/c/io/fopen, Visited 23OCT2010. http://www.cppreference.com/wiki/c/io/fopen


Download ppt "Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016."

Similar presentations


Ads by Google