An unintended benefit of CS50 is learning how to read and adhere to specifications, an invaluable skill if one intends to code for pay :)
From the spec:
Incidentally, so that we can automate some tests of your code, we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n.
Your printf prints ...
The problem is simple. You have created two string arrays. Each holds the 26 letter alphabet, one lower case, the other upper case. They are created in sequential memory. In this case, the UC array is first, followed immediately by the lc array.
You then try to print them out as strings, using the base variable (i.e., uppercase and lowcase) as the ...
It's not enough to apply the %26 to the cipher. You need to shift your character into range 0-25
plain[i] - 'A'
then add cipher
plain[i] - 'A' + cipher
then apply %26 to wrap numbers back to 0-25
(plain[i] - 'A' + cipher) % 26
and shift back to letters
(plain[i] - 'A' + cipher) % 26 + 'A'
Similar for lowercase, just using 'a' instead of 'A'.
Your code reads from the outside of cipher which contains unknown values because your cipher is not terminated by \0.
Here is the link that you can debug memory errors or unexpected behavior in the future. The instruction for other C psets.
I copy & pasted the error message here.
Memory access error: reading from the outside of a memory space; ...
You've got a few issues, but the one most affecting you is that you are printing values in your shift function that aren't printable. You shouldn't be printing anything in your shift function. What happens is you end up with, say, a value of 8 (backspace) or a value of 13 (carriage return), which, when printed to the terminal, do exactly that. You end up ...
check50 does an automated test.
This means that some program will look at your output, and tries to see whether it is the same as expected.
Actually, the specifications say:
we ask that your program’s last line of output be only the minimum number of coins possible: an integer followed by \n.
Therefore, the program looks at all your output, and checks (...
The problem is in vertical scaling - the code isn't complete.
The code is scaling horizontally just fine. It also looks like it's set up to repeat processing each line for vertical replication, but it doesn't go back to the beginning of the line before trying to reprocess it.
The net effect is that the code is correctly scaling horizontally, but ...
The double characters come from two printfs being executed for one character (you have an if and an if-else, the first might print sometimes, the other would always print).
The way you're dealing with the wrap-around is less-than-ideal. For example, if plain[i] were 'Z' and key were 7, 'Z'+7 would be 'a', but so would be 'a'+0. Instead, you should ...
It does print it, but as you haven't included any '\n' after it, it's printing at the beginning of your command line.
printf("%d\n", coins); //Output
That ought to fix it.
If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the ...
It's not the \n line feed. (I wish they'd fix that slightly misleading message.)
It's giving you an error because 22 is the wrong answer! 22 coins means 16 quarters, 1 dime, 1 nickel and 4 pennies. It should be 16 quarters and 2 dimes. Try printing out the contents of j right after it was created and initialized. ;-)
If this answers your question, ...
Actually, it is processing your encoded letters. Your formula for encoding the letters is wrong. It produces ASCII values that correspond to unprintable control characters.
You should review the class materials on how to encode a letter, paying strict attention to ASCII values vs. numbers from 0 to 25 inclusive.
If this answers your question, please click ...
Fseek works by moving the file pointer, indicated by the FILE pointer in our case, forward or backward. So whenever you use functions that work with file streams, you should keep in mind where the file position pointer. So when you make changes to the output file using fputs, you are moving the file pointer. Clearly, you can see why when you try to read and ...