Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 14 Strings and Searching.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 14 Strings and Searching."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 14 Strings and Searching

2 String example No. 2 Read up to 1000 names and ages into a table of structures, and use a linear search to locate a target name and to print that person’s age. In array, table, each entry comprises a name and age: nick25 claude55 ted96 graham89 fred78 and a target of graham.

3 Struct and Prototypes #include "stdio.h" #include "string.h“ struct entry { char name[21]; int age; }; // prototypes int read_table(struct entry [], int); int linear_search(struct entry[],int,char[]);

4 Main function (beginning) int main(void) { struct entry table[1000]; // max of 1000 entries int n, position; char target[21]; n = read_table(table, 1000); // returns actual no. of entries(max 1000)

5 printf("\ntype target name: "); gets(target); position = linear_search(table, n, target); // finds position of target in table if (position == -1) { printf("\n%s not found", target); } else { printf("\n%s is %d years old!", table[position].name, table[position].age); } return 0; }// end main

6 int read_table(struct entry table[], int max) { int i = 0;// line count & index char line[81];// max line of 80 printf("type name (max 20 letters)”); printf(“ and age on same line\n"); printf("press control/d to finish\n"); while (gets(line) != NULL && i < max) {// while not end of input sscanf(line, "%s%d", table[i].name, &table[i].age); // table[i].name is a string // ie already an address! i++;// counts lines & indexes } return i;// actual number of lines }// end read table

7 Linear Search Function int linear_search(struct entry table[], int n, char target[]) {int i; for (i=0; i<n; i++) { if (strcmp(table[i].name, target) == 0) { // if names are same return i; } }// end for loop return -1; // not found }// end linear search

8 Binary Search Instead of using a linear search we define a binary search which requires names to be sorted, and a target of sue

9 subscript nameage 0alan23start 1bill45 2brian76 3colin45 4eric34 5fred54 6john44 7nick25 8peter23middle 9ricky34 10sue23 11ted98 12terry65 13tracy65 14wayne33 15`winny57 16zak88end

10 Binary Search Binary search is much faster than linear search. For an ordered list of N entries, linear search requires a maximum of N array accesses, and binary search requires a maximum of log 2 N (rounded up). Examples: for N = 1 000 log 2 N = 10 (rounded up) for N = 1 000 000 log 2 N = 20 (rounded up)

11 Logarithms base 2 The Logarithm of a number is the power the base (i. e. 2) is raised to get the number. Examples: 2 2 = 4solog 2 4 = 2 2 3 = 8solog 2 8 = 3 2 4 = 16solog 2 16 = 4 2 5 = 32solog 2 32 = 5 2 6 = 64solog 2 64 = 6 2 10 = 1 024solog 2 1024 = 10 2 20 = 1 048 576solog 2 1048576= 20

12 Binary Search Function int binary_search(struct entry table[], int n, char target[]) { // table SORTED in ascending order int start, end, mid, found; start = 0; end = n – 1; found = 0;// found = false

13 while (start <= end && !found) { mid = (start + end)/2;// round down if(strcmp(table[mid].name,target)==0) {// if same name found = 1; // found = true } else if (strcmp(target,table[mid].name) < 0) {// if target less end = middle-1; // ignore 2nd half } else// if target greater { start = middle+1;// ignore 1st half } // end if }// end while

14 if (found) // found != 0, ie true { return middle; } else { return -1; } } // end binary search


Download ppt "Introduction to C Programming CE00312-1 Lecture 14 Strings and Searching."

Similar presentations


Ads by Google