c++April 16

c++April 16 - double a double q = 14.3 char pChar1 char...

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

View Full Document Right Arrow Icon
April 16, 2008 Multiply dimensioned Arrays Void zero (double x[], double y[][d2], double z[][d2][d3, int d1) { for (int i=0; i<d1; i++) { x[i]= 0.0; for(int j=0; j<d2;j++) { y[i][j]=0.0; for (int k=0;k<d3;k++) z[i][j][k]=0.0; } } } dynamic allocation 1. one simple allocation; programmer does indexing – array must be rectangular delete []A; 2. more complex allocation but simple conventional indexing – array can be ragged void caller 1() { int d1, d2, d3; cin>>d1>>d2>>d3; double * Q=allocate1(d1,d2);
Background image of page 1

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

View Full Document Right Arrow Icon
int I, j; cin>>i>>j; double v; cin>>v; set1(Q, d1, d2, I,j,v); double* allocate 1(int d1, intd2) { double* p = new double[d1*d2]; //zero array: for(int i=0; i<d1; i++) for(int j=0; j<d2;J++) p[i][j]=0.0; { int loc = i*d2+j; p{loc]=0.0; } void set1(double* x, int d1, int d2, int I, int j, double v) { int loc = i*d2+j; //offset x[loc]=v; //should do validation on I and j; // Net effect: x[i][j] = v
Background image of page 2
void usePointers()
Background image of page 3

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

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { double a; double q = 14.3; char* pChar1; char* pChar2 = NULL; double* mary = &a; char stop = '#'; pChar1 = &stop; int val = 27; double* wilma = &q; int* fred = &val; // Now how do they work? *wilma = -4.3; cout << "q = " << q << endl; // works the other way, too: val = 43; cout << "I point to: " << *fred << endl; double* another = wilma; *another = 2.3; *wilma = 2.0 * *wilma; cout << "q = " << q << endl; } void caller2() { int d1, d2; cin>>d1>.d2; double** Q=allocate(d1, d2); int I, j; cin>>i>>j; doule v; cin>>v; set1(Q,d1,d2,I,j,v); double** allocate2(int d1, int d2) { double** A= new double*[d1]; for(int i=0; i<d1; i++) A[i}=new double[d2]; //initialize to zero for (int i=0; i<d1; i++) for(int j=0; j<d2; j++) A[i][j]=0.0; Return A; } void set2(double**A int d1, int d2, int I, int j; double v) { Api][j]=v; }...
View Full Document

{[ snackBarMessage ]}

Page1 / 5

c++April 16 - double a double q = 14.3 char pChar1 char...

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

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