cours_progra/bac1/q2/algo/tp3/ExoArray.java
Debucquoy 4fd7542f03
.
2023-09-20 15:18:20 +02:00

78 lines
1.7 KiB
Java

import java.lang.Math;
public class ExoArray {
public static void main(String[] args) {
int[] powers = new int[20], odds = new int[20], alterns = new int[20];
for (int i = 0; i < 20; i++) {
powers[i] = (int) Math.pow(2, i+1);
odds[i] = i * 2 + 1;
alterns[i] = (int)Math.pow(-1, i) * i;
}
display(powers);
System.out.println(ordered(powers));
display(odds);
System.out.println(ordered(odds));
display(alterns);
System.out.println(ordered(alterns));
System.out.printf("%d, %d, %d\n", sum(powers), sum(odds), sum(alterns));
System.out.printf("%.2f, %.2f, %.2f\n", average(powers), average(odds), average(alterns));
}
public static void display(int[] in){
String dis = "";
for(int i: in){
dis = dis + i + " ";
}
System.out.println(dis);
}
public static int sum(int[] in){
int sum = 0;
for(int i: in){
sum += i;
}
return sum;
}
public static float average(int[] in){
return sum(in)/in.length;
}
public static int[] zipSum(int[] in1, int[] in2){
if (in1.length != in2.length) // Fail Check
return null;
int[] ret = new int[in1.length];
for (int i = 0; i < in1.length; i++) {
ret[i] = in1[i] + in2[i];
}
return ret;
}
public static float[] zipAverage(int[] in1, int[] in2){
if (in1.length != in2.length) // Fail Check
return null;
float[] ret = new float[in1.length];
for (int i = 0; i < in1.length; i++) {
ret[i] = in1[i] + in2[i] / 2;
}
return ret;
}
public static boolean ordered(int[] in){
for (int i = 1; i < in.length; i++) {
if(in[i] < in[i-1])
return false;
}
return true;
}
public static int max(int[] in){
int max = 0;
for (int i : in) {
max = max < i ? i : max;
}
return max;
}
}