read the next character into ch using getchar();

Slides:



Advertisements
Similar presentations
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
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.
Ch. 8 Functions.
Procedures and Control Flow CS351 – Programming Paradigms.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Run-Time Storage Organization
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Stack and Heap Memory Stack resident variables include:
Test 2 Review Outline.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Computer Science 210 Computer Organization
Arrays Low level collections.
CS1010 Programming Methodology
Principles of programming languages 4: Parameter passing, Scope rules
Lecture-5 Arrays.
Java Review: Reference Types
Stack Data Structure, Reverse Polish Notation, Homework 7
User-Defined Functions
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
C++ Data Types Simple Structured Address Integral Floating
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
Functions Inputs Output
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Structured Programming (Top Down Step Refinement)
Chapter 16 Pointers and Arrays
Arrays.
The method invocation mechanism and the System Stack
Circular Buffers.
Pointers and Arrays.
Array-Based Sequences
Lecture 18 Arrays and Pointer Arithmetic
Programming in C Pointer Basics.
Understanding Program Address Space
Programming with Pointers
The switch Statement Topics Multiple Selection switch Statement
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Subroutines – parameter passing
Functions with arrays.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Pointers and Arrays
Your questions from last session
Chapter 6: User-Defined Functions I
7 Arrays.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Arrays.
Chapter 9: Pointers and String
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
Lecture 20 – Practice Exercises 4
SPL – PS2 C++ Memory Handling.
Presentation transcript:

read the next character into ch using getchar(); int ch; int count = 0; read the next character into ch using getchar(); while (ch is not EOF AND count < 100) { s[count] = ch; count = count + 1; } initial design pseudo-code int main() { char s[100]; /* read_into_array */ /* print_reverse */ return 0; } Overall design int ch; int count = 0; ch = getchar(); while ( ch != EOF && count < 100) { s[count] = ch; count = count + 1; } - here we can also write (ch=getchar())!=EOF.  use of assignment expression! What is the value of count at the end of read_into_array? Translating the read_into_array pseudo-code into code.

Now let us design the code fragment print_reverse Suppose input is HELP<eof> s[0] s[1] s[2] s[3] s[99] The array char s[100] `H’ `E’ `L’ `P’ i count 4 index i runs backwards in array int i; set i to the index of last character read. while (i >= 0) { print s[i] i = i-1; /* shift array index one to left */ } PSEUDO CODE

index i runs backwards in array s[0] s[1] s[2] s[3] s[99] The array char s[100] `H’ `E’ `L’ `P’ count 4 i int i; set i to index of the last character read. while (i >= 0) { print s[i] i = i-1; } PSEUDO CODE int i; i = count-1; while (i >=0) { putchar(s[i]); i=i-1; } Code for printing characters read in array in reverse - putchar(c) prints the character corresponding to int c. Translating pseudo code to C code: print_reverse

Putting it together int main() { char s[100]; /* read_into_array */ /* print_reverse */ return 0; } Overall design The code fragments we have written so far. int count = 0; int ch; ch = getchar(); while ( ch != EOF && count < 100) { s[count] = ch; count = count + 1; } int i; i = count-1; while (i >=0) { putchar(s[i]); i=i-1; } print_reverse code read_into_array code.

/*print_in_reverse */ #include <stdio.h> int main() { char s[100]; int count = 0; int ch; int i; return 0; } /* the array of 100 char */ /* counts number of input chars read */ /* current character read */ /* index for printing array backwards */ ch = getchar(); while ( ch != EOF && count < 100) { s[count] = ch; count = count + 1; } /*read_into_array */ Putting code together i = count-1; while (i >=0) { putchar(s[i]); i=i-1; } /*print_in_reverse */

Let us trace the execution. We will do this for part read_into_array HELLO<eof> INPUT i ch `H’ `E’ `L’ `O’ `L’ eof #include <stdio.h> int main() { char s[100]; int count = 0; int ch, i; … s[99] s[0] s[2] s[3] s[4] s[1] `H’ `E’ `L’ ch = getchar(); while ( ch != EOF && count < 100) { s[count] = ch; count = count + 1; } `L’ `O’ count 4 3 5 1 2

/*print_in_reverse */ #include <stdio.h> int main() { char s[100]; int count = 0; int ch; int i; return 0; } /*read_into_array */ while ( (ch=getchar()) != EOF && count < 100 ) { s[count] = ch; count = count + 1; } Neat trick! i = count-1; while (i >=0) { putchar(s[i]); i=i-1; } /*print_in_reverse */

Arrays: Recap Indices always start at 0 in C indices Arrays are a consecutively allocated group of variables whose names are indexed. int a[100]; a[0]=27; a[1]=-59; a[2]=1; … char s[50]; s[0]=‘H’; s[1]=‘e’; a[0] a[1] a[2] a[99] 27 -59 1 102357 indices s[0] s[1] s[2] s[3] s[4] … s[49] ‘H’ ‘e’ ‘l’ ‘o’ Indices always start at 0 in C

Passing arrays to functions Write a function that reads input into an array of characters until EOF is seen or array is full. int read_into_array (char t[], int size); /* returns number of chars read */ int read_into_array (char t[], int size) { int ch; int count = 0; ch = getchar(); while (count < size && ch != EOF) { t[count] = ch; count = count + 1; } return count; read_into_array takes an array t as an argument and size of the array and reads the input into array. int main() { char s[100]; read_into_array(s,100); /* process */ }

s int read_into_array (char t[], int size) { int ch; int count = 0; ch = getchar(); while (count < size && ch != EOF) { t[count] = ch; count = count + 1; } return count; } read_into_array t size 10 s[0] s[1] s[2] s[3] s[9] ‘W’ ‘I’ ‘N’ 2 1 3 count ch ‘I’ ‘W’ EOF ‘N’ int main() { char s[10]; read_into_array(s,10); … return value return addr main.3 main 3 Input WIN<eof>

read_into_array s t ch EOF s[0] s[1] s[2] s[3] s[9] main ‘W’ ‘I’ ‘N’ size 10 3 count addr main.3 return value State of memory just prior to returning from the call read_into_array() paint_hostel(room h[], number-of-rooms) { for (room r = 0; r < number-of-rooms; goto-next-room) paint_room(hostel[r]); } iit () room hostel1[200]; room hostel2[300]; room hostel3[300]; … if ( … ) { paint_hostel(hostel1, 200); } if ( … ) { paint_hostel(hostel2, 300); /* OK: last 100 rooms do not need painting */ } if ( … ) { paint_hostel(hostel3, 400); /* Not OK: There are no rooms beyond 300 */ }

s s[0] s[1] s[2] s[3] s[9] main ‘W’ ‘I’ ‘N’ State of memory just after returning from the call read_into_array(). All local variables allocated for read_into_array() on stack may be assumed to be erased/de-allocated. Only the stack for main() remains, that is, all local variables for main() remain. Behold !! The array s[] of main() has changed! THIS DID NOT HAPPEN BEFORE! WHAT DID WE DO DIFFERENTLY? Ans: we passed the array s[] as a parameter!!

paint_hostel(char hostel[], int number-of-rooms) { int r; for (r = 0; r < number-of-rooms; r++) paint_room(hostel[r]); } iit () char hostel1[200]; char hostel2[300]; char hostel3[300]; … if ( … ) paint_hostel(hostel1, 200); if ( … ) paint_hostel(hostel2, 200); // OK: last 100 rooms // may not need painting if ( … ) paint_hostel(hostel3, 400); // Not OK: There are no rooms beyond 300

paint_hostel200(char hostel[200]) { int r; for (r = 0; r < 200; r++) paint_room(hostel[r]); } paint_hostel300(char hostel[300]) for (r = 0; r < 300; goto-next-room) iit () { char hostel1[200]; char hostel2[300]; char hostel3[300]; // Are these correct? EXERCISE!! if ( … ) paint_hostel200(hostel1); if ( … ) paint_hostel300(hostel2); if ( … ) paint_hostel300(hostel1); if ( … ) paint_hostel200(hostel3);

Parameter Passing Basic steps: Create new variables (boxes) for each of the formal parameters allocated on a fresh stack area created for this function call. Copy values from actual parameters to the newly created formal parameters. Create new variables (boxes) for each local variable in the called procedure. Initialize them as given. Let us look at parameter passing more carefully.

int main() { int s[10]; read_into_array(s,10); … int read_into_array (char t[], int size) { int ch; int count = 0; /* … */ } s[0] s[1] s[2] s[9] s Array variables store address!! The stack of main just prior to call s is an array. It is a variable and it has a box. The value of this box is the address of the first element of the array.

Parameter Passing: Arrays Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. Copy values from actual parameters to the newly created formal paramters. int main() { char s[10]; read_into_array(s,10); … int read_into_array (char t[], int size) { int ch; int count = 0; /* … */ } s[0] s[1] s[2] s[9] s t size t copies the value in s, so t points to the same address as s. 10 s and t are the same array now, with two different names!! s[0] and t[0] refer to the same variable, etc..