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...

  • USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write...

    USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √−1. You should represent a complex number here as two values of type double. You should name the variables real...

  • 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...

  • Write an ARM assembly language program to translate the following sequence of statements . Assume x...

    Write an ARM assembly language program to translate the following sequence of statements . Assume x and y are memory locations that store two unsigned integers. Use the following: x is in R1, y is in R2, and z in R3. Make sure that your program works for any value of x and y. if (x > 15) {     x = 1;     if (y > 15) {          y = 2;         } else {       y =...

  • 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...

  • C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and...

    C++ Write a function called reverseArray(char[] , int) that accepts in an array of chars and the size of the array.as parameters. The function should then create a new array that is the same size as the original array. The function should copy the elements from the first array into the second array in reverse order. So if the initial array is {'a' , 'b', 'c'} then the new array will be {'c', 'b', 'a'} Then in your main(), create...

  • 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...

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