CS50 Pset2 Readability last part of the code won`t work - cs50

I'm having trouble with the last part of the code for pset2 readability.
It will grade ok.
But when grade >= 16, it does not print Grade 16+
And when grade < 1, it does not print Before Grade 1
This is the code
if (grade >= 0 || grade < 16)
{
printf("Grade %i\n", (int) round(grade));
}
else if (grade >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Before Grade 1\n");
}

if (grade >= 0 || grade < 16) { printf("Grade %i\n", (int) round(grade));
You shouldn't put || you should put &&
if (grade >= 0 && grade < 16) { printf("Grade %i\n", (int) round(grade));

Related

cs50 Credit issue with identifying card company

I'm completely new to programming and have been working through pset1 this week but credit is proving to be a bit tough. I've managed to get the checksum working but I'm having real trouble identifying which company the card belongs to, the last part of the code seems to introduce more errors every time I try to fix it. I would really appreciate it if anybody could point me in the right direction.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
long long ccnumber;
do
{
ccnumber = get_long_long("Credit card number: ");
}
while (ccnumber < 0);
long long cc = ccnumber;
int s1;
while (cc > 0)
{
int l1 = cc % 10;
s1 = s1 + l1;
cc = cc / 100;
}
cc = ccnumber / 10;
int s2;
while (cc > 0)
{
int l1 = cc % 10;
l1 = l1 * 2;
if (l1 > 9) l1 = l1 % 10 + l1 / 10;
s2 = s2 + l1;
cc = cc / 100;
}
int s3 = s1 + s2;
if (s3 % 10 == 0)
s3 = true;
long long n = ccnumber;
int count = 0;
while (n != 0)
{
n /= 10;
++count;
}
int twonumbers = ccnumber;
while(twonumbers >= 100)
{
twonumbers = twonumbers / 10;
}
int firstnumber = ccnumber;
while(firstnumber >= 10)
{
firstnumber = firstnumber / 10;
}
if (twonumbers == 34 || 37 && (count == 15 ))
printf("AMERICAN EXPRESS\n");
else if (twonumbers == 51 || 52 || 53 || 54 | 55 && (count == 16 ))
printf("MASTER CARD\n");
else if (firstnumber == 4 && (count == 13 || 16 ))
printf("VISA\n");
else
printf("INVALID\n");
}
Your ending if statements are not correct:
if (twonumbers == 34 || 37 && (count == 15 ))
...
should be
if ((twonumbers == 34 || twonumbers == 37) && (count == 15 ))
And the same for each if statement - you have to repeat what is being compared for every or statement or the compiler will assume "true" as in this case, 37 is no equal to 0.
Not, you can also compare ranges as well:
else if (twonumbers >= 51 && twonumbers <= 55 && count == 16)
Note I also correct a binary or there on the last compare (you had only 1 | and not 2 ||)
The last is
else if (firstnumber == 4 && (count == 13 || count == 16 ))
I also question what:
int s3 = s1 + s2;
if (s3 % 10 == 0)
s3 = true;
is doing as this will actually always be "true" in a C sense, but in reality, you don't actually use s3 anywhere so I am confused.
Lastly, turn on all compiler warning. It would have told you about a lot of the things I mention here.
Now, I am not saying that this is the only thing wrong here, but it was the most obvious.

how do I properly re-prompt a statement in mario.c?

im having problems with my code. at first, it would reject the answer inputted and stopped the code. but as soon as I added a return function. the code would abruptly end. what should I do?
here is my code. it's in C
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do
{
height = get_int("pick a number, any number!: ");
}
while ( height < 1 && height > 8);
// here is my problem
return (-3);
if ( height > 0 && height < 9)
{
int counter = 0;
for ( int row = 0; row < height; row++)
{
if ( counter != height)
{
for ( int spaces = (height - 1) - counter; spaces > 0; spaces--)
{
printf (" ");
}
for ( int hashes = 0; hashes <= counter; hashes++)
{
printf ("#");
}
printf(" ");
for ( int hashes = 0; hashes <= counter; hashes++)
{
printf ("#");
}
printf ("\n");
counter++;
}
}
}
}
P.S. I'm kinda new to this
You have a mistake in your do-while loop. You should say
while (height < 1 || height > 8)
Your current code says
while (height < 1 && height > 8)
which makes no sense because the height can never be both smaller than 1 and greater than 8.

CS50 Credit: Always getting INVALID return

Was attempting Credit from CS50 and kept getting INVALID return from my code. I approached this problem by using arrays even though it may not have been the best method. Code compiles with no issues.
My pseudocode logic was:
1) obtain card number
2) use loop to find number of digits
3) check if card contains 13, 15 or 16 digits
4) if so, write digits from long into array
5) have a copy of original array to multiply every other number by 2
6) add the digits of the product
7) check for card length and starting digits
Here is my code:
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// Get credit card number
long num = get_long("Number: ");
// Find number of digits
int digits = 0;
while (num > 0)
{
num /= 10;
digits++;
}
// Check if number of digits is within possible range
if (digits != 13 && digits != 15 && digits != 16)
{
printf("INVALID\n");
}
int originalnumber[digits];
// Write each digit of credit card number into an array
for (int i = digits - 1; i >= 0; i--)
{
originalnumber[i] = num % 10;
num = num / 10;
}
// Multiply alternate digits by 2
int number[digits];
for (int i = 0; i < digits; i++)
{
number[i] = originalnumber[i];
}
for (int i = 1; i < digits; i+=2)
{
number[i] = number[i] * 2;
}
// Add product digits
int sum = 0;
int temp;
for (int i = 0; i < digits; i++)
{
temp = (number[i] % 10) + ((number[i] / 10) % 10);
sum = sum + temp;
}
// Check for card length and starting digits
// AMEX
if (digits == 15)
{
if (originalnumber[14] == 3 && sum % 10 == 0 && (originalnumber[13] == 4 || originalnumber[13] == 7))
{
printf("AMEX\n");
return 0;
}
}
// MasterCard
if (digits == 16)
{
if (originalnumber[15] == 5 && sum % 10 == 0 && (originalnumber[14] == 1 || originalnumber[14] == 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5))
{
printf("MASTERCARD\n");
return 0;
}
}
// Visa
if (digits == 13)
{
if (originalnumber[12] == 4 && sum % 10 == 0)
{
printf("VISA\n");
return 0;
}
}
if (digits == 16)
{
if (originalnumber[15] == 4 && sum % 10 == 0)
{
printf("VISA\n");
return 0;
}
}
printf("INVALID\n");
return 1;
}
I tried debug50 and it seems that when I try to sum the digits together using temp and sum, the loop completes with sum still being 0. May I know what is wrong here? Is the flow of my pseudocode wrong or are there any glaring mistakes that I may have overlooked? (stared at this for way too long..)
Thank you in advance!
If sum is always 0, regardless of whether that is what you expect, sum % 10 would always be 0, so that is not the "false" that is failing the tests.
Which should direct your attention to originalnumber.
What is the value of num after this loop?
while (num > 0)
{
num /= 10;
digits++;
}

CS50 pset 1 - Credit, more comfortable

I have the following errors
:( identifies 5105105105105100 as MASTERCARD
expected "MASTERCARD\n", not "INVALID\n"
:( identifies 4111111111111111 as VISA
expected "VISA\n", not "INVALID\n"
:( identifies 4012888888881881 as VISA
expected "VISA\n", not "INVALID\n"
but for the card to be correct the last digit should be which is correct in my case. Please help
------------------- CODE -----------------------
#include <cs50.h>
#include <stdio.h>
int main()
{
long long cardNumber;
// get a card number from the user
do
{
printf("Your card number please: ");
//scanf("%lld", &cardNumber);
cardNumber = get_long_long();
}
while (cardNumber < 0);
//check the length of the card
int counter = 0;
long long cardNumberNeo = cardNumber;
while (cardNumberNeo > 0)
{
cardNumberNeo = cardNumberNeo / 10;
counter++;
}
if (counter != 15 && counter != 16 && counter != 13)
{
printf("INVALID\n");
}
// Array of card number
cardNumberNeo = cardNumber;
int cardNumberArr[counter], cardNumberArrNeo[counter], i;
for (i=0; i<counter; i++)
{
cardNumberArr[counter-i-1] = cardNumberNeo % 10;
cardNumberArrNeo[counter-i-1] = cardNumberArr[counter-i-1];
cardNumberNeo = cardNumberNeo / 10;
}
for (int i = 1; i < counter; i+=2)
{
cardNumberArrNeo[i] = cardNumberArrNeo[i] * 2;
}
int oddNumber = 0;
int temp;
for (int i = 0; i < counter; i++)
{
temp = (cardNumberArrNeo[i] % 10) + (cardNumberArrNeo[i]/10 % 10);
oddNumber = oddNumber + temp;
}
if (oddNumber % 10 == 0)
{
// Check the type of the card
if ( ((cardNumberArr[0] == 3 && cardNumberArr[1] == 4) || (cardNumberArr[0] == 3 && cardNumberArr[1] == 7)) && counter == 15 )
{
printf("AMEX\n");
}
else if (cardNumberArr[0] == 5 && cardNumberArr[1] >= 1 && cardNumberArr[1] <= 5 && counter == 16)
{
printf("MASTERCARD\n");
}
else if (cardNumberArr[0] == 4 && (counter == 13 || counter == 16 ))
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else
{
printf("INVALID\n");
}
return 0;
}
This will work.....as long as the credit card number has an odd length. Remember, Luhn's algorithm rule [emphasis added]:
Multiply every other digit by 2, starting with the number’s
second-to-last digit, and then add those products' digits together.
This loop for (int i = 1; i < counter; i+=2) processes the wrong digits for a card with even length. Such numbers need to start at the 0th index in order to end up at the correct place (ie penultimate digit).

Trying to add a char to my if statement

This is Java/Groovy (we're learning syntax first). I've been stuck on it for 2 days.
This is my code:
char am = "a";
char pm = "p";
def to24hour(int hour) {
if (hour >= 1 && hour <= 12) {
int newHour = hour + 12;
return newHour;
} else if (hour >= 13 && hour <= 24) {
int newHour = hour
return newHour;
} else if (hour == 0){
int newHour = hour
return "invalid number used!";
} else {
return "invalid number used!";
}
}
to24hour(3);
This is the question:
Write a function called to24Hour that takes two parameters: an hour value and a letter that should be either A or P. The function converts a time specified as AM or PM to hours on the 24 hour clock. So, for example, calling the function with values 5 and P should return 17 whereas 5 and A should return 5. What are the possible problems with such a function (if invalid values are used)? How could you deal with these problems?
Try this:
char am = "a";
char pm = "p";
int to24hour(int hour, char clock) {
if (hour >= 1 && hour <= 11) {
if(clock == "p") return hour + 12
else return hour
} else if(hour == 12) {
if(clock == "p") hour
else return 0
} else {
throw Exception("invalid number used!")
}
}
[1..12, (1..11).toList() + [0]]
.transpose()
.each { assert to24hour(it[0], am) == it[1] }
[1..12, (13..23).toList() + [12]]
.transpose()
.each { assert to24hour(it[0], pm) == it[1] }

Resources