Thread: Output error

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    6

    Output error

    Tho gram supposed to produce Min and Max values of entered numbers. It gives Max but it always shows 0 as minimum value. Does anyone know what happened?

    Code:
    //largeSmall.c
    
    
    #include <stdio.h>
    
    
    int max, min;
    
    
    int compare(int num1, int num2, int num3, int num4);
    
    
    int main(void) {
    	int i, ent1, ent2, ent3, ent4;
    	FILE *csci;
    
    
    	for (i = 1; i <= 4; ++i) {
    
    
    		csci = fopen("csci.txt", "w");
    
    
    		printf("Enter four numbers: ");
    		scanf("%d %d %d %d", &ent1, &ent2, &ent3, &ent4);
    
    
    		compare(ent1, ent3, ent2, ent4);
    		printf("The biggest number: %d. The smallest number is: %d \n", max, min);
    	}
    	fclose(csci);
    
    
    }
    
    
    int compare(int num1, int num2, int num3, int num4) {
    
    
    	if (num1 > num2 && num1 > num3 && num1 > num4) {
    		max = num1;
    		return max;
    
    
    	}
    	else if (num2 > num1 && num2 > num3 && num2 > num4) {
    		max = num2;
    		return max;
    	}
    
    
    	else if (num3 > num1 && num3 > num2 && num3 > num4) {
    		max = num3;
    		return max;
    	}
    	else if (num4 > num1 && num4 > num2 && num4 > num3) {
    		max = num4;
    		return max;
    	}
    
    
    	else if (num1 < num2 && num1 < num3 && num1 < num4) {
    		min = num1;
    		return min;
    	}
    	else if (num2 < num3 && num2 < num1 && num2 < num4) {
    		min = num2;
    		return min;
    	}
    	else if (num3 < num2 && num3 < num1 && num3 < num4) {
    		min = num3;
    		return min;
    	}
    	else if (num4 < num2 && num4 < num3 && num1 < num1) {
    		min = num4;
    		return min;
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your last 4 test for minimum are all 'else if'. So, they will only be checked if no maximum is found.
    Solution: delete the word 'else' on line 62, so that the first minimum test will become a normal 'if'-test.
    Other have classes, we are class

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    6

    0 output

    Quote Originally Posted by WoodSTokk View Post
    Your last 4 test for minimum are all 'else if'. So, they will only be checked if no maximum is found.
    Solution: delete the word 'else' on line 62, so that the first minimum test will become a normal 'if'-test.
    Code:
    //largeSmall.c
    
    
    
    
    #include <stdio.h>
    
    
    int max, min;
    int compare(int num1, int num2, int num3, int num4);
    
    
    
    
    int main(void) {
    	int i, ent1, ent2, ent3, ent4;
    	FILE *csci;
    	for (i = 1; i <= 4; ++i) {
    		csci = fopen("csci.txt", "w");
    		printf("Enter four numbers: ");
    		scanf("%d %d %d %d", &ent1, &ent2, &ent3, &ent4);
    		compare(ent1, ent3, ent2, ent4);
    		printf("The biggest number: %d. The smallest number is: %d \n", max, min);
    	}
    	fclose(csci);
    }
    
    
    
    
    int compare(int num1, int num2, int num3, int num4) {
    	if (num1 > num2 && num1 > num3 && num1 > num4) {
    		max = num1;
    		return max;
    	}
    	else if (num2 > num1 && num2 > num3 && num2 > num4) {
    		max = num2;
    		return max;
    	}
    
    
    	else if (num3 > num1 && num3 > num2 && num3 > num4) {
    		max = num3;
    		return max;
    	}
    	else if (num4 > num1 && num4 > num2 && num4 > num3) {
    		max = num4;
    		return max;
    	}
    
    
    
    
    	if (num1 < num2 && num1 < num3 && num1 < num4) {
    		min = num1;
    		return min;
    	}
    	else if (num2 < num3 && num2 < num1 && num2 < num4) {
    		min = num2;
    		return min;
    	}
    	else if (num3 < num2 && num3 < num1 && num3 < num4) {
    		min = num3;
    		return min;
    	}
    	else if (num4 < num2 && num4 < num3 && num1 < num1) {
    		min = num4;
    		return min;
    	}
    }
    Still gives me that 0 in the output

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Sorry, i havn't see that this is a function.
    A function can return only one value.
    If the execution hit a 'return', then it go back to the caller.
    In your case, if the maximum value is found the function hit a return.

    You can make 2 functions, one for maximum and one for minimum,
    or you give the function two pointers and they assign the founded values to that pointer.
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Sep 2015
    Posts
    6
    Thanks for the answer! I will make 2 functions since I do not know how to work with pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in output....
    By shivagonal in forum C Programming
    Replies: 36
    Last Post: 04-10-2015, 04:52 AM
  2. Output error
    By juice in forum C++ Programming
    Replies: 21
    Last Post: 10-02-2011, 09:01 PM
  3. sprintf output error
    By Ji33my in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2010, 10:53 PM
  4. rcp error output question
    By cristane in forum Linux Programming
    Replies: 1
    Last Post: 07-18-2005, 09:47 PM
  5. Error End of OutPut
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 07-01-2005, 08:43 AM

Tags for this Thread