Programming and Graphics

(Kiana) #1

3.2 Vector and matrix initialization 51


Problems


3.1.1.Are the following three statements equivalent?


c++;
c+=1;
c=c+1;

3.1.2.Implement the conditional operator to compute the absolute value of a
real number.


3.2 Vector and matrix initialization...................


To declare and initialize a vectorvwhose three elements are real numbers
registered in double precision, we write


double v[3] ={1.0, 2.0, 4.5};

or


double v[] ={1.0, 2.0, 4.5};

which sets:v[0] = 1.0,v[1] = 2.0,v[2] = 4.5.


If we declare and initialize:

double v[5] ={1.0, 2.0};

then: v[0] = 1.0,v[1] = 2.0,v[2] = 0.0,v[3] = 0.0,v[4] = 0.0. Thus, the
uninitialized values of a partially initialized vector are set to zero.


If we only declare and not initialize by stating:

double v[5];

then the vector components are undefined.


Declaration and initialization must be done in a single line. We may not
first declare and then initialize a vector.


Similarly, we can write

char u[3]={78, 34, 78};

char e[10]={’a’, ’b’, ’c’};
Free download pdf