One Dimensional Array and Multi-Dimensional Arrays PDF
One Dimensional Array and Multi-Dimensional Arrays PDF
One Dimensional Array and Multi-Dimensional Arrays PDF
Lecture 6
(1)
Lecture 6
(2)
One-Dimensional Arrays
Some concepts in mathematics can not be
represented in a natural way using the types
weve seen so far. One such example is vectors.
double
v[0] =
v[1] =
v[2] =
v[3];
1.0;
0.5;
0.1;
/* x-component */
/* y-component */
/* z-component */
double x[100];
double l2norm = 0;
int
i;
for (i=0 ; i < 100 ; i++)
x[i] = i;
l2norm = sqrt(l2norm);
(math.h: sqrt(a) computes
a.)
Marco Kupiainen
marcok@nada.kth.se
NADA
Marco Kupiainen
marcok@nada.kth.se
NADA
Lecture 6
(3)
(4)
Multi-dimensional arrays
A matrix
x0,0
..
.
...
..
.
x0,n1
..
.
xm1,0
...
xm1,n1
Rmn
0 i1 < 10
0 i2 < 3
0 i3 < 5
Marco Kupiainen
marcok@nada.kth.se
NADA
Lecture 6
Marco Kupiainen
marcok@nada.kth.se
NADA
Lecture 6
(5)
Lecture 6
(6)
return nrm;
}
type *parameter);
(Other parameters declared as before.)
Marco Kupiainen
marcok@nada.kth.se
NADA
Marco Kupiainen
marcok@nada.kth.se
NADA
Lecture 6
(7)
Lecture 6
(8)
Plotting
Other options:
Marco Kupiainen
marcok@nada.kth.se
NADA
Lecture 6
(9)
Plotting an example
Lecture 6
(10)
#include "plot.h"
#include <math.h>
int main() {
/* Initialize plotter */
Plotter p = pl_alloc();
double x[30], y[30];
int i;
for (i=0 ; i<30 ; i++) {
x[i]=(6.28*i)/29; y[i]=sin(x[i]);
}
/* Add arrays x,y for plotting */
pl_add(p, 30, x, y, "rx-");
/* Set labels */
pl_xlabel(p, "x"); pl_ylabel(p, "sin(x)");
/* Plot */
pl_plot(p);
Marco Kupiainen
marcok@nada.kth.se
Marco Kupiainen
marcok@nada.kth.se
NADA
NADA