Computational Arrays and Matrix Computations

Computational Arrays and Matrix Computations -...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Computational Arrays and Matrix Computations Outline: Introduction to Matrix Declaration of Computational Arrays Initialization of Computational Arrays Array Reference Formatted Input/Output of Computational Arrays Array Operations Pass Computational Arrays to Functions Type Generic Array Functions Some Commonly Used Array Functions Sample Problem
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaration of Computational Arrays Computational arrays are treated as first-class objects in Ch. Computational arrays are declared with the type qualifier array defined in array.h . Programs using computational arrays should include the header file array.h . At the interactive prompt in Ch shell, computational arrays can be used without header file array.h. The code segment below declares computational arrays a1, a2, a3, a4. One-dimensional arrays a1, a2 , and a3 are of int type with ten elements. The subscript ranges of arrays a1 and a2 are from 0 to 9. The subscript range of array a3 is from 1 to 10. Array a4 is a two- dimensional array. #include <array.h> int main() { array int a1[10]; // a1[0], . .., a1[9] array int a2[0:9]; // a2[0], . .., a2[9] array int a3[1:10];// a3[1], . .., a3[10] array double a4[10][10]; /* . .. */ }
Background image of page 2
Initialization of Computational Arrays Computational arrays can be initialized when they are declared in the same manner as C arrays. By default, computational arrays are initialized to zeros. For example, array int a1[3] = {1, 2, 3}; array double a4[][3] = {{1, 2, 3}, {1, 2, 3}}; array double a5[3][3] = {1, 2, 3, 1, 2, 3};
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array Reference Whole Arrays – The name of a computational array can be used to access a whole array. – For example, the following code fragment adds each element of a to the corresponding element of b . The arrays a and b are treated as vectors as in the linear algebra. array int a[20], b[20]; a = a + b; – This feature make the program much more simpler than the code using normal C arrays. It can be shown by comparing following two examples.
Background image of page 4
#include <stdio.h> #define N 2 #define M 3 int main() { int a[N][M] = {1,2,3, 4,5,6}; int b[N][M]; int i, j; printf("b = \n",); for(i=0; i<N; i++) { for(j=0; j<M; j++) { b[i][j] = a[i][j]+a[i][j]; printf("%d ", b[i][j]); } printf("\n",); } printf("b = \n",); for(i=0; i<N; i++) { for(j=0; j<M; j++) { b[i][j] = 3*a[i][j]; printf("%d ", b[i][j]); } printf("\n",); } return 0; } Program: (Using C arrays)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Output: b = 2 4 6 8 10 12 b = 3 6 9 12 15 18
Background image of page 6
Program: (Using Computational arrays) #include <stdio.h> #include <array.h> #define N 2 #define M 3 int main() { array int a[N][M] = {1,2,3, 4,5,6}; array int b[N][M]; b = a+a; printf("b = \n%d", b); b = 3*a; printf("b = \n%d", b); return 0; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Output: b = 2 4 6 8 10 12 b = 3 6 9 12 15 18
Background image of page 8
Array elements Similar to C arrays, the operator [ n ] can be used to access elements of computational arrays, where n is a valid subscript. For example, the following code fragment array int a[20], b[20]; b[1] = a[2] + b[2]; adds the third element of a to the third element of b , and saves the result to the second element of b .
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/21/2008 for the course EME 005 taught by Professor Cheng during the Fall '07 term at UC Davis.

Page1 / 52

Computational Arrays and Matrix Computations -...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online