Overview
This page documents the core array operations in the Actividad-1 application, including user input collection, difference calculations, average computation, and formatted output.Array Input Operations
First Array Input (Lines 14-19)
The first loop collects 7 integer values from the user and stores them inarray1:
Main.java:14-19
Operation Details
Operation Details
Key Features:
- Prompts user with “Ingrese 7 valores para el primer arreglo”
- Uses zero-based indexing (
i) but displays one-based numbering (i + 1) for user clarity - Reads each integer using
sc.nextInt() - Accumulates sum: Each input value is immediately added to
sumaTotal(line 18) - Loop iterates based on
array1.lengthfor maintainability
Second Array Input (Lines 21-26)
This section contains a critical bug that affects the average calculation.
array2:
Main.java:21-26
Critical Bug on Line 25
Critical Bug on Line 25
Bug Description:Expected Behavior:Impact:
Main.java:25
- The sum accumulates
array1values twice instead of includingarray2values - This causes the average calculation to be incorrect
- The bug results in:
average = (sum(array1) * 2) / 14instead of(sum(array1) + sum(array2)) / 14
array1 = [1, 2, 3, 4, 5, 6, 7] and array2 = [10, 20, 30, 40, 50, 60, 70]:- Actual sum:
28 + 28 = 56(array1 counted twice) - Expected sum:
28 + 280 = 308 - Actual average:
56 / 14 = 4.0 - Expected average:
308 / 14 = 22.0
Difference Calculation (Lines 29-31)
Calculates element-wise differences between the two arrays:Main.java:29-31
How It Works
How It Works
Logic:
- Iterates through each index from 0 to 6
- Subtracts corresponding elements:
arrayDiferencia[i] = array1[i] - array2[i] - Stores results in the
arrayDiferenciaarray
| Index | array1[i] | array2[i] | arrayDiferencia[i] |
|---|---|---|---|
| 0 | 10 | 5 | 5 |
| 1 | 15 | 8 | 7 |
| 2 | 20 | 12 | 8 |
| 3 | 7 | 9 | -2 |
| 4 | 30 | 25 | 5 |
| 5 | 18 | 20 | -2 |
| 6 | 22 | 15 | 7 |
Average Calculation (Line 34)
Computes the average of all input values using explicit type casting:Main.java:34
Type Casting Explanation
Type Casting Explanation
Why Casting is Necessary:Java performs integer division by default when both operands are integers. Without casting:How It Works:
(double) sumaTotalconverts the integer sum to a double- Division then operates in floating-point arithmetic
- Result is stored in
promediovariable with full precision
Output Formatting
Average Output (Line 36)
Displays the average with exactly 2 decimal places:Main.java:36
printf Format Specification
printf Format Specification
Format String Breakdown:
%: Marks the start of a format specifier.2: Specifies 2 decimal places of precisionf: Indicates floating-point number format
| Value | Output |
|---|---|
| 4.071428 | ”El promedio es: 4.07” |
| 22.5 | ”El promedio es: 22.50” |
| 10.0 | ”El promedio es: 10.00” |
| 7.999 | ”El promedio es: 8.00” (rounded) |
Difference Array Output (Lines 38-41)
Displays all elements of the difference array:Main.java:38-41
Output Format Details
Output Format Details
Features:Note: The output format lacks spacing (e.g., “Posición0:5” instead of “Posición 0: 5”).
- Line 38: Prints header with newline escape sequence (
\n) - Lines 39-41: Iterates through all 7 elements
- Displays position (index) and corresponding difference value
Operation Summary
Input Operations
Two sequential loops collect 7 integers each from user input
Difference Calculation
Element-wise subtraction: array1[i] - array2[i]
Average Calculation
Sum divided by total elements (14) with double precision
Formatted Output
Printf for average (2 decimals), loop for difference array
Performance Characteristics
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| First array input | O(n) where n=7 | O(1) auxiliary |
| Second array input | O(n) where n=7 | O(1) auxiliary |
| Difference calculation | O(n) where n=7 | O(1) auxiliary |
| Average calculation | O(1) | O(1) |
| Output display | O(n) where n=7 | O(1) auxiliary |
| Total | O(n) | O(n) for arrays |
See Also
- Main Class - Complete class structure and variable declarations
- Input Format - Understanding input requirements (includes bug documentation)
- Output Format - Understanding program output