[2023] CS50 - (Week 1) Credit Solution | Walkthrough & Guide for Beginners | By Anvea

preview_player
Показать описание
(This solution has been checked and verified for 2023.)

This is CS50. CS:50 Introduction to Computer Science is an entry-level course taught by David J. Malan. Languages that you will learn include C, Python, SQL and Javascript plus CSS and HTML.

In this video, I will walk you through the solution for Credit (More Comfortable).

TIMESTAMPS
00:00 - Introduction
00:15 - Background
00:53 - Formulating the Structure of Your Code
01:10 - Prompt User for Card Number
02:23 - Identify of Card is Valid (Luhn's Algorithm)
17:14 - Identify the Type of Card

DISCLAIMER
I've just started learning too so I am by no means an expert. However, I feel that as a beginner who have just learnt and understood the concepts, perhaps I could share with you the thought process and steps that worked for me too!

SOLUTIONS FOR CS50 PSETS

LIKE AND SUBSCRIBE
If you found this video helpful, do give a like and subscribe to be notified when I post more videos for subsequent problem sets.

ENROL NOW

GET IN TOUCH
Рекомендации по теме
Комментарии
Автор

I’m so happy that I’m not the only one who found this challenging. I felt like crying myself to sleep

Emad-nljk
Автор

Instead of hard coding to check the value of each card. You can use a while loop with a counter variable and a total variable.
1. Set counter variable and total variable to 0
2. create duplicate of user card number(so you can handle it without changing original)
3. use while loop to iterate over each digit starting from the end by using card % 10
4. use counter to keep track if even or odd placed number starting from the end
5. if odd just add to total
6. if even double it and the using % and / add the individual digits to the total
7. resave card by using card/=10 this will take of the last digit
8. add 1 to counter
9. Keep running loop until no digits are left.

Now you have a counter variable which will have the total digits and the total of the card numbers that you can check if total % 10 != 0 to see if it is invalid.

int total = 0;
int counter = 0;
while (card_numbers > 0)
{
if(counter % 2 == 1){
int num2 = card_numbers % 10 * 2;
total += num2 % 10;
total += num2 / 10;
}

else if(counter % 2 == 0){
total += card_numbers % 10;
}
card_numbers /= 10;
counter++;
}

stefanmiranda
Автор

By far the best explanation of this problem set I've seen so far. You broke it down nicely and made it very easy to understand. Great work, and thank you!

danelopez
Автор

from line 23 to line 30,

cardx = cardx/10 + cardx%10 would also work since cardx is defined as an integer and it can take max 18.

gokalpaksekili
Автор

Pset 1 credit is very difficult and complicated for a beginner, thank you so much for your explanation video Liked and subcribed!

CsCm
Автор

Your video is extremely appreciated. To be honest i would have NEVER been able to do this problem without it. I don't know if i'm alone in this situation, but after everything is explained I understand and it makes sense but without the video i'm completely clueless...

repp
Автор

Neat explanation. I solved this problem in 200 lines of code, but I pushed hard to make it with my own humble understanding of this logic.But it's definitely interesting to see how it's been done with more efficient design.

artyomd
Автор

Thank u for the immense work u put in, for me as a beginner who is absolutely clueless, ur derailed break down is class. Thank u so much, I know there's guys saying u could of did it a different way, but u thought of people like me. Thank you

stantonreddy
Автор

to help with the zeros create your get powers of 10 function and use it wherever you need.
- to do this import math on top `#include <math.h>function`
- declare your function above main `long power10(int numberOfZeros);`
- express it below main :
```
long power10(int numberOfZeros)
{
return (long)pow(10, numberOfZeros);
}
```
and then just use it . ex you need 10 to the power of 14? just put power10(14) and you will have it!

Goodbirb
Автор

Tysm, I was litteraly struggling for this. Thank god you told me about %100

aryangupta
Автор

very well organized video. Also you are a good teacher. Thank you very much for making this problem too easy for us ❤

ahsanulhaque
Автор

You broke down your explanation in such a way that even a kid will understand. Thanks

anocksteve
Автор

Very good explanation. This helped me to understand this problem much clearer. Obviously the solution here needs refining. For example, loops could be better used in the solution. But at least now, I can go away and find a better way of the coding the latter situation. But this video was for sure a good starting point.

guitarman
Автор

Thank you very much, I am very proud to have found you here, your videos have helped me a lot. <3 hi from Brazil 🇧🇷🇧🇷🇧🇷🇧🇷

ThepxLXNerd
Автор

Thank you for creating these videos. The way you present the explanations on top of your clear speaking voice, it truly makes learning the solution better!

neonsnek
Автор

Amazing explanation. You should do more videos!
You have a very good skill in explaining and teaching. Thank you.

esraashahien
Автор

Os seus vídeos explicativos dos exercícios do cs50 são beautiful!!!!🥰

edmilsonxavierdosreis
Автор

for loops can also be used to make the code shorter and easier to read

kelvinamoah
Автор

Dear Anvea (or someone), please tell me why in line 52 you use placeholder "%s" to printf(*%s\n", "INVALID"); . Why you can't just printf("INVALID");, especially in this code we didn’t store string "INVALID" to use it after comma or any else.

Thanks in advance. And of course, thank you very much for the video, it's great

artaround
Автор

This is the best! Thank you so much for this well detailed explanation. The math and the thought process is quite amazing. Great work. Subscribed with notifications enabled

fortunatenwachukwu