You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

**dust****Member**- Registered: 2005-06-04
- Posts: 152
- Website

I think my logic's right, but I'm not sure where I'm going wrong here. Simple 2 procedures for an exercise (this isn't homework, I'm not in school just learning on my own) in the structured programming book. Basically need to write a function that takes 3 int and returns the sum of the 2 largest of the numbers squared.

```
(define (sqr n)
(* n n))
(define (sum-of-squares x y z)
(define (largest-number (cond
(and (> x y) (> x z) x)
(and (> y x) (> y z) y)
(and (> z x) (> z y) z))))
(define (second-largest (cond
(and (= largest-number x)
(= largest-number y) (> y z) y)
(and (= largest-number y)
(= largest-number x) (> x z) x)
(if (= largest-number z)
(or (= largest-number z)
(> x y))
x
(or (= largest-number y)
(> y x))
y))))
(+ (sqr largest-number) (sqr second-largest)))
```

I know its horridly cumbersome right now, but i'm just learning the language and using only the few things the book teaches you before getting to this exercises. suggestions on fixing this procedure?

Writing stories for a machine.

Offline

**cmp****Member**- Registered: 2005-01-03
- Posts: 350

try:

`(cond ((and (> x y) (> x z)) x) ...) `

a new parenthesis.

but you could also use (max x (max y z)) to retrive the largest number (I guess there is a function named max in scheme).

btw.: I guess it would be easier to get the lowest number an just subtract it from the result.:

```
(-
(+ (sqr x) (sqr y) (sqr z))
(sqr
(min x
(min y z))
)
)
```

Offline

**dust****Member**- Registered: 2005-06-04
- Posts: 152
- Website

Ok, no more errors, now its just bugged lol. I can't see wear my logic is wrong (its 6:50am and the coffee here at work tastes like asswater).

```
(define (square n)
(* n n))
(define (fst-largest n1 n2 n3)
(cond ((and (> n1 n2) (> n1 n3)) n1)
((and (> n2 n1) (> n2 n3)) n2)
((and (> n3 n1) (> n3 n2)) n3)))
(define (scnd-largest nm1 nm2)
(if (>= nm1 nm2) nm1 nm2))
(define (sums x y z)
(define l1 (fst-largest x y z))
(define l2 (cond (= l1 x) (scnd-largest y z)
(= l1 y) (scnd-largest x z)
(= l1 z) (scnd-largest x y)))
(+ (square l1) (square l2)))
```

calling sums with 1 2 3 returns 10....now if my head is on right, shouldn't it return 13?

Writing stories for a machine.

Offline

**cmp****Member**- Registered: 2005-01-03
- Posts: 350

I guess you have made the same mistake:

use (cond ((= l1 x) (scnd-largest y z)) .. instead of (cond (= l1 x) (scnd-largest y z) ..

Offline

**dust****Member**- Registered: 2005-06-04
- Posts: 152
- Website

:pounds head on desk:

lol thanks.

Writing stories for a machine.

Offline

**cmp****Member**- Registered: 2005-01-03
- Posts: 350

no problem, I needed some time to figure this out, too.

Offline

**dust****Member**- Registered: 2005-06-04
- Posts: 152
- Website

Finally working and appears to be bug free *pounds on wood*:

```
(define (square n)
(* n n))
(define (fst-largest n1 n2 n3)
(cond ((and (>= n1 n2) (>= n1 n3)) n1)
((and (>= n2 n1) (>= n2 n3)) n2)
((and (>= n3 n1) (>= n3 n2)) n3)))
(define (scnd-largest nm1 nm2)
(if (>= nm1 nm2) nm1 nm2))
(define (sums x y z)
(define l1 (fst-largest x y z))
(define l2 (cond ((= l1 x) (scnd-largest y z))
((= l1 y) (scnd-largest x z))
((= l1 z) (scnd-largest x y))))
(+ (square l1) (square l2)))
```

Writing stories for a machine.

Offline

Pages: **1**