Tag Info

11

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 ...

7

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; 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 "...

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 ...

3

Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together. Given a number that consists of an odd (i.e., not divisible by 2) number of digits, it won't really matter where you start. Example: 12345 The second-to-last digit is 4, not 2. However, if you started with 2, you'd still get ...

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

No need to initialize card_number to 0 before you initialize it to GetLongLong(). Your variable doesn't really store a different value. Actually, you don't print your value the correct way. To print a long long, you should use %lld not %llo #include <stdio.h> int main(void) { unsigned long long x = 4111111111111111; printf("%llo\n", x); ...

2

Based off what I read in the pset description here, the highest columns of the pyramids should be only one # wide instead of two. So for an input of 5, the pyramid should be:

2

Looking at your output, it appears that you have an extraneous leading space on each line. For instance, the first line should be #**#\n, not *#**#\n (spaces replaced with * for clarity) If this answers your question, please accept the answer to remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)

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

I like that you don't want to use CS50's tools and use tools that you are going to have available everywhere outside of CS50. I was reviewing your code. I tried to solve it with a fflush(stdin) line, since I had that working for me on a Windows program in the past, but it did not work in this case. However, I found this very useful post and built an answer ...

2

Yes, don't use scanf. You have a library that helps you avoid using scanf. The reason is probably related to the fact that scanf still keeps the string you input in its buffer and it won't empty it, nor will it do anything useful with it. scanf is a function that will only work as expected when it reads what you expected (which means it's not really good ...

1

I had the exact same issue. The output looks correct but it is not. Just ask yourself if you really need what you called "spaces2"

1

Taking the first one through the example in the spec: 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 + 8 + 1 + 8 + 6 + 6 + 1 + 6 + ...

1

I would pick small numbers for h (e.g., 1 and 2), determine what the expected results should look like and follow the logic of my code using a pencil and a paper. for example, when h == 1, I should get: # # let's see what happens following the logic of your code! first, if (h < 0) seems to be redundant because the condition of the for loop basically ...

1

I had to change my printf function in order to make a new line after each's line last block.

1

To be frank with you, your program has serious flaws that will not allow it to work in any case. But just to answer your question: why it always prints INVALID4 It's because of this line int sumdigitstotal = number[0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15] + tenscount; because 0+1+2+3+4+5+6+7+8+9+10+11+12+13+14+15 is equal to 110 so the above line is ...

1

By the time you get to line 48 number[i] = creditcardnumber % 10; creditcardnumber is 0 (since you changed it in your counting the digits loop). So that array bit will simply fill with zeros. Next: int sumdigitstotal = number[1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16] + tenscount; What are you expecting this to do? That's simply going to go out to ...

1

You have multiple syntax mistakes. For example: multipliednumber[2|4|6|8|10|12|14|16] = number[2|4|6|8|10|12|14|16] * 2; is not a valid expression in C. What exactly are you trying to achieve? Also if you have multiple expressions in an if block, they should be enclosed in curly braces {}. So the following block will not execute as you intend. else ...

1

If you are trying to make sure that, if you have 2 digits, you are summing them as individuals, this one line equation will handle that: c1 % 10 + c1 / 10; Examples. If c1 = 5 (ie, only one digit) 5%10 is 5 5/10 is 0 5+0 = 5 If c1 = 18 (ie, two digits, so 1+8) 18%10 is 8 18/10 is 1 8+1 = 9

1

Well len can't possibly be in the same time both 15 and 16 and 13 (which is what your condition implies). Instead use while ( len != 15 && len != 16 && len != 13); Edit Let's start with only one variable inside the condition: do { printf("Give me a number\n"); scanf ("%d", &number); } while (number != 0); Now the loop will ...

1

Why not input the number as a string to begin with? (This would also make it easier to check the length is right - you seem to be comparing the size of the number, rather than its length, with 10). Then it is already an array. Of course then you will store the ascii codes of the digits initially, rather than their values, but the conversion is easy.

1

A couple of other things: int i,a,b; declares a new int called i which isn't the one you are incrementing. Instead you should say for (int i = 14; etc) For what it's worth, at this stage in the class, you won't have covered how strings work in C and I think you're better off dealing with the long long number as a number. You can't say: m = n[i]; That ...

1

The problem is in spaces at the second half of pyramid. I have all strings of my output the same length. But check50 wants them to end right after the last # symbol.

1

let's run through the first example: for (int i = 0; i < number_of_digits / 2; i++) { val_1 = (cc_number % mod_val)/n; agg_val_1 = agg_val_1 + val_1 * 2; mod_val = mod_val + 100; n = n + 100; printf ("%i\n", val_1); } cc_number is 1234 loop i = 0: val_1 = (1234%100)/10; --> = 34/10 --> = 3 agg_val_1 = agg_val_1 + 3*2; --&...

1

pow() returns a double and so does floor(). Working with doubles is not the best thing to do since they're not precise. You could lose some data and we don't really have to work with them at all here. I see you're over-complicating things a little bit. You don't really need all that maths to get an array of integers. An easy way to do that is to convert ...

1

Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together.

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