0% found this document useful (0 votes)
15 views4 pages

Exercisel6 Com by HQ

Uploaded by

hamza sheikh
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)
15 views4 pages

Exercisel6 Com by HQ

Uploaded by

hamza sheikh
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/ 4

Faculty of Computing and Information Technology (FCIT) Department of

Computing Indus University, Karachi

Lab No. 6: Arrays Application


Exercise 1
Write a program using arrays to solve the problem. The problem is to read 10 numbers, get
the average of these numbers, and find the number of the items greater than the average.

Source code
import java.util.Scanner;

public class ArrayAverageAndCount {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Read 10 numbers into an array


double[] numbers = new double[10];
System.out.println("Enter 10 numbers:");

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


System.out.print("Enter number " + (i + 1) + ": ");
numbers[i] = scanner.nextDouble();
}

// Calculate the average of the numbers


double sum = 0;

for (double number : numbers) {


sum += number;
}

double average = sum / numbers.length;

// Count the number of items greater than the average


int countGreater = 0;

for (double number : numbers) {


if (number > average) {
countGreater++;
}
}

// Display the results


Faculty of Computing and Information Technology (FCIT) Department of
Computing Indus University, Karachi
System.out.println("\nAverage of the numbers: " + average);
System.out.println("Number of items greater than the average: " +
countGreater);

scanner.close();
}
}
Output

Exercise 2:
Write a Java program to multiply two matrices of 3x3. Suggest the data
Source Code
public class MatrixMultiplication {
public static void main(String[] args) {
// Sample data for two matrices (3x3)
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[][] matrix2 = {
Faculty of Computing and Information Technology (FCIT) Department of
Computing Indus University, Karachi
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};

// Multiply the matrices


int[][] result = multiplyMatrices(matrix1, matrix2);

// Display the result


System.out.println("Matrix 1:");
printMatrix(matrix1);

System.out.println("\nMatrix 2:");
printMatrix(matrix2);

System.out.println("\nResultant Matrix:");
printMatrix(result);
}

// Function to multiply two matrices


private static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;

if (cols1 != rows2) {
System.out.println("Matrix multiplication is not possible.");
return null;
}

int[][] result = new int[rows1][cols2];

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


for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}

return result;
}
Faculty of Computing and Information Technology (FCIT) Department of
Computing Indus University, Karachi
// Function to print a matrix
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}
Output

You might also like