Jump to content

Can u see what is wrong with my coding?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Zeeyo

Zeeyo

    Newbie

  • Members
  • Pip
  • 2 posts
Here are the question

a) create an array to store a maximum of 20 integers.
b) input (unknown number) integers into the array
c) display the position (index) of the smallest and largest element in the array
d) compute and display the average value in the array

Here are my coding


import java.util.*;


public class Main

{

public static void main(String[] args)

{


final int ARRAY = 20;

int[] numbers = new int[ARRAY];

getValues(numbers);

getAverage(numbers);

lowestValue(numbers);

highestValue(numbers);

}


private static void getValues(int[] array)

{


Scanner a = new Scanner(System.in);



for (int i = 0; i < array.length; i++) //while loop sentinel

{

System.out.print(" Enter number " + (i + 1) + ": ");

array[i] = a.nextInt();


}

int[] numbers = new int[8];

int highest = numbers[0];

for (int i = 0; i < numbers.length; i++)

{

if (numbers[i] > highest)

{

highest = numbers[i];

}

}


int lowest = numbers[0];

for (int i = 1; i < numbers.length; i++)

{

if (numbers[i] > lowest)

{

lowest = numbers[i];

}

}

}


private static void getAverage(int[] numbers)

{

double total = 0;

double average;

for (int i = 0; i < numbers.length; i++)

{

total += numbers[i];

}

average = total / numbers.length;

System.out.println("\nThe average is: " + average);

}


private static void lowestValue(int[] numbers)

{

int lowest = numbers[0];

for (int i = 1; i < numbers.length; i++)

{

if (numbers[i] < lowest)

{

lowest = numbers[i];

}

}

System.out.println("The lowest is: " + lowest);

}


private static void highestValue(int[] numbers)

{

int highest = numbers[0];

for (int i = 0; i < numbers.length; i++)

{

if (numbers[i] > highest)

{

highest = numbers[i];

}

}

System.out.println("The highest is: " + highest);

}

}

The error i did was:
a) The highest and lowest values
(the question ask for index)
b) my lecturer ask me to use a while loop sentinel for question (b) and i don't know how to do a while loop sentinel.

Thanks for the help :)

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
I'll answer error b since error a should be a trivial fix.

A while loop simply looks like this in Java:
(Pseudocode is written in green.)

while ([I][COLOR="#2e8b57"]conditional statement[/COLOR][/I]) {

    [I][COLOR="#2e8b57"]body of loop[/COLOR][/I]

}

You can come up with any conditional statement you want. As long as the conditional evaluates to true, the loop body will execute. (Hint: It's up to the statements in the body of the loop or the conditional statement itself to change some state which will eventually cause it to evaluate to false.)

In your case, you'll have to have some way for the user to signal the program that they are done entering information before the maximum 20 integers has been reached. So your conditional will be two-fold: A boolean AND of your signalling conditional and the next index being less than 20 (your array's maximum length).

Hope that helps.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users