C programming question Here is the question and then I\'ll put my code to check
ID: 3882164 • Letter: C
Question
C programming question
Here is the question and then I'll put my code to check what is the error
Problem B: Write a function that has two integer parameters and returns an integer that equals the argument that is smaller in absolute value. If the two arguments have the same absolute value but different sign, the function should return the positive one. If the two arguments are identical, their (common) value should be returned. Write a mainprogram that tests your function with enough test cases to give you confidence in the correctness of your function. Use assertions and, optionally, calls to printf. The function prototype should be
int smallerabsval(int x, int y);
#include <assert.h>
#include <stdio.h>
int abs(int x);
int smallerabsval( int x, int y){
x = 3 , y = -5
}
{
if(x==y)
return x;
int absx = abs(x);
int absy=abs(y);
if(absx > abs y) return y;
else if(absy < absx) return x;
return absx;}
int main(){
int err = smallerabdval(absx, absy)
assert(absx==3 && absy==5 );
int err = smallerabsval(absx, absy)
}
Explanation / Answer
#include <assert.h>
#include <stdio.h>
int abs(int x);
int smallerabsval(int x, int y);
int smallerabsval(int x, int y)
{
if (x == y)
return x;
int absx = abs(x);
int absy = abs(y);
if (absx > absy)
return y;
else if (absy > absx)
return x;
else {
if(x > y) {
return x;
} else {
return y;
}
}
return absx;
}
int main()
{
assert(smallerabsval(4, 5) == 4);
assert(smallerabsval(5, 4) == 4);
assert(smallerabsval(-4, 5) == -4);
assert(smallerabsval(4, -5) == 4);
assert(smallerabsval(-4, -4) == -4);
assert(smallerabsval(4, -4) == 4);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.