{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

week5a_sol

# week5a_sol - CS 61A Notes W eek 5a Vectors(solu8ons Just...

This preview shows pages 1–2. Sign up to view the full content.

CS61A Notes – Week 5a: Vectors (solu8ons) Just When You Were Ge?ng Used to Lists QUESTIONS 1. Write a procedure (sum-of-vector v) that adds up the numbers inside the vector. Assume all data felds are valid numbers. You can use an accumulator or a helper like this: (define (sum-of-vector v) (define (helper index) (cond ((= index (vector-length v)) 0) (else (+ (vector-ref v index) (helper (+ index 1)))))) (helper 0)) 1. Write a procedure (vector-copy! src src-start dst dst-start length) . APer the call, length elements in vector src star8ng From index src-start should be copied into vector dst star8ng From index dst-start . STk> a => #(1 2 3 4 5 6 7 8 9 10) STk> b => #(a b c d e f g h i j k) STk> (vector-copy! a 5 b 2 3) => okay STk> a => #(1 2 3 4 5 6 7 8 9 10) STk> b => #(a b 6 7 8 f g h i j k) (define (vector-copy! src src-start dst dst-start length) (if (> length 0) (begin (vector-set! dst dst-start (vector-ref src src-start)) (vector-copy! src (+ src-start 1) dst (+ dst-start 1) (-

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

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

{[ snackBarMessage ]}

### Page1 / 2

week5a_sol - CS 61A Notes W eek 5a Vectors(solu8ons Just...

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

View Full Document
Ask a homework question - tutors are online