Implement a function that returns an integer that is one greater than the intege
ID: 3923767 • Letter: I
Question
Implement a function that returns an integer that is one greater than the integer passed into it. The function has the following signature. Write test code with assertions to verify that your function is correctly implemented. For example, the following tests that the function works correctly for several different values. You need to include the cassert header in your code in order to use the assert function. Implement a function that increases the value of an integer passed into it by 1 The function has the following signature. Write test code with assertions to verify that you implemented the function correctly. Unlike the previous exercise, you can not call the addOne function with a constant because the function s argument is a reference. Write a function named isEven that takes an integer argument n and returns a boolean value. Implement the function so that it returns true when n is even and false when n is odd.Explanation / Answer
Exercise 1:
#include<stdio.h>
#include <assert.h>
int main()
{
int addOne(int k);
//If expression evaluates to TRUE, assert() does nothing.
//If expression evaluates to FALSE, assert() displays an error message on stderr
//and aborts program execution.
assert(addOne(-3) == -2);
printf(" Result: %d", addOne(-3));
assert(addOne(0) == 1);
printf(" Result: %d", addOne(0));
assert(addOne(1) == 2);
printf(" Result: %d", addOne(1));
}
int addOne(int k)
{
return (++k);
}
Output:
Result: -2
Result: 1
Result: 2
Exercise 2:
#include<stdio.h>
#include <assert.h>
void addOne(int *k)
{
(++*(k));
}
int main()
{
//If expression evaluates to TRUE, assert() does nothing.
//If expression evaluates to FALSE, assert() displays an error message on stderr
//and aborts program execution.
int x = 10;
//Address of X is passed
addOne(&x);
assert((x) == 11);
printf(" Result: %d", x);
x = 20;
addOne(&x);
assert((x) == 21);
printf(" Result: %d", x);
}
Output:
Result: 11
Result: 21
Exercise 3:
#include<stdio.h>
#include <assert.h>
#include<stdbool.h>
bool isEven(int k)
{
if(k%2==0)
return (true);
else
return (false);
}
int main()
{
//If expression evaluates to TRUE, assert() does nothing.
//If expression evaluates to FALSE, assert() displays an error message on stderr
//and aborts program execution.
assert(isEven(12) == true);
printf(" Result: %d", isEven(12));
assert(isEven(6) == true);
printf(" Result: %d", isEven(6));
assert(isEven(5) == false);
printf(" Result: %d", isEven(5));
}
Output:
Result: 1
Result: 1
Result: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.