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

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    11

    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. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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. #3
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    You need to code all this inside main function.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by smokeyangel View Post
    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. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by fry8 View Post
    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. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    11
    Quote Originally Posted by smokeyangel View Post
    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. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Question: Can't compile with "end1"
    By Lillers in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2010, 04:23 AM
  2. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  3. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  4. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  5. Finding files question
    By geek@02 in forum Windows Programming
    Replies: 12
    Last Post: 09-02-2008, 03:31 AM