5 Replies - 429 Views - Last Post: 21 July 2014 - 06:38 AM Rate Topic: -----

#1 misheekoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Removing negative number from Array

Posted 20 July 2014 - 05:42 PM

Hello, I am currently working on this assignment.

Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter).

My question is, how can I remove the negative number from the array and calculate the average of the posive elements without the negative ones? This is my code so far...


import java.util.Scanner;
import javax.swing.JApplet;

public class Parameters
{
	//-------------------------------------
	//Calls the average and minimum methods
	//with different numbers of parameters
	//-------------------------------------
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		double mean1, mean2, mean3, mean4;
		int count =0;
		int[] list20 = new int[20];
		int i = 0;
		mean1 = average(42,69,37);
		mean2 = average(35,43,93,23,40,21,75);
		mean3 = average(13);
		mean4 = average();
		
		System.out.println("mean1 = " + mean1);
		System.out.println("mean2 = " + mean2);
		System.out.println("mean3 = " +mean3);
		System.out.println("mean3 = " +mean4);
		
		System.out.println("\nEnter up to 20 non-negative numbers:");
		
		for(i=0; i<list20.length; i++)
		{
			System.out.print("Number " + (i+1) + ": ");
			list20[i]=scan.nextInt();
			list20[count++] =list20[i];
				
			if(list20[i] < 0)
				break;	
		
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Removing negative number from Array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Removing negative number from Array

Posted 20 July 2014 - 06:02 PM

Scan the value into a variable first, then evaluate it before placing it into the array. The directions say, do not store the value into the array. Meaning you should not be doing the scan.nextInt() directly into the array. Put it into a variable, if variable is < 0, then you know you are at the end and simply don't put the value into the array.

:)
Was This Post Helpful? 1
  • +
  • -

#3 misheekoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Re: Removing negative number from Array

Posted 20 July 2014 - 07:10 PM

View PostMartyr2, on 20 July 2014 - 06:02 PM, said:

Scan the value into a variable first, then evaluate it before placing it into the array. The directions say, do not store the value into the array. Meaning you should not be doing the scan.nextInt() directly into the array. Put it into a variable, if variable is < 0, then you know you are at the end and simply don't put the value into the array.

:)/>


This might be embarassing... but how do I add the variable into the array?

System.out.println("\nEnter up to 20 non-negative numbers:");
		for(i=0; i <=list20.length; i++)
		{
			System.out.print("Number "+ (i+1) + ": ");
			input = scan.nextInt();
			
			if(input > 0)
			{
				input = list20[i];           //This is wrong..
			}
			else
				break;


Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 795
  • View blog
  • Posts: 3,473
  • Joined: 03-December 12

Re: Removing negative number from Array

Posted 20 July 2014 - 07:31 PM

list20[i] = input;


For starters. Now, what is the length of list20 at the start of the loop? Generally, a while loop is used for a sentinel loop. You loop while a condition is true, (not a negative number).
Was This Post Helpful? 1
  • +
  • -

#5 misheekoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-July 14

Re: Removing negative number from Array

Posted 20 July 2014 - 07:49 PM

View Postastonecipher, on 20 July 2014 - 07:31 PM, said:

list20[i] = input;


For starters. Now, what is the length of list20 at the start of the loop? Generally, a while loop is used for a sentinel loop. You loop while a condition is true, (not a negative number).


OMG..I've been doing it the wrong way round all this time?! ...
Well.. to answer your question.. I defined list20 to have 20 elements. But then the elements in the array starts at 0.

Thank you again.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Removing negative number from Array

Posted 21 July 2014 - 06:38 AM

... and your while loop truth conditions must be

a. there are more numbers to enter (You can ask the user to enter 'q' to quit)
b. the number of valid numbers already entered does not exceed the number of available array elements
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1