how can i find the position of maximum value in this code [on hold]
up vote
0
down vote
favorite
I wrote this code but i do not know how can i wrote the code gets the position of maximum value. Actually i am beginner and i do not know arrays and methods completely.
package findposition;
import java.util.Scanner;
public class findposition {
public static void main(String args) {
Scanner input=new Scanner(System.in);
int counter, max = 0, num = 0;
counter = 1;
while (counter<=10) {
System.out.println("Enter the " + counter + ". number:");
num=input.nextInt();
counter++;
}
max = Math.max(num, max);
System.out.printf("Maximum value is %d", max);
}
}
java arrays indexing position
New contributor
put on hold as too broad by Seelenvirtuose, Owen Pauling, greg-449, Umair, GhostCat yesterday
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 1 more comment
up vote
0
down vote
favorite
I wrote this code but i do not know how can i wrote the code gets the position of maximum value. Actually i am beginner and i do not know arrays and methods completely.
package findposition;
import java.util.Scanner;
public class findposition {
public static void main(String args) {
Scanner input=new Scanner(System.in);
int counter, max = 0, num = 0;
counter = 1;
while (counter<=10) {
System.out.println("Enter the " + counter + ". number:");
num=input.nextInt();
counter++;
}
max = Math.max(num, max);
System.out.printf("Maximum value is %d", max);
}
}
java arrays indexing position
New contributor
put on hold as too broad by Seelenvirtuose, Owen Pauling, greg-449, Umair, GhostCat yesterday
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
Java
!==Javascript
!
– Nina Scholz
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
1
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I wrote this code but i do not know how can i wrote the code gets the position of maximum value. Actually i am beginner and i do not know arrays and methods completely.
package findposition;
import java.util.Scanner;
public class findposition {
public static void main(String args) {
Scanner input=new Scanner(System.in);
int counter, max = 0, num = 0;
counter = 1;
while (counter<=10) {
System.out.println("Enter the " + counter + ". number:");
num=input.nextInt();
counter++;
}
max = Math.max(num, max);
System.out.printf("Maximum value is %d", max);
}
}
java arrays indexing position
New contributor
I wrote this code but i do not know how can i wrote the code gets the position of maximum value. Actually i am beginner and i do not know arrays and methods completely.
package findposition;
import java.util.Scanner;
public class findposition {
public static void main(String args) {
Scanner input=new Scanner(System.in);
int counter, max = 0, num = 0;
counter = 1;
while (counter<=10) {
System.out.println("Enter the " + counter + ". number:");
num=input.nextInt();
counter++;
}
max = Math.max(num, max);
System.out.printf("Maximum value is %d", max);
}
}
java arrays indexing position
java arrays indexing position
New contributor
New contributor
edited yesterday
Jérôme Teisseire
9721819
9721819
New contributor
asked yesterday
Sena
61
61
New contributor
New contributor
put on hold as too broad by Seelenvirtuose, Owen Pauling, greg-449, Umair, GhostCat yesterday
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as too broad by Seelenvirtuose, Owen Pauling, greg-449, Umair, GhostCat yesterday
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
2
Java
!==Javascript
!
– Nina Scholz
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
1
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday
|
show 1 more comment
2
Java
!==Javascript
!
– Nina Scholz
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
1
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday
2
2
Java
!== Javascript
!– Nina Scholz
yesterday
Java
!== Javascript
!– Nina Scholz
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
1
1
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
You would need something like this:
public int getIndexOfMaxValue( int array )
{
int maxIndex = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[maxIndex] ) maxIndex = i;
}
return maxIndex;
}
In order to use the code from above you could try the following:
public static void main(String args)
{
int a = new int { 12, 44, 23, 56, 23, 78, 13 };
int maxIndex = getIndexOfMaxValue(a);
System.out.println("Index position of Maximum value in an array is : " + maxIndex );
}
New contributor
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
add a comment |
up vote
0
down vote
Though its a simple code, You have to learn that from your logic. you can follow this
import java.util.Scanner;
public class App {
public static void main(String args) {
Scanner input=new Scanner(System.in);
//dont need to instantiate that if you declare void findMax(int arrayOfnum) static
App app = new App();
int counter;
counter=1;
System.out.println("Enter how many number you want to take :");
int total=input.nextInt();
int arrayOfnum = new int[total];
//taking input on a array
while (counter<=total) {
System.out.println("Enter the "+counter+". number:");
arrayOfnum[counter]=input.nextInt();
counter++;
}
//method call
app.findMax(arrayOfnum);
}
private void findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
}
}
Here arrayOfnum is a one dimensional array whereyou can keep numbers that you take as input and then using private void findMax(int arrayOfnum) method you can find maximum value and position. I didnot return nothing from this method but if you want to return something from this method like you want to return position then you have to declare return type like private int findMax(int arrayOfnum) . and then implementation will be like this.
private int findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
return (pos+1);
}
You can make this method like public, protected etc. which is called access modifier.
If you declare this method as static then you do not need to create App app = new App(); instance in main method. You can directly access static method from main method like this findMax(arrayOfnum); instead of app.findMax(arrayOfnum); . Because main method is static. you can not access non-static variables from a static context. JVM keep main method is static by default.
NB: Try to learn highlighted terms from internet.
thank you for your answer @flopcoder
– Sena
yesterday
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You would need something like this:
public int getIndexOfMaxValue( int array )
{
int maxIndex = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[maxIndex] ) maxIndex = i;
}
return maxIndex;
}
In order to use the code from above you could try the following:
public static void main(String args)
{
int a = new int { 12, 44, 23, 56, 23, 78, 13 };
int maxIndex = getIndexOfMaxValue(a);
System.out.println("Index position of Maximum value in an array is : " + maxIndex );
}
New contributor
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
add a comment |
up vote
0
down vote
You would need something like this:
public int getIndexOfMaxValue( int array )
{
int maxIndex = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[maxIndex] ) maxIndex = i;
}
return maxIndex;
}
In order to use the code from above you could try the following:
public static void main(String args)
{
int a = new int { 12, 44, 23, 56, 23, 78, 13 };
int maxIndex = getIndexOfMaxValue(a);
System.out.println("Index position of Maximum value in an array is : " + maxIndex );
}
New contributor
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
You would need something like this:
public int getIndexOfMaxValue( int array )
{
int maxIndex = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[maxIndex] ) maxIndex = i;
}
return maxIndex;
}
In order to use the code from above you could try the following:
public static void main(String args)
{
int a = new int { 12, 44, 23, 56, 23, 78, 13 };
int maxIndex = getIndexOfMaxValue(a);
System.out.println("Index position of Maximum value in an array is : " + maxIndex );
}
New contributor
You would need something like this:
public int getIndexOfMaxValue( int array )
{
int maxIndex = 0;
for ( int i = 1; i < array.length; i++ )
{
if ( array[i] > array[maxIndex] ) maxIndex = i;
}
return maxIndex;
}
In order to use the code from above you could try the following:
public static void main(String args)
{
int a = new int { 12, 44, 23, 56, 23, 78, 13 };
int maxIndex = getIndexOfMaxValue(a);
System.out.println("Index position of Maximum value in an array is : " + maxIndex );
}
New contributor
edited yesterday
New contributor
answered yesterday
Andrea Calin
895
895
New contributor
New contributor
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
add a comment |
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
2
2
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Considering that the poster stated a lack of understanding of arrays, you might want to include a bit about how to glue it together.
– Jeroen Steenbeeke
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
Thanks for the idea. I have updated my answer.
– Andrea Calin
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
thank you Andrea @AndreaCalin
– Sena
yesterday
add a comment |
up vote
0
down vote
Though its a simple code, You have to learn that from your logic. you can follow this
import java.util.Scanner;
public class App {
public static void main(String args) {
Scanner input=new Scanner(System.in);
//dont need to instantiate that if you declare void findMax(int arrayOfnum) static
App app = new App();
int counter;
counter=1;
System.out.println("Enter how many number you want to take :");
int total=input.nextInt();
int arrayOfnum = new int[total];
//taking input on a array
while (counter<=total) {
System.out.println("Enter the "+counter+". number:");
arrayOfnum[counter]=input.nextInt();
counter++;
}
//method call
app.findMax(arrayOfnum);
}
private void findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
}
}
Here arrayOfnum is a one dimensional array whereyou can keep numbers that you take as input and then using private void findMax(int arrayOfnum) method you can find maximum value and position. I didnot return nothing from this method but if you want to return something from this method like you want to return position then you have to declare return type like private int findMax(int arrayOfnum) . and then implementation will be like this.
private int findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
return (pos+1);
}
You can make this method like public, protected etc. which is called access modifier.
If you declare this method as static then you do not need to create App app = new App(); instance in main method. You can directly access static method from main method like this findMax(arrayOfnum); instead of app.findMax(arrayOfnum); . Because main method is static. you can not access non-static variables from a static context. JVM keep main method is static by default.
NB: Try to learn highlighted terms from internet.
thank you for your answer @flopcoder
– Sena
yesterday
add a comment |
up vote
0
down vote
Though its a simple code, You have to learn that from your logic. you can follow this
import java.util.Scanner;
public class App {
public static void main(String args) {
Scanner input=new Scanner(System.in);
//dont need to instantiate that if you declare void findMax(int arrayOfnum) static
App app = new App();
int counter;
counter=1;
System.out.println("Enter how many number you want to take :");
int total=input.nextInt();
int arrayOfnum = new int[total];
//taking input on a array
while (counter<=total) {
System.out.println("Enter the "+counter+". number:");
arrayOfnum[counter]=input.nextInt();
counter++;
}
//method call
app.findMax(arrayOfnum);
}
private void findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
}
}
Here arrayOfnum is a one dimensional array whereyou can keep numbers that you take as input and then using private void findMax(int arrayOfnum) method you can find maximum value and position. I didnot return nothing from this method but if you want to return something from this method like you want to return position then you have to declare return type like private int findMax(int arrayOfnum) . and then implementation will be like this.
private int findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
return (pos+1);
}
You can make this method like public, protected etc. which is called access modifier.
If you declare this method as static then you do not need to create App app = new App(); instance in main method. You can directly access static method from main method like this findMax(arrayOfnum); instead of app.findMax(arrayOfnum); . Because main method is static. you can not access non-static variables from a static context. JVM keep main method is static by default.
NB: Try to learn highlighted terms from internet.
thank you for your answer @flopcoder
– Sena
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Though its a simple code, You have to learn that from your logic. you can follow this
import java.util.Scanner;
public class App {
public static void main(String args) {
Scanner input=new Scanner(System.in);
//dont need to instantiate that if you declare void findMax(int arrayOfnum) static
App app = new App();
int counter;
counter=1;
System.out.println("Enter how many number you want to take :");
int total=input.nextInt();
int arrayOfnum = new int[total];
//taking input on a array
while (counter<=total) {
System.out.println("Enter the "+counter+". number:");
arrayOfnum[counter]=input.nextInt();
counter++;
}
//method call
app.findMax(arrayOfnum);
}
private void findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
}
}
Here arrayOfnum is a one dimensional array whereyou can keep numbers that you take as input and then using private void findMax(int arrayOfnum) method you can find maximum value and position. I didnot return nothing from this method but if you want to return something from this method like you want to return position then you have to declare return type like private int findMax(int arrayOfnum) . and then implementation will be like this.
private int findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
return (pos+1);
}
You can make this method like public, protected etc. which is called access modifier.
If you declare this method as static then you do not need to create App app = new App(); instance in main method. You can directly access static method from main method like this findMax(arrayOfnum); instead of app.findMax(arrayOfnum); . Because main method is static. you can not access non-static variables from a static context. JVM keep main method is static by default.
NB: Try to learn highlighted terms from internet.
Though its a simple code, You have to learn that from your logic. you can follow this
import java.util.Scanner;
public class App {
public static void main(String args) {
Scanner input=new Scanner(System.in);
//dont need to instantiate that if you declare void findMax(int arrayOfnum) static
App app = new App();
int counter;
counter=1;
System.out.println("Enter how many number you want to take :");
int total=input.nextInt();
int arrayOfnum = new int[total];
//taking input on a array
while (counter<=total) {
System.out.println("Enter the "+counter+". number:");
arrayOfnum[counter]=input.nextInt();
counter++;
}
//method call
app.findMax(arrayOfnum);
}
private void findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
}
}
Here arrayOfnum is a one dimensional array whereyou can keep numbers that you take as input and then using private void findMax(int arrayOfnum) method you can find maximum value and position. I didnot return nothing from this method but if you want to return something from this method like you want to return position then you have to declare return type like private int findMax(int arrayOfnum) . and then implementation will be like this.
private int findMax(int arrayOfnum) {
int num=0,max=0,pos=0;
for(int i=0;i<arrayOfnum.length;i++)
{
if(arrayOfnum[i]>max)
{
max = arrayOfnum[i];
pos = i;
}
}
System.out.printf("Maximum value is %d and position is %d",max,pos+1);
return (pos+1);
}
You can make this method like public, protected etc. which is called access modifier.
If you declare this method as static then you do not need to create App app = new App(); instance in main method. You can directly access static method from main method like this findMax(arrayOfnum); instead of app.findMax(arrayOfnum); . Because main method is static. you can not access non-static variables from a static context. JVM keep main method is static by default.
NB: Try to learn highlighted terms from internet.
edited yesterday
answered yesterday
flopcoder
383111
383111
thank you for your answer @flopcoder
– Sena
yesterday
add a comment |
thank you for your answer @flopcoder
– Sena
yesterday
thank you for your answer @flopcoder
– Sena
yesterday
thank you for your answer @flopcoder
– Sena
yesterday
add a comment |
2
Java
!==Javascript
!– Nina Scholz
yesterday
Maximum position where exactly? You don't save all entered numbers so there isn't really any position to find. Also note that because you don't save all numbers entered and only ever save the last entered number your Maximum value will always be the last entered number (or 0 if it is negative)
– OH GOD SPIDERS
yesterday
1
It might be helpful to go through each line of your program and add a comment detailing EXACTLY what you think that line does. Your code isn't unsalvageable, but it's rather clear that (as you admitted yourself) you don't quite understand a few key concepts to make this work
– Jeroen Steenbeeke
yesterday
@NinaScholz i know but thank you
– Sena
yesterday
@OHGODSPIDERS thank you
– Sena
yesterday