can this be in programming language C, thanks Suppose you want to implement a si
ID: 3750890 • Letter: C
Question
can this be in programming language C, thanks
Suppose you want to implement a simple library called numbers that will export a lot of functions to find out about numbers (e.g.. positive, negative, even, odd etc.) For the time being, suppose your library exports only two functions int even (int x) and int positive(int x) . These functions will return 1 if the given number is even and positive, respectively; otherwise return 0. Define an interface named numbers.h exporting the above two functions (give the boilerplate lines (#ifnde, #de fine, #endif)) and implement numbers.c /**numbers.h */ /*numbers.c / What is the purpose of the boilerplate lines in the above file? Now suppose you want to implement an application that asks user to first enter a number. It then prints " this is a positive/negative even/odd number" on the screen. Implement myapp.c using the functions from the above library. myapp.c ****/ * myapp.c cont'd ***/Explanation / Answer
Boiler plate code protects against multiple inclusions of the same header file.
vagrant@ubuntu-xenial ~/c/numbers> cat numbers.h
#ifndef _NUMBERS_H_
#define _NUMBERS_H_
int even(int x);
int positive(int x);
#endif
vagrant@ubuntu-xenial ~/c/numbers> cat numbers.c
#include <numbers.h>
int even(int x)
{
if (x % 2 == 0)
return 1;
return 0;
}
int positive(int x)
{
if (x >= 0)
return 1;
return 0;
}
vagrant@ubuntu-xenial ~/c/numbers> cat myapp.c
#include <stdio.h>
#include <numbers.h>
int main()
{
int num;
int pos, eve;
char *oppos, *opeve;
printf("Please enter a number: ");
scanf("%d", &num);
pos = positive(num);
eve = even(num);
if (pos) {
oppos = "positive";
} else {
oppos = "negative";
}
if (eve) {
opeve = "even";
} else {
opeve = "odd";
}
printf("%d is %s and %s ", num, oppos, opeve);
}
vagrant@ubuntu-xenial ~/c/numbers> cat Makefile
CC=gcc
all:
$(CC) -I. numbers.c myapp.c -o myapp
clean:
rm -f myapp
Put all of the files in the same directory and issue make to generate the binary. Sample Output -
vagrant@ubuntu-xenial ~/c/numbers> make
gcc -I. numbers.c myapp.c -o myapp
vagrant@ubuntu-xenial ~/c/numbers> ./myapp
Please enter a number: 4
4 is positive and even
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.