print two dimensional string column wise - arrays

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");
}
}

Related

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] + " ");
}
}
}

Printing patterns using arrays in c

The problem is to print this pattern:
5555555555
5444444445
5433333345
5432222345
5432112345
5432112345
5432222345
5433333345
5444444445
5555555555
This is my code:
#include<stdio.h>
int main()
{
int i,k,j,n,p;
printf("enter the no : ");
scanf("%d",&n);
p = n;
k = 0;
int a[2*n][2*n];
while (p>=1)
{
for(i=0+k;i<2*n-k;i++)
{
for(j=0+k;j<2*n-k;j++)
{
if(i == 2*n-k||i == k||j == k||j == 2*n-k)
{
a[i][j]=p;
}
else
{
a[i][j]= 8;
}
}
printf("\n");
}
k++,p--;
}
for(i=0;i<2*n;i++)
{
for(j=0;j<2*n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
return 0;
}
The result I get is:
5555555555
5444444448
5433333388
5432222888
5432118888
5432188888
5432888888
5438888888
5488888888
5888888888
Your code is overly complex. You should utilize the symmetry. Replace you while-loop with this code.
// Because of symmetry, both i and j can loop to n instead of 2*n
for(i=0; i<n; i++) {
p = n;
for(j=0; j<n; j++) {
// Assign four cells at once due to both horizontal and vertical symmetry
a[i][j] = a[i][2*n-j-1] = a[2*n-i-1][j] = a[2*n-i-1][2*n-j-1] = p;
if(j<i)
p--;
}
}

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;
}
}

Array index out of bounds in sieve of eratosthenes

I am having trouble with ArrayIndexOutOfBounds.
I am new to coding and I don't fully understand this problem and I am unable to fix it. Any help is appreciated.
public class Primes {
public static void main(String[] args) {
final int SIZE = 10000;
boolean[] numberIsPrime = new boolean[SIZE];
int rowCounter = 0;
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
for( int index = 2; index <= SIZE; index++) {
if( numberIsPrime[index] = true){
for( int i = index; i <= SIZE; i++){
numberIsPrime[index * i] = false;
}
}
}
for( int index = 1; index <= SIZE; index++){
if( numberIsPrime[index] = true){
System.out.println(index + " ");
rowCounter++;
if( rowCounter == 10){
System.out.println();
}
}
}
}
}
You should tag the question with the language you're using, but I'm gonna assume it's Java. The problem is
boolean[] numberIsPrime = new boolean[SIZE];
...
for( int index = 1; index <= SIZE; index++) {
numberIsPrime[index] = true;
}
The first line declares numberIsPrime as an array of size 10000. That means you can access numberIsPrime[0], numberIsPrime[1], ... numberIsPrime[9999]. You can't access numberIsPrime[10000].

flipped 2D array not filling properly

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;
}
}

Resources