I made a recursive exponential function and originally called the recursive function as return num * power(num, exp--), however I had to change it to exp-1 because the first method broke the program. Why do I have to use exp-1?
#include <iostream>
using namespace std;
int power(int num, int exp);
int main()
{
cout << power(5, 3) << endl;
return 0;
}
int power(int num, int exp)
{
if (exp == 0)
return 1;
else
return num * power(num, exp-1);
}
That's because, exp-- firstly uses your value for invoking power() function and then gets decremented. In that case, the value passed to the function remains 3. Therefore, it goes into the infinite loop.
You should use either --exp or exp-1.
Why do I have to use exp-1?
You could also pre-increment --exp which gives you the incremented value.
Post-increment exp-- takes current value and decrements afterwards.
In your case that never changes the value.
You take current the value of exp and you're passing it by value to function.
You need --exp, not exp--. exp-- is post-decrement, meaning that it will only be executed after the function call. So, exp-- is not equivalent to exp-1 in your code, but --exp is: the value will be decremented first, and then the function will be called with the decremented value.
Related
I've made this example to show what I'm talking about. I want to know if there is a way to run through main() without resetting value to 0.
int main(){
int value = 0;
value++;
cout << value << endl;
main();
}
Before answering the question, your example has two big problems
Calling, or even taking the address of, main is not allowed.
Your function has infinite recursion which makes your program have undefined behavior.
A different example where value is saved between calls could look like this. It uses a static variable, initialized to 0 the first time the function is called, and is never initialized again during the program execution.
#include <iostream>
int a_function() {
static int value = 0;
++value;
if(value < 100) a_function();
return value;
}
int main(){
std::cout << a_function(); // prints 100
}
If you want to keep the variable value local to the main function, you can declare it as static int value = 0;.
As has been pointed out in various comments though, recursively calling any function without an escape mechanism like you are is a bad idea. Doing it with main is a worse idea still apparently not even possible.
Let's see the first code:
The following code displays the value of n=10:
#include<iostream>
int main()
{
int n=10;
int*p=&n;
*p++;
std::cout<<n;
return 0;
}
The following code displays the value of n=11:
#include<iostream>
int main()
{
int n=10;
n++;
std::cout<<n
return 0;
}
p++ increments the pointer. You would need (*p)++ to increment the value.
Operator precedence.
The first case is parsed as *(p++); - first increment the address and then dereference. This does not modify any values.
The second case merely increments the value itself.
I'm new to programming. I was trying to get the sum of the equation added to the previous value when I noticed some strange behavior.
If I declare int result inside int main () then I get a blank answer, but if I declare int result outside int main () then I get these values: 6,11,16...91,96,101. It doesn't make sense to me since I have no other function.
Why does this happen?
#include<iostream>
using namespace std;
int main ()
{
int y =1;
int result;
while (result <100)
{
result = y +5;
cout << result << ",";
y = result;
}
}
Within a function, int result; declares a variable named result, but doesn't initialize it to any particular value. Until you assign it a value it could be anything, and the behavior when reading from it is undefined. Thus when you read its value in your while condition it could be anything; your loop may execute or it may not. You need to supply an initial value for result to make the behavior of your program well-defined:
int result = 0;
Unlike local variable, global variables are defined to be initialized to a default value when no initial value is explicitly provided, so when you read the value of result in your while condition, it is 0, and your loop executes.
newbie question here, can anybody provide an answer to why this loop is not terminating? I am refrencing the address of the variable, then de-referencing it and adding one to it. By my logic it should terminate by ~ the 10th step.
Thanks!
#include <iostream>
using namespace std;
int addtoi(int intern);
int main(){
for(int i = 0; i < 10;addtoi(i)){
}
}
int addtoi(int intern){
int *pt;
pt = &intern;
++*pt;
cout << *pt << "\t" << intern << "\n";
return 0;
}
Your loop is not terminating because any changes that you made to intern inside addtoi stay inside addtoi. The fact that you take a pointer there has no effect, because it's too late: you are taking a pointer of a local variable, which is a copy of i passed in.
You can fix this by passing in intern by reference, like this:
int addtoi(int& intern) {
intern++;
cout << intern << "\n";
return 0;
}
You do not need to take its address - simply modifying it will change the value of i inside the loop.
You don't change the value of i anywhere in your loop. You do pass its value to addtoi, but passing a value can't change the value of the variable whose value you took. So why would it ever end?
If you pass a reference or pointer to i to addtoi, then it can change i's value. But if you only pass the value itself, then it can only change the value passed to it, which has no effect on i's value.
What do you think would happen if you did this:
addtoi(1);
Do you think that constant 1 would somehow change? Same thing -- if you pass a value, you can't change anything in the caller since all you got was a value.
Consider:
int j = 1;
int i = 1;
addtoi(i);
addtoi(j);
addtoi(1);
These three calls all pass the same value to addtoi, 1. It makes no difference how you obtain that value if all you pass is the value.
The loop is not terminating because when you do addtoi(i), the value of i is send to the addtoi() function and not the original i variable. Its a classic example of pass by value.
Very simple question:
I was fiddling with basic C++ (being very new to programming) and I got into trouble while declaring a global variable to do some addition
#include <iostream>
int x,y;
int sum(int, int)
{
return x + y;
}
int main()
{
using namespace std;
cout << "The sum of 10 and 4 is: " << sum(10,4) << endl;
return 0;
}
Changing "int x,y;" to "int x,y = 0" has the same result: The sum equates to 0.
Could someone explain this odd behavior? Thanks!
Your function always returns the sum of global variables x and y, which are always 0. x and y are implicitly set to zero at the program startup. You never change their values, so they remain zero forever. The sum of two zeros is zero, no surprise here.
You pass 10 and 4 to your function, but the function itself completely ignores what is passed to it, i.e. it ignores its parameters (they are not even named). It always sums global x and y, which are always 0.
If you want your function to sum its arguments, you have to name the function parameters and use them
int sum(int a, int b)
{
return a + b;
}
And now you don't need any global variables at all. (main remains as is.)
Alternatively, if you so desire, you can get rid of the parameters completely and sum the global variables instead
int x,y;
int sum()
{
return x + y;
}
but in this case you will have to pass the values to sum through those global variables, not as function arguments
int main()
{
using namespace std;
x = 10;
y = 4;
cout << "The sum of 10 and 4 is: " << sum() << endl;
return 0;
}
This latter approach is here just for illustrative purposes. It is definitely not a good programming practice.
What you have in your code is a weird disconnected hybrid of these two approaches, which can't possibly work.
In order to fix the issue, the thing requires changing is the sum function.
int sum(int a, int b){
return a+b; //a,b here are referring to the inputs, while what you did was referring to the global variable..
}
Besides, try not to use global variables, usually you would end up with lots of troubles.
Another thing, I don't think your way of defining a function is correct. The inputs have to look like this instead:
int sum(int a, int b)
Unless you wanna declare the function first and provide the actual implementation later, you are not suppose to miss the name of the inputs!
when you are just globally declare the variables x,y ,they implicitly set to zero value.in your function definition,you are just giving the datantype of args, not the args names.so when you returning the sum of x,y ,it returns zero.and the value passed by the main function goes nowhere.
your program must look like this
#include<iostream>
int x,y;
int sum(x,y)
{
return x+y;
}
int main()
{
int v,a,b;
cout<<"values of a and b";
cin>>a>>b;
v=sum(a,b)
cout<<"their sum is"<<v;
}
when you explicitly define the value in second case
i.e int x,y=0;
you are just explicitly giving the value of value y to 0 while the x implicitly remains 0 and since you are not giving the args name,the ultimately result return biy the function is zero,
Seems that you only need x and y inside your add function, so make them local to the function. There is no reason to make them global. Follow the "least accessibility" idiom to prevent other parts of your program from mistakenedly modifying variables.
You might need a global variable supposed you want to define a well known parameter that every function needs to know and yet modifiable during run time. If you want it fixed, then a global constant would be more proper.
Hope that helps.