dmilicev

scramble_float_v1.c

May 16th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.09 KB | None | 0 0
  1. /*
  2.  
  3.     scramble_float_v1.c
  4.  
  5.     Task from Abdulwali Sahak:
  6.     https://web.facebook.com/100005322190041
  7.  
  8. Write a C program as per following instruction:
  9. - Main function has 3 float type variables.
  10. - Initialize 3 variables in main function.
  11. - You will pass 3 float variables to a function named Scramble.
  12. - The function will assign the value of 1st float to 2nd float, value of 2nd float to the 3rd
  13. float, and assign sum of 2nd float and 3rd float to 1st float.
  14. - Call the function in main function and value of 3 float variables must be changed by the
  15. function as per the instruction above.
  16. - Print the new values.
  17.  
  18.  
  19.     You can find all my C programs at Dragan Milicev's pastebin:
  20.  
  21.     https://pastebin.com/u/dmilicev
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26.  
  27. // The function will assign the value of 1st float to 2nd float, value of 2nd float to the 3rd
  28. // float, and assign sum of 2nd float and 3rd float to 1st float.
  29. // Arguments are passed to function by references (pointers, addresses of variables).
  30. // Because of that, in function their values are changed,
  31. // and after function, in main() their values are changed.
  32. void scramble( float *x1, float *x2, float *x3 )
  33. {
  34.     float temp;
  35.  
  36.     printf("\n function scramble() \n");
  37.  
  38.     temp = *x2;     // Before overwriting x2, store its value in temp.
  39.     *x2 = *x1;
  40.     *x3 = temp;
  41.     *x1 = *x2 + *x3;
  42. }
  43.  
  44. // Arguments are passed to function by values.
  45. // Because of that, in function their values are changed,
  46. // but after function, in main() their values aren't changed.
  47. void scramble_v1( float x1, float x2, float x3 )
  48. {
  49.     float temp;
  50.  
  51.     printf("\n function scramble_v1() \n");
  52.  
  53.     temp = x2;      // Before overwriting x2, store its value in temp.
  54.     x2 = x1;
  55.     x3 = temp;
  56.     x1 = x2 + x3;
  57. }
  58.  
  59. // Swap three numbers in cycle
  60. // Arguments are passed to function by references (pointers, addresses of variables).
  61. // Because of that, in function their values are changed,
  62. // and after function, in main() their values are changed.
  63. void scramble_v3( float *x1, float *x2, float *x3 )
  64. {
  65.     float temp1, temp2;
  66.  
  67.     printf("\n function scramble_v3() \n");
  68.  
  69.     temp1 = *x2;    // Before overwriting x2, store its value in temp1.
  70.     *x2 = *x1;
  71.     temp2 = *x3;    // Before overwriting x3, store its value in temp2.
  72.     *x3 = temp1;
  73.     *x1 = temp1 + temp2;
  74. }
  75.  
  76. int main(void)
  77. {
  78.     float x1, x2, x3;   // Main function has 3 float type variables.
  79.  
  80.     x1 = 10.0;          // Initialize 3 variables in main function.
  81.     x2 = 20.0;
  82.     x3 = 35.0;
  83.  
  84.     printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
  85.  
  86.     scramble_v1( x1, x2, x3 );
  87.  
  88.     printf("\n After:  x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
  89.  
  90.  
  91.     printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
  92.  
  93.     scramble( &x1, &x2, &x3 );
  94.  
  95.     printf("\n After:  x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
  96.  
  97.  
  98.     x1 = 10.0;      // reset values, because they are changed above
  99.     x2 = 20.0;
  100.     x3 = 35.0;
  101.  
  102.     printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
  103.  
  104.     scramble_v3( &x1, &x2, &x3 );
  105.  
  106.     printf("\n After:  x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
  107.  
  108.  
  109.     return 0;
  110.  
  111. } // main()
Add Comment
Please, Sign In to add comment