week5a_sol

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

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

View Full Document Right Arrow Icon
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) (-
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/14/2010 for the course EECS 61A taught by Professor Harvey during the Summer '08 term at Berkeley.

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 Right Arrow Icon
Ask a homework question - tutors are online