2

For example if I enter inRange(1,6) then it must print {2,2,2,3,5} for the below array. I am not sure if my logic is right. Is there a better way to do this? I am also not sure of how to construct my return statement. I want to do this without using arraylist or array.

public class AgeCount {
    static int[] a=new int[]{1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34};

    public AgeCount(){

    }
    public static int inRange(int b,int e)
    {
        int store[]=new int[a.length];
        int count=0;
        for (int i = 0; i < a.length; i++) {
                if(a[i]>b && a[i]<e)
            {
                    return a[i];
            }

        }
        return 0;
}   
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
STRANGER
  • 49
  • 7
  • From your example, it seems that you want to include items equal to the start of the range but exclude items equal to the end. However, your code will accept numbers equal to the end, so it is inconsistent with the example. Please clarify. Also, your current code will return a single number and will print nothing. Do you want to print all values, return all values, or something else? Can the input array be changed in place? – Ted Hopp Jun 14 '15 at 07:18
  • sorry my mistake....i have edited it – STRANGER Jun 14 '15 at 07:31
  • This is still not clear. What is the return value supposed to mean? Where is the output to be done? Can the original array be modified? – Ted Hopp Jun 14 '15 at 07:50

8 Answers8

0

If your method only has to print the numbers of the given range, it doesn't have to return anything :

public static void inRange(int b,int e)
{
    int count=0;
    System.out.print('{');
    boolean first = true;
    for (int i = 0; i < a.length; i++) {
        if(a[i]>=b && a[i]<=e)
        {
            if (!first) {
                System.out.print(',');
            } else {
                first = false;
            }
            System.out.print(a[i]);
        }

    }
    System.out.print('}');
}   

This is assuming the order of the output doesn't matter. If it does matter, sort the input array prior to the loop.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Nice start so far! Even though you said you didn't want to use array or ArrayList, it makes the most sense here to use one. I would use an ArrayList of Integers instead of just an array, because we don't know the length yet. Instead of saying return a[i];, you would say store.append(a[i]);. I haven't tested this code, so there may be an error or two, so please correct me if I'm wrong, but here's the fixed version:

public class AgeCount {
    static int[] a=new int[]{1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34};

    public AgeCount(){

    }
    public static void inRange(int b,int e)
    {
        ArrayList<Integer> store = new ArrayList<Integer>();
        int count=0;
        for (int i = 0; i < a.length; i++) {
                if(a[i]>=b && a[i]<=e)
            {
                    store.append(a[i]);
            }

        }
        println(store);
    }
}
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
rb612
  • 5,280
  • 3
  • 30
  • 68
0

Java 8 approach:

int[] arr = new int[] {1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34};
int res[] = Arrays.stream(arr).filter(n -> n  >= b && n <= e).toArray();
System.out.println(Arrays.toString(res));

I'm not sure why you don't want to use arrays or some kind of a list. If it's for homework purposes, then instead of returning a value from the method, print it if you only want to display the result. Otherwise, you should consider using List.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • it was a past year exam question with restricting the student to use only the return types specified – STRANGER Jun 14 '15 at 07:18
0

Java8:

public static void main(String[] args) {
    int[] arrayToFilter = new int[]{1, 2, 45, 6, 3, 2, 1, 2, 5, 6, 65, 45, 43, 21, 34, 34};

    int upperLimit = 5;
    inRange(arrayToFilter, upperLimit);
}

private static void inRange(int[] arrayToFilter, int upperLimit) {
    String sortedAndLimitedString = Arrays.stream(arrayToFilter)
            .sorted()
            .filter(value -> value < upperLimit)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining(",", "{", "}"));

    System.out.println(sortedAndLimitedString);
}

Output: {1,1,2,2,2,3}

rgrebski
  • 2,354
  • 20
  • 29
0

I assumed that the result must be sorted.

   public static int[] inRange(int b,int e) {
        return IntStream.of(a)
            .filter(n -> n > b && n < e)
            .sorted()
            .toArray();
    }

    System.out.println(Arrays.toString(AgeCount.inRange(1, 6)));
    // -> [2, 2, 2, 3, 5]

```

  • sorry I was incosistent with my example...I have changed it and i am retricted to write a function with only int return type – STRANGER Jun 14 '15 at 07:33
0

This sounds like a homework question. Still here goes.

Your return being a single int it has to be something like 122235 which represents all the ints satisfying your range condition.

So you use BitSet class and set the bits when found in range, which you can convert to an int like above and return.

satks
  • 161
  • 1
  • 3
0

Here is the simple & the shortest solution.

import java.util.*;

public class AgeCount
{
     static int[] a=new int[]{1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34};

    public AgeCount()
    {
    }
    public static void inRange(int b,int e)
    {
        int store[]=new int[a.length];
        Arrays.sort(a);
        for (int i = b; i < e; i++)
        {
            System.out.print(a[i+1]+",");
        }
    }   
}

And this is how you return a value for the same.

//sort an array, get a defined index values, and print it on the screen.

import java.util.*;

public class AgeCount
{
    static int[] a=new int[]{1,2,45,6,3,2,1,2,5,6,65,45,43,21,34,34};

    public AgeCount()
    {
    }
        public static int[] inRange(int b,int e)
    {
        int store[]=new int[a.length];
        int[] myRange = new int[e-b];

        Arrays.sort(a);

        for (int i = b; i < e; i++)
        {
            for (int j = 0; j < (e-b); j++)
            {
                myRange[j] = a[i+1];
            }
            System.out.print(a[i+1]+",");
        }
        return myRange;
    }   
}
VD007
  • 267
  • 2
  • 15
0

Well first things first, it seems like you aren't fully clear on what you are trying to do. And that's okay! Sometimes a problem can seem overwhelming and confusing if we're not really sure what's supposed to happen.

I recommend when you're starting a new task you take some time to decompose the task. Take a piece of paper and try and break it down into its smallest and simplest parts and go from there, coding and testing it bit by bit. The basics of the SDLC (try googling the software development lifecycle) could help you here too - it's all about figuring out what you are trying to achieve and what are the various things you need to implement to get there.

And please, Don't panic and throw code you don't understand down! You'll only sink deeper into the confusion!

Burkely91
  • 902
  • 9
  • 28