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) { int input = -1; double average = 0.0;    /* Use...
#include <stdio.h> int main(void) { int input = -1; double average = 0.0;    /* Use this for your input */ scanf("%d", &input);    /* This is the only output you need */ printf("%.1lf", average); return 0; } Please write a C coding program (make it simple coding)  that find the average of sequence integer. when user enter integers, once at a time. Also, If the user enters 0 (zero), the code will calculates the average and then prints it, and...
#define _CRT_SECURE_NO_WARNINGS // Put your code below: #include <stdio.h> int main(void) { int day, hightemp[10], lowtemp[10],...
#define _CRT_SECURE_NO_WARNINGS // Put your code below: #include <stdio.h> int main(void) { int day, hightemp[10], lowtemp[10], numbers, highesttemp, highestday, lowesttemp, lowestday, numbers2 = 0, hightotal = 0, lowtotal = 0, averageday = 0, numbers3; double averagetemp; printf("---=== IPC Temperature Calculator V2.0 ===---"); printf("\nPlease enter the number of days, between 3 and 10, inclusive: "); scanf("%d", &numbers); if (numbers < 3 || numbers > 10) { printf("\nInvalid entry, please enter a number between 3 and 10, inclusive: "); scanf("%d", &numbers); printf("\n");...
#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck...
#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck */ for(int i = 0; i < DECK_SIZE; i++) { deck[i] = i + 1; }    /* Get the cut position as an integer input */    /* Verify that the input is valid */    /* Cut the deck */    /* Print the resulting deck with one element on each line */ return 0; } Write a program that "cuts" a...
#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++;    }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT