1 / 23

Two-week ISTE workshop on Effective teaching/learning of computer programming

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

Download Presentation

Two-week ISTE workshop on Effective teaching/learning of computer programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. 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’;

  6. 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

  7. 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

  8. 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;

  9. 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

  10. 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

  11. 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)

  12. Searching for a value in an array Roll Marks 1001 72 1002 45 1003 91 1004 86 1006 38 1008 53 1009 65

  13. 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); }

  14. Searching for a given value in the array

  15. Finding a root by bisection method

  16. 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].

  17. Consider x-axis as discrete point

  18. [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

  19. 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;

  20. 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

  21. 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

  22. Program for finding marks of a student

  23. THANK YOU

More Related