2 Replies - 601 Views - Last Post: 03 September 2013 - 03:52 AM Rate Topic: -----

#1 cherrypepsi2013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-March 13

help with find the lowest number in a array.

Posted 01 September 2013 - 07:34 PM

The user enters x amount of numbers into an array.
I can not get it to print out the lowest number. It shows the last number the user enters as the lowest number.
example:

enter how many numbers. 5
count 0 1
count 1 2
count 2 5
count 3 3
count 4 4
1
2
5
3
4
total is 15
average is 3
high number is 5
low number is 4


import java.util.Scanner;

public class array2
{
public static void main (String args[])
{
Scanner input = new Scanner (System.in);
 
int number=0;
int x= 0 ;
int total = 0;
int a;
int sum = 0;
int average = 0;

System.out.print("Please enter how many numbers.\n");
number = input.nextInt();

int array [] = new int[number];

System.out.printf("Enter the  %d  numbers now.\n", number);
for (int i = 0; i < array.length; i++)
{    
array[i] = input.nextInt();
}
System.out.println ("Your number in the array\n");
printArray(array);
// find the sum
for (int i : array)
{
    sum +=i;
}
System.out.printf ("Total is %d: \n ", sum);
// find the average
average = sum/array.length;
System.out.printf ("Average is %d: \n ",average);

//  find the high number
int high = array[0];
for(int i = 1 ; i < array.length; i++)
{
if(high < array [i])
{
high = array[i];
}
}
System.out.printf("The high number is %d \n",high);
//  find the low number
int low = array[0];
for (int i = 0 ; i < array.length; i++)
{
    if (low  > array[i]);
    {
low = array[i];
    }
}
System.out.printf("The low number is %d \n",low);
}
public static void printArray(int array[])
{
int total = 0;    
int i;    
int n = array.length;
for ( i = 0; i < n; i++)
{
System.out.printf("Count %d %d \n",i, array[i]);
}
}    
}




*Edited: fixed code tags

This post has been edited by pbl: 01 September 2013 - 07:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help with find the lowest number in a array.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with find the lowest number in a array.

Posted 01 September 2013 - 07:39 PM

Fixed code tags but code indentation is still horrible

  int min = array[0];
  for(int i = 1; i < array.length; ++i) {
    if(array[i] < min)
      min = array[i];



should do the job
Was This Post Helpful? 1
  • +
  • -

#3 The Arcanist  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 32
  • Joined: 27-August 13

Re: help with find the lowest number in a array.

Posted 03 September 2013 - 03:52 AM

It won't give the correct output because of the following code you made :
for (int i = 0 ; i < array.length; i++)
	{
	    if (low  > array[i]);
	    {
	low = array[i];
	    }



Their is a semicolon after the if statement which shouldn't be there.

Other than that, I would recommend you to follow the conventions and start the class name with a capital letter, remove few variables which remain unused throughout your code and use proper indentation to make your code more readable.
Cheers. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1