flipped 2D array not filling properly - arrays

My 2D array will not fill properly I want the original array to stay unmodified, which it does, but then the next array to be flipped. For example 111, 222, 333 should be 333, 222, 111. If anyone could help it would be appreciated.
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int rows = 0, cols = 0;
System.out.println("Please enter the size or your rows first and then columns:");
Scanner input = new Scanner (System.in);
rows = input.nextInt();
cols = input.nextInt();
int [][] my2DArray = new int [rows][cols];
System.out.print("Now fill the array with the numbers you would like: ");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
my2DArray[i][j] = input.nextInt();
}
System.out.println("");
}
System.out.println("Here is the origional unmodified array.");
for(int i = 0; i < my2DArray.length; i++)
{
for(int j = 0; j < my2DArray[i].length; j++)
{
System.out.print(my2DArray[i][j] + " ");
}
System.out.println("");
}
flipMy2DArray(my2DArray);
}
public static int[][] flipMy2DArray(int[][] inMy2DArray)
{
int [][] flipMy2DArray = new int [inMy2DArray.length][inMy2DArray.length];
for(int row = flipMy2DArray.length-1; row > 0; row--)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
flipMy2DArray[row][cols] = inMy2DArray[row][cols];
}
System.out.println("");
}
for(int row = 0; row < flipMy2DArray.length; row ++)
{
for(int cols = 0; cols < flipMy2DArray[row].length; cols++)
{
System.out.print(flipMy2DArray[row][cols]+ " ");
}
System.out.println("");
}
return flipMy2DArray;
}
}

Related

Need help on solving this Arrays problem using java

Make an array of integer (score) with 10 members. Randomize the
content with value between 0-100. For each of the member of array,
visualize the value using “-” for each ten. For example: score[0] = 55 will be visualized as “-----" (Using Java).
public class w9lab1 {
public static void main(String args[]) {
double[] temperature = new double[7];
for (int i = 0; i < 7; i++) {
temperature[i] = Math.random()*100;
}
for (int i = 0; i < 7; i++) {
System.out.println(temperature[i]);
}
double totalTemperature = 0;
for (int i = 0; i < 7 ; i++) {
totalTemperature += temperature[i];
}
double maxTemperature = temperature[0];
for (int i = 1; i < 7; i++){
if (temperature[i] > maxTemperature){
maxTemperature = temperature[i];
}
}
System.out.println("Temperatur maximum adalah " + maxTemperature);
}
}
import java.util.Random;
import java.util.Arrays;
public class w9lab1 {
public static void main(String[] args) {
Random random = new Random();
int[] score = new int[10];
for (int i = 0; i < score.length; i++) {
score[i] = random.nextInt(101);
for (int n = 1; n <= score[i] / 10; n++)
System.out.print('-');
System.out.println();
}
System.out.println(Arrays.toString(score));
}
}

Reverse an Array without using reverse Function and other array the same array to be reverse all elements in the same array. size fix

// I am tring but cant do it
int[] arr = {1,3,4,2,5,6};
int n = arr.length,x = 0;
for (int i = 0 ; i < n; i++ ) {
x++;
System.out.println(arr[i]);
}
//try this:
public class reverseArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = new int[] { 1,3,4,2,5,6 };
System.out.print("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
// method #1: by using extra array
System.out.println();
System.out.print("Reversed array method 1: ");
int j = 0;
int revArr[] = new int[] { arr.length };
for (int i = arr.length - 1; i >= 0; i--) {
revArr[j] = arr[i];
System.out.print(revArr[j] + " ");
}
// method #2: without using extra array
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
System.out.println();
System.out.print("Reversed array method 2: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

number of subarray with average greater than average of the rest of the elements of array

We are given a array of size < 2000
and A[i]< 10^6.I know the bruteforce approach.Can we do better i.e in linear time ?
I am checking each subarray and comparing its average with the other elements.
public class FindingSubArray {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> b = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
double avg1 = getAverage(i,j,arr);
double avg2 = getAverageOfRest(i,j,arr);
//System.out.println(avg1+" "+avg2);
if(avg1 > avg2) {
a.add(i+1);
b.add(j+1);
}
}
}
System.out.println(a.size());
for(int i=0;i<a.size();i++){
System.out.println(a.get(i)+" "+b.get(i));
}
}
private static double getAverageOfRest(int i, int j, int[] arr) {
double result = 0;
int count = 0;
for(int k=0;k<i;k++) {
result += arr[k] ;
count ++;
}
for(int k=j+1;k<arr.length;k++) {
result += arr[k] ;
count ++;
}
if(count > 0)
return result/count;
else
return 0;
}
private static double getAverage(int i, int j, int[] arr) {
double result = 0;
int count = 0;
for (int k = i; k <= j; k++) {
result += arr[k] ;
count ++;
}
if(count > 0)
return result/count;
else
return 0;
}
}

print two dimensional string column wise

I have two dimensional String array. Each cell can have different string size. i want to print it column wise. I have tried :
public void printArr(String[][] arr) {
for(int i=0;i<arr.length;i++) {
for(int j=1;j<arr[i].length;j++)
{
System.out.println(arr[i][j]);
}
System.out.println("\n");
}
}
But it is printing the array row-wise. Any help will be appreciated.
Array Structure : It has fixed number of rows but each row can have different column length.
from what i understand:
int i = 0;
int j = 0;
int max = 0;
for (int k = 0; k < arr.length; k++)
{
if (max < arr[k].length)
{
max = arr[k].length;
}
}
for (i = 0; i < max; i++)
{
for (j = 0; j < arr.length; j++)
{
if (i >= arr[j].length)
{
System.out.print(" ");
}
else
{
System.out.print(arr[j][i] + " ");
}
}
System.out.println();
}
I hope its helpful.
public void printArr(String[][] arr) {
int row = arr.length;
int column = arr[0].length;
for(int i=0;i<column;i++) {
for(int j=1;j<row;j++)
{
System.out.println(arr[j][i]);
}
System.out.println("\n");
}
}

Find the largest number and its index in the random array

This is my current code for generating a set of 20 random numbers:
int[] array_1 = new int[20];
Random rand = new Random();
for (int i = 0; i < array_1.Length; i++)
{
array_1[i] = rand.Next(10,100);
Console.Write("{0} ", array_1[i]);
}
int highest = 0;
int highestIndex = 0;
for (int i = 0; i < array_1.Length; i++)
{
if (array_1[i] > highest)
{
highest = array_1[i];
highestIndex = i;
}
}

Resources