0% found this document useful (0 votes)
8 views3 pages

Arrays

Uploaded by

petrenp86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Arrays

Uploaded by

petrenp86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package actionswitharrays;

import java.util.Arrays;

public class actions1 {

public static void main(String[] args) {


System.out.println();
System.out.println("First task(summa and number of elements)");
System.out.println();
int index;
int sum = 0;
int num = 0;
int[] array = {100, 21, 40, 52, 76, 80, -1, -2, -4, -53, -7, -8};
System.out.println("These are elements of massiv: ");
for(index = 0; index <= array.length - 1; index++) {
System.out.print(array[index] + "; ");
}
System.out.println();
System.out.println("This is summa of those elements: ");
for(index = 0; index <= array.length - 1; index++) {
if (array[index]%2==0) {
sum = sum + array[index];
}
}
System.out.print(sum + ";");
System.out.println();
System.out.println("This is number of those elements: ");
for(index = 0; index <= array.length - 1; index++) {
if (array[index]%2==0) {
num = num + 1;
}
}
System.out.print(num + ";");
System.out.println();
System.out.println();
System.out.println("Second task(max among negative and min among
positive elements)");
System.out.println();
System.out.println("This is maximal among negative elements: ");
int max = (int)Double.NEGATIVE_INFINITY;
int min = (int)Double.POSITIVE_INFINITY;
for (index = 0; index < array.length; index++) {
if (max < array[index] & array[index] < 0) {
max = array[index];
}
}
System.out.println(max);
System.out.println("This is minimal among positive elements: ");
for (index = 0; index < array.length; index++) {
if (array[index] > 0 && array[index] < min) {
min = array[index];
}
} System.out.println(min);
System.out.println();
System.out.println("Third task( Increasing positive elements and
decreasing negative elements)");
System.out.println();
System.out.println("Increasing positive elements");
int[] array2 = new int[array.length];
int indexpos = 0;
int indexneg = 0;

for (index = 0; index < array.length; index++) {


if (array[index] > 0) {
array2[indexpos] = array[index] + 1;
indexpos++;
}
}
for (index = 0; index < indexpos; index++) {
System.out.print(array2[index] + "; ");
}
System.out.println();
System.out.println("Decreasing negative elements");
for (index = 0; index < array.length; index++) {
if (array[index] < 0) {
array2[indexneg] = array[index] - 1;
indexneg++;
}
}
for (index = 0; index < indexneg; index++) {
System.out.print(array2[index] + "; ");
}
System.out.println();
System.out.println();
System.out.println("Fourth task(exchanging min and max)");
System.out.println();
int[] array3 = new int[array.length];
int max2 = array[0];
int min2 = array[0];
int minindex = 0;
int maxindex = 0;
for (index = 0; index < array.length; index++) {
if (max2 < array[index]) {
max2 = array[index];
}
if (array[index] < min2) {
min2 = array[index];
}
}
for (int i = 0; i < array.length; i++) {
if (array[i] == min2) {
minindex = i;
}
if (array[i] == max2) {
maxindex = i;
}
}
for (int i = 0; i < array.length; i++) {
if (i == minindex) {
array3[i] = max2;
} else if (i == maxindex) {
array3[i] = min2;
} else {
array3[i] = array[i];
}
}
System.out.println(Arrays.toString(array3));
System.out.println();
System.out.println("Fifth task(elimination of min and max)");
System.out.println();
int[] array4 = new int[array.length];
array[minindex] = 0;
array[maxindex] = 0;
int elim = 0;
for (int i = 0; i < array.length; i++) {
if(i != minindex & i != maxindex)
array4[elim] = array[i];
elim++;
}
int index2 = 0;
for (int i = 0; i < array.length; i++) {
if (i != minindex && i != maxindex) {
array4[index2] = array[i];
index2++;
}
}
int[] array5 = new int[index2];
for (int i = 0; i < index2; i++) {
array5[i] = array4[i];
}

System.out.println(Arrays.toString(array5));
System.out.println();
System.out.println("Sixth task(average and number elements bigger than
it)");
System.out.println();
System.out.println("This is summa of all elements of massiv: ");
int sum2 = 0;
for(index = 1; index <= array.length - 1; index++) {
sum2 = sum2 + array[index];
}
double average = (double)sum2/array.length;
System.out.println(average);
System.out.println("This is number elements bigger than average: ");
int num2 = 0;
for(index = 1; index <= array.length - 1; index++) {
if (average > array[index]) {
num2 = num2 + 1;
}
}
System.out.println(num2);

}
}

You might also like