Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting gives inconsistent results, due to loop bound
    primarykey
    data
    text
    <p>Program Design, our first homework assignment was to take 4 integer values, add the 2 highest together and subtract the lowest two and square that result. Finally, compare the 2 values together to see if they are equal or not. </p> <p>For example, if you were to enter: <code>20 10 60 40</code> </p> <p>You'd get </p> <pre><code>60 + 40 = 100 </code></pre> <p>and </p> <pre><code>20 - 10 = 10 --&gt; 10^2 = 100 </code></pre> <p>So, <code>100 == 100</code></p> <p>I wrote my program and tested it for various values which all returned correct results. My professor told me my program failed for all 10 test inputs and he sent me the results he got. The results he got aren't the same as mine, and I don't know what's going on. I emailed him, and he told me one of my for loops has incorrect bounds. He's right, but I still get the right results, so...?</p> <p>Here's the code, any help would be appreciated!</p> <pre><code>/* // Author: Jesse W // Assignment 1 // Desciption: // This program inputs four integer numbers a, b, c and d and // determines if the sum of the two largest numbers is the same // as the squared difference of the two smallest numbers */ #include &lt;stdio.h&gt; /* 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", &amp;a, &amp;b, &amp;c, &amp;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 &lt; ( array_size - 1 ); k++) { for (f = 0 ; f &lt; array_size ; f++) { if (array[f] &gt; array[f+1]) /* For decreasing order use &lt; */ { 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload