CS 211:
Introduction to Computer Programming II
| Instructor: Brian M. Dennis | |
| Teaching Assistants: | |
| Tom Lechner, Bin Lin | |
| http://www.cs.northwestern.edu/~bmd/cs211/ | |
| #include <iostream> | |
| using namespace std; | |
| int linearSearch(const int a[], int k, const int size); | |
| int main() { | |
| const int arraySize = 100; | |
| int a[arraySize]; | |
| int key; | |
| for (int i = 0; i < arraySize; i++) { | |
| a[i] = 2 * i; | |
| } | |
| cout << "Enter integer search key: "; | |
| cin >> key; | |
| int element = linearSearch(a, key, arraySize); | |
| if (element != -1) { | |
| cout << "Found value in element: " << element << endl; | |
| } else { | |
| cout << "Value not found" << endl; | |
| } | |
| return 0; | |
| } | |
| int linearSearch(const int array[], int key, int sizeOfArray) { | |
| for (int j = 0; j < sizeOfArray; j++) { | |
| if (array[j] == key) { | |
| return j; | |
| } | |
| } | |
| return -1; | |
| } | |
| int linearSearch(const int array[], int key, int sizeOfArray) { | |
| const int *t_array = array; | |
| while (t_array < (array + sizeOfArray)) { | |
| if (*t_array++ == key) { | |
| return (t_array – array – 1); | |
| } | |
| } | |
| return -1; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int binarySearch(const int a[], int k, int low, int high, int size) | |
| int main() { | |
| const int arraySize = 100; | |
| int a[arraySize]; | |
| int key; | |
| for (int i = 0; i < arraySize; i++) { | |
| a[i] = 2 * i; | |
| } | |
| cout << "Enter integer search key: "; | |
| cin >> key; | |
| int element = binarySearch(a, key, 0, arraySize – 1, arraySize); | |
| if (element != -1) { | |
| cout << "Found value in element: " << element << endl; | |
| } else { | |
| cout << "Value not found" << endl; | |
| } | |
| return 0; | |
| } | |
| int binarySearch(const int a[], int searchKey, int low, int high, | |
| int size) | |
| { | |
| int middle; | |
| while (low <= high) { | |
| middle = (low + high) / 2; | |
| if (searchKey == a[middle]) { | |
| return middle; | |
| } else if (searchKey < a[middle]) { | |
| high = middle – 1; | |
| } else { | |
| low = middle + 1; | |
| } | |
| } | |
| return -1; | |
| } |
| // nonconstant ptr to nonconstant data | |
| #include <cctype> | |
| void convertToUppercase(char *sPtr) { | |
| while (*sPtr != '\0') { | |
| if (islower( *sPtr )) { | |
| *sPtr = toupper( *sPtr ); | |
| } | |
| ++sPtr; | |
| } | |
| } |
| // nonconstant ptr to constant data | |
| void printCharacters((const char)* sPtr) { | |
| for ( ; *sPtr != '\0'; sPtr++) { | |
| cout << *sPtr; | |
| *sPtr = 'A'; // Should be an error | |
| } | |
| } |
| // constant ptr to nonconstant data | |
| int main() { | |
| int x, y; | |
| int * const ptr = &x; | |
| *ptr = 7; | |
| ptr = &x; | |
| return 0; | |
| } |
| // constant ptr to constant data | |
| int main() { | |
| int x = 5, y; | |
| const int *const ptr = &x; | |
| *ptr = 7; | |
| ptr = &y; | |
| return 0; | |
| } |
| // #include <cstring> | |
| char *strcpy(char *s1, const char *s2); | |
| char* strncpy(char* s1, const char* s2, | |
| size_t n); | |
| char* strcat(char* s1, const char *s2); | |
| char* strncat(char *s1, const char* s2, | |
| size_t n); | |
| int strcmp(const char* s1, const char *s2); | |
| int strncmp(const char* s1, const char* s2, size_t n); | |
| size_t strlen(const char *s); |
| #include <cstring> | |
| #include <iostream> | |
| using namespace std; | |
| int main(int argc, char* argv[]) { | |
| int total_length = 0; | |
| for(int i = 1; i < argc; i++) { | |
| total_length += strlen(argv[i]); | |
| } | |
| cout << "Total characters: " << total_length | |
| << endl; | |
| return 0; | |
| } |
| #include <iostream> | |
| #include <cstring> | |
| using namespace std; | |
| int main(int argc, char *argv[]) { | |
| const int BUF_SIZE = 1024; | |
| char buf[BUF_SIZE] = { '\0' }; | |
| for(int i = 1; i < argc; i++) { | |
| strcpy(buf, argv[i]); | |
| } | |
| return 0; | |
| } |
| #include <iostream> | |
| #include <cstring> | |
| using namespace std; | |
| int main(int argc, char *argv[]) { | |
| const int BUF_SIZE = 1024; | |
| char buf[BUF_SIZE] = { '\0' }; | |
| for(int i = 1; i < argc; i++) { | |
| strcat(buf, argv[i]); | |
| } | |
| cout << "Command line glommed together.\n"; | |
| cout << buf << endl; | |
| return 0; | |
| } |
| #include <iostream> | |
| using namespace std; | |
| int binarySearch(char** a, char* k, | |
| int low, int high, int size); | |
| int main() { | |
| char* strings[] = { "Go", "Northwestern", | |
| "Wildkats", "You" }; | |
| char search_key[1024]; | |
| cout << "Enter string search key: "; | |
| cin >> search_key; | |
| int element = binarySearch(strings, search_key, | |
| 0, 3, 3); | |
| if (element != -1) { | |
| cout << "Found value in element: " << element | |
| << endl; | |
| } else { | |
| cout << "Value not found" << endl; | |
| } | |
| return 0;} |
| int binarySearch(char** a, char* searchKey, | |
| int low, int high, int size) | |
| { | |
| int middle; | |
| while (low <= high) { | |
| middle = (low + high) / 2; | |
| int cmp_res = strcmp(searchKey, a[middle]); | |
| if (!cmp_res) { | |
| return middle; | |
| } else if (cmp_res < 0) { | |
| high = middle - 1; | |
| } else { | |
| low = middle + 1; | |
| } | |
| } | |
| return -1; } |
| #include <cstdlib> | |
| #include <time> | |
| char* random_string(int n_chars) { | |
| srand( time(0)); | |
| const int max_size = 80; | |
| char buf[max_size] = {'\0'}; | |
| for (int i = 0; | |
| (i < n_chars) && (i < max_size)); | |
| i++) { | |
| int offset = rand() % 26; | |
| buf[i] = 'a' + offset; | |
| } | |
| return buf; | |
| } |
| #include <iostream> | |
| #include <cstring> | |
| using namespace std; | |
| int main(int argc, char* argv[]) { | |
| int buf_length = 0; | |
| for(int i = 1; i < argc; i++) { | |
| buf_length += strlen(argv[i]); | |
| } | |
| buf_length++; | |
| char* buf = new char[buf_length]; | |
| buf[0] = '\0'; | |
| for (int j = 1; j < argc; j++) { | |
| strcat(buf, argv[i]) | |
| } | |
| cout << "Command line glommed together\n"; | |
| cout << buf << endl; | |
| delete buf; | |
| return 0; | |
| } |
| Reading | ||
| Catch up on previously assigned stuff | ||