# Thread: noob c ++ question. finding a minimum value

1. ## noob c ++ question. finding a minimum value

Here is the question:

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

---------------------------------------------------------------------------------------------------------------

This is the code I have, but when sumbit it the computer says that it is wrong. Could someone please tell me what is wrong with the program. Thanks.

Code:
```

min ( x , y , z ) {
min = x ;

if(x < y  && x < z)

{
min = x;
return min;
}

if((y < x) && (y < z)){

min = y;
return min;

}

if((z < x) && (z < y)){

min = z ;

return min;

}```

2. It looks ok computation-wise.

but when sumbit it the computer says that it is wrong.
The code snipped you provided won't compile because:

- You have mismatched braces - you need another close brace to close the function definition (after the last return)
- You need to provide argument types and return types in your specification of min().

Code:
`min ( x , y , z ) {`
This is not legal - should be int min(int x, int y, int z) {

Also, you are using min without declaring it -- "int min = x;" is the appropriate line. I'm aware that you said:

Assume that all the variables have already been declared and that x , y , and z have been assigned values).
Which might mean you have a global min so maybe you don't need to declare it. I still would though -- I'm not keen on having computational functions side-effect global variables (for various reasons that mostly apply to larger systems, but it's good practice).

3. You need to code all this inside main function.

4. Originally Posted by smokeyangel
It looks ok computation-wise.

The code snipped you provided won't compile because:

- You have mismatched braces - you need another close brace to close the function definition (after the last return)
- You need to provide argument types and return types in your specification of min().

Code:
`min ( x , y , z ) {`
This is not legal - should be int min(int x, int y, int z) {

Also, you are using min without declaring it -- "int min = x;" is the appropriate line. I'm aware that you said:

Which might mean you have a global min so maybe you don't need to declare it. I still would though -- I'm not keen on having computational functions side-effect global variables (for various reasons that mostly apply to larger systems, but it's good practice).

I tried using int min (int x, int y, int z) and it said I didn't need to declare/cast anything so it's not that, but I can't figure out why it isn't working.

5. Originally Posted by fry8
I tried using int min (int x, int y, int z) and it said I didn't need to declare/cast anything so it's not that, but I can't figure out why it isn't working.
What is "it"? Some automated checker that verifies your code is correct? Or just the compiler?

I just compiled your code, and it worked fine (with the modifications I mentioned). But I am not sure if this actually fits with what you're supposed to do with your assignment. If you have initialised global variables x,y,z,min already then the function min() shouldn't take any arguments (just use the globals directly).

Here's what I have:

Code:
```int min (int x , int y , int z ) {
int min = x ;

if(x < y  && x < z)

{
min = x;
return min;
}

if((y < x) && (y < z)){

min = y;
return min;

}

if((z < x) && (z < y)){

min = z ;

return min;

}

}

int main(void)
{
int mini = min(1,2,3);
mini = min(2,1,3);
mini = min(3,2,1);
}```

6. Originally Posted by smokeyangel
What is "it"? Some automated checker that verifies your code is correct? Or just the compiler?

I just compiled your code, and it worked fine (with the modifications I mentioned). But I am not sure if this actually fits with what you're supposed to do with your assignment. If you have initialised global variables x,y,z,min already then the function min() shouldn't take any arguments (just use the globals directly).

Here's what I have:

Code:
```int min (int x , int y , int z ) {
int min = x ;

if(x < y  && x < z)

{
min = x;
return min;
}

if((y < x) && (y < z)){

min = y;
return min;

}

if((z < x) && (z < y)){

min = z ;

return min;

}

}

int main(void)
{
int mini = min(1,2,3);
mini = min(2,1,3);
mini = min(3,2,1);
}```
its a computer checker and everything I submitted in my first post is all the information I have

7. How confusing! I'm guessing then that the computer has a bunch of test data for x,y,z and runs a few tests on the program then checks the value of min. Since you weren't told to implement this particular interface it makes sense that the computer doesn't understand it.

Can you try:

Code:
```extern int x, y, z, min;

void minf (void) {
min = x ;

if(x < y  && x < z)

{
min = x;
}

if((y < x) && (y < z)){

min = y;

}

if((z < x) && (z < y)){

min = z ;

}

}

int main(void)
{
minf();
}```
Functionally equivalent to putting it all in main().