Question

In: Computer Science

Programming in C (not C++) Write the function definition for a function called CompareNum that takes...

Programming in C (not C++)
Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.

Solutions

Expert Solution

Function defnition and the complete program are given below.Detailed comments are included for better under standing the code.Screen shot of the code and output are also attached.if find any difficulty,feel free to ask in comment section.Please do upvote the answer.Thank you

Function definition

int compareNum(double num)
{
   //declaring double variable number
   double number;
   //prompt user to enter number
   printf("Enter a number: ");
   //reading number
   scanf("%f",&number);
   //if both are equal,return 0
   if(num==number)
       return 0;
   //if num is less than user enterd double,return -1
   else if(num<number)
       return -1;
   //if num is greater than user enterd double,return 1
   else
       return 1;
}

Complete program

#include<stdio.h>
//function prototype declaration
int compareNum(double);
int main(void)
{
   //declaring double variable d
   double n;
   //integer variable result
   int result;
   //prompt to enter double number
   printf("Enter a double number: ");
   //reading number
   scanf("%f",&n);
   //calling compareNum() function
   result=compareNum(n);
   //printing returned value
   printf("%d",result);
   return 0;  
}
int compareNum(double num)
{
   //declaring double variable number
   double number;
   //prompt user to enter number
   printf("Enter a number: ");
   //reading number
   scanf("%f",&number);
   //if both are equal,return 0
   if(num==number)
       return 0;
   //if num is less than user enterd double,return -1
   else if(num<number)
       return -1;
   //if num is greater than user enterd double,return 1
   else
       return 1;  
}

Screen shot of the code

Screen shot of the output


Related Solutions

Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT