-3

Here is the sheet that I need to follow https://i.stack.imgur.com/lsu70.jpg

Here is my code as of now

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>

void arithmetic();
int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals);
int askOperand1();
int askOperand2();
unsigned char operand1;
unsigned char operand2; 
unsigned char control_signals;
const int ACC = 16; //ACC = ACCUMULATOR

int main()
{

    for(;;)
    {
        system("cls");
        ALU(operand2,operand2,control_signals);
    }
    getch();
    return 0;
}

int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals)
{
    operand1=askOperand1();
    operand2=askOperand2();

    int pos1,pos2;
    unsigned char bin8_1[]  = "00000000";
    unsigned char bin8_2[]  = "00000000";

    /*OPERAND 1*/
    for (pos1 = 8; pos1 >= 0; --pos1)
    {
        if (operand1 % 2) 
        bin8_1[pos1] = '1';
        operand1 /= 2;
    }
    printf("\n\nBinary Equivalence of Operand 1: %s",bin8_1);

    /*OPERAND 2*/
    for (pos2 = 8; pos2 >= 0; --pos2)
    {
        if (operand2 % 2) 
        bin8_2[pos2] = '1';
        operand2 /= 2;
    }
    printf("\n\nBinary Equivalence of Operand 2: %s",bin8_2);


    /*ARITHMETIC FUNCTIONS*/
    int option, remainder = 0, sum[ACC], k;
    arithmetic();
    scanf("%d",&option);
    switch(option)
    {
        case 1: //ADDITION
             while (bin8_1 != 0 || bin8_2 != 0)
            {
                sum[k++] =(bin8_1 % 10 + bin8_2 % 10 + remainder) % 2;
                remainder =(bin8_1 % 10 + bin8_2 % 10 + remainder) / 2;
                bin8_1 = bin8_1 / 10;
                bin8_2 = bin8_2 / 10;
            }
            if (remainder != 0)
            sum[k++] = remainder;
            --k;
            printf("Sum of two binary numbers: ");
            while (k >= 0)
            printf("%d", sum[k--]);
            break;
        case 2: //SUBTRACTION VIA 2'S COMPLEMENT
            break;
        case 3: //MULTIPLICATION
            break;
        case 4: //DIVISION
            break;
    }

}
int askOperand1()
{
    int ask1;
    printf("\n\nEnter Operand 1(in decimal): ");
    scanf("%d",&ask1);
    if(ask1>255)
    {
        printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask1;
}
int askOperand2()
{
    int ask2;
    printf("\nEnter Operand 2(in decimal): ");
    scanf("%d",&ask2);
    if(ask2>255)
    {
        printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask2;
}
void arithmetic()
{
    printf("\n\n");
    printf("[1] ADDITION\n");
    printf("[2] SUBTRACTION\n");
    printf("[3] MULTIPLICATION\n");
    printf("[4] DIVISION\n");
    printf("\nOption: ");
}

Input has to be in decimal from 0-255 only. And then it will be converted to binary. That two binaries will be added and then print out a 16 bit output. I also don't know anything about control_signals variable and I can't ask my teacher about it because he's away for 1 week.

wait what
  • 3
  • 4
  • I'm sorry, but I don't believe you when you say that `bin8_1` and `bin8_2` are `unsigned char`. Show me. The compiler is telling you they're `unsigned char *`. – Steve Summit May 16 '18 at 11:31
  • 4
    How about posting a [MVCE](https://stackoverflow.com/help/mcve)? Show the declarations for `bin8_1` and `bin8_2`. – mhawke May 16 '18 at 11:32
  • 2
    In your [previous question](https://stackoverflow.com/questions/50367681/converting-a-decimal-to-a-16-bit-binary-using-unsigned-char-and-without-string), you were asked not to convert the numbers to a string in the comments. Why are you still using `unsigned char*` ? – Ajay Brahmakshatriya May 16 '18 at 11:32
  • @SteveSummit here https://imgur.com/a/LrjqrGz – wait what May 16 '18 at 11:33
  • @AjayBrahmakshatriya because it's what the paper told me to do. I just followed the instructions. Here look https://imgur.com/a/JuLpQZt – wait what May 16 '18 at 11:34
  • 2
    @DeanzTinio Two hints: (1) C is not a language to learn by trial and error -- you really need to work through a tutorial on some of the basics first. (I can offer you [mine](https://www.eskimo.com/~scs/cclass/cclass.html).) (2) You'll get better help here on SO if you paste properly-formatted code right into question rather than posting image links, because many people won't bother to chase those. – Steve Summit May 16 '18 at 11:34
  • @DeanzTinio show your code _here_ by [edit]ing your question. – Jabberwocky May 16 '18 at 11:35
  • @DeanzTinio where does your question sheet ask you to use `unsigned char*` ? It asks you to use `unsigned char`. – Ajay Brahmakshatriya May 16 '18 at 11:35
  • I will be posting a new question with complete code. – wait what May 16 '18 at 11:38
  • 1
    @DeanzTinio dont post a new question, but [edit] this one. – Jabberwocky May 16 '18 at 11:38
  • Okay sorry. Check my post again @MichaelWalz – wait what May 16 '18 at 11:43
  • `bin8_1` and `bin8_2` should be `unsigned char`. You are declaring them as _array_ of `unsigned char`. Forget about array of chars and/or strings for your problem. – Jabberwocky May 16 '18 at 12:01
  • 1
    @DeanzTinio One more hint: One of the most important skills in programming is *attention to detail*. In C, there's an enormous difference between types (1) `unsigned char`, (2) `unsigned char *` (pointer to unsigned char), and (3) `unsigned char []` (array of unsigned char). In your original question you insisted your variables were `unsigned char`, but your compiler told you otherwise, and then when Ajay asked you why you were using `unsigned char *`, you said it was because you had to (even though that contradicted what you'd said earlier). Be careful with this stuff! Details matter! – Steve Summit May 16 '18 at 12:35

1 Answers1

1

You declared bin8_1 and bin8_2 as unsigned char bin8_#[].
This means it's an array of unsigned char.

You're then comparing this array to an int. Try with a simple unsigned char.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Ika
  • 21
  • 5