"while" loop not ending when it should [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
Here's the very basic code. The loop will start, even if the condition is false, and it won't end.
int oldpick = 6;
int pick = rand() % 5;
while (pick = oldpick){
pick = rand() % 5;
}

pick = oldpick
This assignes the value of oldpick to pick, and then enters the loop. I think you wanted ==
while (pick == oldpick){
Also, this line:
pick = rand() % 5;
will not give a number higher than 4, so the condition could never be realised, even with ==
All of this could have been seen if warnings had been activated when compiling.

You have a simple typographical error in your while loop condition...
while (pick = oldpick) should be while (pick == oldpick) for equals or while (pick != oldpick) for not equals.
Plus, pick will never ever equal 6 as 6 % 5 == 5

Related

Strongest Neighbour question in GeekForGeeks [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 1 year ago.
Improve this question
Input:
n = 6
arr[] = {1,2,2,3,4,5}
Output: 2 2 3 4 5
Explanation: Maximum of arr[0] and arr[1]
is 2, that of arr[1] and arr[2] is 2, ...
and so on. For last two elements, maximum
is 5.
A standard array problem and I know the right solution to it too but I tried using the max() function in the C++ std library and I'm getting this
For Input:
6
1 2 2 3 4 5
your output is:
22345
This is how my function looks like
void maximumAdjacent(int sizeOfArray, int arr[]){
for (int i = 0; i<sizeOfArray-1; i++) {
cout << std::max(arr[i+1], arr[i]) << "";
}
}
On submission this answer isn't accepted and I can't seem to figure out why?
This might be a dumb answer but it looks like you are missing spaces between the numbers. I see the "" in your string and you might need a " " instead. Without the space, it is one giant number. Does that help?

What's the problem with this recursive case? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
guys I faced a problem with the recursive case of a power of a number given its base and exponent. Why is it not working properly and returns 0 everytime?
#include <bits/stdc++.h>
using namespace std;
int qn(int n,int q)
if(q==1)
return 0;
return n*qn(n,q-1);
}
int main() {
cout << qn(2,2);
}
You return 0 at the end of the recursion, and multiply with that value. Will always yield 0.
Seriously, this is very easy to figure out using only a pencil and some paper.
qn(2, 2) called
q is 2 so you execute n * qn(n, q - 1)
Replacing values you have 2 * qn(2, 1)
qn(2, 1) called
q is 1, so if (q == 1) is true
thus 0 is returned
You remplace qn(2, 1) by 0 in 2 * qn(2, 1)
You have 2 * 0
That give 0
If you use a debugger, this is even easier as you can execute the program step by step and simply note everythings it does.
By the way, as written, your code would not even compile because an opening { is missing after int qn(int n,int q).

Array of 12 elements- find count- practice [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
Could you guys please walk and help me with this practice question?
I'm unable to figure out how the answer is 5.
int arr[12] = { 1,3,5,0,7,2,0,4,4,0,8,8 };
int count = 0;
for (int i = 0; i<11; i++) {
if (arr[i] = arr[i + 1])
count++;
else
count--;
}
cout << count << endl;
In your example you have :
if (arr[i] = arr[i + 1])
which is the =, not ==. It is assigning not checking for equality. So in the example:
if (a = 3) {
You will assign a to 3 and check if 3 is true, which it is. This leads to an easy look at why the answer is 5:
arr=> { 1,3,5,0,7,2,0,4,4,0,8,8 };
count=> 1,2,1,2,3,2,3,4,3,4,5
And if you are interested, look at the array after you have completed. It will look like this:
{3,5,0,7,2,0,4,4,0,8,8,8} // Everything has been moved down 1 (except for the final member)
See a live example of this here.

Infinite Loop when comparing a number in 2d array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
Trying to write a function that sees if the number in the array is increasing or decreasing compared to the previous.
Getting an infinite loop.
for(int col=0; col < 5; col++) {
newArray[col][0] = printthis[col][0];
for(int row = 2; row < 5; row++) {
cout << col << "\t" << row << "\n";
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])) {
newArray[col][row] = "Up";
}
else {
newArray[col][row] = "Down";
} //if else
}//inner loop
}
Here the loop index is decreased, so it will always stay at value 2, note the --row:
if(stoi(printthis[col][row]) > stoi(printthis[col][--row])){
You probably want:
if(stoi(printthis[col][row]) > stoi(printthis[col][row-1])){
Also the loop should probably start at row = 1 instead of 2, to compare to the first row instead of second.

save variable from if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.