lab exp declaring arrays)

6
(Object Oriented Programming) EXERCISE Declaring Arrays

Upload: daman-toor

Post on 16-Jul-2015

451 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Lab exp declaring arrays)

(Object Oriented Programming)

EXERCISE

Declaring Arrays

Page 2: Lab exp declaring arrays)

<STUDENT NAME><SECTION>

<DATE>

I. OBJECTIVES:

At the end of the experiment, students must be able to:Cognitive

a) understand how single and multi-dimensional array executes

b) understand the logic of arrays declaration and accession.

c) understand how arrays are manipulated and used.

Psychomotor:

a) construct a program using arrays

b) compile and debug the error of the program

Affective

a) appreciate the concept behind this experiment

II. BACKGROUND INFORMATION

Array is a sequence of objects of the same type and packaged together under one identifier name. The first element in an array is at position zero: array [0]. There are three steps to create an array: Declaration, Construction and Initialization.

Declaring one-dimensional

data_type name_of_array[integer_expression];

Example:

Referencing an element in a one-dimensional array

Page 3: Lab exp declaring arrays)

The syntax is:<array name> [<index>]

Array Assignment

Example:

Array Controlling

III.EXPERIMENTAL PROCEDURE:

1. Create a program that will accept a decimal value input and then choose to either convert it to one of the following number systems:

1 – Decimal to Binary2 – Decimal to Octal3 – Decimal to Hexadecimal

USE ARRAYS TO FORM THE NEWLY CONVERTED NUMBER

Sample Output:Enter a positive decimal value: 14value to which number system:1 – Decimal to Binary2 – Decimal to Octal3 – Decimal to HexadecimalEnter choice: 1Equivalent of 14 in binary is 1110

Page 4: Lab exp declaring arrays)

2. Write a complete Java program that will fill an array with int values read in from the keyboard, one per line, and outputs their sum as well as all the numbers read in, with each number annotated to say what percentage it contributes to the sum. Your program will ask the user how many integers there will be, that will determine the length of the array.

Sample Output: How many integers will you enter? 4 Enter 4 integers, one per line: 2 1 1 2 The sum is 6. The numbers are: 2 which is 33.33% of the sum. 1 which is 16.67% of the sum. 1 which is 16.67% of the sum. 2 which is 33.33% of the sum

3. Create a program that will store student information in two dimensional array studentarr[3][3], the program should be able to display the names of the students and their corresponding grades and averages.

Studentarray[3][3]={{“peter”, 75,77},{“clark”, 78, 80},{“logan”,82,84}};

Page 5: Lab exp declaring arrays)

IV. QUESTION AND ANSWER:

1. Why is it necessary, being a programmer, to use arrays?. What are the risks of not using arrays?

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

2. When do usually single dimensional array is used?_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Page 6: Lab exp declaring arrays)

IV. QUESTION AND ANSWER:

1. Why is it necessary, being a programmer, to use arrays?. What are the risks of not using arrays?

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

2. When do usually single dimensional array is used?_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________