Java calculate 1*3*5*7*...* to user given double [closed] - java

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 3 years ago.
Improve this question
i am trying to make java program that ask number from user and runs following calculation: 1*3*5*7*....*usergivendouble. I think for loop is best for this but not sure how to make such loop. i tried
for(double i=1;i<=n;i+=2)
{
n*= 2;
}
but it just never stops asking new number.
im new to java and all help is appreciated!

Assuming n is the user given number, increasing n inside the loop is the problem. You increase n inside the loop and also use it as the loop's end condition. This causes an infinite loop, as the loop's condition is never met.
You need to change the code to be:
double multiplyRestult=1;
for(double i=1;i<=n;i+=2)
{
multiplyRestult*= i;
}

Related

Using threads for insertion-sort [closed]

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 days ago.
Improve this question
I am using insertion-sort to sort the k first values in my array,
First i start by sorting the k first in decreasing order, after that i check the [k+1,n] values if they are larger thatn the lowest number in k, being k-1.
the check is done with threads, where I give every thread a segment of [k+1,n], where each of them will check the values in their segment if they are larger than k-1, if yes they will set the value in the correct place.
My problem is that the parallel version is MUCH slower than if I do it in a sequence, like 100x slower, the test i gave was makin k = 100 and n = 10milion.
Anyone know if you can parallel insertion sort?
If code is needed I can post

why does the loop on this program time out? [closed]

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 4 years ago.
Improve this question
it works fine for some strings, but for some reason it does not work for the last three. I see no difference to the type of strings being entered and I expect indx to evaluate to -1, but for some reason it does not on the last three strings. I don't understand why.
edit: problem solved. As you guys said, I was taking the substring of str instead of news within the loop. Sorry for such a simple mistake guys, I'm just starting to code and these are the details I need to pay attention to more. Also as I am working within the codingbat website there is no debugger, but I also want to highly recommend that website for other beginners. It will give you many example problems to begin coding on. Thanks again.
enter image description here
code:
public String stringYak(String str) {
int indx = str.indexOf("yak");
String news =str;
for(;indx!=-1;)
{
news = (str.substring(0,indx) + str.substring(indx+3,str.length()));
indx = news.indexOf("yak");
}
return news;
}
Because loop never breaks.
You are taking substring from str index from news inside the loop.
May be you want to take both substring and index from news.

Java insertion sort not working properly [closed]

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 6 years ago.
Improve this question
enter image description here
This is the code screenshot link.
I am edit many time but every time the output is not completely sorted.
Your problem is very simply: you are increasing k far too often within your code!
Meaning: you are already looping with k; so you got:
for (int k=0; k < a.length; k++) { // dont use hardcoded "9" here btw!
and then you have
k++ again in the loop body. You simply dont have to do that!
Meaning: your k is growing faster than it should. But as your loop stops when k reaches 9; you are not processing all elements in your array!
Plus: insertion sort doesn't work with iterating your array once!
You have to keep iterating until all elements are in their place! You really want to study/think more about this algorithm. You, not us that is!
And as said, dont use hard-coded limits for arrays. You already say once that your array should contain 10 elements. From there on, you should be using a.length only! Besides: only use one-letter names for loop counters and such things. "a" is a pretty bad name for an array; why dont you call it "numbers" for example.

java boolean array and turning every third value to false [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The Boolean array is initialized to true. I need to know how to turn every third value to false and also going through the array over and over. It is essentially duck duck goose without the randomness.
This sounds like a beginners programming exercise, so I'm going to just give you a hint or two:
Go back to your textbook, lecture notes, tutorial and reread the stuff on for loops. Focus on the old style ones.
Think about how to write a for loop that steps through integer index values in the pattern that you require.
Re "... going through the array over and over" - not sure what you mean, but maybe the hint for this is to think about using nested loops; i.e. a loop inside another loop.
But the most important advice is to try and work this out for yourself.
Well I'm really not sure what you mean by going through the array over and over but the following code will turn every third value to false.
for (int i = 0; i < myVar.length; i++) {
if (i % 3 == 0) {
myVar[i] = false;
}
}
Edit: Oops someone beat me to it while I was typing lol.

My program gives an index out of bounds error [closed]

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
int i=0;
while(!a.isEmpty()) {
if(a.toCharArray()[i]==' ') {
System.out.println(a.toCharArray()[i]);
}
i++;
}
When I run this program it's giving me an error: index out of bounds. How can I solve it?
You're not changing a in the loop, so a.isEmpty() will not change and you'll continue looping until i goes out of bounds. Did you mean:
while (i < a.length()) {
And as an aside, a.toCharArray()[i] can (and should) simply be a.charAt(i) (as #MartijnCourteaux also points out).
There are already a couple of answers that give solutions to the problem. This answer is about how you could have found the problem yourself.
Most array index out of bounds errors in loops are due to incorrect loop condition, so that the condition goes on being true even after the index has reached the end of the loop. Your loop condition did not depend on i, so the only way it could change from true to false is if a were changing inside the loop.
If you expected a to change, you should have put System.out.println(a) inside the loop, and monitored the value of a. You would have seen it not change.
Alternatively, if the isEmpty test were a typo, and you meant to test something that depends on i, desk checking your loop condition should have found it.
You should change your loop to this instead:
char[] c=a.toCharArray();
for(i=0; i<c.length; i++){

Categories