Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

FinalExam Review Sp2005: Finding Max for Two Structures

Post Answers, Questions, Comments, etc. here.

(Back to Final Exam Review Sp2005)


I'm not sure where to start on this one, could you maybe direct me to which powerpoint slides may be helpful for this question?
Nope – this is one where you just have to think about it and do it. Think about last() Mark Guzdial

A)
public void findMax(int [] array){

int largestindex=0, largestvalue=array[0];

for (int x=1; x<array.getLength(); x++){
	if(array[x]>largestvalue){
		largestvalue=array[x];
		largestindex=x;}}
System.out.println("the largest value is" + largestvalue + ", and it's at position " +largestindex);}


:/ it can't be that simple can it
Yes it can.

B)
public void findMax(intNode head){

intNode current=head,next;
int index=0,largestindex=0,largestvalue=head.getValue;

while(intNode.getNext()!=null){
	index++;
	next=current.getNext();
	if(next.getValue()>current.getValue()){
		largestvalue=next.getValue();
		largestindex=index;}
	current=next;}
System.out.println("the largest value is" + largestvalue + ", and it's at position " +largestindex);}

Did you try these yet? Mark Guzdial


Maybe i am losing my mind due to lack of sleep, but if want to test out this void method where do we do it. Or do we just make a whole new class?
For A., you'd probably make a new class and try it there. For B, you'd have to actually implement intNode, but that should be pretty easy for you now (especially open DrJava and open notes :-) Mark Guzdial

Doesn anyone know if those two methods are indeed a correct way to solve the problem? I see Professor guzdial mentioned "last()" in his first satement. Maybe there is another way to solve these problems.
last() as an example of how to walk a linked list. These look pretty cood to me. Mark Guzdial

On the last one, doesnt it need to be "largestindex = index+1" b/c you are getting the value of the next, not the current?

well, i don't think so, i increased the index at the beginning of the loop for that reason. it's already representing the next node, if "head" is at position 0

I didn't even notice that, i was looking at my code where i had the index++ at the bottom, my bad.

I don't understand part B.

for B. you have to traverse the REMOVEDkeList with "current = current.next;" but each time evaluating the int-value with "current.getValue()" and if(current.getValue > what is stored as largest value) then set largestValue to "current.getValue()" and keep traversing till node.getNext == null (the end).

anonymous

makes a little more sense to me after I read this: Assume that IntNode inherits from LLNode, and each IntNode contains a int instance variable named value, which you should access through the public int accessor getValue().

right. each intNode has a value (current.getValue()) which stores its integer and a next (current.next) for traversing the linked list.

anonymous
Good discussion!



Link to this Page