Professional Documents
Culture Documents
Lab 1: Arrays
Objectives:
Background:
It is a linear data structure and it's mainly used to store similar data. An array is a
particular method of storing elements of indexed data. Elements of data are stored
sequentially in blocks within the array. Each element is referenced by an index, or
subscripts.
The index is usually a number used to address an element in the array. For example, if
you were storing information about each day in August, you would create an array
with an index capable of addressing 31 values -- one for each day of the month.
Indexing rules are language dependent, however most languages use either 0 or 1 as
the first element of an array.
Lab Example:
1. Simulate the execution of the following method with each of the following arrays
passed as its parameter, and write the value it would return:
Solution:
Array Value returned
{5} 0
{3, 12} 9
{4, 2, 10, 8} 6
{1, 9, 3, 5, 7} 8
{8, 2, 10, 4, 10, 9} 2
2. Write a static method named findMin that returns the minimum value in an array of
integers. For example, if a variable named list refers to an array containing the values
{16, 12, 25, 44}, the call of findMin(list) should return 12 (the smallest value in the
list). You may assume that the array has at least one element.
Test your code with the following class:
Solution:
Lab Exercise:
a constructor that initializes the department name and creates the array.
Datastruc
3.Write a test application named Lab1Test. In the main method, do the following:
Input the department name and create a department object.
Input the number of courses in the department.
Input the id and name of each course and add it to the department
(Hint: Use a loop).
Print the courses.
Input the id of a course to be deleted. Delete the course.
Print the courses.
Sample Output
This section includes common system/syntax errors that may occure during the lab.