-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2.java
100 lines (89 loc) · 2.6 KB
/
assignment2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Random;
import java.util.Scanner;
public class assignment2 {
public static int[] max_min(int arr[])
{
int min=99;
int max=-99;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>max)
max=arr[i];
if(arr[i]<min)
min=arr[i];
}
int res[] = new int[2];
res[0]=max;
res[1]=min;
return res;
}
public static int[] average(int arr[])
{
int res[]= new int[arr.length];
int sum = 0;
for ( int i = 0;i < arr.length; i++)
{
sum += arr[i];
}
int avg = sum / arr.length;
for ( int i = 0; i < arr.length; i++)
{
res[i] = arr[i] - avg;
}
return res;
}
public static int[] odd_even (int arr[]){
int res[]=new int[2];
int sum_even=0;
int sum_odd=0;
for (int i=0 ; i<arr.length ; i++){
if(i%2 ==0)
sum_even+=arr[i];
else
sum_odd+=arr[i];
}
res[0]=sum_even;
res[1]=sum_odd;
return res;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Random rn = new Random();
int size = 1;
System.out.println("Enter the size of the array");
size = scan.nextInt();
int [] arr = new int[size];
for(int i = 0; i<size; i++){
int rand = rn.nextInt(0,100);
arr[i] = rand;
}
System.out.println("Array is: ");
for(int i = 0; i<size; i++){
System.out.println(arr[i]);
}
int choice = 5;
while(choice != 0){
System.out.println("Enter Your Choice");
System.out.println("1. Find the max and min of the array");
System.out.println("2. Find the differences from the average");
System.out.println("3. Find the sum of odd and even integers");
System.out.println("0. Quit");
choice = scan.nextInt();
if(choice == 1){
System.out.println("The max of the array is "+max_min(arr)[0]);
System.out.println("The min of the array is "+ max_min(arr)[1]);
}
if(choice==2){
System.out.println("differences of average is:");
for(int i=0;i<arr.length;i++)
System.out.print(average(arr)[i]+" ");
}
if(choice == 3){
System.out.println("Sum of even indices are: "+ odd_even(arr)[0]);
System.out.println("Sum of odd indices are: "+ odd_even(arr)[1]);
}
if(choice == 0)
break;
}
}
}