1 Replies - 5795 Views - Last Post: 19 December 2012 - 06:44 AM Rate Topic: -----

#1 Activenoob   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 23
  • Joined: 13-November 12

Minimum and Maximum Array Problem

Posted 19 December 2012 - 06:39 AM

Here is the assignment: Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array {14, 1, 22, 17, 36, 7, -43, 5}, there are four elements whose values fall between 1 and 40.
Test Data: 12, 18, 21, 22, 1, 31, 5, 10, 34, 40, 67, 57
Min Number = 1
Max Number = 40
So far I have managed to come up with this code, but I still can't seem to get my program to work.
import java.util.Scanner;
public class ArrayProblem3
{
public static void main (String [] args)
{
Scanner in = new Scanner (System.in);
int [] data = new int [13];
int max = 0;
int min = 0;
int x = 0;
//Asks the user to input a minimum value
System.out.println ("Please enter in a minimum number:");
min = in.nextInt ();
//Asks the user to input a maximum value
System.out.println ("Please enter in a maximum number:");
max = in.nextInt ();

for (int n = 0; n < 12; n ++)
{
//Asks the user to input in 12 integers.
System.out.println ("Please enter in 12 integers");
x = in.nextInt ();
}


if (min > x && x < max)
{
	data [x] ++;
//Displays how many numbers are in the range.
System.out.println ("There are" + data [n] + "numbers in the range");
}





}





}
I am getting 1 error.


H:\AP Computer Science\ArrayProblem3.java:30: cannot find symbol
symbol : variable n
location: class ArrayProblem3
System.out.println ("There are" + data [n] + "numbers in the range");
^
1 error

Tool completed with exit code 1

Is This A Good Question/Topic? 0
  • +

Replies To: Minimum and Maximum Array Problem

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Minimum and Maximum Array Problem

Posted 19 December 2012 - 06:44 AM

Line 26 to 31 are out of the for-loop's scope

for (int n=0; n<12; n++) {
    //In the scope of the for loop
    //This part will be executed 12 times
    //the variable n exists here
}

//Out of the for-loop
//The variable n no longer exists


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1