Download presentation
Presentation is loading. Please wait.
1
CISC105 – General Computer Science Class 11 – 07/10/2006
2
Some Array Properties When you pass arrays as parameters they are always passed by reference and are changeable in a function. Example array_func_pass1.carray_func_pass1.c You can “lock” an array from being changeable within a function by using const in the parameter declaration see array_func_pass2.c array_func_pass2.c
3
Binary Search Revisited A Binary Search is a way to quickly search a sorted array by eliminating half of the possibilities on each iteration Binary Search ExampleBinary Search
4
Array Sort Revisited Bubble Sort – Scan array and “bubble” the highest value to last position in the array –Example – Sorry no code - but I bet you’ll see this in Project 2! How does this differ from Selection Sort?
5
Multidimensional Arrays Arrays Can be Declared in more than 1 dimension (a 2-D array is like a table) –You can declare an array with 3 + dimensions! –Tic-Tac-Toe exampleTic-Tac-Toe
6
Strings A string is a array of characters ( char ) We have used strings already! –printf(“Hello world!\n”); We declare string the same way we would any other array –char name[20]; - Creates a character array (or string) of length 20 – can only use 19 characters as we need 1 space for the ‘\0’ or NULL character We will use the placeholder %s to input/output a string Example string1.cstring1.c
7
Strings How long would earch character array be if we declared: –char first[] = “John”; –char middle[] = “Q”; –char last[] = “Public”; A NULL or ‘\0’ is automatically added to end of a scanf or initialized string Using scanf will only return a string up to a whitespace. See string2.cstring2.c
8
The string.h library C has a library of string function that you can use to make you life easier… You will need to use these functions in the next project!!! They are included in the string.h library string3.c – strcpy & strncpy
9
The string.h library String concatenation (string4.c)string4.c –strcat(string1, string2) – concatenates string2 on string1 –strncat(string1, string2, num) – concatenates n number of string2 char on string1 String comparison (string5.c)string5.c –strcmp(string1,string2) – compares the entire string –strncmp(string1, string2, num) – compares the first n characters
10
The string.h Library String Length (string6.c)string6.c –Strlen(string) returns the length of the string up to the ‘\0’ character String Token (string7.c) –strtok(string, delimiter) returns the first token in the string – returns NULL if nothing is left in the string. –NOTE: Using strtok will alter the string you pass – only pass the string if you no longer need it in the original for otherwise pass a copy
11
Array of Pointers We can declare an array of pointers to strings (like a multidimensional array) See string8.c
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.