The design of the program, our first homework, was to take 4 integer values, add the 2 highest values together and subtract the least two and the square of this result. Finally, compare the two values together to make sure they are equal or not.
For example, if you were to enter: 20 10 60 40
You'll get
60 + 40 = 100
and
20 - 10 = 10 --> 10^2 = 100
So 100 == 100
I wrote my program and tested it for various values that all returned the correct results. My professor told me that my program failed for all 10 test materials, and he sent me the results that he received. The results do not match mine, and I do not know what is happening. I emailed him and he told me that one of my cycles has incorrect ratings. He's right, but I still get the right results, so ...?
Here is the code, any help would be appreciated!
#include <stdio.h> /* Complete the code for this program below */ int main() { int a, b, c, d, f, k, swap; int array_size = 4; int return_val; int sum, difference, square; int small_1, small_2, large_1, large_2; int array[array_size]; //Gather input //printf("Enter integer values for a, b, c and d.\n"); return_val = scanf("%d %d %d %d", &a, &b, &c, &d); //Validate input if (return_val != 4) { printf("INVALID INPUT\n"); } else { //Assign values to array array[0] = a; array[1] = b; array[2] = c; array[3] = d; //Sort array for (k = 0 ; k < ( array_size - 1 ); k++) { for (f = 0 ; f < array_size ; f++) { if (array[f] > array[f+1]) /* For decreasing order use < */ { swap = array[f]; array[f] = array[f+1]; array[f+1] = swap; } } } //Assign sorted values to new variables small_1 = array[0]; small_2 = array[1]; large_1 = array[2]; large_2 = array[3]; //Compute math sum = large_1 + large_2; difference = small_1 - small_2; square = difference * difference; //Compute logic if(sum == square) { printf("%d equals %d.\n", sum, square); } else { printf("%d does not equal %d.\n", sum, square); } return 0; } }