Solved

# pointers and invoking functions???

Posted on 2004-11-23
1,855 Views
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 .

0
Question by:jenmaz

LVL 86

Expert Comment

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 Comment

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

LVL 86

Assisted Solution

>>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

LVL 45

Accepted Solution

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

## Featured Post

### Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.