Java Array Programming Exercises

Replacing Non-Positive Integers

The following example demonstrates how to iterate through an integer array of size 10, replacing any non-positive values (zero or negative) with 1. The program first reads the input values, processes the array to enforce the positive constraint, and then prints the updated values.

import java.util.Scanner;

public class PositiveReplacement {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] data = new int[10];

        // Read input values
        for (int i = 0; i < 10; i++) {
            data[i] = input.nextInt();
        }

        // Replace non-positive numbers
        for (int i = 0; i < 10; i++) {
            if (data[i] <= 0) {
                data[i] = 1;
            }
        }

        // Output the result
        for (int i = 0; i < 10; i++) {
            System.out.printf("X[%d] = %d%n", i, data[i]);
        }
    }
}

Generating a Geometric Sequence

This exercise involves populating an array of 10 integers where the first element is provided by the user, and each subsequent element is double the value of the preceding one. This creates a geometric progression with a common ratio of 2.

import java.util.Scanner;

public class GeometricProgression {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] sequence = new int[10];
        
        // Initialize the first element
        sequence[0] = input.nextInt();

        // Generate the rest of the sequence
        for (int i = 1; i < 10; i++) {
            sequence[i] = sequence[i - 1] * 2;
        }

        // Print the sequence
        for (int i = 0; i < 10; i++) {
            System.out.printf("N[%d] = %d%n", i, sequence[i]);
        }
    }
}

Filtering Floating-Point Values

This problem requires processing an array of 100 floating-point numbers. The objective is to identify and print only those values that are less than or equal to 10.0, along with their respective indices.

import java.util.Scanner;

public class ValueFilter {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double[] measurements = new double[100];

        // Read 100 double values
        for (int i = 0; i < 100; i++) {
            measurements[i] = input.nextDouble();
        }

        // Filter and display values <= 10
        for (int i = 0; i < 100; i++) {
            if (measurements[i] <= 10.0) {
                System.out.printf("A[%d] = %.1f%n", i, measurements[i]);
            }
        }
    }
}

Matrix Row Analysis

In this example, the program performs operations on a specific row of a 12x12 matrix. The user specifies the target row index and an operation type ('S' for sum or 'M' for average). The program reads the matrix data, calculates the sum of the specified row, and modifies the result to an average if requested.

import java.util.Scanner;

public class MatrixRowOperation {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int rowIndex = input.nextInt();
        char operation = input.next().charAt(0);

        double[][] grid = new double[12][12];

        // Populate the matrix
        for (int r = 0; r < 12; r++) {
            for (int c = 0; c < 12; c++) {
                grid[r][c] = input.nextDouble();
            }
        }

        // Calculate the sum of the specified row
        double total = 0;
        for (int c = 0; c < 12; c++) {
            total += grid[rowIndex][c];
        }

        // Calculate average if operation is 'M'
        if (operation == 'M') {
            total /= 12.0;
        }

        System.out.printf("%.1f%n", total);
    }
}

Tags: java Arrays Data Structures algorithms Programming Exercises

Posted on Tue, 15 Sep 2026 16:40:43 +0000 by fabiuz