Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Chapter 5: Hashing Hash Tables
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
Chapter 8 Arrays and Strings
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
BBS514 Structured Programming (Yapısal Programlama)1 Character Processing, Strings and Pointers,
STRINGS & STRING HANDLING FUNCTIONS STRINGS & STRING HANDLING FUNCTIONS.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CSC 211 Data Structures Lecture 13
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Characters and Strings
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Fundamentals of Characters and Strings
CS111 Computer Programming
C++ Programming Lecture 20 Strings
Presentation transcript:

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

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 A.Magnetic core B.Electronic Valves C.Semiconductor memory D.Cathode ray tube E.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 contains several useful functions to handle operations on such strings. We should use #include ; 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

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

Searching for a given value in the array

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

Consider x-axis as discrete point

Similarity with a sorted array [0] [1] [2] [3] [4] [5] [6] [7]

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] [1] [2] [3] [4] [5] [6] [7]

Behaviour of Binary search 2 iteration lo hi mid [0] [1] [2] [3] [4] [5] [6] [7]

Program for finding marks of a student

THANK YOU