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

#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); }
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]);...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void)...
REWRITE THE FOLLOWING CODES USING FOR LOOP. PLS USE C LANGUAGE FORMAT #include <stdio.h> int main(void) {      int count ; scanf("%d",&count);           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                            return 0; }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
#include #include #include #define WORDLENGTH 30 #define MAX 100 struct car { char model[WORDLENGTH]; int year;...
#include #include #include #define WORDLENGTH 30 #define MAX 100 struct car { char model[WORDLENGTH]; int year; int milage; }; typedef struct car Car; Car createCar(char model[], int year, int milage) { Car c; strcpy(c.model, model); c.year = year; c.milage = milage; return c; } void regCars(Car reg[], int *pNrOfCars) { char again[WORDLENGTH] = "yes", model[WORDLENGTH]; int year, milage; while (strcmp(again, "yes") == 0) { printf("Ange model:"); scanf("%s%*c", model); printf("Ange year:"); scanf("%d%*c", &year); printf("Ange milage:"); scanf("%d%*c", &milage); reg[*pNrOfCars] = createCar(model, year,...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int main() {    const char* filename = "samples.coe"; const int N = 1024; FILE* file = fopen(filename, "w"); if (file == NULL) { perror("fopen"); } fprintf(file, "; These are 1024 sample values in range -1 to 1,\n"); fprintf(file, "; Sine Wave 0\n"); fprintf(file, "memory_initialization_radix = 10;\n"); fprintf(file, "memory_initialization_vector\n"); double values[N]; double delta = M_PI / (N - 1); for (int i = 0; i...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int...
*/ #include using namespace std; void start (int boxes [10]); void move (int squares [10], int x, int y, int z); void add (int arr [10], int first, int last); void print (int arr [10]); int main () {     int my_arr [10];         cout << "The original array is:\n";     print (my_arr);         start (my_arr);     cout << "\n\nThe array after start is:\n";     print (my_arr);         move (my_arr, 2, 4, 6);     cout << "\n\nThe...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT