[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1923
  • Last Modified:

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 .

0
jenmaz
Asked:
jenmaz
  • 2
2 Solutions
 
jkrCommented:
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
 
jenmazAuthor 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
 
jkrCommented:
>>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.

It would help you less if you wouldn't learn anything from your homework.
0
 
Kent OlsenData 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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now