0% found this document useful (0 votes)
13 views7 pages

Java Array Operations and Methods Guide

The document contains a series of Java programming exercises focusing on array manipulation, including declaring arrays, finding maximum values, counting even numbers, searching elements, and working with 2D arrays. It provides code snippets for each task, demonstrating various array operations such as calculating averages, finding odd numbers, and implementing methods to modify arrays. Additionally, it includes a bonus exercise on generating Pascal's Triangle.

Uploaded by

Esraa Elsayed
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)
13 views7 pages

Java Array Operations and Methods Guide

The document contains a series of Java programming exercises focusing on array manipulation, including declaring arrays, finding maximum values, counting even numbers, searching elements, and working with 2D arrays. It provides code snippets for each task, demonstrating various array operations such as calculating averages, finding odd numbers, and implementing methods to modify arrays. Additionally, it includes a bonus exercise on generating Pascal's Triangle.

Uploaded by

Esraa Elsayed
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

Name: Mohamed Walid

ID: 25100241

1- Declare an array of 10 integers. Fill it with the numbers from 1 to 10 using for loop,
then print them in reverse order.

int[] numbers = new int[10];


for (int i =1 ; i < 10; i++) {
numbers[i-1] = i;
}
for (int i = 9; i >= 0; i--) {
[Link](numbers[i]);
}

2- Write a Java method that takes array of integers as input and return the largest
element.

public static int Biggest(int[] numbers) {


int biggest = numbers[0];
for (int i = 0; i < [Link]; i++) {
if (numbers[i] > biggest) {
biggest = numbers[i];
}
}
return biggest;
}

In main :

public static void main(String[] args) {


int[] arr1 = {5, 12, 8, 3, 20};
[Link]("The Biggest num is " + Biggest(arr1));

}
3- Write a Java method that count how many even numbers exist in an integer array.

public static int even(int[] numbers) {


int total = 0;
for (int i = 0; i < [Link]; i++) {
if (numbers[i] % 2 == 0) {
total++;
}
}
return total;
}

in main:

public static void main(String[] args) {


int[] arr = {1, 2, 3, 4, 5, 6};
[Link]("Even numbers are: " + even(arr2));

4- Given an array {4, 2, 9, 1, 7, 3}. Write code to search for a number and print “Found”
or “Not Found”.

int[] array = {4, 2, 9, 1, 7, 3};


int searchnum = 7;
boolean found = false;

for (int i = 0; i < [Link]; i++) {


if (array[i] == searchnum) {
found = true;
break;
}
}

if (found) {
[Link]("Found");
} else {
[Link]("Not Found");
}
5- Write a Java method that accepts 2D array and prints the main diagonal values.

public static void maindiagonal(int[][] matrix) {


for (int i = 0; i < [Link]; i++) {
[Link](matrix[i][i] + " ");
}
[Link]();
}
6- Implement 2D array of student grades (3 students × 4 subjects) and calculate each
student’s average grade.
int[][] grades = new int[3][4];
grades[0][0] = 100; grades[0][1] = 90; grades[0][2] = 78; grades[0][3] = 92;
grades[1][0] = 88; grades[1][1] = 65; grades[1][2] = 95; grades[1][3] = 84;
grades[2][0] = 92; grades[2][1] = 89; grades[2][2] = 90; grades[2][3] = 91;

for (int student = 0; student < 3; student++) {


int total = 0;
for (int subject = 0; subject < 4; subject++) {
total = total + grades[student][subject];
}
double average = total / 4.0;
[Link]("Student: " + (student + 1) + " average: " +
average);
}

7- Write a method that takes int array and returns array including maximum and
minimum number.

public int[] maxmin(int[] numbers) {


int max_mum = numbers[0];
int mini_mum = numbers[0];

for (int i = 0; i < [Link]; i++) {


if (numbers[i] > max_mum) {
max_mum = numbers[i];
}
if (numbers[i] < mini_mum) {
mini_mum = numbers[i];
}
}

int[] result = new int[2];


result[0] = max_mum;
result[1] = mini_mum;
return result;
}
8- Write a Java method that takes 2D array and returns 1D array including all odd
numbers.

public int[] oddnumbers(int[][] matrix) {


int oddscount = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] % 2 != 0) {
oddscount++;
}
}
}

int[] oddnumbers = new int[oddscount];


int position = 0;
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] % 2 != 0) {
oddnumbers[position] = matrix[i][j];
position++;
}
}
}
return oddnumbers;
}

9- Create a Java method that takes 2D array as input and increment each element by 2.

public void two(int[][] matrix) {


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = matrix[i][j] + 2;
}
}
}
10- Write a Java code that fills an array with all multiples of 7 from 1 to 100.

int[] multiples = new int[100];


for (int i = 0; i < 100; i++) {
multiples[i] = (i + 1) * 7;
}

for (int i = 0; i < 10; i++) {


[Link](multiples[i] + " ");
}

11- Create a jagged array of 4 rows where row i has (i × 2 + 1) elements.

int[][] jagged = new int[4][];

for (int row = 0; row < 4; row++) {


int size = row * 2 + 1;
jagged[row] = new int[size];

for (int col = 0; col < size; col++) {


jagged[row][col] = (row + 1) * 10 + col;
}

12- Create a Java method that prints the average of each row and the average of each
column.

public static void Average() {


int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

for (int row = 0; row < [Link]; row++) {


int total = 0;
for (int col = 0; col < numbers[row].length; col++) {
total = total + numbers[row][col];
}
double avg = total / 3.0;
[Link]("Row " + (row + 1) + " average: " + avg);
}

for (int col = 0; col < 3; col++) {


int total = 0;
for (int row = 0; row < [Link]; row++) {
total = total + numbers[row][col];
}
double avg = total / 3.0;
[Link]("Column " + (col + 1) + " average: " + avg);
}
}
13- Create a Java method that checks if two 2D arrays are symmetric.

static boolean symmetric(int[][] a, int[][] b){


for(int i = 0; i < [Link]; i++)
for(int j = 0; j < a[i].length; j++)
if(a[i][j] != b[j][i])
return false;
return true;
}

14- Write a method that swaps the first and last elements of an array.

static void swap(int[] arr){


int temp = arr[0];
arr[0] = arr[[Link] - 1];
arr[[Link] - 1] = temp;
}

15- Create a Java method (zeroing) that sets entire row and column to zero if an
element is zero.

static void zeroing(int[][] arr){


boolean[] rows = new boolean[[Link]];
boolean[] cols = new boolean[arr[0].length];

for(int i = 0; i < [Link]; i++)


for(int j = 0; j < arr[i].length; j++)
if(arr[i][j] == 0){
rows[i] = true;
cols[j] = true;
}

for(int i = 0; i < [Link]; i++)


for(int j = 0; j < arr[i].length; j++)
if(rows[i] || cols[j])
arr[i][j] = 0;
}
Bonus: Write Java code that creates the first 20 rows of Pascal’s Triangle.

Public void pascal(int n){


int[][] p = new int[n][];
for(int i = 0; i < n; i++){
p[i] = new int[i + 1];
p[i][0] = 1;
p[i][i] = 1;
for(int j = 1; j < i; j++)
p[i][j] = p[i - 1][j - 1] + p[i - 1][j];
}
}

You might also like