Question

Write a C function to compute the following output (int) based on two input (int) variables:...

Write a C function to compute the following output (int) based on two input (int) variables:

output = a*a + b*b - 2*a*b

If the value of output is '0', then return -1 instead.

Call the C function 4 times from main() with values of 'a' and 'b' as follows and print output values for each case.

a = 3, b = 4

a = 1, b = 1

a = -2 b = 3

a = -4 b = -5

Homework Answers

Answer #1
#include <stdio.h>

int getResult(int a, int b){
    int result = a*a + b*b - 2*a*b;
    if(result==0){
        return -1;
    }
    else{
        return result;
    }
}

int main()
{
    printf("%d\n",getResult(3,4));
    printf("%d\n",getResult(1,1));
    printf("%d\n",getResult(-2,3));
    printf("%d\n",getResult(-4,-5));

    return 0;
}

Know the answer?
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
Similar Questions
How would I write this function in C programming language? Function header: int input(int *a, int...
How would I write this function in C programming language? Function header: int input(int *a, int *b, int *c) This function should attempt to input 3 integers from the keyboard and store them in the memory locations pointed to by a, b, and c. The input may not be successful in reading all three values. The function should return the number of values that were successfully read.
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function...
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function overloading. PSEUDOCODE: Declare necessary variables. Create a class with add(int,int) ,add(float,float) as member functions and necessary variable. add(int,int) is used to add two integer values. add(float,float) is used to add two float values. Using object call the required function with corresponding input. Display the output.
(C++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int...
Give the output of the program using Call-By-Reference: int i, a[3]; void f (int x, int y){ x = (x*y) mod 3; y = y – x; } main(){ i = 0; a[0] = 1; a[1] = 2; a[2] = 0; f(i, a[i]); print(“%d %d %d %d\n”, i, a[0], a[1], a[2]); f(a[i], a[i]); print(“%d %d %d\n”, a[0], a[1], a[2]); }
In C programming language write a function that takes an integer n as input and prints...
In C programming language write a function that takes an integer n as input and prints the following pattern on the screen: 1 (n times) 2 (n-1 times) .n (1 time) For example, if n was 5, the function should print 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of n. Use the following main function to test your code: int main() { int n, sn; cout << "Enter q to quit or an integer: "; while ( cin >> n ) { sn = sumofdigits(n); cout << "sumofdigits( " << n << " ) = " << sn; cout << "\nEnter q to quit or an integer: "; }...
How would I write the following program in C programming language? Function header: int sumsort(int *a,...
How would I write the following program in C programming language? Function header: int sumsort(int *a, int *b, int *c) This function should arrange the 3 values in the memory locations pointed to by a, b, and c in ascending order and also return the sum of the contents of the memory locations a, b, and c.
Write a function that takes as its parameter two integer variables and an output stream varlable....
Write a function that takes as its parameter two integer variables and an output stream varlable. The two integers correspond to the numerator, and denominatorThe function should check if the denominator is zero, then it should print out the message "Otherwise , it should calculate and print the quotient and the remainder to a file. c++
Write a C++ program to do the following: Declare and assign values to int variables x,...
Write a C++ program to do the following: Declare and assign values to int variables x, y Declare an int variable z; and store the sum of x and y in it Declare a pointer called pz and point it in the heap direction (using new) Store the z value in the heap location using the pz pointer Print the content of the pz pointer Print the pointer (the address)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT