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.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Parameter Passing Mechanisms Reference Parameters Read § §
Parameter Passing Mechanisms Reference Parameters § §
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
Chapter 7: User-Defined Functions II
Structures, Unions, Enumerations
Computer Science 210 Computer Organization
Introduction to Programming Using C
read the next character into ch using getchar();
Pointers.
Hassan Khosravi / Geoffrey Tien
Java Review: Reference Types
Programmazione I a.a. 2017/2018.
User-Defined Functions
Tejalal Choudhary “C Programming from Scratch” Pointers
Computer Science 210 Computer Organization
CSC 253 Lecture 8.
C Passing arrays to a Function
CSC 253 Lecture 8.
Lecture 18 Arrays and Pointer Arithmetic
Parameter Passing in Java
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Simulating Reference Parameters in C
CS111 Computer Programming
Initializing variables
Chapter 7: User-Defined Functions II
Sieve of Eratosthenes The Sieve of Eratosthenes uses a bag to find all primes less than or equal to an integer value n. Begin by creating a bag an inserting.
7 Arrays.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Pointers and dynamic objects
C H A P T E R F I V E Memory Management.
Chapter 6 Modular Programming chap6.
References Revisted (Ch 5)
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Presentation transcript:

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 main() { char s[10]; read_into_array(s,10); … int read_into_array (char t[], int size) { int ch; int count = 0; /* … */ } 2.Copy values from actual parameters to the newly created formal paramters. s[0] s[1] s[2]s[9] s tsize 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..

Implications of copying content of array variable during parameter passing The value of s is copied into t. So the box corresponding to t has the same value as the box corresponding to s. s[0] s[1] s[2]s[9] s t size10 s is an array. In C an array is identified with a box whose value is the address of the first element of the array. They both now contain the address of the first element of the array. 1.In the computer, an address is simply the value of a memory location. 2.For e.g., the value in the box for s would be the memory location of s[0]. 3.When we draw figures, we will show this by an arrow.

The arrow from inside box s to s[0] indicates that s stores address of s[0]. s[0] s[1] s[2]s[9] s t size10 Pointers s points to s[0], or, s is a pointer to s[0]. Referred to as : Passing an actual parameter array s to a formal parameter array t[] makes t now point to the first element of array s. int main() { int s[10]; read_into_array(s,10); } int read_into_array (char t[], int size); Since t is declared as char t[], t[0] is the box pointed to by t, t[1] refers to the box one char further from the box t[0], t[2] refers to the box that is 2 chars further from the box t[0] and so on… Let us see this now.

 t[0] is the box whose address is stored in t. This is same as s[0].  t[1] is the box next to (successor to) the box whose address is stored in t. This is the same as s[1].  t[2] is the box 2 steps next to the box whose address is stored in t; this is same as s[2], etc.. Now suppose we change t[0] using t[0] = ‘A’; Later on, in main(), when we access s[0], we see that s[0] is ‘A’. t[0]t[1]t[2]t[9] s[0] s[1] s[2]s[9] t ‘A’ The box is the same, but it has two names, s[0] in main() and t[0] in read_into_array()

s[0] s[1] s[2]s[9] s t Address Arithmetic s+2 points to s[2], or, s+2 is a pointer to s[2]. Passing an actual parameter array s+2 to a formal parameter array t[] makes t now point to the third element of array s. int main() { int s[10]; read_into_array(s+2,8); } int read_into_array (char t[], int size); Since t is declared as char t[], t[0]=s[2] is the box pointed to by t, t[1]=s[3] refers to the box one char further from the box t[0], t[2]=s[4] refers to the box that is 2 chars further from the box t[0] and so on…

Argument Passing: Array vs Simple Type When a basic datatype (such as int, char, float, etc) is passed to a function a copy of the value is created in the memory space for that function, after the function completes its execution, these values are lost. When an array is passed to a function the address of the first element is copied, any changes to the array elements are visible to the caller of the function.

Example: Dot Product Problem: write a function dot_product that takes as argument two integer arrays, a and b, and an integer, size, and computes the dot product of first size elements of a and b. Declaration of dot_product int dot_product(int a[], int b[], int); OR int dot_product(int [], int [], int);

#include int dot_product (int[], int[], int); int main(){ int vec1[] = {2,4,1,7,-5,0, 3, 1}; int vec2[] = {5,7,1,0,-3,8,-1,-2}; printf("%d\n", dot_product(vec1, vec1, 8)); printf("%d\n", dot_product(vec1, vec2, 8)); return 0; } int dot_product (int a[], int b[], int size){ } OUTPUT Convert to C

#include int dot_product (int[], int[], int); int main(){ int vec1[] = {2,4,1,7,-5,0, 3, 1}; int vec2[] = {5,7,1,0,-3,8,-1,-2}; printf("%d\n", dot_product(vec1, vec1, 8)); printf("%d\n", dot_product(vec1, vec2, 8)); return 0; } int dot_product (int a[], int b[], int size){ int p = 0, i; for(i=0;i<size; i++) p = p + (a[i]*b[i]); return p; } OUTPUT

Generating Prime Numbers Problem: Given a positive integer N, generate all prime numbers up to N. A Greek mathematician Eratosthenes came up with a simple but fast algorithm Sieve of Eratosthenes

On a piece of paper, write down all the integers starting from 2 till N. Starting from 2 strike off all multiples of 2, except 2. Next, find the first number that has not been struck and strike off all its multiples, except the number. Continue until you cannot strike out any more numbers. The numbers that have not been struck, are PRIMES.

Generating Prime Numbers using Sieve of Eratosthenes No more numbers can be marked. Algorithm terminates. Primes up to 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97. Going up to √N is enough.

Sieve of Eratosthenes: Program int prim[10000]; // global array void sieve(int n) { int i, j = 2; prim[0]=0; prim[1]=0; for (i=2; i<=n; i++) prim[i] = 1; while (j <= n) { if (prim[j] == 0) { // composite continue; } for (i=2*j; i<=n; i=i+j) prim[i] = 0; j++; } } int main() { int i, n; scanf("%d", &n); // check n < sieve(n); // set primes for (i=2; i<=n; i++) { if (prim[i] == 1) printf("%d\n", i); } return 0; } j++; j