COMP2051 Assignment 4 – Arrays

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

1) Write JavaScript statements to accomplish each of the following tasks:
a. Display the value of the seventh element of array f.
b. Initialize each of the five elements of one-dimensional array g to 8.
c. Total the elements of array c, which contains 100 numeric elements.
d. Copy 11-element array a into the first portion of array b, which contains 34 elements.
e. Determine and print the smallest and largest values contained in 99-element floatingpoint array
w.
f. Consider a two-by-three array t that will store integers.
g. Write a statement that declares and creates array t.
h. How many rows does t have?
i. How many columns does t have?
j. How many elements does t have?
k. Write the names of all the elements in the second row of t.
l. Write the names of all the elements in the third column of t.
m. Write a single statement that sets the elements of t in row 1 and column 2 to zero.
n. Write a series of statements that initializes each element of t to zero. Do not use a repetition
structure.
o. Write a nested for statement that initializes each element of t to zero.
p. Write a series of statements that determines and prints the smallest value in array t.
q. Write a statement that displays the elements of the first row of t.
r. Write a statement that totals the elements of the fourth column of t.
s. Write a series of statements that prints the array t in neat, tabular format. List the column
subscripts as headings across the top, and list the row subscripts at the left of each row.
2) Use a one-dimensional array to solve the following problem: A company pays its salespeople
on a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for
that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9
percent of $5000, or a total of $650. Write a script (using an array of counters) that obtains the gross
sales for each employee through an XHTML form and determines how many of the salespeople
earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to
an integer amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
2
h) $900–999
i) $1000 and over
3) Write statements that perform the following operations for a one-dimensional array:
a. Set the 10 elements of array counts to zeros.
b. Add 1 to each of the 15 elements of array bonus.
c. Display the five values of array bestScores, separated by spaces.
4) Use a one-dimensional array to solve the following problem: Read in 20 numbers, each of which is
between 10 and 100. As each number is read, print it only if it is not a duplicate of a number that has
already been read. Provide for the “worst case,” in which all 20 numbers are different. Use the smallest
possible array to solve this problem.
5) Label the elements of three-by-five two-dimensional array sales to indicate the order in which they are
set to zero by the following program segment:
a. for ( var row in sales )
b. for ( var col in sales[ row ] )
c. sales[ row ][ col ] = 0;
6) Write a script to simulate the rolling of two dice. The script should use Math.random to roll the first die
and again to roll the second die. The sum of the two values should then be calculated. [Note: Since each
die can show an integer value from 1 to 6, the sum of the values will vary from 2 to 12, with 7 being the
most frequent sum, and 2 and 12 the least frequent sums. Your program should roll the dice 36,000
times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Display the
results in an XHTML table. Also determine whether the totals are reasonable (e.g., there are six ways to
roll a 7, so approximately 1/6 of all the rolls should be 7).]