in this assignment
make the program read 10 integers and store them in an array. Then,
pass this array to a method that finds the largest integer in the array, and
counts its occurrences. Your method should return a string of the form
largestNumber

if you don't understand your assignment, you should ask your teacher for more information.

but, since it's a very easy assignment, if I were you, I would try to write it, if only step by step.

so:
step1 - write the code to read 10 integers and store them in an array
** test this
step2 - write a method to find the highest element of this array
** test this
step3 - re-write your method that it returns the String object you should and print it
** test this
step4 - add a while loop around the code you have so far, and add a check after
(or before) each run to ask the user whether he wants to continue
** test this

this good or no

Scanner input=new Scanner(System.in);

System.out.println("Enter 10 integer (if you enter 10 the input will be ended): ") ;
 int integer = input.nextInt() ; 
 
 while ( integer != 10 ){
        
 System.out.println("Enter 10 integer (if you enter 10 the input will be ended): ") ;
integer = input.nextInt() ;

 }
}
}

Edited 4 Years Ago by Hussam Alahmadi: n/a

what is about this?

int number,count;
      int [] data;
      
Scanner input=new Scanner(System.in);



 System.out.print("Type the number of integers that will be entered: ");
 number=input.nextInt();
 data = new int[number];
 
  for(count=0; count<number; count++)
 {
 System.out.print("Enter number "+(count+1)+": ");
 data[count]=input.nextInt();
 }

Edited 4 Years Ago by Hussam Alahmadi: n/a

You will need to compare the values in the array, storing the higest in a temp value.

After that then you will need to search the array for that value and increment a counter every time you find that value.

You will need to compare the values in the array, storing the higest in a temp value.

After that then you will need to search the array for that value and increment a counter every time you find that value.

Can you explain more?

Will will loop through the array. Comparing the current index with the next index. Whichever index is higher, you will need compare to the value stored in the temporary variable. If the value in the temp variable is higher, keep it. If not, then write the higher value in the temp.


Example.

if (array[i] > array[i + 1] && array[i] > tempvar)
{
     tempvar = array[i]
} 
else if(array[i + 1] > array[i] && array[i + 1] > tempvar)
{
     tempvar = array[i + 1]
}

After you have looped through the array and stored the highest value, you can then use

if (array[i] == tempvar)
{

 numofinstances += 1
}

Where numofinstances is the number of the occurances of that number. Loop through a second time, checking all the values.

Edited 4 Years Ago by Begginnerdev: n/a

the way you were going was good.
he BeginnerDev means something like this:

int nr, count, highest;

read count;
perform count times{
  read nr;
  if ( nr higher than highest ){
    highest = nr;
  }
  add nr to array;
}

now you'll have about the same as you have before, except, you'll just have added step2 in another way then I described.

thank you for help me

ok what about this

int max =0;
 Scanner input=new Scanner(System.in);
        System.out.println("Enter 10 numbers:");
       
        int array[]=new int[10];
        int count=0;
        for(int i=0;i<array.length;i++){
            array[i]=input.nextInt();
            if (array[i] > max) {
                max = array[i];
               }
         }
         for(int i=0;i<array.length;i++){
             if(array[i]==max){
                 count++;
             }
         }
        System.out.println("Largest Number is "+max+" which occurs "+count+" times");

Edited 4 Years Ago by Hussam Alahmadi: n/a

Salam alikum.. That works. You should run it before you submit your assignment brother.
Your assignment spec is saying that you need to have a method that compares Max and Min values.

Good luck, ya5y

Salam alikum.. That works. You should run it before you submit your assignment brother.
Your assignment spec is saying that you need to have a method that compares Max and Min values.

Good luck, ya5y

How can i do method that compares Max and Min values?

You will need to the the reverse of what you did to get max.


ie. Look for lowest and compare for lowest value and store it in another temp.

Then compare the two after all calculations are completed.

You will need to the the reverse of what you did to get max.


ie. Look for lowest and compare for lowest value and store it in another temp.

Then compare the two after all calculations are completed.

i cant understand you

can you explain in code?

this my code

package hw7;

import java.util.Scanner;

public class HW7 {
 
public static void main(String[] args) {
int max =0;
 Scanner input=new Scanner(System.in);
        System.out.println("Enter 10 numbers:");
       
        int array[]=new int[10];
        int count=0;
        for(int i=0;i<array.length;i++){
            array[i]=input.nextInt();
            if (array[i] > max) {
                max = array[i];
               }
         }
         for(int i=0;i<array.length;i++){
             if(array[i]==max){
                 count++;
             }
         }
        System.out.println("Largest Number is "+max+" which occurs "+count+" times");
        
temp(max,array,count);
}
public static int temp(int max , int[]array ,int count){
    
    
}
}

What I miss, I hope that you tell me?

this is what your assignment says

Then,
pass this array to a method that finds the largest integer in the array, and
counts its occurrences. Your method should return a string of the form
largestNumber

in this assignment
make the program read 10 integers and store them in an array. Then,
pass this array to a method that finds the largest integer in the array, and
counts its occurrences. Your method should return a string of the form
largestNumber > occurrenceCount. Then in the main method print
the returned string. Also, repeat the process until the user declines to con-
tinue. Here is a sample run:
Enter 10 integers: 1 11 7 12 4 2 12 1 5 12
Largest No->Occurrence Count : 12->3
Again? Enter Y or N
N

I don't think that there's a need to compare the MAX and MIN values if you imply what you learned from stultuske's last post as you've already completed your assignment requirements

Edited 4 Years Ago by zeroliken: n/a

This my code it's good but i should declare (n)

if i declare (n) the max number will be 0 (n=number)

import java.util.Scanner;

public class H7 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 numbers:");

int array[]=new int[10];

for(int i=0;i<array.length;i++){
array[i]=input.nextInt();

}
System.out.println(findMax(array));
}

public static String findMax(int[]array ){
int max=0,count=1 ;

if (n > max) {
max = n;
count = 0;
}
if (n == max)
count++;

return "" + max + " > " + count;



}
}

Edited 4 Years Ago by Hussam Alahmadi: n/a

you already wrote the code

for(int i=0;i<array.length;i++){
            array[i]=input.nextInt();
            if (array[i] > max) {
                max = array[i];
               }
         }

you'll need to remove one line, though, to find the maximum. the counting of occurences you've also posted yourself already. use those pieces of code within that method, both zeroliken and I already told you to do so.

if it still won't work, copy the code you have now and the error message, don't just say: "Shows error" because that tells us exactly nothing

The code work but i can not declare n

how can i change n by another Variable?

this my new code After amendment

import java.util.Scanner;

public class H7 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 numbers:");

int array[]=new int[10];

for(int i=0;i<array.length;i++){
array[i]=input.nextInt();

}
System.out.println(findMax(array));
}

public static String findMax(int[]array ){
int max=0,count=1 ;

if (n > max) {
max = n;
count = 0;
}
if (n == max)
count++;

return "" + max + " > " + count;



}
}

Edited 4 Years Ago by Hussam Alahmadi: n/a

you don't need n, you don't declare n.
the code I've posted in my previous post, is code you have written, tested and made your program work.
you want a for loop that iterates over the elements of the array
you want to compare max to the element your loop just provided the index of (array, remember?)
so, you'll find the max number.

after this, you'll run a second loop that counts the number of times that max value occurs in your array.

again: this is code you already showed us you have written.
go back into this thread and you will find the algorithm to get the number of occurences, the algorithm to find max is in my previous post, you just need to remove the line in which you are reading new elements in the array, since you've already stored the numbers in the array.

The code is work now thank all of you

you really help me I have learned how to deal with such as this questions

thank you (:

Edited 4 Years Ago by Hussam Alahmadi: n/a

This question has already been answered. Start a new discussion instead.