Question

In: Computer Science

#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...

#include <stdio.h>
#define MAX 8 //Byte = 8 bits

void func_and(int a[], int b[], int result[]) {
    for(int i=0; i < MAX; i = i + 1){
        result[i] = a[i] & b[i];
        }
    }

void func_or(int a[], int b[], int result[]) {
    for(int i=0; i < MAX; i = i + 1){
        result[i] = a[i] || b[i];
    }
}

void func_not(int a[], int result[]) {
    for (int i = 0; i < MAX; i = i + 1) {
        result[i] = !a[i];
    }
}

void func_1s_comp(int a[], int result[]) {
    for (int i = 0; i < MAX; i = i + 1) {
        result[i] = !a[i];
    }
}

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

    for (int i = 0; i < MAX; i = i + 1) {

        result[i] = !a[i];

        if(result[i] == '1' && carry == 1)
        {
            compTwo[i] = '0';
        }
        else if(result[i] == '0' && carry == ddd1)
        {
            compTwo[i] = '1';
            carry = 0;
        }
        else
        {
            compTwo[i] = result[i];
        }

        compTwo[MAX] = '\0';
    }
}


int main(void) {

    int choice;
    int carry = 1;
    int compTwo[];
    int x[MAX];
    int y[MAX];
    int z[MAX];

    do {

        printf("Enter the command number: \n0. Exit\n1. AND\n2. OR\n3. NOT\n4. 1's complement\n5. 2's complement\n6. 2's complement*\n");
        scanf("%d", &choice);

        switch (choice) {

            case 0: //Exit

                break;

            case 1: //AND

                printf("Enter the first binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &x[i]);
                }

                printf("Enter the second binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &y[i]);
                }

                func_and(x, y, z);

                printf("The first number AND second binary yield: ");
                for (int i = 0; i < MAX; i = i + 1) {
                    printf("%d", z[i]);
                }
                printf("\n");

            case 2: //OR

                printf("Enter the first binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &x[i]);
                }

                printf("Enter the second binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &y[i]);
                }

                func_or(x, y, z);

                printf("The first number OR second binary yield: ");
                for (int i = 0; i < MAX; i = i + 1) {
                    printf("%d", z[i]);
                }
                printf("\n");

            case 3: //NOT

                printf("Enter the binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &x[i]);
                }

                    func_not(x, z);

                    printf("The NOT for this number is: ");
                    for (int i = 0; i < MAX; i = i + 1) {
                        printf("%d", z[i]);
                    }
                    printf("\n");


            case 4: //1's complement

                printf("Enter the binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &x[i]);
                }

                func_1s_comp(x, z);

                printf("The 1st complement of this binary number is: ");
                for (int i = 0; i < MAX; i = i + 1) {
                    printf("%d", z[i]);
                }
                printf("\n");


            case 5://2's complement

                printf("Enter the binary number: \n");
                for (int i = 0; i < MAX; i = i + 1) {
                    scanf("%d", &x[i]);
                }

                func_2s_comp(x, z);

                printf("The 2nd complement of this binary number is: ");
                for (int i = 0; i < MAX; i = i + 1) {
                    printf("%d", z[i]);
                }
                printf("\n");

            case 6://2's complement*
                        break;
                }
        }

        while (choice != 0);

        return 0;
}

I just have to add 2's complemenet and 2* complement which in the 2 functions which I dont know how to do. 2* complement is "Start from the right to left until you see digit 1, then pass it and NOT the digits after that. For instance, 2’s complement of 110100 is 001100"

Solutions

Expert Solution

/*First of all I think you have mistaken in declaring variables as comptwo[] is declared in main() but you used it in

func_2s_comp() it will show error so try declaring those variables globally or use similar variable in other functions.*/

/*I am going with the defination you provided for 2s* complement*/

//I am writing this complete sample code.

#include<stdio.h>
#define MAX 8
void main()
{ int i;
int a[MAX];
for(i=0;i<MAX;i++)
scanf("%d",&a[i]);
abc(a);
for(i=0;i<MAX;i++)
printf("%d",a[i]);


}

void abc(int bin[])//Function to calculate 2s *complement
{
int i,j;
//To get right most index where bit is 1
for(i=0;i<MAX;i++)
{
if(bin[MAX-1-i]==1)
{
i=MAX-1-i;
break;
}
}
//flipping all the bits to the left of rightmost 1
for(j=0;j<i;j++)
{
if(bin[j]==0)
bin[j]=1;
else
bin[j]=0;
}
}

void xyz(int bin[])//2s complement
{
int i,j;
//To get 1s complement
for(i=0;i<MAX;i++)
{
if(bin[i]==0)
    bin[i]=1;
else
    bin[i]=0;
}
//To get right most index where bit is 0
for(i=0;i<MAX;i++)
{
if(bin[MAX-1-i]==0)
    {
    i=MAX-1-i;
    break;
    }
}
bin[i]=1;//flipping rightmost 0

//flipping all the 1s to the right of rightmost 0
for(j=i+1;j<MAX;j++)
{
        bin[j]=0;
}
}
 

//OUTPUT

1. 2s * complement

2. 2s complement

//Thankyou


Related Solutions

example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
Iterative Merge Sort is not working #include #include #include int* b; void merge(int a[], int left,...
Iterative Merge Sort is not working #include #include #include int* b; void merge(int a[], int left, int mid, int right) {    int i = left, j = mid + 1, k = left;    while (i <= mid && j <= right)    {        if (a[i] < a[j])            b[k++] = a[i++];        else            b[k++] = a[j++];    }    for (; i <= mid; i++)    {        b[k++]...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
A byte is made up of 8 bits although a byte is rarely represented as a...
A byte is made up of 8 bits although a byte is rarely represented as a string of binary digits or bits. Instead, it is typically presented as a pair of hexadecimal digits where each digit represents 4 bits or half a byte or "nibble." We have been reading in hex digits and outputting their decimal value, but what we really want to do is create a byte from a pair of hex digits read from input. Create a C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT