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
I keep getting this error "Control may reach end of non-void function". Can anyone help me get through? I am stuck here. Thank you.
bool Vector_double::erase(int ind1, int ind2){
if(ind1 >= count || ind2 >= count || ind1 >= ind2){
if(ind1 == ind2){
erase(ind1);
return true;
}
return false;
}
else{
double *narr = new double[count - ind2 - 1];
for(int i = ind2 + 1; i < count; ++i){
narr[i - ind2 - 1] = arr[i];
}
for(int i = ind1; i < ind1 + count - ind2 - 1;++i){
arr[i] = narr[i - ind1];
}
count = count - (ind2 - ind1) - 1;
}
}
Your function promises to return something (bool). However, the code contains paths that don't return anything. In order to fix this bug, start by documenting (in plain English) what the returned value is supposed to express. Then, adjust your code.
Related
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 1 year ago.
Improve this question
I dont really understand i have written return in the end but still it gives error
CODE :
int factorial(int num)
{
int N;
if (num > 1)
{
N = (num * factorial(num--));
}
else
return N;
}
int main()
{
cout << factorial(5);
return 0;
}
ERROR : warning: control reaches end of non-void function [-Wreturn-type]
16 | }
Your issue is that you don't return anything. If you look at the flow of the program you can see that for num > 1 you do the factorial stuff and for num <= 1 you just return N. For num > 1 the return statement is never reached. This issue can be fixed by removing the else, BUT that leaves an other issue mentioned, namely that for num <= 1 N is never initialised. If you initialise it to 1 that should solve that, but as people pointed out you don't need N, you can do return num * factorial(num - 1); and simply return 1 for num <= 1. The final problem with your code is that you do num * factorial(num--). factorial(num--) will call factorial(num), when you would need factorial(num-1), because num-- is the post-decrement operator.
Other suggestions in the comments are good to heed as well, like implementing guards from integer overflow and the like.
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
Sensor_Fusion ranges from 3 to -3, the motor output is correct between 3 to -1, showing 1, -1 for Motor A and B respectively and then switching to the second elseif statement however when sensor fusion goes below -1 MotorA shows 1 instead of -1 and MotorB shows 0.996024 instead of 1. Does anyone know what the problem is?
if (Sensor_Fusion >= 1) {
MotorA = 1;
}
else if (-1 < Sensor_Fusion && Sensor_Fusion < 1) {
MotorA = Sensor_Fusion;
}
else {
MotorA = -1;
}
if (Sensor_Fusion >= 1) {
MotorB = -1;
}
else if (1 > Sensor_Fusion && Sensor_Fusion > -1) {
MotorB = 0 - Sensor_Fusion;
}
else {
MotorA = 1;
}
Your last line has the error, it should be MotorB = 1; not MotorA = 1;.
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.
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.
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 6 years ago.
Improve this question
I'm trying to generate and print out 5 numbers from 1 to 5, but not in sequence. I'm using a self-written function, 'appearBefore' that will check whether the number has appeared before.
The function appearBefore will return '0' if the number has not appeared before, and '1' if the function has appeared before.
At the moment, the do-while loop doesn't get out of the loop even when 0 is returned. The program never ends. Any recommendations on what I can do?
EDITS - The downvotes sure comes fast. I have added the counter++, but it still does not work. Perhaps someone can advice on the inner-loop?
while (count < 5) {
repeat = 1;
do {
randomNumber = rand() % 4 + 1;
cout << randomNumber;
repeat = appearBefore(randomNumber);
cout << " " << repeat << endl;
} while (repeat == 1);
//Add the number into an array of numbers that have appeared before
checker[counterForChecker] = randomNumber;
counterForChecker++;
counter++;
}
This is the function appearBefore (the variables are global variables):
int appearBefore(int number) {
int x = 0;
int match = 0;
while (x < counterForChecker+1) {
if (checker[x] == number) {
match = 1;
break;
}
else {
match = 0;
}
x++;
}
return match;
}
You check for count < 5 while increasing counterForChecker.
Set the while condition to
while (counterForChecker < 5)
or increase the counter
counter++; // counterForChecker++;
(Assuming that counter++; actually says count++;...)
If x and k are positive, x % k is a number between 0 and k - 1.
So you have four possible values to choose from (1,2,3,4), and you're looping until you've found five unique values.
That will never end well.
To generate numbers from 1 to 5, use rand() % 5 + 1;
You'll want to change the variable count. This is now not done, so since the value does not change the loop will not end.
The loop is running while count < 5 and you never increment count.
Did you mean to use:
while (counterForChecker < 5)
If not, add:
++count;
at the end of the while loop
Use a debugger and step through the code, looking at the values of the variables as you do so. You'll learn more about how everything is working.