Question

Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three...

Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three double variables as input. The function should move the value of the smallest variable into the first variable, the middle value into the middle variable, and the largest value into the third variable. Call your function from within your program using the arguments 7.8, 3.5, and 1.1 in order to demonstrate that it works (make sure your logic works for any order of largest, smallest, middle however). Comment your code, make sure it looks professional, and use meaningful variable names. (NOTE: This function does something similar to swap3.c--I recommend using it as a model.) For example: if I had three double variables as follows: x=7.8, y=3.5, z=1.1 then I would call moveEm like so: moveEm(&x, &y, &z); After moveEm() returned x would contain 1.1, y would contain 3.5, and z would contain 7.8.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


// function to move the value of the smallest variable into the first variable, the middle value
// into the middle variable, and the largest value into the third variable
void moveEm(double *a, double *b, double *c)
{
   int temp;

      // case 1
     if(*a > *b && *a > *c && *b > *c)
     {
          temp = *a;
          *a = *c;
          *c = temp;
     }

     //case 2
     else if(*a > *b && *a > *c && *b < *c)
     {
          temp = *a;
          *a = *b;
          *b = *c;
          *c = temp;
     }

     // case 3
     else if(*a < *b && *a < *c && *b > *c)
     {
          temp = *b;
          *b = *c;
          *c = temp;
     }
   
     // case 4
     else if(*a > *b && *a < *c && *b < *c)
     {
          temp = *a;
          *a = *b;
          *b = temp;
     }

     // case 5
     else if(*a < *b && *a > *c && *b > *c)
     {
          temp = *a;
          *a = *c;
          *c = *b;
          *b = temp;
     }
}


int main()
{
   double a,b,c,d;
   printf("Enter value of a: ");
   scanf("%lf",&a);
   printf("Enter value of b: ");
   scanf("%lf",&b);
   printf("Enter value of c: ");
   scanf("%lf",&c);

   printf("\n");

   moveEm(&a,&b,&c);
   printf("After swapping:\n");
   printf("a = %lf\n",a);
   printf("b = %lf\n",b);
   printf("c = %lf\n",c);


   return 0;
}


/*
output:

Enter value of a: 2
Enter value of b: 4
Enter value of c: 3

After swapping:
a = 2.000000
b = 3.000000
c = 4.000000


Enter value of a: 4
Enter value of b: 1
Enter value of c: 2

After swapping:
a = 1.000000
b = 2.000000
c = 4.000000


Enter value of a: 4
Enter value of b: 2
Enter value of c: 1

After swapping:
a = 1.000000
b = 2.000000
c = 4.000000


Enter value of a: 1
Enter value of b: 4
Enter value of c: 3

After swapping:
a = 1.000000
b = 3.000000
c = 4.000000


Enter value of a: 1
Enter value of b: 3
Enter value of c: 4

After swapping:
a = 1.000000
b = 3.000000
c = 4.000000


Enter value of a: 7.8
Enter value of b: 3.5
Enter value of c: 1.1

After swapping:
a = 1.100000
b = 3.500000
c = 7.000000


*/

Add a comment
Know the answer?
Add Answer to:
Write a C program that includes a function called moveEm() that accepts the ADDRESSES of three...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • I need help making my array into a function that can called by the main function...

    I need help making my array into a function that can called by the main function using my program. This is C programming int prime (int x) { int i; i = 2; while (i < x) { if (x % i == 0) return 0; i++; } return 1; } int main (void){ int n; scanf ("%d", &n); if (n < 1) { printf ("Error: at least one data value must be provided.\n"); return 1; } int a[n]; int...

  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Write a program with a function named isEqualArr that accepts a pair of arrays of integers...

    Write a program with a function named isEqualArr that accepts a pair of arrays of integers as parameters and returns true if the arrays contain the same elements in the same order. If the arrays are not the same length, your function should return false. For example, if the following arrays are declared: int[] arr1 = {10, 20, 30, 40, 50, 60}; int[] arr2 = {10, 20, 30, 40, 50, 60}; int[] arr3 = {20, 3, 50, 10, 68}; The...

  • 8. (10%) Consider the following JavaScript program: // The main program var x,y; function f10{ var...

    8. (10%) Consider the following JavaScript program: // The main program var x,y; function f10{ var y, z; function f20{ var x, Z, P; function f30{ var x, z, ; Assume that the execution of this program is in the following unit order: main calls f3, f3 calls f1, f1 calls 12. a) Assuming that static scoping is in effect, indicate which version of each of the following variables will be visible in f2 by filling in each blank with...

  • 2. a)Write the ARM ALP conditional code snippet for the following statements written in C-language. Assume R1 to Rn as06 variables Let R1, R2, R3 contain the starting addresses of arrays X, Y and Z r...

    2. a)Write the ARM ALP conditional code snippet for the following statements written in C-language. Assume R1 to Rn as06 variables Let R1, R2, R3 contain the starting addresses of arrays X, Y and Z respectively Use Register R4 for variable i. Display appropriate messages. While (i+10) else Z[i] XiYi; b)i Write a program to display a message "This is an examination Question" on the screen using 06 a function sub program Note the following Address of the string to...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”,...

    Write a program that contains a main function, a “double custom_operator_function(int op_type, double x, double y)”, a “double min(double,double)”, a double max(double x,double y), and a double pow(double x, int y). The main function is used to call the custom operator function. Next, the custom operator function calls the min/max/pow functions based on the op_type. From the main, call the custom operator function with the three inputs 1/-3/5, 2/4/5, and 3/4/5. Print the result of each input to screen. i)...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT