• C

# pointers and invoking functions???

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 .

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
The latter one is quite easy to handle - you take the address of an 'int' and pass it to the function, e.g.

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)
0
Author Commented:
Hi there,

I attempted to answer both as you have answered me earlier today and this is not right.  The answer should call a function (which I am not sure how to do) and the other is not right because I am just suppose to find the min of the x, y z and assign min to the smallest. So this does not help me either.

Thanks any ways.

Jennifer
0
Commented:
>>The answer should call a function (which I am not sure how to do)

Well, you call the function like

int n = 42;

zeroIt(&n);

>>and the other is not right because I am just suppose to find the min of the x, y z and assign min to the smallest.

Well, I showed you the *method*. Since this clearly is your homework, you should invest some time in adapting that to finding the minimum. Hint: Don't set 'min' to '0' initially...

>>So this does not help me either.

0
Data Warehouse Architect / DBACommented:
Hi jenmaz,

Your functions to determine the minimum and maximum values are fine.  You should move the declaration for 'max' to the beginning of the function, but other than that, they should work fine.  (They are wordy, and will probably look better without all of the superfluous brackets, but they should work!)

Simply call them from another function.

Least = minimum (value1, value2, value3)

Most = maximum (value1, value2, value3)

Kent
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.