Samul's Training and Nutrition Log

Oh my, lol

Mistyped this once and “find all and replace” did the rest of the damage

As per points (B) and (C), I see where you’re coming from and I agree.

However, we are required to use either printf or puts in our programs as output needs to be sent to the standard output.

When it comes to point (B), the problem is that we are asked to be as time-efficient as possible and the program needs to be essential. All we have to do is code a solution to that logic, and we are evaluated based on the logic of the program.

If I were writing a program for myself or someone else but not as a school assignment, I would employ verbose errors and most of what you mentioned. Unfortunately errors can’t be verbose with these program because we have to implement a specific output, and automating all the checks is outside of the scope of what we’re asked to do and it doesn’t increase our score to do so.

But still, when I’m by myself, that’s a different matter.

Hence my opening remark more or less.

Here’s one of my assignments

hopefully I didn’t screw anything up or break anything when substituting variable names with english equivalents.
This is a bit longer so i understand if you don’t wanna have to look at the whole thing

and here’s an example input that you can use to test it

I’ve glanced through it all, so if I omit mentioning something that doesn’t necessarily mean that it receives my stamp of approval. But a few comments,

Document your state-machine. I don’t know that much about ISBN off hand. Include a link to a wikipedia article or whatever you have based your implementation on. Clarify things, even if they are obvious to you. For instance, why do you increment i first before performing the assignment as opposed to using i++.

c = input[++i];

Probably obvious when you wrote it, not obvious to a reader that is trying to spend the minimal amount of time reading your code (as would be the case with other programmers that have to use your code when you are doing this as your full-time job)

/* 
Looks for a book and returns a pointer to it if found, a NULL pointer otherwise. 

If isbn parameter is '0', the book is searched by author and title, 
otherwise a search via isbn is performed
*/
Book *findPtr(Book *lPtr, char isbn[], char author[], char title[]) { 

Clearly this is two functions,

Book *findPtrByISBN(Book *lPtr, char isbn[])
Book *findPtrByAuthorAndTitle(char author[], char title[])

This should also be two functions,

void updateAvailableCopies(Book *lPtr, char mod) { // increase or decrease the copies of the book passed as argument, depending on whether it's called in 'p' mode (prestito means borrow in italian) or 'r' mode (return)

namely,

returnACopy(Book *lPtr)
borrowACopy(Book *lPtr)

You often use else-branches when they are not necessary. Here is your code,

    if(!(strcmp(author,lPtr->author)) && !(strcmp(title,lPtr->title))) { // if author and title match, i found the book
        return lPtr;
    } else {
        return findPtr(lPtr->nextPtr,"0",author,title);
    }

Here’s how I’d write it,

    if(!(strcmp(author,lPtr->author)) && !(strcmp(title,lPtr->title))) { // if author and title match, the book was found
        return lPtr;
    } 

    return findPtr(lPtr->nextPtr,"0",author,title);

even better,

bool hasAuthor(Book *lPtr, char *author) {
    return strcmp(author, lPtr->author) == 0;
}

bool hasTitle(Book *lPtr, char *title) {
    return strcmp(title, lPtr->title) == 0;
}

if(hasAuthor(lPtr, author) && hasTitle(lPtr, title)) { 
    return lPtr;
} 

return findPtr(lPtr->nextPtr,"0",author,title);

now we turned your comment into code!

Finally, I believe you could maybe have used another variable name than lPtr. Also, In C can a long printf statement be broken up into multiple lines? - Stack Overflow

And I believe this is an anti-pattern,

int copiesUnav;

instead, go for

int copiesAvailable;

Do not force me to have to carry a negative, it’s too much mental overhead. I’d also clean-up the struct to make sure that you cannot have any impossible states. Now, if you forget to increment/decrement your copiesUnav or copiesTot they could end up out of sync.

The best C would allow you to have is,

struct book {
    char isbn[7];
    char title[151];
    char author[151];
    int initialNumberOfCopies; // Never change this, keep it as a sanity check in your rent/return functions
    int copiesAvailableForRent;
    int copiesBorrowed;
    struct book *nextPtr;
};

and this will make it easier to check if a book can even be borrowed, just return copiesAvailableForRent > 0.

1 Like

I’m sorry, we weren’t told much in the assignment text. It wasn’t me being lazy but rather the information given was fairly limited. That’s not even how actual ISBN code works. They just told us “in this program, the ISBN is a six-digit number.”

Thank you for the feedback once again. I’ll make sure to reread everything more thoroughly once I’m done studying calculus (which I’m doing right now), so I can absorb the information better. It all sounds like great advice.

1 Like

Db ohp 24 kg x 10,9,6
Cable lateral raises 3 sets
Front plate raise 2 sets
Ez bar curls 3 sets
Incline curls 2 sets
Rope pushdown 3 sets
Db skullcrusher 2 sets

Squat 112,117,122 kg x 5
Deadlift 132,142,152 kg x 5

Bench 72 kg x 12,10,9
Incline db press 26 kg x 9, 24 kg x 9,6
Dips x 12,10,8
V bar pushdown 3 sets
Db skullcrusher 2 sets

Something magic must be in the air cuz threads be vanishing left and right today, amirite @dagill2?

Mine just got moved to Off Topic. I guess Chris just chose the winner for us.

1 Like

Oh lol didn’t realize yours got moved

1 Like

I was just going to delete to be fair, but whatever works. As long as there’s still a little “common room” style thread going.

Nope, not right. Flame-free 3 is in off-topic. Your thread was removed because the 20-character rule has always been in place and the post would’ve been deleted where ever it popped up. Having it as the first post of a thread took the thread with it.

3 Likes

Don’t start getting on the defensive. I made that joke because I thought both mine and his thread had been deleted, which I didn’t understand.

I didn’t care to check other sections because it’s always been in bigger stronger leaner.

I thought I cleared that up when I answered @dagill2’s post just above and I realized only one had been deleted, the other one moved. That’s why I used the word “threads” to begin with

Pretty sure I was just explaining what happened to the threads. Not sure how that’s defensive, but, um, okay.

3 Likes

I read that as an explanation more than a defense, honestly.

Edit: yeah, what he said.

1 Like

@Chris_Colucci @dagill2

Yeah my bad, I read the post in a hurry and misinterpreted the tone.

2 Likes

Bb row up to 72 kg x 10 (3 sets)
Neutral-Grip lat pulldown up to 60 kg x 11 (3 sets)
Seated lat row 40 kg x 11, 35 kg x 10
Face pull 2 sets
Cross body Hammer curl 3 sets
Spider curl 3 sets

Db ohp 24 kg x 11, 9, 7
Cable laterals 3 sets
Front plate raises 2 sets
Db curl 3 sets
Incline curl 2 sets
Rope pushdown 3 sets
Db skullcrusher 2 sets

Looks like I’ve been nailing the mini cut. One more week to go.