Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
package allSortings;
import java.util.Scanner;
public class MergeSort {
public static int A[],B[];
void mergeArray(int A[],int first,int mid,int last){
int i=first,j;
j=mid;
while(i<=mid && j<=last)
{
if(A[i]<A[j])
B[first++]=A[i++];
else B[first++]=A[j++];
}
while(i<=mid)
B[first++]=A[i++];
while(j<=last)
B[first++]=A[j++];
}
void copyArray(int A[],int last,int B[])
{
int i=0;
while(i<last)
{
A[i]=B[i];i++;
}
}
void splitArray(int A[],int first,int last)
{
if(first<last)
{
int mid=first+last/2;
System.out.println("first:"+first);
splitArray(A,first,mid);
splitArray(A,mid+1,last);
//mergeArray(A,first,mid,last);
//copyArray(A,last,B);
}
}
public static void main(String args[])
{
int n;
A=new int[100];
B=new int[100];
System.out.println("Enter the no. of elements in the Array:"+"\n");
Scanner input;
input=new Scanner(System.in);
n=input.nextInt();
MergeSort m1=new MergeSort();
for(int i=0;i<n;i++)
A[i]=input.nextInt();
System.out.println("\nThe Original array is:");
for(int i=0;i<n;i++)
System.out.format("%d"+" ",A[i]);
m1.splitArray(A,0,n-1);
System.out.println("\nThe Sorted array is:");
for(int i=0;i<n;i++)
System.out.format("%d"+" ",A[i]);
}
}
I keep getting at allSortings.MergeSort.splitArray(MergeSort.java:34).Guys any clue (I am new to Java, so I dont know to use a debugger)?
The value of "first" variable always gets 2 and then does not change.
You're only dividing one summand by 2, but you should divide the sum by 2 instead. Replace
int mid=first+last/2;
with
int mid=(first+last)/2;
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
while(n>0) {
int count=0;
n= n/10;
count++;
}
System.out.println(count);
In your code, you have declared the variable count in the while loop block and so it is a local variable and the scope of the variable is limited till while loop block.
If you want the program to execute successfully, you should declare the variable inside the main method block but outside the while loop block.
Like this
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n > 0){
n = n/10;
count++;
}
System.out.println("Count = "+count);
}
Scope of a variable is the part of the program where the variable is accessible.
Refer this article for more details Scope in Java
Also to correct the code,
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n>0) {
count=0;
n= n/10;
count++;
}
System.out.println(count);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}
You could use print instead of println:
System.out.print(i);
Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}
I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}
In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....
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 4 years ago.
Improve this question
It keeps throwing the exception array index out of bounds for the multiply method, but dimensions are correct. The error appears at this line:
mm[r][c]+=multiplier[i][j]*multiplicand[j][i];
and at this line:
k.multiply_matrix(temp1,temp2,i1,h,i2);
I want to know why the exception is thrown and possible modification for my code to run correctly.
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class matrixmultiplication
{
public static void main(String args[])
{
matrixmultiplication k=new matrixmultiplication();
System.out.println("enter how many rows and how many columns");
Scanner c=new Scanner(System.in);
String f=c.next();
String[] x=f.split(",");
int i1=Integer.parseInt(x[0]);
int i2=Integer.parseInt(x[1]);
int[][] temp1=k.enter_thematrix(i1,i2);
System.out.println("how many columns in the second matrix");
int h=c.nextInt();
int[][] temp2=k.enter_thematrix(i2,h);
k.multiply_matrix(temp1,temp2,i1,h,i2);
}
public int[][] enter_thematrix(int d1,int d2)
{
Scanner c1=new Scanner(System.in);
int the_matrix[][]=new int[d1][d2];
for (int col=0;col<d2;col++)
{
for(int row=0;row<d1;row++)
{
the_matrix[row][col]=c1.nextInt();
}
}
return the_matrix;
}
public void print_matrix(int [][]mm,int a,int s)
{
for (int col=0;col<s;col++)
{
for(int row=0;row<a;row++)
{
System.out.print(mm[a][s]+"\t");
}
System.out.println();
}
}
public void multiply_matrix(int [][]multiplier,int [][]multiplicand,int r,int c,int innerd)
{
int [][]mm=new int[r][c];
for(int c1=0;c1<c;c1++)
{
for(int r1=0;r1<r;r1++)
{
for(int i=0;i<innerd;i++)
{
for(int j=0;j<innerd;j++)
{
mm[r][c]+=multiplier[i][j]*multiplicand[j][i];
}
}
}
}
print_matrix(mm,r,c);
}
}
The problem is that you pass i1 and i2 to the method, multiply_matrix. However i1 and i2 are the amount of rows and columns. Arrays start at zero. So if you have a 2d array with two rows and two columns it will have a max index of 1. (0 and 1 are the two positions in the array) So when you call the line:
mm[r][c]+=multiplier[i][j]*multiplicand[j][i];
You will always have an indexoutofboundsexception because (in this example of a 2 by 2 array) you will be calling the index (2,2) which does not exist. You need to pass that arguments i1-1 and i2-1 to the method.
Also a side note I would not have the user enter the rows and columns on the same line and then split it. This is error prone. If a user enters a number, comma, space, and then another number, this will cause an error. Or if the user doesn't type a comma this will also cause an error. It would be better to call nextInt on two separate lines. Like this:
System.out.println("enter how many rows: ");
Scanner c=new Scanner(System.in);
int i1 = c.nextInt();
System.out.println("Enter how many columns: ");
int i2 = c.nextInt();
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
package com.mohamadibrah;
import java.util.Scanner;
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println(returnAverage(printNumbers(enterNumbers()))); // 10
}
public static double returnAverage(double[] array){
double result=0;
for(int i=0;i<array.length;i++) {
result += array[i];
}
System.out.println("the average is:");
return (double)result/(array.length);
}
public static double[] enterNumbers(){
System.out.println("please enter the number of values:");
int sca = sc.nextInt();
double[] myAverage = new double[sca];
System.out.println("please enter the numbers:");
for(int i=0;i<myAverage.length;i++){
double input = sc.nextDouble();
myAverage[i] = input;
}
return myAverage;
}
public static double[] printNumbers(double[] array1){
System.out.println("your numbers are:");
double[] array2 = new double[array1.length];
for(int i=0;i<array2.length;i++) {
System.out.println("the number "+i+" is:"+array1[i]);
array2[i] = array1[i];
}
return array2;
}
}
the one with the "10" in the comment
is it good programming to stack multiple methods together if everything is working fine? or is it better to keep them separated as much as possible
The issues here are debuggability and readability.
Debuggability:
When you debug such a code, it can get pretty tricky to inspect each inner method return value.
Readability:
Asside from it being hard to follow, since the reader needs to read the code from the inside out, you miss the chance to give a meaningful name to a variable that holds the inner method return value, thus explaining to the reader what it is.
Consider the following alternate code with the above points in mind:
List<Double> userChosenNumbers = enterNumbers();
Double average = returnAverage(userChosenNumbers);
System.out.println(average);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
import java.util.*;
public class Fact
program to find factorial numbers
{
Scanner sc=new Scanner(System.in);
int n;
Fact()
an empty constructor
{
}
void accept()
{
System.out.println("Enter the number");
n=sc.nextInt();
System.out.println(pact(n));
}
**int pact(int n)**
here is where my program says that it is missing a semicolon
(
if(n==1)
return 1;
else
return n*fact(n-1);
}
public static void main()
{
Fact obj=new Fact();
obj.accept();
}
}
Apart from the fact that
int pact(int n)
(
should be
int pact(int n)
{
You have String args[] missing as the arguments in the main method.
public static void main(String[] args){
After return statement you used closed curly, and no open curly is there .
You started with open parenthesis and closed with curly.
Just change that.
Try this. Removed invalid language constructs.
import java.util.Scanner;
public class Fact {
Scanner sc = new Scanner(System.in);
int n;
Fact() {
}
void accept() {
System.out.println("Enter the number");
n = sc.nextInt();
System.out.println(fact(n));
}
int fact(int n) {
if (n == 1)
return 1;
else
return n * fact(n - 1);
}
public static void main(String[] args) {
Fact obj = new Fact();
obj.accept();
}
}
If you copied the code correctly, you need to use a { instead of ( in this part of the code.
(
if(n==1)
return 1;
else
return n*fact(n-1);
}
so it should be
{
if(n==1)
return 1;
else
return n*fact(n-1);
}