[-] milon@lemm.ee 1 points 10 months ago

Yes, that helps. Thanks. I see now how n goes from 1 to 2 to 3...etc. Now not so sure how i = 1 when the for loop starts.

[-] milon@lemm.ee 2 points 10 months ago

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?

[-] milon@lemm.ee 1 points 10 months ago

Gotcha. Thanks for the explanation.

[-] milon@lemm.ee 1 points 10 months ago

I see. I guess my understanding was that the recursion was over after the recursive call, but it's actually for all the code in draw().

[-] milon@lemm.ee 1 points 10 months ago

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.

[-] milon@lemm.ee 1 points 10 months ago

Right. I was aware it was recursion as stated in the title of my post. I had two questions specific to where the for loop returns after printing #.

[-] milon@lemm.ee 2 points 10 months ago

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.

[-] milon@lemm.ee 1 points 10 months ago
[-] milon@lemm.ee 2 points 10 months ago

It's supposed to be a pyramid but not my code. It's an example of a recursive function from a CS50 lecture and I'm just trying to understand how the code works line by line.

32
submitted 10 months ago by milon@lemm.ee to c/programming@programming.dev

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++?

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");
}
[-] milon@lemm.ee 2 points 11 months ago
[-] milon@lemm.ee 3 points 11 months ago* (last edited 11 months ago)

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.

[-] milon@lemm.ee 3 points 11 months ago

Sorry. It's in C. Updated post. Yes those are titles. I just included the relevant portions rather than the entire code.

8
submitted 11 months ago* (last edited 11 months ago) by milon@lemm.ee to c/programming@programming.dev

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)
    { ...
10
submitted 11 months ago by milon@lemm.ee to c/programming@programming.dev

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 <= 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 '<=' 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;
    }
13
submitted 1 year ago* (last edited 1 year ago) by milon@lemm.ee to c/programming@programming.dev

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?)


#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;
}
view more: next ›

milon

joined 1 year ago