COMP 14

advertisement
14-13.1
COMP 14
Loop and array self-help exercises: Answers
1. Write a loop to display the integers 0 through 9 in ascending order.
for (var i=0; i<10; i++)
{ alert(i);}
2. Write a loop to display the integers 9 through 0 in descending order.
for (var i=9; i>=0; i--)
{ alert(i);}
3. Write a loop to sum the integers 0 through 100.
var sum = 0;
for (var i=0; i<=100; i++)
{ sum = sum + i;}
// At the end of the loop, sum contains the sum of 0+1+…100.
4. Write a loop (plus one additional line of code) to calculate the average of the integers 0
through 100.
var sum = 0;
for (var i=0; i<=100; i++)
{ sum = sum + i;}
var average = sum/101;
5. Write code to create an array called grades and put the numbers
0, 10, 20, 30, 40, 50, 60, 70, 80, 90 into the array.
Several ways to do this
// Option 1: put values in when array is defined.
var grades = new Array(0,10,20,30,40,50,60,70,80,90);
// Option 2: Put all elements into the array at once,
// but after the array has been defined.
var grades = new Array;
grades = [0,10,20,30,40,50,60,70,80,90];
// Option 3: use a loop.
var grades = new Array;
for (var i=0; i<10; i++)
{ grades [i] = i*10; }
14-13.2
6. Write code to put 10 random integers in the range 0…99 into an array.
var grades = new Array;
for (var i=0; i<10; i++)
{ grades[i] = Math.round(100*Math.random());}
The remaining exercises use the array of random integers created in exercise 6.
7. Display only those elements of the array that are even. Generalize your code to handle an
array of any size.
for (var i=0; i<grades.length; i++)
{ if (grades[i]%2 == 0) // Test to see if even.
{ alert (grades[i]);}
}
8. Create another array containing only those array elements that are in the range 25…75.
var newGrades=new Array;
var newGradesIndex=0;
for (var i=0; i<grades.length; i++)
{ if (grades[i]>=25 && grades[i]<=75)
{ newGrades[newGradesIndex]=grades[i];
newGradesIndex++;
}
}
// newGrades now contains elements from grades whose values
// are in 25…75.
9. Find the sum of the numbers in the array.
var sum=0;
for (var i=0; i<grades.length; i++)
{ sum = sum + grades[i];}
10. Find the average of the numbers in the array.
var sum=0;
for (var i=0; i<grades.length; i++)
{ sum = sum + grades[i];}
var average = sum/grades.length;
14-13.3
11. Find the smallest number in the array.
// Find the smallest element in the array.
var minSoFar = grades[0];
for (var i=1; i<grades.length; i++)
{ if (grades[i] < minSoFar)
{ minSoFar = grades[i];} // New smaller value.
}
// minSoFar contains the minimum value.
12. Find the index of the smallest number in the array.
// Find the index of the smallest element in the array.
var minIndexSoFar = 0;
for (var i=1; i<grades.length; i++)
{if (grades[i] < grades[minIndexSoFar])
{ minIndexSoFar = i;} // New smaller value.
}
// minIndexSoFar contains the index of the minimum value.
13. Write a function that takes an array as its parameter and returns the index of the smallest
element.
function minIndex(a)
{
// Return the index of the smallest element in
// the array a.
var minIndexSoFar = 0;
for (var i=1; i<a.length; i++)
{ if (a[i] < a[minIndexSoFar])
{ minIndexSoFar = i;} // New smaller value.
}
// minIndexSoFar contains the index of the minimum
// value.
return minIndexSoFar;
}
14-13.4
14. Write a function that takes an array and an index as its parameters and returns the index of
the smallest element in the subarray starting at the specified index.
function minIndex(a,s)
{
// Return the index of the smallest element in
// the subarray a[s…s.length-1].
// Restriction: s must be an integer in the range
//
0…a.length-1.
var minIndexSoFar = s;
for (var i=s+1; i<a.length; i++)
{ if (a[i] < a[minIndexSoFar])
{ minIndexSoFar = i;} // New smaller value.
}
// minIndexSoFar contains the index of the minimum
// value in a[s…s.length-1].
return minIndexSoFar;
}
15. Write a function to swap two elements of an array. Parameters are the array and the
indices of the two elements to be swapped.
function swap(a, i, j)
{
// Swap the ith and jth element of the array a.
// Restrictions: a must be an array;
//
i and j must be integers in the
//
range 0…a.length-1.
var temp = a[i];
a[i] = a[j];
a[j] = temp;
}
16. Use the functions from exercise 14 and 15 to implement a selection sort function. The
parameter will be the array to be sorted.
function sort(a)
{
// Sort the array a in ascending order by selection sort.
// Restriction: a contains numbers only.
for (var i=0;i<a.length-1;i++)
{
// Swap the smallest in a[i…a.length-1] with a[i].
{swap(a, i, minIndex(a, i)); }
}
}
14-13.5
17. Given that JavaScript uses call by value (i.e. gives the function a copy of the parameter,
not the parameter itself), why do your swap and sort functions work?
The array name is actually a reference to the array itself. JavaScript passes a copy of
the reference to the function, but the array referenced by the original reference and by
the copy are the same.
18. Write a function that takes an array as its parameter and returns a sorted version of that
array. The original array is not altered.
// Returns a sorted copy of array a.
function sortedVersion(a)
{
// Make a copy of array a.
var copy = new Array;
for (var i=0; i<a.length; i++)
{ copy[i] = a[i]; }
// Sort the copy.
sort(copy);
// Return the sorted copy.
return copy;
}
19. Write a function to search an array looking for key. Return the position of the first
occurrence of key, or, if key isn’t found, return -1.
// Search list. Return the location of the first occurrence
// of key in list. If key is not in the list, return -1.
function search(list, key)
{
for (var i=0;i<list.length;i++)
{
if (list[i] == key)
return i;
}
// If we survive to here, key wasn’t found.
return -1;
}
14-13.6
20. Write a function to search an array looking for key. Return an array containing all the
locations where key was found.
// Search list. Return an array containing all locations
// in the array where key was found.
function searchAll(list, key)
{
var foundList = new Array; // Contains locations where
// key was found.
var foundCount=0;
// Count of the number of
// occurrences of key found
// so far.
for (var i=0; i<list.length; i++)
{
if (list[i] == key)
{
foundList[foundCount] = i;
// Add to found location
// to found list.
foundCount++;
}
}
// Return found array. The array will be empty if no
// occurrences of key were found.
return foundList;
}
Download