Fundamentals of Programming Session 15

Similar documents
Fundamentals of Programming Session 19

Fundamentals of Programming Session 12

Fundamentals of Programming Session 19

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20

Fundamentals of Programming Session 13

Fundamentals of Programming Session 4

Fundamentals of Programming Session 14

Fundamentals of Programming Session 8

Fundamentals of Programming Session 25

C: How to Program. Week /Apr/23

Fundamentals of Programming Session 7

Chapter 7 Array. Array. C++, How to Program

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 24

Fundamentals of Programming Session 23

Fundamentals of Programming Session 2

Fundamentals of Programming Session 9

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Introduction to Programming

Introduction to Programming

Introduction to Programming

C Arrays Pearson Education, Inc. All rights reserved.

Fundamentals of Programming Session 24

C Arrays. O b j e c t i v e s In this chapter, you ll:

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Programming for Engineers Arrays

Fundamentals of Programming. Lecture 11: C Characters and Strings

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 11 Introduction to Programming in C

Fundamentals of Programming Session 1

A Fast Review of C Essentials Part I

BLM2031 Structured Programming. Zeyneb KURT

1 Lexical Considerations

C: How to Program. Week /Mar/05

Lexical Considerations

UNIT- 3 Introduction to C++

ESC101N: Fundamentals of Computing End-sem st semester

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 1

Lexical Considerations

Chapter 4 Defining Classes I

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Fundamental of Programming (C)

Chapter 11 Introduction to Programming in C

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 6. Arrays. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

COEN244: Class & function templates

Arrays (Deitel chapter 7)

Fundamentals of Programming. Lecture 3: Introduction to C Programming

16.216: ECE Application Programming Fall 2011

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 11 Introduction to Programming in C

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Chapter 7 Functions. Now consider a more advanced example:

Lecture 04 FUNCTIONS AND ARRAYS

Arrays and Applications

Chapter 11 Introduction to Programming in C

Review of the C Programming Language for Principles of Operating Systems

Fundamentals of Programming Session 25

C: How to Program. Week /Apr/16

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Introduction to Programming session 24

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Pointers as Arguments

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Lecture 03 Bits, Bytes and Data Types

G Programming Languages - Fall 2012

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Review of the C Programming Language

Constructors for classes

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Portland State University. CS201 Section 5. Midterm Exam. Fall 2018

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

Programming in C and C++

More about BOOLEAN issues

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Array. Prepared By - Rifat Shahriyar

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Arrays Data structures Related data items of same type Remain same size once created Fixed-length entries

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Fundamental of Programming (C)

Exercise 7 References, Arrays, Vectors

Arrays and Pointers in C & C++

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

by Pearson Education, Inc. All Rights Reserved.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

Objectives of This Chapter

C Programming for Engineers Functions

Transcription:

Fundamentals of Programming Session 15 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Array Examples Passing Arrays to Functions 2

Array Examples 3 Arrays that are static are initialized once at compile time. If you do not explicitly initialize a static array, that array s elements are initialized to zero by the compiler. Figure 6.11 demonstrates function staticarrayinit with a local static array and function automaticarrayinit with a local automatic array. Function staticarrayinit is called twice (lines 12 and 16). The local static array in the function is initialized to zero by the compiler (line 25). The function prints the array, adds 5 to each element and prints the array again.

4 Array Examples

5 Array Examples

6 Array Examples

7 Array Examples

Passing Arrays to Functions 8 To pass an array argument to a function, specify the name of the array without any brackets. For example, if array hourlytemperatures has been defined as int hourlytemperatures[ 24 ]; the function call modifyarray( hourlytemperatures, 24 ) passes array hourlytemperatures function modifyarray. and its size to Unlike char arrays that contain strings, other array types do not have a special terminator. For this reason, the size of an array is passed to the function, so that the function can process the proper number of elements.

Passing Arrays to Functions 9 C automatically passes arrays to functions by reference the called functions can modify the element values in the callers original arrays. The name of the array evaluates to the address of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored. Therefore, when the called function modifies array elements in its function body, it s modifying the actual elements of the array in their original memory locations. Figure 6.12 demonstrates that an array name is really the address of the first element of an array by printing array, &array[0] and &array using the %p conversion specifier a special conversion specifier for printing addresses.

Passing Arrays to Functions The %p conversion specifier normally outputs addresses as hexadecimal numbers. Although entire arrays are passed by reference, individual array elements are passed by value exactly as simple variables are. Such simple single pieces of data (such as individual ints, floats and chars) are called scalars. To pass an element of an array to a function, use the subscripted name of the array element as an argument in the function call. 10

11 Passing Arrays to Functions

Passing Arrays to Functions 12 For a function to receive an array through a function call, the function s parameter list must specify that an array will be received. For example, the function header for function modifyarray (that we called earlier in this section) might be written as void modifyarray( int b[], int size ) indicating that modifyarray expects to receive an array of integers in parameter b and the number of array elements in parameter size. The size of the array is not required between the array brackets. If it s included, the compiler checks that it s greater than zero, then ignores it. Specifying a negative size is a compilation error. Figure 6.13 demonstrates the difference between passing an entire array and passing an array element.

13 Passing Arrays to Functions

14 Passing Arrays to Functions

15 Passing Arrays to Functions

16 Passing Arrays to Functions

Passing Arrays to Functions There may be situations in your programs in which a function should not be allowed to modify array elements. Because arrays are always passed by reference, modification of values in an array is difficult to control. C provides the type qualifier const to prevent modification of array values in a function. Figure 6.14 demonstrates the const qualifier. 17

18 Passing Arrays to Functions

19 Passing Arrays to Functions

Question 1 Write a program to take a sentence and reverse it using recursion. You cannot use arrays! 20

Answer 1 21 void Reverse(); int main(){ printf("enter a sentence:"); Reverse(); return 0; } void Reverse(){ char c; scanf("%c", &c); if( c!= '\n'){ Reverse(); printf("%c",c); } }

Question 2 What is the output of the following code? void foo( int [] ); int main(){ int ary[4] = {1, 2, 3, 4}; foo(ary); printf("%d ", ary[0]); } 22 void foo(int p[4]){ int i = 10; p = &i; printf("%d", p[0]); } Answer: 101

Question 3 Given array ary with no repetitive elements and size of N, and number Sum. Write a function to recursively determine if ary has a subset that sum of its elements is equal to Sum. Print one of those subsets. 23

Answer 3 24 int hassubset(int array[], int N, int Sum) { if (N == 0) { return (Sum == 0); } if (hassubset(array, N-1, Sum - array[n-1])) { printf("%d ", array[n - 1]); return 1; } else if (hassubset(array, N-1, Sum)) { return 1; } else { return 0; } }