CS 108 Computing Fundamentals November 2, 2017.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Lecture 9: Character and String
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming Day 2 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
©Brooks/Cole, 2003 Chapter 2 Data Representation.
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.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
CS 108 Computing Fundamentals March 26, Class Notes Last day to withdraw from a class is Monday, April 6 Next week I sent s to a small number.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CS 108 Computing Fundamentals March 31, Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
CS 108 Computing Fundamentals March 3, Let's Review The Homework.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Definition of the Programming Language CPRL
User-Written Functions
User Interaction and Variables
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Chapter 4 C Program Control Part I
Chapter 2 - Introduction to C Programming
CSE 1320 Search, Sort and Strings
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Arrays in C.
Chapter 8 - Characters and Strings
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 8b: Strings BJ Furman 15OCT2012.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Week 9 – Lesson 1 Arrays – Character Strings
Chapter 2 - Introduction to C Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Chapter 2 - Introduction to C Programming
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Lesson #6 Modular Programming and Functions.
Homework Reading Programming Assignments Finish K&R Chapter 1
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Chapter 2 - Introduction to C Programming
C++ Programming Lecture 20 Strings
Introduction to C Programming
Presentation transcript:

CS 108 Computing Fundamentals November 2, 2017

Review Quizzes #16 and #17

Review Exam #2

Posted later today… due on Tuesday. GHP #19 Posted later today… due on Tuesday.

Compare with ITERATION Recursion Pronunciation: ree-kur-zhun Function: noun Etymology: Late Latin recursion, recursio, from recurrere 1 : RETURN 2 : the determination of a succession of elements (as numbers or functions) by operation on one or more preceding elements according to a rule or formula involving a finite number of steps 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first. Compare with ITERATION

Recursion A recursive function is a function that calls itself. Anything that can be solved iteratively can be also be solved recursively and vice versa. Recursion is sometimes useful because the recursive solution to a problem can sometimes be expressed more simply and succinctly than an iterative solution. Let’s look at a classic example

Example: Factorial Main Entry: factorial Function: noun 1 : the product of all the positive integers from 0 to n ; symbol n! 2 : the quantity 0! arbitrarily defined as equal to 1 http://en.wikipedia.org/wiki/Factorial

Example: Factorial factorial(0) = 1 (by definition… see previous slide) factorial(1) = 1 * 1 factorial(2) = 2 * 1 *1 factorial(3) = 3 * 2 * 1 *1 factorial(4) = 4 * 3 * 2 *1 *1 factorial(5) = 5 * 4 * 3 * 2 *1 *1 factorial(6) = 6 * 5 * 4 * 3 * 2 *1 *1 factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 *1 *1 factorial(8) = 8 *7 * 6 * 5 * 4 * 3 * 2 *1 *1

First: Factorial Via Iteration #include <stdio.h> // 1.c int factorial ( int ) ; int main (void) { int input = 0 , answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n" , input , answer ) ; return (0) ; } // continued on next slide

First: Factorial Via Iteration // continued from previous slide int factorial ( int passed_input ) { int index = 1 , result = 1; for ( index = 1 ; index <= passed_input ; index++ ) result = result * index ; } return (result) ;

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) Is there a general way to state our observation of factorials (above) into rules for an algorithm using factorial(n) as a starting point?

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) If n = = 0 then answer = 1 … factorial(0) = 1 For all other cases, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? The next slide might be more helpful.

Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * factorial(0) factorial(2) = 2 * factorial(1) factorial(3) = 3 * factorial(2) factorial(4) = 4 * factorial(3) factorial(5) = 5 * factorial(4) factorial(6) = 6 * factorial(5) factorial(7) = 7 * factorial(6) factorial(8) = 8 * factorial(7) For all cases other than n = = 0, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? factorial ( n ) = ??

Factorial Via Recursion First: answer = 1 if n = = 0 … factorial(0) = 1 Second: otherwise: factorial(n) = n * factorial( n - 1 ) Notice that the identifier "factorial" appears on both sides of the assignment operator How would we write a factorial function to implement these two rules into a C algorithm ?

Factorial Via Recursion First: the solution is 1 if n == 0 Second: the solution is n * factorial( n - 1 ) if n > 0 int factorial ( int n ) { if n = = 0 return 1 ; else return ( n * factorial ( n – 1 ) ); }

Understanding Recursion You can think of a recursive function call as if it were calling a completely separate function Understand this about recursively called functions: the operations that can be performed by recursively called functions are the same, but the data that is input to each recursively called function is different as are the address of recursive function memory objects The next slide might help to visualize the sameness of operations and the differences in data through the use of different (but very, very similar) functions... we’ll pass factorial_a the value 3.

Understanding Recursion int factorial_a ( int m ) { if ( m = = 0 ) return 1; else return m * factorial_b ( m - 1 ) ; } int factorial_b ( int n ) { if ( n = = 0 ) return 1; else return n * factorial_c ( n - 1) ; } int factorial_c ( int q ) { if( q = = 0 ) return 1; else return q * factorial_d ( q - 1) ; } int factorial_d ( int r ) { if( r = = 0 ) return 1; else return r * factorial_e ( r - 1) ; }

Let's develop a symbol table for the previous slide which is incorporated in this program (this program can handle 0 – 3 as inputs, which is enough to demonstrate the concepts) : http://web.cs.sunyit.edu/~urbanc/cs_108_nov_02a.txt Let's look at the addresses and values of the variables/parameters http://web.cs.sunyit.edu/~urbanc/cs_108_nov_02b.txt Notice that each version of the factorial functions does the same thing Therefore, we can condense this into something more general

Recursion Example: Factorial #include <stdio.h> // 2.c int factorial ( int ) ; int main (void) { int input = 0 , answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n" , input , answer ) ; return 0 ; } int factorial ( int passed_input ) if ( passed_input = = 0) // if cutting, remove the extra space between = and = return 1 ; else return ( passed_input * factorial ( passed_input - 1 ) ) ;

Let's develop a symbol table for the previous slide which is incorporated in this program: http://web.cs.sunyit.edu/~urbanc/cs_108_nov_02c.txt

Questions to Guide The Use of Recursion Can you define the problem in terms of smaller problem(s) of the same type? Think about the factorial problem: factorial(n) = n * factorial ( n-1 ) if n > 0 Does each recursive call reduce the size of the problem? As the problem "shrinks", will you eventually reach a point where an obvious solution presents itself? Again, think about the factorial problem: factorial (0) = 1

Fundamentals of Characters and Strings Characters in C consist of any printable or nonprintable character in the computer's character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences. A character is usually stored in the computer as an 8-bits (1 byte) unsigned integer. The integer value stored for a character depends on the character set used by the computer on which the program is running.

Fundamentals of Characters and Strings There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code)

Fundamentals of Characters and Strings There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code)

Example: ASCII character #include <stdio.h> // 3a.c int main(void) { char demo_Z = 'Z'; char demo_a = 'a'; printf("\n ASCII integer value for A is %d", 'A' ) ; printf("\n ASCII integer value for Z is %d", demo_Z ) ; printf("\n ASCII integer value for a is %d", demo_a ) ; printf("\n ASCII integer value for z is %d", 'z' ) ; printf("\n\n\n"); printf("\n 65 in ASCII represents %c", 65 ); printf("\n 90 in ASCII represents %c", 90 ); printf("\n 97 in ASCII represents %c", 97 ); printf("\n 122 in ASCII represents %c \n\n", 122 ); return 0 ; }

Example (continued) equivalent to #include <stdio.h> //4a.c   int main(void) { char ch; printf("\n\nEnter a character: "); scanf("%c", &ch); if ( ch >= 'A' && ch <= 'Z' ) printf("\nCapital letter.\n"); } return 0 ; #include <stdio.h> //5a.c   int main(void) { char ch; printf("\n\nEnter a character: "); scanf("%c", &ch); if ( ch >= 65 && ch <= (65+25) ) printf("\nCapital letter.\n"); } return 0 ; equivalent to

Character Type: char In C, characters are indicated by single quotes: char input_char = 'P'; printf("input_char is %c\n", input_char );

Strings In C a string is really an array of characters that ends with a special character: the null character ( '\0' ) … we use double quotes: "this is a string" // " " means the \0 is added But you can't use an assignment operator to assign a string to a character array after it has been declared (need to use the string copy function) char demo[80]; demo = "this is a string"; // Unacceptable assignment

Strings A null ( '\0' ) character is placed to mark the end of each string String functions use '\0' to locate end of string (so you don't need to pass a string's length as argument to a string function) t s i h a \0 g n r

Fundamentals of Characters and Strings A string in C is an array of characters ending with the null character ( '\0' ). It is written inside a double quotation mark ( " " ) A string may be assigned (in a declaration) to either a char array or to a char pointer (this is a declaration, so the = is allowed): char color[ ] = "green";

In C Strings are Character Arrays (logically) Strings in C, in a logical sense, are arrays of characters which terminate with a delimiter (\0 or ASCII NULL character). char str_1 [ ] = {'H', 'e', 'l', 'l', 'o', '\0'}; char str_2 [10] = {'\0'}; str_2 is a ten-element array that can hold a up to nine characters + \0 Strings in C need the delimeter to determine where the string ends (remember: the length of a string is dynamic… it isn't determined before runtime) It's the delimiter position (not the size of the array) that determines the length of the string

Accessing String Characters Like any array, the first element of a string in C is at index 0. The second is at index 1, and so on … char str_2 [10] ; str_2[0] = 'P'; str_2[1] = 'u'; str_2[2] = 'l'; str_2[3] = 'p'; str_2[4] = '\0'; str_2 now contains the string "Pulp" + \0 + 5 unused (at this time) "blanks"

String Literals String literals are given as a string inside double quotes. You've used string literals many times already printf("Enter an inter value:"); String literals are often used to initialize a string array/pointer variable char str_1[10] = "Roswell"; char str_1 [ ] = "Roswell"; Remember: the NULL is added automatically to the end (if there is space available)

Entering Strings with scanf ( ) Use %s format specifier: %s scans up to but does not include next white space %ns scans the next n characters or up to the next white space, whichever is first You only need to provide the starting address of the string with %s Example: scanf ("%s", str_1); scanf ("%2s", str_1); No ampersand (&) necessary when inputting strings (%s) into character arrays! But you an use the & if you like.

String Functions Pretty straightforward The text is pretty clear Practice questions at the end of the chapter are useful Play with your food The textbook and MPL have plenty of easy-to-understand info

Arrays Bootcamp http://web.cs.sunyit.edu/~urbanc/cs_108_array_bootcamp.pdf