Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-dimensional arrays in C

Similar presentations


Presentation on theme: "Multi-dimensional arrays in C"— Presentation transcript:

1 Multi-dimensional arrays in C
They do not exist (Really!) However, they are simulated using arrays whose elements are themselves arrays Example: float arr[20][10]; This is an array with 20 elements, each of which is an array with 10 elements, each of type float

2 One-dimensional arrays in C, revisited
float arr[10]; declares an array of 10 floats stored contiguously and allocates space for it float arr[]; declares an array of floats - the array is of unknown size

3 Multi-dimensional arrays in C –what’s allowed
float arr[20][10]; This array has 20 elements, each of which is an array with 10 elements, of type float The first 6 elements of the array arr are illustrated = this is 6 * 10, or 60, floats

4 Multi-dimensional arrays in C –what’s allowed
float arr[][10]; OK This is an array with an unknown number of elements, each of which is an array with 10 elements, each of type float The first 6 elements of the array arr are illustrated = this is 6 * 10, or 60, floats

5 Multi-dimensional arrays in C –what’s allowed
float arr[10][]; Not OK This can’t be an array with 10 elements of any type, because we don’t know where the lowest level elements are Cannot tell where array elements begin or end

6 Multi-dimensional arrays in C
The most important thing to remember is the pattern [][], [][][], etc. for indices An element in a 2-D array is accessed by two indices: array_name[i][j] Don’t use commas array_name[i, j] wrong, wrong, wrong, wrong

7 Multi-dimensional arrays in C
There can be 2- dimensional arrays, 3- dimensional arrays, 4- -dimensional arrays, etc. As with 2-dimensional arrays, only the leftmost size limit can be empty int a[][10]; int b[20][10]; float c[20][300]; float d[][2]; double e[][10][1] are all OK float f[10][]; char g[][10][][1] are not

8 Two-dimensional arrays in C
The same patterns apply to multi-dimensional arrays as one-dimensional ones: iteration and accumulation C is a good language for engineering because multi-array elements are stored contiguously Usually, two different indices are needed to access multi-dimensional arrays – we only show 2-D arrays in our examples

9 Initializing a 2-D array, nested loops
#define SIZE 3 #include <stdio.h> int main( int argc, char * argv[]) { int i, j; double m1[SIZE][SIZE]; /* initialize array */ for( i =0 ; i < SIZE; i++) for (j = 0; j < SIZE; j++) m1[i][j] = i;

10 Printing the array – nested loops
/* Echo the input arrays */ printf("The input array is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", m1[i][j]); printf("\n"); } } /* end of program (for now) */

11 Example – linear algebra package
/* Routines for operations on square matrices */ #define SIZE 3 #include <stdio.h> int main( int argc, char * argv[]) { int i, j, k; char choice; double m1[SIZE][SIZE]; double m2[SIZE][SIZE]; double output[SIZE][SIZE]; double scalar;

12 puts("Welcome to the cheap vector algebra package.");
/* initialize array */ for( i =0 ; i < SIZE; i++) for (j = 0; j < SIZE; j++) { m1[i][j] = i; m2[i][j] = 3.0 * j + i; output[i][j] = 0; }

13 /* Echo the input arrays */
printf("The first input array is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", m1[i][j]); printf("\n"); } printf("\nAnd the second input array is:\n");

14 for (i = 0; i < SIZE; i++)
{ for (j = 0; j < SIZE; j++) printf("%6.2lf", m2[i][j]); printf("\n"); } /* Now ask user for input: add, subtract two vectors, multiple by a scalar, computer dot product */

15 Guess what comes next! puts("Enter a + for addition, a - for subtraction,"); puts("a * for multiplication by another square matrix"); puts("or . for multiplication by a scalar"); scanf("%c", &choice);

16 switch (choice) { case '+': for ( i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) output[i][j] = m1[i][j] + m2[i][j]; break; case '-': output[i][j] = m1[i][j] - m2[i][j];

17 case '*': for ( i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) { output[i][j] = 0; for (k = 0; k < SIZE; k++) output[i][j] += m1[i][k] * m2[k][i]; } break; case '.': puts("Enter the scalar you wish to multiply by"); scanf("%lf", &scalar); output[i][j] = scalar * m1[i][j];

18 default: puts("Enter your choice again"); scanf("%c", &choice); } /* end switch */ printf("Your selection was %c ", choice); printf(" and the output is:\n"); for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%6.2lf", output[i][j]); printf("\n"); } } /* end main */


Download ppt "Multi-dimensional arrays in C"

Similar presentations


Ads by Google