Question:
Compile a function, return the difference between the maximum and minimum values in a double array, and test the function in a simple program.
It's easy to have no sense about the question, but it's just a bit confusing, because the answer is
The code is as follows: |
Copy code |
Printf ("The gap between max and min is: % gn", gap (array, WIDTH )); |
What is % g? I did not record the notes in my notes-specifiers, but I did not go into the chapters in the book for details. I just classified it as a floating point.
Result output:
2.6
9.2
The dif between max and min is: 6.6
There is no extra 0, and it looks quite comfortable.
So, I understood % g, which is a good thing.
The full version I wrote:
The code is as follows: |
Copy code |
# Include <stdio. h> # Define WIDTH 6 Float dif (double [], int num ); Int main (void) { Double array [] = {4.3, 5.3, 2.6, 9.2, 2.8, 3.6 }; Printf ("The dif between max and min is: % g", dif (array, WIDTH )); Return 0; } Float dif (double array [], int num) { Int I; Float max, min; For (I = 0, max = * array, min = * array; I <num; I ++ ){ If (* (array + I)> max) Max = * (array + I ); If (* (array + I) <min) Min = * (array + I ); } Printf ("% gn", min ); Printf ("% gn", max ); Return max-min; } |