Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
1 Arrays and Strings. 2 An array is a collection of variables of the same type that are referred to through a common name. It is a Data Structure which.
Introduction to C Programming CE Lecture 13 Strings in C.
Lecture 08 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 24, 2002.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
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.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
WEEK 6 Class Activities Lecturer’s slides.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Arrays and Strings Lecture 30. Summary of Previous Lecture In the previous lecture we have covered  Functions Prototypes Variable Scope  Pointers Introduction.
Structured Programming Approach Module VIII - Additional C Data Types Arrays Prof: Muhammed Salman Shamsi.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays (Chapter 5)‏ Definition Applications One-Dimensional –Declaration –Initialization –Use Multidimensional.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Arrays. Arrays are objects that help us organize large amounts of information.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
1 Two-Dimensional Arrays. 2 Terminology Two-dimensional arrays represent matrices A matrix contains a number of values of the same data type The values.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Computer Programming BCT 1113
Lecture 7 Arrays 1. Concept of arrays Array and pointers
Lecture-5 Arrays.
Command-Line Arguments
Module 2 Arrays and strings – example programs.
Arrays in C.
14th September IIT Kanpur
Lecture 10 Arrays.
(Numerical Arrays of Multiple Dimensions)
Multidimensional array
Presentation transcript:

Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n; ++i) res += b[i]; return res; } main() { int a[] = {0, 1, 2, 3, 4, 5, 6}; printf("%d\n", sum(a, 2)); printf("%d\n", sum(a, 4)); } The printouts are 1 and 6.

Passing an Array v.s. Passing a Single Variable void func(int a[], int n) { a[0] += 1; ++n; } main() { int b[2] = {0, 0}; int n = 0; printf("%d %d %d\n", b[0], b[1], n); func(b, n); printf("%d %d %d\n", b[0], b[1], n); } Passing an array is to pass an address. Passing a single variable is to pass a copy of the value.

String-Handling Functions String Concatenation With the declaration and initialization: s1[10] ="The "; s2[4] = "CZ"; The function (with string.h included) strcat(s1, s2); append the string s2 to the end of string s1, i.e., s1 now becomes "The CZ".

String-Handling Functions String Comparison With the declaration and initialization: s1[10] ="The "; s2[4] = "CZ"; s3[5] = "The "; The function (with string.h included) strcmp(s1, s2) returns a positive value (for 'T' is after 'C' according to ASCII table). strcmp(s1, s3) returns 0 (for the two strings are the same).

String-Handling Functions String Copy/String Length With the declaration and initialization: s1[10] ="The "; s2[10] = "CZ"; The function (with string.h included) strcpy(s1, s2) copy the second string s2 into the first string s1. strlen(s1) returns 4 (for there are four characters before the null character '\0').

String-Handling Functions String in String With the declaration and initialization: s1[10] ="The "; s2[10] = "e"; The function (with string.h included) strstr(s1, s2) return a pointer to character where s2 is part of s1 (s2 is a substring of s1). For this example, it returns the address of character 'e' is string s1.

Example on Strings #include main() { char s[] = "We study"; char t[] = "uoga uoga"; char v[15] = "did"; printf("%d\n", strlen(s)); printf("%d\n", strcmp(s,t)); printf("%s\n", strcpy(s,v)); printf("%s\t%s\n", s, v); printf("%s\n", strcat(v, t)); printf("%s\n", strstr(t, "og")); } The program prints: did diduoga uoga oga uoga

Computing a String's Length int length(char s[]) { int cnt; for(cnt = 0; s[cnt] != '\0'; ++cnt) ; return cnt; } #include main() { printf("\"otter\"\t%d\n", length("otter")); printf("\"\"\t%d\n", length("")); printf("\"a\"\t%d\n", length("a")); } The outputs are: "otter" 5 "" 0 "a" 1

Multidimensional Arrays The dimension of an array is the number of indices of the array. float temps[10]; –one dimensional array with 10 elements. float t[12][50]; –two dimensional array with 12*50=600 elements. The elements are t[0][0], t[0][1], t[0][2],..., t[0][49], t[1][0], t[1][1],...,..., t[11][48], t[11][49].

Point of Views of Multidimensional Array You can think of one dimensional array as a vector, and two dimensional array as a matrix (only the index starts from 0). You can view a multidimensional array as (one-dimensional) array of array. For example, t[12][50] is an array t[12], each of them is an array of 50 elements.

Initializing Multidimensional Arrays Multidimensional array can be initialized element by element: int nums[2][3]; nums[0][0] = 2; nums[0][1] = 4; nums[0][2] = 6; nums[1][0] = -9; nums[1][1] = -7; nums[1][2] = -5; Or initialized during declaration: int nums[2][3] = { {2, 4, 6}, {-9, -7, -5}}

Multidimensional Array as Arguments The dimensions of the array must be specified in function definition. E.g., print_table(int t[10][4]) {... } main() { int t[10][4];... Print_table(t); } The first size can be omitted.

Matrix Multiplication Problem: Provide a general function for matrix multiplication. The function should multiply two n by n matrices and store the product in a third n by n matrix. The product, Z, of two matrices, X and Y, is defined by Z(i,j) =  k X(i,k) Y(k,j) where 0  i, j  n - 1, and summation index k runs from 0 to n-1.

Sample Input/Output Input matrix size: 3 Input first matrix by row Matrix m1: Input second matrix by row Matrix m2: Product m3: For example, 22 of the (0,0) element in m3 is obtained by 1*0 + 2*1 + 4*5 = 22

The Prototype and Main Program #include #define MAXSIZE 20 void store(int m[][MAXSIZE], int n), mult(int m1[][MAXSIZE], int m2[][MAXSIZE], int m3[][MAXSIZE], int n), print(int m[][MAXSIZE], int n); main() { int n; int m1[MAXSIZE][MAXSIZE]; int m2[MAXSIZE][MAXSIZE]; int m3[MAXSIZE][MAXSIZE]; printf("Input matrix size:"); scanf("%d", &n); printf("Input first matrix by row\n"); store(m1, n); printf("\nMatrix m1:\n"); print(m1, n); printf("Input second matrix by row\n"); store(m2, n); printf("\nMatrix m2:\n"); print(m2, n); mult(m1, m2, m3, n); printf("\nProduct m3:\n"); print(m3, n); }

Function for Matrix Multiplication /* Multiply matrix m1 by m2, store the product in matrix m3 */ void mult(int m1[][MAXSIZE], int m2[][MAXSIZE], int m3[][MAXSIZE], int n) { int i, j, k; for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) { m3[i][j] = 0; for(k = 0; k < n; ++k) m3[i][j] += m1[i][k] * m2[k][j]; }

Read/Print Matrix /* Store/read data in matrix by row */ void store(int m[][MAXSIZE], int n) { int i, j; for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) scanf("%d", &m[i][j]); } /* Print the matrix */ void print(int m[][MAXSIZE], int n) { int i, j; for(i = 0; i < n; ++i) { for(j = 0; j < n; ++j) printf("%d ", m[i][j]); putchar('\n'); }

Computational Complexity Time Complexity –The number of operations needed as a function of the problem size. –In our matrix computation example, we need about n 3 computation for matrices of size n. We say our program (algorithm) has a time computational complexity of order n 3. n Time Units , ,000, ,000,000,000

Computational Complexity Space Complexity –The number of memory cells needed as a function of the problem size. –In our matrix computation example, we need about n 2 memory space for matrices of size n. We say our program (algorithm) has a space computational complexity of order n 2. A good algorithm should have smallest CPU time and memory space usage.

Reading/Home Working Read Chapter 6, page 251 to 282. Work on Problems –Section 6.5, page 253, exercise 1, 3. –Section 6.6, page 261, exercise 1, 3. –Section 6.8, page 271, exercise 1, 3. Check your answers in the back of the textbook. Do not hand in.