Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My problem is to add the sum of each row in a 2d array, and put those values in a new 1d array.
This is my code
public static int[] sumRow(int[][] N){
int[] rowSum = new int[N.length];
for(int i = 0; i<N.length;i++){
for(int j = 0; j<N[i].length; j++){
rowSum[i] = N[i][j] + N[i+1][j+1];
}
}
return rowSum;
}
But it is not working, please help.
public static int[] sumRow(int[][] N){
int[] rowSum = new int[N.length];
for(int i = 0; i<N.length;i++){
rowSum[i] = 0; //<= initialize value
for(int j = 0; j<N[i].length; j++){
rowSum[i] += N[i][j]; //<= sum of row
}
}
return rowSum;
}
You have written most of the code right but you need to add each row so, you need to add N[0][1], ....N[0][N[0].length - 1] in row 0. Now just plug i and j values and write on paper to be much clear.
Try this.
public static int[] sumRow(int[][] N) {
return Stream.of(N)
.mapToInt(a -> IntStream.of(a).sum())
.toArray();
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
For the question below use 2d array greetings seen below:
![image of 2D array][1]
"bob"
"joe"
"billy"
"george"
"janise"
"dell"
The code that I wrote is:
public class c
{
// instance variables - replace the example
below with your own
private int columns; int rows;
/**
* Constructor for objects of class c
*/
public c()
{
int columns = 2;
int rows = 3;
String[][] newArray = new String[columns][rows];
newArray[0][0] = "bob";
newArray[0][1] = "joe";
newArray[1][0] = "billy";
newArray[1][1] = "george";
newArray[1][2] = "janise";
newArray[2][2] = "dell";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
}
}
When creating the array, you need to use new String[rows][columns], not new String[columns][rows].
The third row should then be assigned using newArray[2][0] and newArray[2][1].
You can use an array initializer to create the array in a much easier way:
String[][] newArray = { { "hello", "ni hao" },
{ "konnichiwa", "hola" },
{ "guten tag", "bonjour" } };
So... your code is close to being correct.
The first mistake is that you mixed the columns and rows: You are defining your array to have two columns, but in the end, you are using three (0, 1, 2).
The second mistake is in the last two assignments: You really did confuse the numbering here.
Your finished constructor should look like this:
int columns = 2;
int rows = 3;
String[][] newArray = new String[rows][columns];
newArray[0][0] = "hello";
newArray[0][1] = "ni hao";
newArray[1][0] = "konnichiwa";
newArray[1][1] = "hola";
newArray[2][0] = "guten tag";
newArray[2][1] = "bonjur";
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(newArray[i][j]);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Kindly help me with the underlying program as I am stuck. I'm a newbie programmer.
import java.util.*;
public class Source
{
static int maxProduct(int arr[]) {
int n = arr.length ;
if (n < 2)
{
System.out.println("NA");
return Integer.MIN_VALUE;
}
int a = arr[0];
int b = arr[1];
for(int i = 0; i<n; i++) {
for (int j = i+1; j<n; j++) {
if (arr[i]*arr[j] > arr[0]*arr[1]) {
a = arr[i];
b = arr[j];
}
}
}
return maxProduct;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int size = s.nextInt();
int[] arr = new int[size];
for(int i = 0; i < size; i++) {
arr[i] = s.nextInt();
}
int answer = maxProduct(arr);
System.out.print(answer);
}
}
You should change
if (arr[i]*arr[j] > arr[0]*arr[1])
to
if (arr[i]*arr[j] > a * b)
Since arr[0]*arr[1] is just the original max product, so you shouldn't be comparing against it.
Also note that your solution is not as efficient as it can be, since you are using a nested loop, which requires O(n^2) running time.
You can achieve linear (O(n)) running time if you use the fact that the max product is either the product of the two highest positive values or the product of the two lowest negative values. This means that if you find these 4 numbers, which can be done with a single loop, you'll find the max product.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Assume I have one dimensional array
int[] arr = new int[]{1,2,3,4,5,6)
and I need transfer all data from this arr in two-dimensional array
int[][]array = new int[2][1];
bellow code:
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[i];
}
}
return result:
[[1], [2]]
I need:
[[1,2,3], [4,5,6]]
How to achieve it?
Your 2d array is too small. To get the output you want, it needs to have a shape of 2 by 3.
int[][] array = new int[2][3];
You will also need to change the loop that builds the array. It has to keep track of the index for the original array separately from the indices of the 2d array.
int indexForArr = 0;
for (int i = 0; i <array.length ; i++) {
for (int j = 0; j <array[j].length ; j++) {
array[i][j] = arr[indexForArr];
indexForArr = indexForArr + 1;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
class arr{
//array sorting to find maximum value
public static void main(String[]args){
int[] array={1,6,4,5,2};
int n;
int i,j;
for( i=0;i<(array.length*2);i++){
for( j=0;j<array.length;j++){
if(array[j]>array[j+1]){
array[j]=array[j+1];
array[j+1]=array[j];
}
}
}
System.out.println(array[array.length]);
}
}
>
Can anyone tell me why i am getting runtime error and this sorting method will work or not?
Dont reinvent the wheel.., you are able to use arrays, so then use the array help class too...
:-)
public static void main(String[] args) {
final int[] array = { 1, 6, 4, 5, 2 };
System.out.println("Array before sort " + Arrays.toString(array));
Arrays.sort(array);
System.out.println("Array before sort " + Arrays.toString(array));
}
you will likely have an indexOutOfbounds exception because your second loop:
for( j=0;j<array.length;j++){
if(array[j]>array[j+1]){
array[j]=array[j+1];
array[j+1]=array[j];
}
}
J loops through teh array then you attempt to index the array at J + 1 which on the last element in the array would push it out of bounds thus throwing an outOfBoundsException
for( j=0;j<array.length;j++){
if(array[j]>array[j+1]){
array[j]=array[j+1];
array[j+1]=array[j];
}
}
Since array.length = 5 and j < array.length, the value array[j+1] in the last round of the inner loop cause array out of bound exception.
The swap needs another variable to hold array[j] before changing it.
e.g:
int x;
for( j=0;j<array.length - 1;j++){
if(array[j]>array[j+1]){
x = array[j];
array[j]=array[j+1];
array[j+1]=x;
}
}
The bubble sort algorithm can be implemented as follows
public static void sort(int[] arr){
int len = arr.length;
int k = 0;
for(int j = 0 ; j < len-1; j++){
for(int i= 0+k; i < len-1; i += 2){
if(arr[i] <= arr[i+1])
continue;
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
k++;
if ( k % 2 == 0)
k = 0;
} }
The inner loop must alternately start at indexes 0 and 1 so as not to always swap the same pairs (k variable).
The others have already pointed out why you're getting an exception.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
import java.util.*;
import javax.swing.*;
public class Practice {
public static void main(String[] args){
int[] numbers = {1, 2, 3, 14, 15, 16, 17};
getSmall(numbers);
}
public static void getSmall(int[] ar){
int small=0;
for(int i=0; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
}
The program is to find the smallest number in the array, there is no compiler error but it doesn't show the correct result.
Thank you in advance!
public static void getSmall(int[] ar){
int small=ar[0];
for(int i=1; i<ar.length; i++){
if(ar[i]<small)
small = ar[i];
}
System.out.println(small);
}
Setting small initially to 0 is a bad idea as you are hoping that your array contains an element less than that for the answer to be correct.
A common idiom is to initialise small to ar[0] (having, of course, first checked that ar contains at least 1 element). Then run your loop from 1.
for(int i = 1;
(I dislike initialising small to a very large number since that puts the answer to your function in an undefined state if ar does not have any elements.)
Change
int small = 0
to
int small = ar[0];
Don't init small=0,
change to int small = ar[0]
You can also use
int small=Integer.MAX_VALUE;
in place of
int small=0;
this code will print small value, give the max of integer
Change this
public static void getSmall(int[] ar) {
int small = 0;
for (int i = 0; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
to
public static void getSmall(int[] ar) {
int small = ar[0];
for (int i = 1; i < ar.length; i++) {
if (ar[i] < small)
small = ar[i];
}
System.out.println(small);
}
because variable small is not assigned to any value from your array