Hot answers tagged

19

An array is not strictly required to solve this. You already have the mechanism to extract the last digit from a number. You just need to extend this by using use division to "shift" the digits the required number of places. For example, if you have the number 1234, to get the second digit you can divide by 10, to get 123. Using 123 mod 10 will give you ...


10

The remainder operator (otherwise known as the modulo operator) % is a binary operator (i.e., takes exactly 2 operands) and operates only on integer types (e.g., short, int, long, long long, etc). It appears from the error message that the variable number1 is of type double. Also, the function pow from the math library returns a value of type double. These ...


6

If you want to store each digit of a number as an element of an array you can do the following. long long number = 1234567890; int digits[10]; for (int i = 0; i < 10; i++) { digits[i] = number % 10; number = number / 10; } // At the end digits = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1} But if you want the elemnts to keep the same order than the original ...


6

No, you're using GetLongLong correctly not printing it correctly to stdout. "%llo" prints it in octal (i.e., base 8) instead of decimal (see more here https://www.cs.colostate.edu/~cs157/Fall15/Printf). Use "%lld" instead. Also, /n simply prints /n. The newline character is '\n'. ETA: there's a typo in your standard i/o library inclusion ("stdio.h" not "...


5

You can use arithmetic to loop through the numbers. In your example: 12345686868 % 10 = 8 which leads on to 12345686868 / 10 = 1234568686 so 1234568686 % 10 = 6 You can carry this on to access to all the numbers in the credit card.


5

Here's some pseudo code: int accumulator = 0 for each digit in the card number { if the digit is at an odd position (position is not divisible by 2) { int factor = multiply the digit x 2 for each digit in factor { add factor digit to accumulator } } } Given your example digits 1234567, this will do ...


5

You could take the number as a string, and access the individual digits as characters (subtract '0' or 48 to obtain their value), or parse the number as a long long int and use for example / and % in a loop to obtain the various digits (% 10 would obtain the last digit, while integer division truncates the result of a division). Ensure you don't change ...


4

% operator is used to perform binary operation on integer or integer-like operands. It is used like this: int i,j; input i , j i = i % j; print i What you are trying to achieve is something similar to this: double i,j; input i , j i = i % j; print i The problem is what do expect it to output when dealing with floating point numbers? For eg. 54.66 % 1.02 ...


4

Found it: American Express starts with 34 or 37, Master Card starts with 51, 52, 53, 54 or 55. This makes those two numbers INVALID despite good checksum :)


3

Good job reaching that point and giving a shot to the hacker pset too! Don't be dismayed if you don't succeed with the first try, keep trying! Now to your question. First of all when you declare a variable, you don't have to put it in parentheses like you have: long long(cc); Something like that is more readable and used mostly: long long cc; Also ...


3

You can use int number_of_digits = (int)log10(1234567) + 1; to get the position of the number of digits of your number. Or use the command int first_digit = (int)(1234567 / 1000000); int remaining_digits = 1234567 - 1000000; and the equivalent, faster one: int remaining digits = (1234567 % 10000000); int first_digit = 1234567 - remaining_digits; :-)


2

It can sometimes happen that a program works fine locally but fails check50. One common cause of this is using uninitialized variables and/or arrays. The block of memory grabbed locally may be entirely null, so the program works fine. But the machine used by check50 may well give you memory filled with "garbage" that causes your program to behave ...


2

1- converting longlong to int causes imprecision since int store 4bytes(32bits) and longlong store 8bytes(64bits) of intger values so you don't need int or float(for floating point numbers) which both are 4 byets you only need longlong because you know that credit card number will be greater than 4000,000,000 which is the maximum number can int or float hold ...


2

You need to play around with the modulus operator for a bit. I don't want to give away too much, but when I did it, I used the truncation of the operator to my advantage ;)


2

You can use a loop. As long as the number is not zero, process its last digit (%10), for example storing it in an array, then divide the number by 10, shifting decimal digits to the right. If you intend to use the original number later, operate on a copy of the number instead, as this process is destructive. That's much more efficient than using floating-...


2

Your counting loop destroys the number. Destroy a copy instead. And stack variables are not initialised by default, so you end up with some arbitrary data as the array isn't filled at all due to your first mistake. While the use of one array for all the digits might be reasonable, the use of extra arrays is definitely not required. You can process one ...


2

If you were to print(warehouse) after this line warehouse = ((c_number / 10)%10)*2 you'd discover that it returns a floating point number. Python doesn't do integer division like C unless you use the // operator (integer division operator).


2

I feel your frustration, but you're looking in the wrong place. The number may pass the checksum calculation, but that isn't the problem. The number is only 10 digits. It should be either 13 or 16 digits for Visa. I don't believe that I saw any code that checks the validity of the number's length. If this answers your question, please click on the check ...


2

FindLength is a pointer, not a long long integer. This means that the code is trying to divide an address by an integer. FindLength = FindLength / 10; In this statement, FindLength contains the address of the long long, not the actual data itself (i.e., the address of card). Why does this need to be a pointer and not just a long long? Also, by using the ...


2

Your algorithm is calculating the correct result. 74 is the octal representation of decimal number 60 (octal uses 8 as base while decimal uses 10). Use ld, not lo to print a long. 7 * 8^1 + 4 * 8^0 = 56 + 4 = 60


2

One thing first, and has higher precedence than or. A or B and C is same as A or (B and C). Keep that in mind when combining the two. In Python, pretty much like in C, firstTwo == 51 or 52 or 53 or 54 or 55 is interpreted as (firstTwo == 51) or (52) or (53) or (54) or (55), five expressions combined with or. As all but the first (which may or may not) are ...


2

The compiler is actually giving a warning, but the compiler flags are set up to treat all warnings as errors. It's still best to eliminate all warnings. Here's the reason. For clarity, let's simplify the expression to the essential parts. while((a) && (b) || (c)) The compiler isn't sure about how these logical operators should be combined. ...


2

Does cs50.h have a function called get_long_long?. There is no function by that name listed in the CS50 Reference. And notice this from the spec: Now, get_long itself will reject hyphens (and more) anyway: It looks like get_long_long has been deprecated in the 2019 version of the course.


2

The function get_int returns an integer and therefore if you wish to access each character by its corresponding index consider first casting it to a string.


2

Assuming the error you are getting is not related to a linking error, which is common, then the error I assume you are getting is loop_check.c:14:15: warning: '&&' within '||' [-Wlogical-op-parentheses] }while(C< 0 && C < 999999999999999 || C < 0 && C > 9999999999999999); ~~~~~^~~~~~~~~~~~~~~~~~~~~~ ~~ ...


2

4111111111111113 isn't a valid card number because it doesn't come out correctly with the Luhn algorithm. It looks like you've got the part of the problem where you check the card's company, but it doesn't look like you have the part of the problem where you check if the card is valid with the Luhn algorithm.


2

When the code is run with the test data that check50 uses, 4111111111111113, it prints INVALID twice. check50 was expecting the program to end, not to print the second INVALID. The problem is obvious with a little testing, but I'll give you a hint. Go into the code and change every line that prints "INVALID". Add a unique number to each printf ...


1

You can condense your switch statements where the cases all return the same output, for example: switch (second_digit) { case 1: case 2: case 3: case 4: case 5: printf("MASTERCARD\n"); break; default: printf("INVALID\n"); break; }


1

What about this 5105105105105100? 1*2 + 5*2 + 0*2 + 1*2 + 5*2 + 0*2 + 1*2 + 0*2 2 + 10 + 0 + 2 + 10 + 0 + 2 + 0 2 + 1 + 0 + 2 + 1 + 0 + 0 + 2 + 0 = 8 8 + 5 + 0 + 1 + 5 + 0 + 1 + 5 + 0 = 25 The checksum is not ending with 0, but cs50 online judge says it's a valid mastercard. I'm totally confused.


1

371449635398431 is a valid AMEX card. From this post: 3 7 1 4 4 9 6 3 5 3 9 8 4 3 1 Okay, let’s multiply each of the underlined bold digits by 2 7*2 + 4*2 + 9*2 + 3*2 + 3*2 + 8*2 + 3*2 That gives us: 14 + 8 + 18 + 6 + 6 + 16 + 6 Now let’s add those products' digits (i.e., not the products themselves) together: 1 + 4 + ...


Only top voted, non community-wiki answers of a minimum length are eligible