14
CGS 3460 Arrays of Arrays (Multidimensional Arrays) We have talked about arrays of ints, characters, floats, etc. In addition, you can have arrays of arrays

Arrays of Arrays (Multidimensional Arrays)

  • Upload
    sidney

  • View
    43

  • Download
    1

Embed Size (px)

DESCRIPTION

Arrays of Arrays (Multidimensional Arrays). We have talked about arrays of ints, characters, floats, etc. In addition, you can have arrays of arrays. Multidimensional Arrays. Declaration type name[ mrows][ncols ]; e.g. int matrix[5][10]; What it means? Declare an array of size mrows - PowerPoint PPT Presentation

Citation preview

Page 1: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Arrays of Arrays(Multidimensional Arrays)

Arrays of Arrays(Multidimensional Arrays)

We have talked about arrays of ints, characters, floats, etc.

In addition, you can have arrays of arrays

Page 2: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Multidimensional ArraysMultidimensional Arrays Declaration

type name[ mrows][ncols ]; • e.g. int matrix[5][10];

What it means?• Declare an array of size mrows• Each element of that array will be

another array of size ncols of the given typ

Access matrix[3][4]; Not matrix[3,4], or matrix(3)(4)

2,21,20,2

2,11,10,1

2,01,00,0

aaa

aaa

aaan columns

j changes

m–by–n matrix

jia ,

m rows

i changes

Page 3: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Multidimensional ArraysMultidimensional Arrays Initialization

int M[2][3] = {{1, 2, 3}, {4, 5,6}};

1 2 3

4 5 6

M[0]

M[1]

Page 4: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Multidimensional ArraysMultidimensional Arrays int M[2][3] = {1, 2, 3, 4, 5, 6};

Numbers will fill up the first row, then the second row,…

1 2 3

4 5 6

M[0]

M[1]

Page 5: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Multidimensional ArraysMultidimensional Arrays int M[2][3] = {[0][0] = 1,[1][2]=3};

1 0 0

0 0 3

M[0]

M[1]

Page 6: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Arrays and PointersArrays and Pointers Recall arrays are pointers int a[10];

a is a pointer to a location in memory where space for 10 ints is reserved

int b[5][20] b is an array of arrays b[0] gives and array of ints of size 20 Since arrays are pointers

• b is also an array of pointers• b can also be considered as a pointer to a location in memory where

space for 5 integer pointers is reserved• b is a pointer to a pointer to an integer

Page 7: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Dynamic Allocation of 2D arraysDynamic Allocation of 2D arrays Remember malloc malloc(sizeof(int) * 15) would reserve space for 15 ints

and return the address of the first int What would malloc(sizeof(int *) * 10) do?

reserve space for 15 integer pointers and return the address of the first int

Page 8: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Dynamic allocation of 2 D ArraysDynamic allocation of 2 D Arrays

Want to declare an integer array with M rows and N columns called A.

What is A’s type? int ** A;

What should A point to? Array of integer pointers (int *) A = malloc(sizeof(int*) * M) A[i] is a different pointer to an int for every I

What should A[i] point to? Array of integers (int) A[i] = malloc(sizeof(int) * N) for all i A[i][j] gives you an int at the i, j index

Page 9: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

SummarySummary

int** A;int M, N, i, j;

A = malloc(sizeof(int*) * M);for(i = 0; i < M; i++){ A[i] = malloc(sizeof(int) * N);}

Page 10: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Two dimensional character arraysTwo dimensional character arrays

char A[5][100] Contains 5 arrays of 100 characters Could contain 5 arrays of strings (with max size 99) A[0] “foo” A[1] “bar” etc.

Page 11: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

Command line argumentsCommand line arguments Command-line arguments

Given after the name of a program in command-line • cp f1 f2• gcc yourfilename

Arguments are passed in to the program from the operating system Flexible

• But not required

Page 12: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

How to InterpretHow to Interpret A command-line interpreter

explains the command The first word is treated as the

name of a program Others as arguments.

These strings are past to main function in C

cp

f1

f2

NULL

cp f1 f2

Page 13: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

How to DeclareHow to Declare In C, these strings are past to the main function main() can actually accept two arguments

number of command line arguments a full list of all of the command line arguments.

Declaration int main ( int argc, char *argv[] )

Page 14: Arrays of Arrays (Multidimensional Arrays)

     

CGS 3460

How to useHow to use argc

Argument count Number of strings – including

executable file name

argv Argument vector Size of argc+1

argv[0]

argv[1]

argv[2]

cp

f1

f2

NULL

cp f1 f2