Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have code for first part can you update it and include second part main.c #include #include #define MAX 8 void func_and(int a[], int b[],

image text in transcribedI have code for first part can you update it and include second part

main.c

#include #include #define MAX 8

void func_and(int a[], int b[], int result[]){

printf("The first number AND second binary yield: ");

for(int i = 0; i

printf("%d", result[i]); } printf(" "); }

void func_or(int a[], int b[], int result[]){ printf("The first number OR second binary yield: ");

for(int i = 0; i

printf("%d", result[i]); } printf(" "); }

void func_not(int a[], int result[]){

printf("Not operation of the enter number is : ");

for(int i = 0; i

printf("%d", result[i]); } printf(" "); }

void func_1s_comp(int a[], int result[]){

printf("The 1's Complement of the entered number is : ");

for(int i = 0; i

if(a[i] == 0) result[i] = 1; else result[i] = 0;

printf("%d", result[i]); } printf(" "); }

void func_2s_comp(int a[], int result[]){

printf("The 2's Complement of the entered number is : ");

int onescomplement[MAX], carry = 1;

for (int i = 0; i

// Finding twoscomplement in C for (int i = (MAX - 1); i >= 0; i--) { if (onescomplement[i] == 1 && carry == 1) result[i] = 0; else if (onescomplement[i] == 0 && carry == 1) { result[i] = 1; carry = 0; }

else result[i] = onescomplement[i]; printf("%d",result[i]);

} printf(" "); }

void func_2s_comp_star(int a[], int result[]){

printf("The 2's Complement of the entered number is : ");

int b[MAX], carry = 1;

for (int i = 0; i

for(int i = (MAX-1); i >= 0; i--){ if(i == (MAX-1)){ if(b[i] == 0){ b[i] = 1; } else { b[i] = 0; carry = 1; } } else { if(carry == 1 && b[i] == 0){ b[i] = 1; carry = 0; } else if (carry == 1 && b[i] == 1){ b[i] = 0; carry = 1; } } printf("%d",result[i]); } printf(" "); } int k = 0;

void input1(int a[]){

printf("Enter the binary number : ");

for (int i = 0; i

if (a[i] == 0 || a[i] == 1) continue; else { printf(" Error input provided"); k = 1;

return; } } }

void input2(int a[], int b[]){

printf("Enter the first binary number : ");

for (int i = 0; i

printf("x[ %d ] =", i); scanf("%d", &a[i]);

if (a[i] == 0 || a[i] == 1) continue; else { printf(" Error"); k = 1; return; } }

printf("Enter the second binary number : ");

for (int i = 0; i

printf("x[ %d ] =", i); scanf("%d", &b[i]);

if (b[i] == 0 || b[i] == 1) continue; else { printf(" Error"); k = 1;

return; } } }

int main() {

int choice = 1;

while(choice != 0){

printf(" 0) Exit"); printf(" 1) AND"); printf(" 2) OR"); printf(" 3) NOT"); printf(" 4) 1's Complement"); printf(" 5) 2's Complement"); printf(" 6) 2's Complement*"); printf(" Enter the command number : ");

scanf("%d",&choice);

int a[MAX]; int b[MAX]; int result[10];

switch(choice){

case 0: { printf(" Thank you !!!"); return 0; }

case 1:

input2(a,b);

if (k == 1) break; else { func_and(a,b,result); break; }

case 2:

input2(a,b);

if (k == 1) break; else { func_or(a,b,result); break; }

case 3:

input1(a); if (k == 1) break; else { func_not(a,result); break;

case 4:

input1(a);

if(k == 1) break; else { func_1s_comp(a, result); break; }

case 5:

input1(a);

if (k == 1) break; else { func_2s_comp(a, result); break; }

case 6:

input1(a);

if (k == 1) break; else { func_2s_comp_star(a, result); break; }

default: printf(" Invalid choice");

}

} return 0; }

}

Lab Assignment You should complete the above program under the name of a project COMP2650_Lab03_{UWinID} that firstly outputs a menu of commands as follows: Enter the command number: 0) Exit 1) AND 2) OR 3) NOT 4) l's complement 5) 2's complement 6) 2's complement* Based on the user's chosen number of commands, the program should then ask for the input(s). After that, the program asks to what base the user wants to see the results. Then, it applies the command and prints out the result in the requested base. For instance, if a user selects (1), the program should accept two inputs as follows: Enter the first binary number: 0 x1 = X7 Enter the second binary number: yo yl = y7 - When the user enters the two binary numbers, the program asks for a base number to print out the result: Enter the output base: 1) Binary 2) Octal 3) Decimal 4) Hexadecimal Then the program applies the AND command on the input x and y and prints the result on the selected base and comes back to the main menu. Other commands should follow the same flow. If the user selects (0), the program ends. Please restrict the user to enter inputs within the range {0,1}. For instance, if the user enters 2,-1, ..., print out an error message and come back to ask for correct inputs. It is required to write a modular program according to the following instructions: 1. Reorganized the previous functions in Lab03 in separate files as I did in this manual. 2. For the base conversion, create conversion.cpp and conversion.h. 3. Write functions in conversion.cpp to output the result according to the selected base by the user by calling the functions from main.cpp file: void to_octal(int a[]) { } void to_decimal(int a[]){} void to_hexadecimal(int a[]){} For converting binary numbers to octal or hexadecimal, you can use either the steps explained in the class or the fast method explained in the lecture assignment Leco 2. For converting to decimal, you can use the sum of powers of 2, as described in the class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions