Notes
Slide Show
Outline
1
CS 211:
Introduction to Computer Programming II
  • Instructor: Brian M. Dennis
  • Teaching Assistants:
  • Tom Lechner, Bin Lin
  • http://www.cs.northwestern.edu/~bmd/cs211/


2
Linear Search

  • #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;
  • }
3
Linear Search

  • 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;
  • }
4
Binary Search

  • #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;
  • }
5
Binary Search
  • 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;
  • }
6
const declaration
  • // nonconstant ptr to nonconstant data
  • #include <cctype>
  • void convertToUppercase(char *sPtr) {
  • while (*sPtr != '\0') {
  • if (islower( *sPtr )) {
  • *sPtr = toupper( *sPtr );
  • }
  • ++sPtr;
  • }
  • }
7
const declaration
  • // nonconstant ptr to constant data
  • void printCharacters((const char)* sPtr) {
  • for ( ; *sPtr != '\0'; sPtr++) {
  • cout << *sPtr;
  • *sPtr = 'A'; // Should be an error
  • }
  • }
8
const declaration
  • // constant ptr to nonconstant data
  • int main() {
  • int x, y;
  • int * const ptr = &x;


  • *ptr = 7;
  • ptr = &x;


  • return 0;
  • }
9
const declaration
  • // constant ptr to constant data
  • int main() {
  • int x = 5, y;
  • const int *const ptr = &x;
  • *ptr = 7;
  • ptr = &y;
  • return 0;
  • }
10
String Functions
  • // #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);
11
String length
  • #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;
  • }
12
String copying
  • #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;
  • }
13
String concatenation
  • #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;
  • }
14
Binary Search w/Strings

  • #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;}
15
Binary Search w/Strings
  • 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; }
16
Dangling Pointers
  • #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;
  • }
17
Dynamic Allocation
  • #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;
  • }
18
That’s a Wrap
  • Reading
    • Catch up on previously assigned stuff