http://www.cplusplus.com/reference/clib ... io/printf/%d is for decimal, %i don't exist.
I will edit this post soon to help him.
Edit #1: As Xian Nox pointed out, the goal of the function is to change the original value of x and y. When we use function, in C, argument, when passed to function, are not the original one(sorry, don't know how to explain that in english). All you need to know, is that once inside the function, you only have the copy of the original variable.
so if I do this:
- Code: Select all
void function(int x);
int main(){
int a = 6;
function(a);
return a;
}
void function(int x){
x = 8;
/*x = a copy of a, when modifying x, you only modify the copy. Totally useless function. A function can't change his parameter. The only way a function can change something outside his scope is either if we use global variable (we avoid global variable as much as possible), or if we use pointer.*/
}
This also explain why function like scanf (its not recommended to use scanf in real situation, there are other alternative more bug proof), it need the address of the variable, else, it can't modify the value. Pointer are a powerful tool if well used. I will try to find other exercise that involve pointer.
Also, we almost never printf from function, except if its the goal a function, or if its for debug purpose, or error checking.(But we prefer to handle error to use return code, so the programmer that use the function handle the error the way he want.) Example of function that could printf is a iterative function for fibonacci number. We don't want to make 1 call for each number and printf the result, but just calling the function one time, and let the job be done. But we could also just store all the number in an array, and print the result outside the function.
Also, about error handling for Factorial, normally, we don't do that, its slower if we check every time for negative number. But its an exercise to also practice using the return, normally, such error handling are made before the call to function, but I find its a good idea to practice putting error handling inside function, using well the return for error handling. I find so many time function that should handle error by returning something other than 0 or void, but they don't. Normally, recursive function don't do such error handling because it will take so much error handling at each call.
But like I said, its a good thing to practice error handling, it will prevent so much bug later. So for the exercise, put the error handling, but for when you will use it, do the error handling outside the function for more speed.
But for the iterative one, I think we should always put a error handling inside the function, at almost no speed cost, its a good idea.
Also, your iterative one look correct, just need the error handling. Its also possible to do the iterative one with just 'i' and x. No 'a' variable. I let you figure out how, but it still good. Just find a way to handle error inside the function for negative value, and in your main, do the check of the return value.
Hints: To do it with only i and x, you will have to use operator like variable++, variable--, ++variable, --variable. But its only for educational purpose I say that you could do it with just 2 variable. In real situation, we try to avoid such operator, except if they are alone on their line, but if used with other operation, we avoid them. For example, in a for loop: for(i = 1;;i++) we know that i++ can't do nothing wrong, but if we do something like this: a = a++ + 5 - 5*--a, what will be the result? Normally, we don't mix these thing with other, to try to prevent error related to priority of operation. These operator may have different priority in operation depending compiler.
In something like this:
x = 4;
int a = ++x *7;
What happen really? Does x is incremented, then multiplied by 7 then assigned to a? Or does x is multiplied by 7, then incremented, then the result assigned to a? It all depend, different compiler will produce different result. does x++ have priority on *, it depend on compiler. All we know about this is that x will be incremented before the = operator. Nothing more.
So don't use those operator inside equation, only alone.
EDIT: Forgot what I said, we can do it using just 2 variable without abusing increment and decrement operator. But for that, we just need to replace the increment operaror with a decrement operator in the for loop header. Was actually playing BF3 when i said we could do it with increment and decrement operator, i was not fully thinking, i dont even remember how i came to the conclusion we could do it with those operator... Its why distraction while programming is a bad thing.
For the exercise you didn't understand, suppose we have an array, with 2 value: int a[2];
a[0] = 5;
a[1] = 53564362;
Now, if we want to exchange a[0] with a[1]...
For the other one, suppose we have 2 array.
We want to exchange the content of x[2] with y[7]