0% found this document useful (0 votes)
12 views2 pages

Lab 2 - Arrays + Looping

The document describes 4 exercises involving loops and arrays: 1) Write a program to print the multiplication table from 1-9 using nested loops and arrays. 2) Write a program to reverse the order of elements in a one-dimensional array without using another array. 3) Write a program to find the minimum gap between adjacent elements in an integer array, where gap is defined as the difference between adjacent elements. 4) Write a program to perform matrix operations like inputting values, deleting rows, displaying the matrix, and checking if a matrix is magic (rows, columns, and diagonals sum to the same number). The program uses a menu system for the user to select the operation.

Uploaded by

Malék cristo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Lab 2 - Arrays + Looping

The document describes 4 exercises involving loops and arrays: 1) Write a program to print the multiplication table from 1-9 using nested loops and arrays. 2) Write a program to reverse the order of elements in a one-dimensional array without using another array. 3) Write a program to find the minimum gap between adjacent elements in an integer array, where gap is defined as the difference between adjacent elements. 4) Write a program to perform matrix operations like inputting values, deleting rows, displaying the matrix, and checking if a matrix is magic (rows, columns, and diagonals sum to the same number). The program uses a menu system for the user to select the operation.

Uploaded by

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

Lab 2 

: loops and arrays

EXERCISE 1
Write a program that produces the multiplication table of 1 to 9 as shown

| 1 2 3 4 5 6 7 8 9
-------------------------------
1 | 1 2 3 4 5 6 7 8 9
2 | 2 4 6 8 10 12 14 16 18
3 | 3 6 9 12 15 18 21 24 27
4 | 4 8 12 16 20 24 28 32 36
5 | 5 10 15 20 25 30 35 40 45
6 | 6 12 18 24 30 36 42 48 54
7 | 7 14 21 28 35 42 49 56 63
8 | 8 16 24 32 40 48 56 64 72
9 | 9 18 27 36 45 54 63 72 81

Exercise 2
Write a program ReverseArray.java that reverses the order of a one-dimensional array  
a[] of int values. Do not create another array to hold the result. 

Exercise 3
Write a program named minGap.java that finds the minimum 'gap' between adjacent values
in an integer array. The gap between two adjacent values in a arrayis defined as the second
value minus the first value. For example, suppose a variable called array is an array of
integers that stores the following sequence of values:
int[] array = {1, 3, 6, 7, 12};

1
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth
gap is 5 (12 - 7). Thus, the program should display 1 because that is the smallest gap in the
array. If the size of the array is less than 2 elements, you should display 0.

Exercise 4
A. Create a new java project called matrix. The main purpose of the program is to offer to the user a
set of operations on matrices
B. Define a variable T that corresponds to a matrix having the size 5*5.
C. Show the menu that allows the user to choose an operation. The user selects an operation by
typing its number. We define in what follows the content of each option. After finishing an
operation, the menu is re-displayed unless the user typed 5 for Exit.

1. The user enters the values to fill the matrix, only positive values are accepted
2. The user enters the index i of the row to delete. Here is an example with i = 2
Before 1 2 4 7 8 After

1 2 4 7 8 2 5 98 5 4

2 5 98 5 4 8 9 3 6 1

7 4 7 9 3 42 2 95 37 6

8 9 3 6 1
42 2 95 37 6

3. Display the content of the matrix


4. A matrix is magic if the sum of each row is equal to the sum of each column and each
diagonal. The following matrix is magic:

16 9 2 7
6 3 12 13
11 14 5 4
1 8 15 10

5. Quit the program

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy