arrays as function arguments array can be used as a function argument. e.g., #include int sum(int...

20
Arrays as Function Arguments Array can be used as a function argument. E.g., #include <stdio.h> 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.

Upload: suzanna-lee

Post on 17-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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;

Arrays as Function Arguments

• Array can be used as a function argument. E.g.,

#include <stdio.h>

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.

Page 2: 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;

Passing an Array v.s. Passing a

Single Variablevoid 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);}

0 0 01 0 0

Passing an array is to pass an address.

Passing a single variable is to pass a copy of the value.

Page 3: 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;

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".

Page 4: 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;

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).

Page 5: 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;

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').

Page 6: 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;

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.

Page 7: 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;

Example on Strings

#include <stdio.h>#include <string.h>

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:8-30diddid diddiduoga uogaoga uoga

Page 8: 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;

Computing a String's Length

int length(char s[]){ int cnt; for(cnt = 0; s[cnt] != '\0'; ++cnt) ; return cnt;}

#include <stdio.h>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

Page 9: 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;

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 aret[0][0], t[0][1], t[0][2], ..., t[0][49],

t[1][0], t[1][1], ..., . . .,t[11][48], t[11][49].

Page 10: 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;

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.

Page 11: 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;

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}}

Page 12: 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;

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.

Page 13: 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;

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 byZ(i,j) = kX(i,k) Y(k,j)

where0 i, j n - 1,

and summation index k runs from 0 to n-1.

Page 14: 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;

Sample Input/Output

Input matrix size: 3Input first matrix by row1 2 43 -1 64 -6 2

Matrix m1:1 2 43 -1 64 -6 2Input second matrix by row0 3 -51 1 -25 -2 7

Matrix m2: 0 3 -51 1 -25 -2 7

Product m3:22 -3 1929 -4 294 2 6

For example, 22 of the (0,0) element in m3 is obtained by

1*0 + 2*1 + 4*5 = 22

Page 15: 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;

The Prototype and Main Program

#include <stdio.h>

#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);}

Page 16: 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;

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]; }}

Page 17: 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;

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'); }}

Page 18: 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;

Computational Complexity

• Time Complexity– The number of operations

needed as a function of the problem size.

– In our matrix computation example, we need about n3 computation for matrices of size n. We say our program (algorithm) has a time computational complexity of order n3.

n Time Units1 12 83 275 12510 1,000100 1,000,0001000 1,000,000,000

Page 19: 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;

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 n2 memory space for matrices of size n. We say our program (algorithm) has a space computational complexity of order n2.

• A good algorithm should have smallest CPU time and memory space usage.

Page 20: 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;

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.