Practical Project 2 - 420-D01-SU: Java Program for Max and Min

Verified

Added on  2022/08/15

|3
|425
|25
Practical Assignment
AI Summary
This assignment presents a Java program designed to identify the maximum and minimum values within an array of integers. The program, written in Java, takes an array of numbers as input, iterates through the array, and determines the largest and smallest elements. The solution includes the complete source code of the Java program, along with a detailed trace table to illustrate the execution flow and variable states at each step. This practical project is part of a larger assignment (Practical Project 2) for a computer science course (420-D01-SU), which requires students to analyze, write pseudocode, and create trace tables for various algorithmic problems. This particular solution addresses exercise 40 from the project brief, focusing on array manipulation and finding extreme values.
Document Page
Question 40: Program for determining largest and smallest number
import java.util.Scanner;
public class Number {
public static void main (String args[])
{
int i, n, max, min;
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the number of elements of array :");
n= sc.nextInt ();
int arr []=new int[n];
System.out.printf ("Enter elements of array");
for (i=0; i<n; i++)
{
arr [i]=sc.nextInt();
}
max= arr[0];
for(i=0;i<n;i++)
{
if(max<arr[i])
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
{
max=arr[i];
}
}
min=arr[0];
for(i=0;i<n;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
System.out.println("Maximum value of array : " + max);
System.out.println("Minimum value of array : " + min);
}
}
Trace table
Step Statement Notes n arr[] max min
1 n=4; Initialise the value
of n=4.
4 ? ? ?
Document Page
2 arr[]={35,15,3,75}; Put the value of
arr[]={35,15,3,75};
4 {35,15,3,75
}
? ?
3 max=arr[0]; Initialise the value
of max=arr[0].
4 {35,15,3,75
}
35 ?
4 for(i=0;i<n;i++) Start the for loop
until i<n.
4 {35,15,3,75
}
35 ?
5 if(max<arr[i]){max=arr[i];} Put the value of
max=75.
4 {35,15,3,75
}
75 ?
6 min=arr[0]; Initialise the value
of min=arr[0].
4 {35,15,3,75
}
75 35
7 for(i=0;i<n;i++) Start the for loop
until i<n.
4 {35,15,3,75
}
75 35
8 if(min<arr[i]){min=arr[i];} Put the value of
min=3.
4 {35,15,3,75
}
75 3
9 System.out.println(max) Print the value of
max=75.
10 System.out.println(min) Print the value of
min=3.
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]