Times vri v1i v2i sum v elements 13 pointers

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: r[3] = {0, 0, 0}; • The prototype for a vector sum funcCon: void v_sum(double v1, double v2, double vr); 12 Passing by Reference Example (cont’d) /* Function: v_sum * Purpose: Computes the sum of two vectors * Input: * v1 – array containing first vector * v2 – array containing second vector * Output: * vr – resultant vector of v1 + v2 */ void v_sum(double v1, double v2, double vr) { int i; // declare loop control variable for(i = 0; i < 3; i++) // loop 3 times vr[i] = v1[i] + v2[i]; // sum v elements } 13 Pointers and Arrays • Pointers and arrays have a close relaConship • Any operaCon that can be done using array subscripCng can be done using pointers. 14 Pointers and Arrays (cont’d) • Recall the array declaraCon int y[10]; /* declares an integer array with */ /* 10 elements */ y[0] y[1] y[2] y[3] y[4] y[5] y[6] y[7] y[8] y[9] y: • Then if py is declared a pointer to an integer int *py; we can assign the pointer to an element of the array by py = &y[0]; 15 Pointers and Arrays (cont’d) • The assignment py = &y[0]; sets py to point to ele...
View Full Document

Ask a homework question - tutors are online