I have 2 questions on pointers. I am not sure how to answer.
1. Question deals with pointing the smallest of 3 integers to one pointer.
Given the integer variables x , y , and z , write a fragment of code that assigns the smallest of x , y , and z to another integer variable min .
Assume that all the variables have already been declared and that x , y , and z have been assigned values).
Here was my answer that I know is wrong.
[c]
int minimum (x,y,z)
{
if (x<y)
{
min=x;
}
else
{
min=y;
}
if (min<z)
{
min;
}
else
{
min=z;
}
return min;
}
int maximum (x,y,z)
{
if (x>y)
{
int max;
max=x;
}
else
{
max=y;
}
if (max>z)
{
max;
}
else
{
max=z;
}
return max;
}
[/c]
2. I am having trouble "invoking a function.
Here is my question and I have to be honest I do not know how to answer.
eroIt is a function that takes one argument and returns no value. The argument is a pointer to int . The function stores the value 0 back into the variable pointed to by the argument.
x is an int variable that has been declared. Write a statement that sets the value stored in x to zero by invoking the function zeroIt .
void zeroIt(int* p) {
*p = 0;
}
Finding the min/max values can be a lot easier, too:
int maximum ( int x, int y, int z) {
int max = 0;
if ( x > max) max = x;
if ( y > max) max = y;
if ( z > max) max = z;
return max;
}
The minimum is the part I'll leave to you so that you at least can lear a bit from your piece of homework :o)