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> 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?
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile,...
#include <stdio.h> #include<math.h> int main (void). { int I, j, k, num_elem; double x[20], y[20],z[20]; FILE*infile, *outfile; infile = fopen (“c6_3.IN”, “r”); outfile = fopen (“c6_3.OUT”, “w”); k = fscanf (infile, “%lf %lf”, &x[0], &y[0]); fprintf (outfile,”k= %d\n”,k); fprintf (outfile, “value of EOF = %d\n”, EOF); i =1; while ( fscanf(infile, “%lf %lf”, &x[i], &y[i]) != EOF) i++; num_elem =I; fprintf(outfile,” x[i] y[i] z[i]\n”); For (j=0; j<num_elem; j++) { Z[j] =sqrt(x[j] *x[j] +y[j]*y[j]); fprintf(outfile,”%7.1f %7.1f %7.1f\n”, x[j] , y[j], z[j]);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT