Help, Building List & finding min and max
I need to build a list and then return the Min and Max number, The code I have just keeps returning 0s. Could any one help? Thnx
private SimpleList<Integer> buildList(String line) {
//Build a list of integers from line
//Assume the line is syntactically correct
SimpleList <Integer> list;
list = new SimpleList <Integer>();
int i = 0;
while (i<line.length()){
list.add(i);
i++;
}
return list;
}
private int findMax(SimpleList<Integer> list) {
//Return the maximum integer in list
list.reset();
int Max = list.next();
while (list.hasNext()){
int t = list.next();
if(t > (Max));
t = Max;
}
return Max;
}
private int findMin(SimpleList<Integer> list) {
//Return the minimum integer in list
list.reset();
int min = list.next();
while (list.hasNext()){
int x = list.next();
if(x < (min));
x = min;
}
return min;
}
Re: Help, Building List & finding min and max
Your logic here is a bit tangled.
For instance, to find the maximum value, you need to do something like this:
max = 0
repeat for each list element:
x = next list element
if x > max then max = x
finally, return max
Re: Help, Building List & finding min and max
What does the reset method do? Do you have to write the findMax and findMin methods? It would much more efficient to find the max and min values as you are building the list. Which is rather an odd method. If you pass the String "frog" to the buildList meethod then the List will contain 0,1,2 & 3., if you pass "help" you get the same List and if you pass "apple" you get 0,1,2,3 & 4. Is that what the method is really supposed to do?
Re: Help, Building List & finding min and max
Well the the buildlist is suposed to build a list from the string entered. Then the FindMax & Min are suposed to figure out what the min and maxium values are are display them.
Re: Help, Building List & finding min and max
Yeah I'm fairly sure we are all aware of what your requirements are. Perhaps you could provide an example of what the input and output is and what you think the output should be. As well as ask a specific question. Is there anything about my previous reply you did not understand?