C programming Assume the + operator is not available. Write a program (addition.
ID: 3889920 • Letter: C
Question
C programming
Assume the + operator is not available. Write a program (addition.c) that takes two numbers as input and display the addition of them. The program should include a function add (int n, int m) that will add two numbers using only recursion and the increment and decrement operators. Either of the two numbers can be zero, positive, or negative.
Hint: add(n, m)= add(++n, --m), if m is positive, and add(n, 0)=n.
This is the program
#include<stdio.h>
int main()
{
int sizeOfA;
int sizeOfB;
//declare arrays to hold the set A and B and initialize all elements to 0
int setA[10] = {0,0,0,0,0,0,0,0,0,0}, setB[10] = {0,0,0,0,0,0,0,0,0,0};
//declare arrays to hold the difference of A and B, A's complement, B's complement and initialize all elements to 0
int diff[10] = {0,0,0,0,0,0,0,0,0,0}, Acomplement[10] = {0,0,0,0,0,0,0,0,0,0}, Bcomplement[10] = {0,0,0,0,0,0,0,0,0,0};
int i, ele; //declare counter variables
//prompt and read the size of set A
printf("Please enter the number of elements in set A: ");
scanf("%d", &sizeOfA);
//prompt and read the elements in set A
printf(" Enter the numbers in set A: ");
for(i = 0; i < sizeOfA; i++)
{
//for every element read set the corresponding index value to 1
scanf("%d", &ele);
setA[ele] = 1;
}
//prompt and read the size of Set B
printf(" Please enter the number of elements in set B: ");
scanf("%d", &sizeOfB);
//prompt and read the elements in set B
printf(" Enter the numbers in set B: ");
for(i = 0; i < sizeOfB; i++)
{
//for every element read set the corresponding index value to 1
scanf("%d", &ele);
setB[ele] = 1;
}
//now compute A - B
for(i = 0; i < 10; i++)
{
//check if the value at a particular index is 1, it means that element is present in the set
if(setA[i] == 1)
{
//check if the corresponding element is present in the other set or not
if(setA[i] != setB[i])
{
//if not present add it to diff set
diff[i] = 1;
}
}
}
//print the result of A - B
printf(" The difference of set A and B is: ");
for(i = 0; i < 10; i++)
{
if(diff[i] == 1)
printf("%d ", i);
}
//now lets compute A's complement
for(i = 0; i < 10; i++)
{
//check if the value at a particular index is 0, it means that element is present in not the set
if(setA[i] == 0)
{
//if not present then make the corresponding value at the index i Acomplement array to 1
Acomplement[i] = 1;
}
}
//print the elements in A's complement
printf(" The complement of set A is: ");
for(i = 0; i < 10; i++)
{
if(Acomplement[i] == 1)
printf("%d ", i);
}
//now lets compute B's complement
for(i = 0; i < 10; i++)
{
//check if the value at a particular index is 0, it means that element is present in not the set
if(setB[i] == 0)
{
//if not present then make the corresponding value at the index i Bcomplement array to 1
Bcomplement[i] = 1;
}
}
//print the elements in A's complement
printf(" The complement of set B is: ");
for(i = 0; i < 10; i++)
{
if(Bcomplement[i] == 1)
printf("%d ", i);
}
return 0;
}
Explanation / Answer
addition.c:
int add (int n, int m) {
if (m == 0)
return n;
if (m > 0)
return add(++n, --m);
return (--n, ++m);
}
int main() {
int a, b;
printf("Enter 2 numbers to add: ");
scanf("%d %d",&a,&b);
printf("Addition of %d and %d is %d ", a, b, add(a, b));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.