230 likes | 321 Views
Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lectures 9, Array examples Friday 2 July 2010. Two-week ISTE workshop on Effective teaching/learning of computer programming. Overview. A quiz Representation of character data
E N D
Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay Lectures 9, Array examples Friday 2 July 2010 Two-week ISTE workshop onEffective teaching/learning of computer programming
Overview • A quiz • Representation of character data • Using arrays to represent character strings • Example of another way of finding roots • Accessing and manipulating elements of an array • Analysis of the quiz from lecture 7 • Loop invariant code
A quiz Q. Computers have memory to store instructions and data. Which technology was used in the earliest digital computer • Magnetic core • Electronic Valves • Semiconductor memory • Cathode ray tube • None of these
Character data type in C • Character constant is written as a single character within quotes ‘a’ ‘P’ ‘o’ ‘O’ ‘5’ ‘0’ ‘\n’ ‘\0’ • Internally, it is represented as an integer value equivalent to its ASCII code. • It occupies one byte which is the smallest addressable unit of memory
Character data type • Each character which you see on your terminal, or which you input using your keyboard is represented by an internal numerical “code” • Typically ASCII code is used (one byte long) • ‘a’ has a code value (in decimal) of 97 • ‘z’ is 122 , ‘A’ is 65, ‘Z’ is 90 • ‘ ’ (space or blank) is 32 ‘\n’ is 10 • One can declare variables in a program of type char char letter1; char letter2 = ‘Y’;
Character strings • The traditional way of representing character strings in c (and thus in c++) is to use a char array charEmployeeFirstName[60]; • To indicate that such an array contains a string, a null value (\0) is stored in the location immediately after the last character of the string
Character strings as char arrays … char sname[60]; // can hold 59 characters • It will be wrong to use sname = “Rajesh Mashruwala”; • Instead, each location has to be assigned a value corresponding to a character sname[0] = ‘R’; sname[1] = ‘a’; …; sname[17] = ‘\0’; Such strings are called Null terminated strings An array overflow may occur, against which our program must guard. So we must check that an index i in sname[i] does not cross 59
Character strings as char arrays … • Since a string stored in this fashion is not a pre-defined data type in C, we cannot perform normal operations like assignment, comparison, concatenation, etc. All these operations must be done by writing program code // find the length of string stored in sname int i, length; for (i = 0; sname[i] != ‘\0’; i++){ continue; } length = i;
string library • The library <string.h> contains several useful functions to handle operations on such strings. We should use #include <string.h>; Some of the functions are: • strcpy (s1, s2) [strncpy(s1,s2,count)] copies s2 into s1 • strcat (s1,s2) [strncat (s1, s2, count)] concatenates s2 onto the end of s1 • strlen (s1) Returns the length of s1
string library … • strcmp(s1, s2) Lexicographically compares strings s1 and s2. It returns an integer value which less than zero if s1 < s2, is 0 if s1 == s2, and is positive otherwise • strstr(s1, s2) Returns a pointer to the first occurrence of s2 in s1. If no match found, a null pointer is returned • strchr(s1,ch) searches for ch instead of s2 • memcpy (s1, s2, count) copies count characters from s2 into s1
string library • Several functions which examine or change a single character (e.g., in a char variable ch) isalnum(ch), isalpha(ch), iscntrl(ch), isdigit(ch), isgraph(ch), islower(ch), isupper(ch), ispunct(ch) isspace(ch) (these return nonzero value if true, zero if false)
Searching for a value in an array Roll Marks 1001 72 1002 45 1003 91 1004 86 1006 38 1008 53 1009 65
Findmarks.cpp // given a roll number, find the marks int roll[100], marks[100], nstudents; int givenroll,foundmarks, position, i; // read all data in arrays cin >> givenroll; for (i=0; i<nstudents; i++){ if (roll[i] == givenroll){ foundmarks = marks[i]; } cout << “Marks for ” << givenroll cout << “are ” << foundmarks; return (0); }
Finding a root by bisection method • Start with a lo and hi values such that f (lo) ∗ f (hi) < 0. • Compute mid and f (mid). • while |if (mid)| > 0 (some small threshold value) • Locate the next interval to be either • [low,mid] or [mid,hi].
[0] 1001 72 [1] 1002 45 [2] 1003 91 [3] 1004 86 [4] 1006 38 [5] 1008 53 [6] 1009 65 [7] 1011 95 Similarity with a sorted array
Binary search int lo =0, hi = n-1; mid = (lo + hi)/2; foundflag =0; While(roll[mid]>givenroll && hi > lo){ //recalculate mid if (roll[mid] > givenroll]) { // roll is towards upper half of the array hi = mid; } else {lo = mid;} mid = (lo+hi)/2; } If (roll[mid]==givenroll) foundflag =1;
Behaviour of Binary search 1 iteration lo hi mid [0] 1001 72 [1] 1002 45 [2] 1003 91 [3] 1004 86 [4] 1006 38 [5] 1008 53 [6] 1009 65 [7] 1011 95
Behaviour of Binary search 2 iteration lo hi mid [0] 1001 72 [1] 1002 45 [2] 1003 91 [3] 1004 86 [4] 1006 38 [5] 1008 53 [6] 1009 65 [7] 1011 95