+ 1

My code doesn't seem to run the function it calls. Please help.

This code takes a value from arr_values and multiplies it by 10 raised to the power in the corresponding position of arr_powers. The code is free from errors and warnings, but it doesn't display the correct values. Please correct me when you see the error. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> double scale(double x, int n); int main(){ double arr_values[] = { 1.0, 2.0, 3.0, 4.0 }; int arr_powers[] = { 4, 3, 2, -2 }; double x; int i, n; for (i = 0; i < 4; i++) { x = arr_values[i]; n = arr_powers[i]; scale(x, n); printf("%.2lf ", arr_values[i]); } return 0; } double scale(double x, int n) { double scale_factor; scale_factor = pow(10, n); return x * scale_factor; }

31st Jan 2021, 8:03 PM
Salma D. Elabsi
Salma D. Elabsi - avatar
3 Answers
31st Jan 2021, 8:21 PM
A͢J
A͢J - avatar
+ 3
because where you call the scale() function, you doesn't assign its return value (result): x = scale(x, n); printf("%.2lf %.2lf ", arr_values[i], x);
31st Jan 2021, 8:12 PM
visph
visph - avatar
0
@visph Thank you so much for your help! on a side note though the code now doesn't need to print arr_values[i], only x is needed.
31st Jan 2021, 8:24 PM
Salma D. Elabsi
Salma D. Elabsi - avatar