Representing a range in Processing - processing

I'm a beginner at Processing, and I was wondering if there is a function that represents an interval of values? In java, I think it's IntRange, but what's the equivalent in Processing?

Java does not have an IntRange class. You might be best off just creating your own class:
class Range{
int low;
int high;
public Range(int low, int high){
this.low = low;
this.high = high;
}
}

Related

Whats wrong with my top down knapsack dp approach?

I am not able to figure out what is wrong with my top down knapsack dp approach, its failing testcases on below link, need help.
Question link: https://www.interviewbit.com/problems/0-1-knapsack/
Here is my code:
int fin(int i,int wt,int curprofit,vector<int>&A,vector<int>&B,int C,int n,vector<vector<int>>&dp)
{
if(i==n)
return curprofit;
if(dp[i][wt]!=-1)
return dp[i][wt];
int ret=0;
ret=max(ret,fin(i+1,wt,curprofit,A,B,C,n,dp));
if(wt+B[i]<=C)
{
ret=max(ret,fin(i+1,wt+B[i],curprofit+A[i],A,B,C,n,dp));
}
return dp[i][wt]= ret;
}
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
int n=A.size();
vector<vector<int>>dp(n+1,vector<int>(C+1,-1));
return fin(0,0,0,A,B,C,n,dp);
}
Here's the fix, but I'd suggest you brush up on your recursion knowledge.
Calculate the max profit on the fly, not passing as the parameter. Otherwise, you'll need to put curprofit also in the dp state which will be costly. You may also see the output by removing the dp[][] caching. Just put up a correct recursive solution & memoize it.
int fin(int i,int wt,vector<int>&A,vector<int>&B,int C,int n,vector<vector<int>>&dp)
{
if(i==n)
return 0;
if(dp[i][wt]!=-1)
return dp[i][wt];
int ret=0;
ret=max(ret,fin(i+1,wt,A,B,C,n,dp));
if(wt+B[i]<=C)
{
ret=max(ret,fin(i+1,wt+B[i],A,B,C,n,dp) + A[i]);
}
return dp[i][wt]= ret;
}
int Solution::solve(vector<int> &A, vector<int> &B, int C) {
int n=A.size();
vector<vector<int>>dp(n+1,vector<int>(C+1,-1));
return fin(0,0,A,B,C,n,dp);
}

NullPointerException in processing with 2D array of objects

I am creating a small game in processing and I am trying to print a 2D array of square objects. I have this NullPointerException and I cannot seem to find anything like it on the web.
int edge = 10;
public int sizeOfRect = 50;
public int numberOfRects = 10;
Rectangle[][] player = new Rectangle[numberOfRects][numberOfRects];
public int k;
public int l;
public int kcount=0;
public int lcount=0;
void setup(){
background(200);
size(565, 565);
}
void draw(){
for(k=edge; k<width-edge; k+=55){
for(l=edge; l<height-edge; l+=55){
player[kcount][lcount].display();
lcount++;
}
lcount=0;
kcount++;
}
kcount=0;
}
and the Rectangle Class
class Rectangle{
int i;
int j;
Rectangle(){
i=k;//xcoor
j=l;//ycoor
}
void display(){
fill(0);
rect(i,j,sizeOfRect,sizeOfRect);
}
}
And finally the exception
Plain.pde:17:0:17:0: NullPointerException Finished. Could not run the
sketch (Target VM failed to initialize). For more information, read
revisions.txt and Help? Troubleshooting. Could not run the sketch.
Thank you in advance
You're creating a 2D array, but you're never filling that array with any objects. In other words, your 2D array is full of null values. That's why you're getting a NullPointerException.
You need to fill your array with values. Here's an example:
player[1][2] = new Rectangle();
You probably want to use a nested for loop to fill your array.

Can we have an if loop for the root section of Quick Union algorithm?

In the Quick Union algorithm imlementation below, inside the root method can we have an if loop (such as if(i != id[i]) instead of the while loop? I think it works just as well. Then why did they use the while loop?
public class QuickUnionUF {
private int []id;
public QuickUnionUF(int N){
id = new int[N];
for(int i=0; i<N; i++) id[i] = i;
}
private int root(int i){
while(i != id[i]) i = id[i];
return i;
}
public boolean connected(int p, int q){
return root(p) == root(q);
}
public void union(int p, int q){
int i = root(p);
int j = root(q);
id[i] = j;
}
}
A component may be represented by a tree higher than one level. To get the component id you need to go down all the way to the root. For example try
s = new QuickUnionUF(3);
s.union(0,1);
s.union(1,2);
System.out.println(s.connected(0,1)); // <== prints false when using 'if'

Need to make program paralleling Sieve of Eratosthenes algorithm in java using arrays

We were assigned to make a java program which paralleled Sieve of Eratosthenes algorithm. I have tried several times in every which way i know of, but haven't been able to get it right. Im supposed to use fill arrays with prime numbers less than the number that was imputed. Here is the code I have, Can someone please help me in double checking the program and/or figuring out why I am getting this error? Any help is appreciated.
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.Arrays;
public class Lab6st
{
static int MAX = 100;
static int i;
static int k;
static int intArray;
static int isPrime;
public static void main(String args[])
{
System.out.println("\nLAB12 100 Point Version");
Scanner input = new Scanner(System.in);
boolean primes[] = new boolean[MAX];
computePrimes(primes);
displayPrimes(primes);
Arrays.fill(primes,true);
}
public static void computePrimes(boolean primes[])
{
System.out.println("\nCOMPUTING PRIME NUMBERS");
for (int i = 1; i < MAX; i++);
{
for (i=1; i < MAX; i++ );
for (k=2; k<i; k++){
int n = i%k;
if (n==0)
{
break;
}
}
if (i==k);
{
primes[i] = true;
}
}
}
public static void displayPrimes(boolean primes[])
{
System.out.println("\n\nPRIMES BETWEEN 1 AND "+ primes.length);
for (int isPrime = 0; isPrime < MAX; isPrime++);
if (primes[isPrime] == true);
System.out.println(Arrays.asList(primes));
}
}
Your algorithm is not the Sieve of Eratosthenes; it's a poor implementation of trial division (the modulo operator gives it away). My essay Programming with Prime Numbers describes the Sieve of Eratosthenes algorithm in detail, discusses the very common error that you made, and includes this implementation in Java:
public static LinkedList sieve(int n)
{
BitSet b = new BitSet(n);
LinkedList ps = new LinkedList();
b.set(0,n);
for (int p=2; p<n; p++)
{
if (b.get(p))
{
ps.add(p);
for (int i=p+p; i<n; i+=p)
{
b.clear(i);
}
}
}
return ps;
}

Algorithm for checking transitivity of relation?

I need to check if relation is transitive or not?
Would you please suggest some algorithm to check the transitivity of relations?
I am storing relation as a boolean matrix there is 1 if elements are related other wise 0 like in graphs.
Thanks.
Much simpler algorithm as my Map/Set version (deleted), now with boolean matrix. Maybe this is easier to understand, even if you don't know Java?
public class Trans
{
final static int SIZE = 4;
static boolean isTransitive(boolean[][] function)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (function[i][j])
{
for (int k = 0; k < SIZE; k++)
{
if (function[j][k] && !function[i][k])
{
return false;
}
}
}
}
}
return true;
}
public static void main(String[] args)
{
boolean[][] function = new boolean[SIZE][SIZE];
for (int i = 0; i < SIZE; i++)
{
function[i] = new boolean[SIZE];
}
function[0][1] = true;
function[1][2] = true;
function[0][2] = true;
function[0][3] = true;
function[1][3] = true;
System.out.println(isTransitive(function));
}
}
Despite this totally sounds like homework...
You'd need to store your relations so that you can look them up by the antecedent very quickly. Then you can discover transitive relations of the type A->B->C, add them to the same storage, and keep going to look up A->B->C->D, etc etc...
Topological sorting may be the right direction. The relationship is transitive if there are no loops in its directed graph representation. If you care about speed, graph algorithms are probably the way to go.

Resources