Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 4 of 4
Thread: Array Help Mostly

10182006, 07:53 AM #1
 Join Date
 Oct 2006
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Array Help Mostly
Alright I have created some code that works as far as random number loading. But i am having difficulty trying to create code that will find the location of the smallest number. Here is the prompt I am giving to clear things up and below is the code i created.
Prompt:Write a JavaScript function called "findSmallest" that takes two parameters: an array of numbers, and the size of the array. The function returns the index of the smallest element (note that I am not asking you to return the smallest value itself... I want the function to tell me where in the array the smallest value is found)
Notice this is only a piece of what I have to do. There is so much more but this is the one thing troubling me. Can anyone help me? if it is easier to email me. than email me at mattkaczco2006@gmail.com. Thank you
<html>
<head>
<title>random</title>
<script type="text/javascript">
<!
function exam1 ( ) {
var max = 25;
var arr = new Array(max);
var sLoc = 1; // just an initial value
getData(arr); // assume this function loads the array with
// integers randomly selected from the
// range 0 to 99 inclusive
document.writeln("<h2>Random original data</h2>");
for (i=0; i<max; i++) {
document.write(" "+arr[i]);
}
// sLoc = findSmallest(arr,max);
// you stomp the 1 init value with the real location
alert("smallest value is here: "+sLoc);
}
function getData (arr) {
var i;
for (i=0; i<arr.length; i++) {
arr[i] = Math.floor(100*Math.random());
}
}
// write your function findSmallest to go here
>
</script>
</head>
<body>
<form name="f" action="">
<input name="b3" type="button" value="go do it"
onclick="exam1()" >
</form>
</body>
</html>
10182006, 08:16 AM
#2
 Join Date
 Sep 2005
 Posts
 1,791
 Thanks
 5
 Thanked 36 Times in 35 Posts
basically, as the order of the numbers is random, you're going to need to loop through them all to find the smallest.
In each iteration of the loop you'll be considering the 'current' array value and deciding if it is the 'currentsmallest'
If you compare the current value to the currentsmallest value, then if the former is smaller than the latter, then 'currentsmallest' takes on the new value. THen move onto the next. When you've finished looping, 'currentsmallest' will contain the smallest number.
As you want the index, as well as keeping track of 'currentsmallest' as you're iterating, you'll want to keep track of 'currentsmallestindex' as well, so you can return this.
Hope that helps
10182006, 02:22 PM
#3
 Join Date
 Oct 2006
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
Can you give me an example of the line of code.. Im actually really confused by this. I know that you have to run a loop to find the smallest value by taking one of the values in the random selection and comparing it.. How do you call a value from the random number count and compare it to another number from it?
10182006, 02:30 PM
#4
 Join Date
 Oct 2006
 Posts
 3
 Thanks
 0
 Thanked 0 Times in 0 Posts
alright. Just formed this code. Is there something i should do different with this. or can you see if this is what you were talkin about?
function findSmallest(arr,max){
var secondArray = new Array(max) // declaration of new Array
secondArray = arr // making secondArray equal the normal array
var where // variable representing where the minimum value is stored
var minimumInArray = secondArray[0]; //variable used to find minimum value in array
for (k=0; k<secondArray.length; k++) {
if (secondArray[k]<minimumInArray) {
minimumInArray = secondArray[k];
}
}
for (i=0; i<max; i++) {
if (minimumInArray == arr[i]) {
where = i;
}
}
return where;
}