Need to print rows of multidimensional array as columns - java

This is my program output:
Enter the size of 2D array:
4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
and I need this instead:
1 12 5 16
2 11 6 15
3 10 7 14
4 9 8 13
I want the 2d array to be of size NxN where n is the integer inputted by the user. I want the first consecutive values to be stored in the even indexed columns from top to bottom and the next consecutive values to be stored in the odd indexed columns from bottom to top.
import java.util.Scanner;
public class Lab6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of 2D array: ");
System.out.println();
int n = input.nextInt();
int arr[][] = new int[n][n];
int inc=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=inc;
inc++;
}
}
transpose(arr);
// now let's print a two dimensional array in Java
for (int[] a : arr) {
for (int i : a) {
System.out.print(i + "\t");
}
System.out.println("\n");
}
}
public static int[][] transpose (int[][] array) {
if (array == null || array.length == 0)//empty or unset array, nothing do to here
return array;
int width = array.length;
int height = array[0].length;
int[][] array_new = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
array_new[y][x] = array[x][y];
}
}
return array_new;
}
}

As Sean pointed out in the comments, your transpose() function returns a new array but you are not capturing that and using it. The original array remains unchanged, which is what you are displaying at the end.
Change:
transpose(arr);
// now let's print a two dimensional array in Java
for (int[] a : arr) {
To:
int[][] newArr = transpose(arr);
// now let's print a two dimensional array in Java
for (int[] a : newArr) {

Try this out. It should do what you have described in the question.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of 2D array: ");
System.out.println();
int n = input.nextInt();
int arr[][] = new int[n][n];
int inc=1;
for(int j = 0; j < n; j += 2) {
for(int i = 0; i < n; i++) {
arr[i][j]=inc++;
}
}
for(int j = 1; j < n; j += 2) {
for(int i = n - 1; i >= 0; i--) {
arr[i][j]=inc++;
}
}
// now let's print a two dimensional array in Java
for (int[] a : arr) {
for (int i : a) {
System.out.print(i + "\t");
}
System.out.println();
}
}

Related

Multidimensional arrays from standard input

I'm trying to figure out how to read in a multidimensional array from standard input given the user provides the row and col size followed by integers of the array
e.g. Input:
2 3 <= row, col of array
8 3 10 < array integers
7 9 6
My code is currently:
int colA = scan.nextInt();
int rowA = scan.nextInt();
int[][] array = new int[rowA][colA];
for (int i = 0; i <rowA;i++){
for (int j=0; j<colA;j++){
array1[i][j] += scan.nextInt();
}
}
And the output of my array is: [[8,3,10,7,9,6]] but what I'd like to do is output [[8,3,10],[7,9,6]]
Here first the row and col value got from users is reversed that is the only mistake i could see
import java.util.Arrays;
import java.util.Scanner;
public class TwoDimensionalArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int rowA = scan.nextInt();
int colA = scan.nextInt();
int[][] array = new int[rowA][colA];
for (int i = 0; i < rowA; i++) {
for (int j = 0; j < colA; j++) {
array[i][j] += scan.nextInt();
}
}
for (int[] innerArray : array) {
System.out.println(Arrays.toString(innerArray));
}
}
}
This is a working one
There are some errors in your code.
You inverted the order of the dimensions asked to the users.
Use this:
int rowA = scan.nextInt();
int colA = scan.nextInt();
Instead of this:
int colA = scan.nextInt();
int rowA = scan.nextInt();
You wrote array1 instead of array in array1[i][j] += scan.nextInt();
Note that you can use array[i][j] = scan.nextInt(); instead of array[i][j] += scan.nextInt()

changing value of the last 3 index in an array

i am currently learning java, bellow are my current code
import java.util.*;
public class numbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How big is the array: ");
int size = input.nextInt();
int[] numb = new int[size];
for (int i=0;i<size;i++)
{
numb[i] = i;
}
//change value of last 3 index of array to 0
for (int i =0; i<size;i++)
{
System.out.println(numb[i]);
}
}
}
Here are how the output if i ran it currently
How big is the array: 7
0
1
2
3
4
5
6
how could i change the value of the last 3 index to 0? bellow are my expected output
How big is the array: 7
0
1
2
3
0
0
0
thanks in advance!
You can loop through the last 3 index and set it to 0.
for (int i = 1; i <= 3; i++) {
num[num.length - i] = 0;
}
You mean?
//change value of last 3 index of array to 0
for (int i = size-1; i >= size-3;i--)
{
numb[i] = 0;
}
//OR
for (int i = size-2; i<size;i++)
{
numb[i] = 0;
}
for (int i = 0; i<size;i++)
{
System.out.println(numb[i]);
}
This should do the trick:
//change value of last 3 index of array to 0
if (size <= 3) {
for (int i =0; i<size;i++)
{
numb[i] = 0;
}
} else {
for (int i=size-3; i<size; i++) {
numb[i] = 0;
}
}
Just subtract 3 from the size, it will work perfectly
for (int i=0;i<size-3;i++)
{
numb[i] = i;
}

Finding the max of a column of a 2d array and save it into a 1d array

okay so I have a 10x10 2d Array and I found out how to calculate the minimum and maximum value of the column. I want to save the minimum values into on 1d array and the maximum values into a separate 1d array. I found out how to do this, but my 1d arrays just print out a bunch of 0s that equal the value so like 16 0s instead of just the number.
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
public class F {
public static void main (String args []) throws Exception
{
File a = new File("C:\\users\\James\\desktop\\A10Array.txt");
Scanner scan = new Scanner(a);
int[][] arr = new int [10][10];
for (int i=0;i<10;i++)
for (int j=0;j<10;j++)
arr[i][j] = scan.nextInt();
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
System.out.print(arr[i][j] +"\t");
}
System.out.println();
} System.out.println();
for (int i = 0; i < 10; i++) {
int minInCol = arr[0][i];
int maxInCol = arr[0][i];
double sum = 0;
for (int j = 0; j < 10; j++) {
sum+=arr[j][i];
if (minInCol > arr[j][i]) {
minInCol = arr[j][i];
}
if (maxInCol < arr[j][i]) {
maxInCol = arr[j][i];
}
}
int[]min = new int [minInCol];
int[]max = new int [maxInCol];
System.out.print(Arrays.toString(max));
}
scan.close();
}
}
int[]min = new int [minInCol];
int[]max = new int [maxInCol];
System.out.print(Arrays.toString(max));
You just create the arrays with the length is the min and max value, but you don't init the array. So by default the out put for empty array is 0

Count how many integers were displayed in an array

I got an 100 random elements array, each element is in range of 0-10, and i need to count each integer how many times it was typed (e.g. 1,2,2,3,8,8,4...)
OUTPUT:
1 - 1
2 - 2
3 - 1
8 - 2
4 - 1
My code so far is:
import java.util.Random;
public class Asses1 {
public static void main(String[] args) {
getNumbers();
}
private static int randInt() {
int max = 10;
int min = 0;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public static int[] getNumbers() {
int number = 100;
int[] array = new int[number];
for (int i = 0; i < array.length; i++) {
System.out.println(randInt());
}
System.out.println(number+" random numbers were displayed");
return array;
}
}
Add this method, which will do the counting:
public static void count(int[] x) {
int[] c=new int[11];
for(int i=0; i<x.length; i++)
c[x[i]]++;
for(int i=0; i<c.length; i++)
System.out.println(i+" - "+c[i]);
}
and change the main into this so that you call the previous method:
public static void main(String[] args) {
count(getNumbers());
}
Also, change the for loop in getNumbers into this in order to fill array with the generated numbers, not just printing them:
for (int i = 0; i < array.length; i++) {
array[i] = randInt();
System.out.println(array[i]);
}
Here is how it can be done in java 8
// Retrieve the random generated numbers
int[] numbers = getNumbers();
// Create an array of counters of size 11 as your values go from 0 to 10
// which means 11 different possible values.
int[] counters = new int[11];
// Iterate over the generated numbers and for each number increment
// the counter that matches with the number
Arrays.stream(numbers).forEach(value -> counters[value]++);
// Print the content of my array of counters
System.out.println(Arrays.toString(counters));
Output:
[12, 11, 7, 6, 9, 12, 8, 8, 10, 9, 8]
NB: Your method getNumbers is not correct you should fix it as next:
public static int[] getNumbers() {
int number = 100;
int[] array = new int[number];
for (int i = 0; i < array.length; i++) {
array[i] = randInt();
}
System.out.println(number+" random numbers were displayed");
return array;
}
int[] array2 = new int[11];
for (int i = 0; i < array.length; i++){
array2[randInt()]++
}
for (int i = 0; i < array.length; i++)
System.out.println(String.valueOf(i) + " - " + String.valueOf(array2[i]));
What I have done is:
Create an helping array array2 for storing number of occurences of each number.
When generating numbers increment number of occurences in helping array.
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
int temp;
for (int i = 0; i < array.length; i++) {
temp=randInt();
if(map.containsKey(temp)){
map.put(temp, map.get(temp)+1);
}else{
map.put(temp, 1);
}
}

Two Dimensional Matrix as Input

how to write two dimensional matrix as input and identifies the number with maximum number of occurrences in the matrix.
Example Input :
2 // no of rows
3 // no of columns
1 2 3 2 3 3 // here the matrix taken as input is 2 x 3 matrix. Remaining six numbers are values for the particular matrix. (elements in the first row are 1 2 3 and elements in the second row are 2 3 3)
Example output : 3
import java.util.*;
class ArrayOccurence
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
sc.nextLine();
int column = sc.nextInt();
sc.nextLine();
int element = 0;
int occurence = 0;
int arr[][] = new int[row][column]; // size of the array
for(int i=0; i < row; i++)
{
for(int j=0; j < column ; j++)
arr[i][j] = sc.nextInt();
}
//Do not modify the code above
/* Enter your code here */
// Do not modify code below
System.out.println("Matrix element "+element+" occurs "+occurence+" times in the matrix");
}
}
Substitute your line which says Enter your code here with the following:
ArrayList<Integer> a = new ArrayList<Integer>();
int oc = 0;
int number = 0;
for(int i=0; i < row; i++)
{
for(int j=0; j < column ; j++){
if(a.isEmpty()){
a.add(arr[i][j]);
}
else if(a.contains(arr[i][j])){
int temp=0;
for(int k=0; k<a.size(); k++){
if(a.get(k) == arr[i][j]){
temp++;
}
}
if(temp > oc){
number = arr[i][j];
oc = temp;
}
a.add(arr[i][j]);
}
else{
a.add(arr[i][j]);
}
}
}
Hope this helps.
You could use the double for loop again to count the occurence of each numbers:
for(int i=0; i < row; i++)
{
for(int j=0; j < column ; j++)
//count occurences with a HashMap<Integer,Integer> or something like that
}
Loop into your HashMap to find the one with most occurence..

Categories

Resources