Skip Navigation
InitialsDiceBearhttps://github.com/dicebear/dicebearhttps://creativecommons.org/publicdomain/zero/1.0/„Initials” (https://github.com/dicebear/dicebear) by „DiceBear”, licensed under „CC0 1.0” (https://creativecommons.org/publicdomain/zero/1.0/)MI
Posts 6
Comments 20
Why can't I use the '|' bitwise operator in this statement?
  • Thanks. I think I understand why I wouldn't want to use it in this case. But what is an example of where I can use it? This makes me think I should avoid using bitwise operators with integers and keep it to strings only, but I know that's not true from what I've learned.

  • Why can't I use the '|' bitwise operator in this statement?
  • I did come across that link but didn't quite understand it. If looking only at 25 | 10, does the code not run as expected because 25 is 5 digits long and 10 is 4 digits long? Is that what's meant by "two equivalent length bit designs"?

    Also, I can't tell if 10 | 4 = 7 or 10 | 4 = 14.

  • Why can't I use the '|' bitwise operator in this statement?

    if coin == 25 | 10 | 5:

    If I replace the '|' with 'or' the code runs just fine. I'm not sure why I can't use '|' in the same statement.

    Doing the following doesn't work either:

    if coin == 25 | coin == 10 | coin == 5:

    I know bitwise operators can only be used with integers, but other then that is there another difference from logical operators?

    29

    Why is my output not formatted as a string?

    ```

    Ask user to enter an expression and display output

    def main(): expression = input("Expression: ")

    print(calculate(splitter(expression)))

    Split expression into components and assign to variables as float values

    def splitter(expression): x, y, z = expression.split()

    return x, y, z

    Calculate expression result

    def calculate(x, y, z): x, z = float(x), float(z)

    if y == "+": return str(round((x + z), 1)) elif y == "-": return str(round((x - z), 1)) elif y == "*": return str(round((x * z), 1)) else: return str(round((x / z), 1))

    main() ```

    I am getting traceback errors for any expression (1 + 1) I enter.

    13
    Why does the for loop repeat in this recursion?
  • Yes - I finally caught that part about n as it's just moving in reverse so it gets decremented. Now I'm not sure about i. In the debugger when the program gets to the for loop both n and i are equal to 1. The n I understand but i?

  • Why does the for loop repeat in this recursion?
  • Thanks. I did see that. I have a general understanding of how recursion works I think where the function calls itself again and again but I don't get why the code (for loop) below the draw(n - 1) is recursive.

  • Why does the for loop repeat in this recursion?
  • Why does the for loop return when it hits the end of the function? Isn't the recursive portion already completed in draw(n - 1)? The rest of it is just normal non-recursive code if I understand it correctly.

  • Why does the for loop repeat in this recursion?

    I used the debugger to examine this code but not understanding a couple areas.

    1. Why does the for loop repeat after it exits to print a new line? If it exits the loop, shouldn't it be done with it?
    2. Why is n incremented and not i as stated with i++? ```void draw(int n);

    int main(void) { int height = get_int("Height: ");

    draw(height); }

    void draw(int n) { if (n <= 0) { return; }

    draw(n - 1);

    for (int i = 0; i < n; i++) { printf("#"); } printf("\n"); }

    30
    Why does isalpha() fail to identify an alphabetic character?
  • Ah ha! Yes, I did check the docs but I think I just glanced over that portion. Be more careful next time. Now that I took another look at the other ctype.h functions, they all return 1 or 0. I think I confused equivalent python built-in functions as those evaluated to true/false. The < is a less than sign but it seems it doesn't render correctly on Lemmy.

  • Why does isalpha() fail to identify an alphabetic character?

    This is in C language. When I call rotate() in main, the function returns false for isalpha() even though the string entered for plaintext uses alphabetic characters. Perhaps it's identifying an alphabetic character by its ASCII value ('A' = 65)? I tried to test that out and used (char) with the letter variable in rotate() but it didn't change anything.

    PORTION OF MAIN ``` string plaintext = get_string("plaintext: ");

    int length = strlen(plaintext); char ciphertext[length];

    for (int i = 0; i &lt; length; i++) { ciphertext[i] = rotate(plaintext[i], key); } ```

    ROTATE FUNCTION char rotate(char letter, int key) { if (isalpha(letter) == true) { ...

    8
    Why does a control path not execute in this function?
  • Ah I see. I had a bad habit of using else if statements instead of else statements because I thought else if could be better in seeing the condition it's testing for so it was clearer. I get the logic is actually different now.

  • Why does a control path not execute in this function?

    In VS I am told this function "does not return a value in all control paths." A bot told me specifically the issue is with this line: else if (letter + key &lt;= 90). It said that if the outcome results in letter + key equally exactly 90 then a value is not returned, but I thought that was covered where '&lt;=' means 'less than or equals.'

    ``` char rotate(char letter, int key) { if (isalpha(letter) == true) { if (letter + key > 90) { int overage = letter + key - 90; letter = 64 + overage;

    while (letter > 90) { overage = letter - 90; letter += overage; }

    return letter; }

    else if (letter + key &lt;= 90) { letter += key; return letter; } }

    else if (isalpha(letter) == false) { return letter; } ```

    12

    What is this format specifier?

    What is %.2f? Why is it not just %f? Is there some additional calculation happening? The half function already does all the calculations including splitting the bill, so I'm not sure what %.2f is. (Btw why is this code not formatting correctly in lemmy?)

    ```// Calculate your half of a restaurant bill

    #include #include

    float half(float bill, float tax, int tip);

    int main(void) { float bill_amount = get_float("Bill before tax and tip: "); float tax_percent = get_float("Sale Tax Percent: "); int tip_percent = get_int("Tip percent: ");

    printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent)); }

    // TODO: Complete the function float half(float bill, float tax, int tip) { bill += (bill * (tax / 100.0)); bill += (bill * (tip / 100.0));

    bill /= 2;

    return bill; } ```

    35