Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- scramble_float_v1.c
- Task from Abdulwali Sahak:
- https://web.facebook.com/100005322190041
- Write a C program as per following instruction:
- - Main function has 3 float type variables.
- - Initialize 3 variables in main function.
- - You will pass 3 float variables to a function named Scramble.
- - The function will assign the value of 1st float to 2nd float, value of 2nd float to the 3rd
- float, and assign sum of 2nd float and 3rd float to 1st float.
- - Call the function in main function and value of 3 float variables must be changed by the
- function as per the instruction above.
- - Print the new values.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // The function will assign the value of 1st float to 2nd float, value of 2nd float to the 3rd
- // float, and assign sum of 2nd float and 3rd float to 1st float.
- // Arguments are passed to function by references (pointers, addresses of variables).
- // Because of that, in function their values are changed,
- // and after function, in main() their values are changed.
- void scramble( float *x1, float *x2, float *x3 )
- {
- float temp;
- printf("\n function scramble() \n");
- temp = *x2; // Before overwriting x2, store its value in temp.
- *x2 = *x1;
- *x3 = temp;
- *x1 = *x2 + *x3;
- }
- // Arguments are passed to function by values.
- // Because of that, in function their values are changed,
- // but after function, in main() their values aren't changed.
- void scramble_v1( float x1, float x2, float x3 )
- {
- float temp;
- printf("\n function scramble_v1() \n");
- temp = x2; // Before overwriting x2, store its value in temp.
- x2 = x1;
- x3 = temp;
- x1 = x2 + x3;
- }
- // Swap three numbers in cycle
- // Arguments are passed to function by references (pointers, addresses of variables).
- // Because of that, in function their values are changed,
- // and after function, in main() their values are changed.
- void scramble_v3( float *x1, float *x2, float *x3 )
- {
- float temp1, temp2;
- printf("\n function scramble_v3() \n");
- temp1 = *x2; // Before overwriting x2, store its value in temp1.
- *x2 = *x1;
- temp2 = *x3; // Before overwriting x3, store its value in temp2.
- *x3 = temp1;
- *x1 = temp1 + temp2;
- }
- int main(void)
- {
- float x1, x2, x3; // Main function has 3 float type variables.
- x1 = 10.0; // Initialize 3 variables in main function.
- x2 = 20.0;
- x3 = 35.0;
- printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
- scramble_v1( x1, x2, x3 );
- printf("\n After: x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
- printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
- scramble( &x1, &x2, &x3 );
- printf("\n After: x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
- x1 = 10.0; // reset values, because they are changed above
- x2 = 20.0;
- x3 = 35.0;
- printf("\n Before: x1 = %f \t x2 = %f \t x3 = %f \n", x1, x2, x3 );
- scramble_v3( &x1, &x2, &x3 );
- printf("\n After: x1 = %f \t x2 = %f \t x3 = %f \n\n", x1, x2, x3 );
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment