Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need a help.i am using matlab code that uses hist command,i need the java equivalent code or the logic behind hist function in matlab so that i can code it in java or c.Thanks in advance.
Simple logic for histogramming (assuming you have known bins of constant width, and you don't need the most efficient code possible):
float x[50]; // assumed to be array of data values
float binWidth, firstBin; // bins of width binWidth; first one centered on firstBin
int numBins; // number of bins
int *bins, tooSmall = 0, tooLarge = 0, ii, indx;
bins = (int*)calloc(numBins * sizeof(int)); // allocate, set to zero
for(ii = 0; ii < 50; ii++) {
indx = floor((x[ii]-firstBin)/binWidth + 0.5);
if (index < 0 ) {
tooSmall++;
}
elseif (index >= numBins) {
tooLarge++;
}
else {
bins[indx]++;
}
}
}
At the end you have a histogram of the data in x, with two counters corresponding to the data that didn't fit in the range (either under, or over range).
Disclaimer: written without compiler to test. Looks "about right" - test it on a known case before relying on it.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have recently started programming in Java for the first time (just as a hobby), and at the moment Im working with a book "Head First Java" that is very good, but I'm really struggling with understanding the exercises.
Like this for example:
class Output {
void go() {
int y = 7;
for(int x = 1; x < 8; x++) {
y++; // is y now 8?
if(x >4) {
System.out.println(++y + " "); // does this make y = 9?
}
if(y > 14) {
System.out.println(" x = " + x);
break; // how does the break key word affect the rest of the loop?
}
}
}
public static void main(String[] args) {
Output o = new Output();
o.go();
}
}
Can someone please explain to me what goes on in this code?
Variable y must be 15, because you increased it's value many times with the for loop.
++y increases it's value by 1. i++ and ++i are very similar but not exactly the same. Both increment the number, but ++i increments the number before the current expression is evaluted, whereas i++ increments the number after the expression is evaluated.
break simply exists from the loop.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
If this function is running:
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
int random = r.nextInt(A.length);
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
if(A[random]==0)
A[random] += 1;
}
}
}
The output some times breaks a bit or freezes eclipse, totally at random, and sometimes it is able to give the whole result, other times it doesn't show the last part of the desired result.
If your A[random] != 0 and still your totalweight() returns < 630, the while loop would be infinite. One possible fix (and the one I think you need) is to move your int random = r.nextInt(A.length); inside your while loop.
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
int random = r.nextInt(A.length);
if(A[random]==0)
A[random] += 1;
}
}
}
Note: This would still loop infinitely if the sum of weight array is still < 630. So you will need additional checks.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make this work as a Python user forced to use Java:
private int parseInt(char[]){
private int parsed = 0
for (i=0 ; i < char.length(); i++;)
{parsed += (10**i) * char[i];}
}
I need to know why this method refuses to work and also how to fix it without using a built in function
First of all, change 10**i to Math.pow(10,i).
Then name your char[] array. Let's call it c.
Then, change c[i] to c[i]-'0', assuming c[i] is a digit.
private int parseInt(char[] c)
{
int parsed = 0;
for (i=0 ; i < c.length; i++)
parsed += Math.pow(10,i) * (c[i]-'0');
return parsed;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to write a program to print all possible permutations with 8 variables where each variable has multiple choices.
For eg. i have
A = {"A1"} //has only one choice
B = {"B1", "B2", "B3", "B4"} // has 4 choices
C = {"C1", "C2"} //has 2 choices
:
:
I = {"I1", "I2", "I3", "I4"} //has 4 choices
My output should be of the form: A-B-C-D-E-F-G-H-I with all possible choices of each variable and the order should be the same.
A1-B1-C1-D1-E1-F1-G1-H1-I1
A1-B2-C1-D1-E1-F1-G1-H1-I1
A1-B3-C1-D1-E1-F1-G1-H1-I1
A1-B4-C1-D1-E1-F1-G1-H1-I1
A1-B1-C2-D1-E1-F1-G1-H1-I1
etc.
I looked at all the other questions here, but i'm not able to figure out if i can use the inbuilt java permutations class for this. I tried writing a recursive program but am stuck when trying to explode each choice into all possible outputs. Appreciate any tips on how to accomplish this in either java/c++/vba since the language is not a consideration. Thanks!
You can write a series of 9 nested loops that does the job in a straightforward way. This would be a mess, though, and it can be done more simply with recursion on the nesting level. (Don't try recursing on the data at each level; that's pointless.) Here's a Java solution in pseudocode:
String[][] DATA = {A, B, ..., I};
void printAll(int recursionLevel, String prefix) {
String[] level = DATA[recursionLevel];
if (recursionLevel == DATA.length - 1) {
// last level -- actually do the output
for (String val : level) {
System.out.println(prefix + "-" + val);
}
} else {
// recurse
if (prefix.length() > 0) {
prefix += "-";
}
for (String val : level) {
printAll(recursionLevel + 1, prefix + val);
}
}
}
You would generate the output by calling:
printAll(0, "");