20.9

4 views
Skip to first unread message

allie

unread,
Dec 5, 2011, 7:04:45 PM12/5/11
to byu-cs-jm-142-Fall-2011
when i call this function on the array, the array i pass in is:

{ 83, 753, 4, 56 }

and after the first part in the array is modified, it becomes:

{ -41, 753, 4, 65 }

1) do you want the differences to be positive, or just since 42 - 83
is -41, should we just allow it to be -41, or do you want us to have
an absolute value function in there?

{ -41, -794, 4, 65 }

2) now that array[0] has become -41, the array[1] is passed in and 753
is subtracted from -41, so it will become -794, instead of having 83 -
753.

do you want us to make our functions not update as the array is sent
through the function until it reaches the end, or will it be updated,
as I showed above?
(does my question make sense? =] )

Jay McCarthy

unread,
Dec 5, 2011, 9:51:47 PM12/5/11
to byu-cs-jm-1...@googlegroups.com
If you pass in

{ 83, 753, 4, 56 }

then you should get back

{ 42 - 83, 83 - 753, 753 - 4, 4 - 56 }

Thus, it is okay to have negative numbers.

As you observe, it will not be possible to do this if you overwrite the values to the left without saving them somewhere.

There are two approaches to solving this:

1) Save the value to the left in a variable (accumulator argument)

2) Perform the computation from the right

You'll only need to pick one of them.

Is that helpful?

Jay
--
Jay McCarthy <j...@cs.byu.edu>
Assistant Professor / Brigham Young University
http://faculty.cs.byu.edu/~jay

"The glory of God is Intelligence" - D&C 93

allie

unread,
Dec 5, 2011, 10:46:45 PM12/5/11
to byu-cs-jm-142-Fall-2011
yes very helpful thank you

> Assistant Professor / Brigham Young Universityhttp://faculty.cs.byu.edu/~jay

Sanjeev

unread,
Dec 7, 2011, 2:27:15 PM12/7/11
to byu-cs-jm-1...@googlegroups.com

tkatuka

unread,
Dec 10, 2011, 4:04:52 AM12/10/11
to byu-cs-jm-142-Fall-2011
Could you please look at this exercise(20opt/9.cc)? My net id is
tkatuka

Thank you.

On Dec 5, 7:51 pm, Jay McCarthy <jay.mccar...@gmail.com> wrote:

> Assistant Professor / Brigham Young Universityhttp://faculty.cs.byu.edu/~jay

Jay McCarthy

unread,
Dec 10, 2011, 8:35:34 AM12/10/11
to byu-cs-jm-1...@googlegroups.com
Looks great
Reply all
Reply to author
Forward
0 new messages