Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the code for a 6-bit adder subtractor combination. Correct any bugs in th

ID: 3720892 • Letter: M

Question

Modify the code for a 6-bit adder subtractor combination. Correct any bugs in the reference code. • Use different files for different C functions, also develop your own header file. • Compile and run your files. • Validate your code by testing it against the following 2 examples: 10+22 and 10-22. Detail your explanations

#include <stdio.h>
#include <string.h>
#define size_device 4

void bin_convert(int num, int *bin_num[]);
void full_adder_sub(int *a[], int *b[], int s, int *Sum[], int *pcout3, int *pcout4);
void one_bit_add_sub(int x, int y, int c, int *pcout, int *psum);
void main(){

int c3, c4, over_flow, s, x, y, i;
int X[size_device], Y[size_device], Sum[size_device];
char OP_TYPE[3];
char num_type[6];
// start !
printf("%s ", "is this ADD or SUB? (you need to type in either ADD or SUB) ");
scanf("%s", &OP_TYPE[0]);
printf("%s %s ", "Operation type is", OP_TYPE);

printf("%s ", "is this a signed or unsigned operation? (you need to type in either SIGNED or UNSIGN) ");
scanf("%s", &num_type[0]);
printf("%s %s ", "type of the data", num_type);

printf("%s ", "Input the two operands x followed by y, in x op_type y (lets limit both x and y within [0 7]):");
printf("%s ", "Input x");
scanf("%d", &x);
bin_convert(x, X);
printf("%3d %3d %3d %3d ", X[0], X[1], X[2], X[3]);
printf("%s ", "Input y");
scanf("%d", &y);
bin_convert(y, Y);
printf("%3d %3d %3d %3d ", Y[0], Y[1], Y[2], Y[3]);


if (OP_TYPE[0] == 'A')
s=0;
else if (OP_TYPE[0] == 'S')
s=1;

for(i=0; i<size_device;i++)
Sum[i]=0;

full_adder_sub(X, Y, s, Sum, &c3, &c4);

if (num_type[0] == 'U')
over_flow= c4;
else if (num_type[0] == 'S')
over_flow= c3 ^ c4;
//printf("%3d %3d ", c3, c4);
printf("%20s %3d %3d %3d %3d %19s %3d ", "the sum is: ", Sum[0], Sum[1], Sum[2], Sum[3], "the overflow is: ",over_flow);
}
void bin_convert(int num, int *bin_num[]) {

int i;
int size_bin_z=4;

for(i=0; i<size_device;i++)
{
bin_num[i]=0;
}

while (num != 0)
{
bin_num[size_bin_z-1] = (int) num % 2;
num = (int) num /2;
size_bin_z--;
}

}
void full_adder_sub(int *a[], int *b[], int s, int *Sum[], int *pcout3, int *pcout4) {
int i;
int size_bin_z=4;

int x, y, sum;
int cin_temp, cout;

cin_temp=s;
cout=*pcout4;

for(i=0; i<size_device;i++)
{
x=a[size_bin_z-1];
if(s == 1)
y=!b[size_bin_z-1];
else if (s ==0)
y=b[size_bin_z-1];
*pcout3=cin_temp;
one_bit_add_sub(x, y, cin_temp, &cout, &sum);
cin_temp=cout;
Sum[size_bin_z-1]=sum;
size_bin_z--;
}
*pcout4=cout;
}
void one_bit_add_sub(int x, int y, int cin, int *pcout, int *psum){
*psum= (x^y)^(cin);
*pcout= x&y | (x^y) & cin;
}

Explanation / Answer

Hi,
To separate the functions into files, we have to separate the declarations and definitons into differnt files like below
main.c file

#include <stdio.h>
#include <string.h>
#include "functions.h>
#define size_device 4

void main(){

int c3, c4, over_flow, s, x, y, i;
int X[size_device], Y[size_device], Sum[size_device];
char OP_TYPE[3];
char num_type[6];
// start !
printf("%s ", "is this ADD or SUB? (you need to type in either ADD or SUB) ");
scanf("%s", &OP_TYPE[0]);
printf("%s %s ", "Operation type is", OP_TYPE);

printf("%s ", "is this a signed or unsigned operation? (you need to type in either SIGNED or UNSIGN) ");
scanf("%s", &num_type[0]);
printf("%s %s ", "type of the data", num_type);

printf("%s ", "Input the two operands x followed by y, in x op_type y (lets limit both x and y within [0 7]):");
printf("%s ", "Input x");
scanf("%d", &x);
bin_convert(x, X);
printf("%3d %3d %3d %3d ", X[0], X[1], X[2], X[3]);
printf("%s ", "Input y");
scanf("%d", &y);
bin_convert(y, Y);
printf("%3d %3d %3d %3d ", Y[0], Y[1], Y[2], Y[3]);


if (OP_TYPE[0] == 'A')
s=0;
else if (OP_TYPE[0] == 'S')
s=1;

for(i=0; i<size_device;i++)
Sum[i]=0;

full_adder_sub(X, Y, s, Sum, &c3, &c4);

if (num_type[0] == 'U')
over_flow= c4;
else if (num_type[0] == 'S')
over_flow= c3 ^ c4;
//printf("%3d %3d ", c3, c4);
printf("%20s %3d %3d %3d %3d %19s %3d ", "the sum is: ", Sum[0], Sum[1], Sum[2], Sum[3], "the overflow is: ",over_flow);
}


function declarations are supposed to be in the header file as follows
functions.h file
void bin_convert(int num, int *bin_num[]);
void full_adder_sub(int *a[], int *b[], int s, int *Sum[], int *pcout3, int *pcout4);
void one_bit_add_sub(int x, int y, int c, int *pcout, int *psum);

and finally the definitions of functions in the functions.c file

void bin_convert(int num, int *bin_num[]) {

int i;
int size_bin_z=4;

for(i=0; i<size_device;i++)
{
bin_num[i]=0;
}

while (num != 0)
{
bin_num[size_bin_z-1] = (int) num % 2;
num = (int) num /2;
size_bin_z--;
}

}
void full_adder_sub(int *a[], int *b[], int s, int *Sum[], int *pcout3, int *pcout4) {
int i;
int size_bin_z=4;

int x, y, sum;
int cin_temp, cout;

cin_temp=s;
cout=*pcout4;

for(i=0; i<size_device;i++)
{
x=a[size_bin_z-1];
if(s == 1)
y=!b[size_bin_z-1];
else if (s ==0)
y=b[size_bin_z-1];
*pcout3=cin_temp;
one_bit_add_sub(x, y, cin_temp, &cout, &sum);
cin_temp=cout;
Sum[size_bin_z-1]=sum;
size_bin_z--;
}
*pcout4=cout;
}
void one_bit_add_sub(int x, int y, int cin, int *pcout, int *psum){
*psum= (x^y)^(cin);
*pcout= x&y | (x^y) & cin;
}


Thumbs up if this was helpful, otherwise let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote