0% found this document useful (0 votes)
6 views

Arrays Exercises

The document provides examples of coding exercises involving arrays in Java. It includes tasks to access and initialize array elements, use multi-dimensional arrays, manipulate strings, and calculate totals and averages stored in arrays.

Uploaded by

jysusjm7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Arrays Exercises

The document provides examples of coding exercises involving arrays in Java. It includes tasks to access and initialize array elements, use multi-dimensional arrays, manipulate strings, and calculate totals and averages stored in arrays.

Uploaded by

jysusjm7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Trassierra High School

Computer Science Department

Arrays: Exercises
1.- Write Java statements to accomplish each of the
following tasks:
a) Display the value of element 6 of array f.
b) Initialize each of the five elements of one-
dimensional integer array g to 8.
c) Total the 100 elements of floating-point array c.
d) Copy 11-element array a into the first portion of
array b, which contains 34 elements.
e) Determine and display the smallest and largest
values contained in 99-element floating point array w.

2.- Consider a two-by-three integer array t.


a) Write a statement that declares and creates t.
b) How many rows does t have?
c) How many columns does t have?
d) How many elements does t have?
e) Write access expressions for all the elements in
row 1 of t.
f) Write access expressions for all the elements in
column 2 of t.
g) Write a single statement that sets the element of t
in row 0 and column 1 to zero.
h) Write a series of statements that initializes each
element of t to zero. Do not use a repetition
statement.
i) Write a nested for statement that initializes each
element of t to zero.
j) Write a nested for statement that inputs the values
for the elements of t from the user.
k) Write a series of statements that determines and
displays the smallest value in t.
l) Write a printf statement that displays the elements
of the first row of t. Do not use
repetition.
m) Write a statement that totals the elements of the
third column of t. Do not use repetition.
n) Write a series of statements that displays the
contents of t in tabular format. List the
column indices as headings across the top, and list
the row indices at the left of each row.

3.- (Sales Commissions) 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% of their gross sales for that week. For
example, a salesperson who grosses $5000 in sales in a week
receives $200 plus 9% of $5000, or a total of $650. Write
an application (using an array of counters) that 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

Web Application Development (Programming Languages) Page 1


Trassierra High School
Computer Science Department

d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
Summarize the results in tabular format.
4.- (Duplicate Elimination) Use a one-dimensional array to
solve the following problem: Write an application that
inputs five numbers, each between 10 and 100, inclusive. As
each number is read, display it only if it is not a
duplicate of a number already read. Provide for the “worst
case,” in which all five numbers are different. Use the
smallest possible array to solve this problem. Display the
complete set of unique values input after the user enters
each new value.

5.- Write an application that calculates the product of a


series of integers that are passed to method product using
a variable-length argument list. Test your method with
several calls, each with a different number of arguments.

6.- (Airline Reservations System) A small airline has just


purchased a computer for its new automated reservations
system. You have been asked to develop the new system. You
are to write an application to assign seats on each flight
of the airline’s only plane (capacity: 10 seats). Your
application should display the following alternatives:
Please type 1 for First Class and Please type 2 for
Economy. If the user types 1, your application should
assign a seat in the first class section (seats 1–5). If
the user types 2, your application should assign a seat in
the economy section (seats 6–10). Your application should
then display a boarding pass indicating the person’s seat
number and whether it is in the first-class or economy
section of the plane.
Use a one-dimensional array of primitive type boolean to
represent the seating chart of the plane. Initialize all
the elements of the array to false to indicate that all the
seats are empty. As each seat is assigned, set the
corresponding elements of the array to true to indicate
that the seat is no longer available.
Your application should never assign a seat that has
already been assigned. When the economy section is full,
your application should ask the person if it is acceptable
to be placed in the first-class section (and vice versa).
If yes, make the appropriate seat assignment. If no,
display the message "Next flight leaves in 3 hours."

7.- Write an application that uses an enhanced for


statement to sum the double values passed by the command-
line arguments. [Hint: Use the static method parseDouble of
class Double to convert a String to a double value.]

8.- (Total Sales) Use a two-dimensional array to solve the


following problem: A company has four salespeople (1 to 4)
who sell five different products (1 to 5). Once a day, each

Web Application Development (Programming Languages) Page 2


Trassierra High School
Computer Science Department

salesperson passes in a slip for each type of product sold.


Each slip contains the following:
a) The salesperson number
b) The product number
c) The total dollar value of that product sold that
day
Thus, each salesperson passes in between 0 and 5 sales
slips per day. Assume that the information from all the
slips for last month is available. Write an application
that will read all this information for last month’s sales
and summarize the total sales by salesperson and by
product. All totals should be stored in the two-dimensional
array sales. After processing all the information for last
month, display the results in tabular format, with each
column representing a particular salesperson and each row
representing a particular product. Cross-total each row to
get the total sales of each product for last month. Cross-
total each column to get the total sales by salesperson for
last month. Your tabular output should include these cross-
totals to the right of the totaled rows and to the bottom
of the totaled columns.

9.- Write an application that allows the introduction of a


string and count the number of words that there are on it.
You have to show each of the words on a separated line.

10.- Write an application that allows to introduce a string


of maximum length 80 characters and shows it upside down.

11.- Write an application that reads a string and prints


it. You have to consider that the string can’t have more
than one blank space between the words.

12.- Write an application that obtains a substring of a


string. You have to introduce two integer values. The first
value is the starting position of the substring in the
original string. The second value is the number of
characters that have the substring.

13.- Write an application that allows to create an array


of integers array[15]. Then you have to find the greatest
number of the array and the position that it is.

14.- In a array called names are stored the names of 5


workers of a company. In other array called times are
stored the numbers of hours that each worker works per
month. You have to implement the application that displays
the name of each worker and his salary. You must know that
the hour is charged at 15.50 euros.

15.- Write an application that allows create an array with


five phrases with maximum length of 50 characters. Make a
program that calculates the longest sentence and shows it,
indicating the position in the array phrases.

16.- Write an application that creates a two-dimensional


array called mat[5][3]. Then, display the sum of each of

Web Application Development (Programming Languages) Page 3


Trassierra High School
Computer Science Department

their ranks and the sum of each of its columns.

17.- You have stored the names of the dishes of a


Restaurant in the array dishes[20]. And you have stored
their prices in the array prices[20]. Make a program that
shows the dishes and their prices. The application must ask
you which plates you prefer. At last, the program must show
the bill.
NOTE: You must apply a 16% IVA to the total bill. You have
to valid if the dish requested is in the list of possible
dishes.

18.- This college has stored the names of students in the


array names[15]. The names of the subjects in the array
subjects[4] and the grade of each student in each course in
the array marks[4][15]. Write an application that shows the
score of each student in each course. The program also must
show the average of each student.

19.- You have the array prov[8] = {"Almería", "CADIZ",


"CORDOBA", "GRANADA", "Huelva", "JAEN" ,"MALAGA",
"SEVILLE"}. And the arrays p1[8] and p2[8] with the votes
obtained by the political party in the Andalucia elections.
Make a program that allows:

a) Create the arrays P1 and P2.


b) You introduce the province’s name an the program must
show the votes of p1 and p2 in that province.
c) The name of the political party winner

Web Application Development (Programming Languages) Page 4

You might also like