C problem requires to find students semester propably from year and age - c

I have an exercise where I have as input the name, the age and the current university year of whoever, and it asks the minimum years for him to graduate, the person's age when he graduates and how many semesters till the graduation. How can we find the semester the person is in? Thought I had found a way but it duplicates logical expressions so it's not corrects.
/*My attempt*/
if(age % 2 == 1 && year == 1 || age % 2 == 0 && year == 1)
semester = 1;
else if(age % 2 == 0 && year == 1 || age % 2 == 1 && year == 1)
semester = 2;
else if(age % 2 == 0 && year == 2 || age % 2 == 1 && year == 2)
semester = 3;
else if(age % 2 == 1 && year == 2 || age % 2 == 0 && year == 2)
semester = 4;
else if(age % 2 == 1 && year == 3 || age % 2 == 0 && year == 3)
semester = 5;
else if(age % 2 == 0 && year == 3 || age % 2 == 1 && year == 3)
semester = 6;
else if(age % 2 == 0 && year == 4 || age % 2 == 1 && year == 4)
semester = 7;
else if(age % 2 == 1 && year == 4 || age % 2 == 0 && year == 4)
semester = 8;

Related

What do these return conditions do in a Boolean function?

On codecademy there exists a course on C, which includes a project on how to make a calendar. This project includes a boolean function which decides if a given year is a leap year or not. Code:
bool is_leap_year(int year) {
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
}
Given my beginner understanding of operators and return statements, my reading of this code would be: "A given year will be a leap year if it is divisible by 4 AND 100 OR 400."
But this would mean that 1992 wouldn't be a leap year, and 1900 would be, which is plainly wrong.
How come then, that when I run the code and input these years, it does return a correct answer?
You appear to think
x || y == 0
means
x == 0 || y == 0
But it doesn't.
x || y == 0 doesn't mean "x or y is equal to zero".
x || y == 0 means "x, or y is equal to zero".
Put more clearly,
x || y == 0 means "(x) is true or (y is equal to zero) is true".
Since true simply means non-zero in C,
x || y == 0
is equivalent to
x != 0 || ( y == 0 ) != 0
That means the formula checks if the year isn't divisible by 100.
year % 4 == 0 Year is divisible by 4.
year % 100 Year isn't divisible by 100.
year % 400 == 0 Year is divisible by 400.
(Year is divisible by 4) and ( (Year isn't divisible by 100) or (Year is divisible by 400) )
How this would normally be stated in English:
It's a leap year if it's divisible by 4, but not by 100. Except years divisible by 400 are leap years.
And here's how things are calculated:
year % 4 == 0 && (year % 100 || year % 400 == 0)
1992 % 4 == 0 && (year % 100 || year % 400 == 0)
0 == 0 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (1992 % 100 || year % 400 == 0)
1 && ( 92 || year % 400 == 0)
1 && 1
1
The right-hand side of || isn't evaluated because its left-hand is true.
This return statement
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
can be equivalently rewritten like
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
or like
return (year % 4 == 0 && year % 100 != 0 ) || (year % 4 == 0 && year % 400 == 0));
The condition means that a leap year is divisible by 4 and either not divisible by 100 or divisible by 400. So 1992 is a leap year because it is divisible by 4 but not divisible by 100. And 1900 is not a leap year because though it is divisible by 4 but it also divisible by 100 and not divisible by 400. That is neither this condition (year % 4 == 0 && year % 100 != 0 ) nor this condition (year % 4 == 0 && year % 400 == 0)) is satisfied.

Difference between if (j%3 != 0 && j%4 != 0) and (!((j%3 == 0) && (j%4 == 0))), and why (!((j%3 == 0) || (j%4 == 0))) works if || means OR

I don't understand why the following syntax are not the same
if (j%3 != 0 && j%4 != 0)
if (!((j%3 == 0) && (j%4 == 0)))
Yet, the followings are.
if (j%3 != 0 && j%4 != 0) /
if (!((j%3 == 0) || (j%4 == 0)))
Why is that
!(A && B) != !A && !B
and
!(A || B) == !A && !B / !(A && B) == !A || !B
This is the mathematic boolean logic.
A and B <=> not (not A or not B)
not A and not B <=> not (A or B)
not (A and B) <=> not A or not B
For proving these equations, use value tables.
0 & 0 = 0 !(1 | 1) = !1 = 0
0 & 1 = 0 !(1 | 0) = !1 = 0
1 & 0 = 0 !(0 | 1) = !1 = 0
1 & 1 = 1 !(0 | 0) = !0 = 1
Say I have a red car and grey house, then "my car is blue and my house is grey" is false (!(A && B)). Yet "my car isn't blue and my house isn't grey" is also false (!(!A && !B)). So !(A && B) and !A && !B clearly aren't equivalent.
The actual negation of "my car is blue and my house is grey" is "my car isn't blue or my house isn't grey" (!A || !B).
We can also clearly see this is the case by plugging all possible values into the equations.
A
B
!(A && B)
!A || !B
!(A || B)
!A && !B
0
0
!(0 && 0) = !0 = 1
!0 || !0 = 1 || 1 = 1
!(0 || 0) = !0 = 1
!0 && !0 = 1 && 1 = 1
0
1
!(0 && 1) = !0 = 1
!0 || !1 = 1 || 0 = 1
!(0 || 1) = !1 = 0
!0 && !1 = 1 && 0 = 0
1
0
!(1 && 0) = !0 = 1
!1 || !0 = 0 || 1 = 1
!(1 || 0) = !1 = 0
!1 && !0 = 0 && 1 = 0
1
1
!(1 && 1) = !1 = 0
!1 || !1 = 0 || 0 = 0
!(1 || 1) = !1 = 0
!1 && !1 = 0 && 0 = 0
Two equivalences that are identified by the above table:
!(A && B) = !A || !B
!(A || B) = !A && !B
These are known as De Morgan's laws.

How to calculate how many days to the person's birthdate from the beginning of their birthyear in C?

This us how much I've gotten
int myd;
int mym;
int myy;
printf("Enter your day of birth:");
scanf("%d", &myd);
printf("Enter your month of birth:");
scanf("%d", &mym);
printf("Enter your year of birth:");
scanf("%d", &myy);
int month;
if (mym == 1 || mym == 3 || mym == 5 || mym == 7 || mym == 8 || mym == 10 || mym == 12)
{month = 31;}
else if (mym == 4 || mym == 6 || mym == 9 || mym == 11)
{month = 30;}
else if (mym == 2 && myy %4 !=0 && myy %100 ==0)
{month = 28;}
else if (mym == 2 && myy %4 ==0 && myy %100 !=0)
{month = 29;}
I'm not sure how to increment the days of the months before the birth month.
If I knew that, maybe I can add that increment + birth date = no. of days until birthday from the beginning of their birth year
You need an array, which contains a number of days in each month. Each element beside the 2nd one will have a literal value, either 30 or 31:
int days_in_month[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
For the 2nd element, representing February, you need to calculate the number of days based on the year. You already have that, but it is not correct. For example this condition:
... && myy % 4 != 0 && myy % 100 == 0)
will never be true. Even after after changing the operator:
... && (myy % 4 != 0 || myy % 100 == 0))
it is still not correct because it will match years divisible by 400, which are leap years. The correct condition is:
(myy % 4 != 0 || myy % 100 == 0) && myy % 400 != 0
or for leap years (basically a negation of the above):
(myy % 4 == 0 && myy % 100 != 0) || myy % 400 == 0
With that, the rest is just a loop through days_in_month, summing the values of days in months previous to the birthday, and at the end adding the day of month of the birthday. You can see the code in godbolt. The output of some examples:
]$ echo -en "1\n3\n1900\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1900: 60
]$ echo -en "1\n3\n1996\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1996: 61
]$ echo -en "1\n3\n1997\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-1997: 60
]$ echo -en "1\n3\n2000\n" | ./a.out
Enter your day of birth:Enter your month of birth:Enter your year of birth:
01-03-2000: 61
As a last point, one could argue that a year starts with Jan 1st, so there is 0 days from the beginning of the year to Jan 1st :-) If that's the case, just subtract 1 from the final result.
EDIT
One more thing, before doing any calculations, validate user inputs first. In particular that the month is between 1 and 12 (inclusive), or otherwise you will start reading garbage past the days_of_month array.
You can use codes like this:
int mym;
int answer[] = {31,0,31,30,31,30,31,31,30,31,30,31};
printf("Enter your month of birth:");
scanf("%d", &mym);
if(mym==2){
if(myy %4 != 0 && myy %100 ==0){
answer[mym] = 28;
}
if(myy %4 ==0 && myy %100 !=0){
answer[mym]= 29;
}
}
else{
printf("%d",answer[mym]);
}
I think you should use array ( like answer[] ) for convenience.

It only runs the first " if " why so? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
it only prints " Sunday " even if I typed another date in. how do I resolve this?
i usually don't use int main() but a friend of mine did this method and it worked but I'm using a program that is written by the school so I can't paste it directly. but I'm pretty sure I copied it corectly
#include <stdio.h>
int date;
int main()
{
printf(" \n\n JUNE 2020 \n");
printf(" SUN MON TUE WED THU FRI SAT \n");
printf(" 1 2 3 4 5 6 \n");
printf(" 7 8 9 10 11 12 13 \n");
printf(" 14 15 16 17 18 19 20 \n");
printf(" 21 22 23 24 25 26 27 \n");
printf(" 28 29 30 \n\n\n");
printf("Here is your schedule for June 2020 \n");
printf("Please select a date: ");
scanf("%d", &date);
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
{
printf("sunday! ");
}
else if (( date == 1 ) || ( date == 8 ) || ( date == 15 ) || ( date == 22 ) || ( date == 29 ))
{
printf("monday! ");
}
else if (( date == 2 ) || ( date == 9 ) || ( date == 16 ) || ( date == 23 ) || ( date == 30 ))
{
printf("tuesday! " );
}
else if (( date == 3 ) || ( date == 10 ) || ( date == 17 ) || ( date = 24 ))
{
printf("wednesday!");
}
else if (( date == 4 ) || ( date == 11 ) || ( date == 18 ) || ( date == 25 ))
{
printf("thursday!");
}
else if (( date == 5 ) || ( date == 12 ) || ( date == 19 ) || ( date == 26 ))
{
printf("friday!");
}
else if (( date == 6 ) || ( date == 13 ) || ( date == 20 ) || ( date == 27 ))
{
printf("saturday!");
}
return 0;
} // end
Your problem
( date || 28 ))
Is always true and hence first if is always true.
Change to
( date == 28 ))
Explanation
( date || 28 ))
Is equivalent to if (date || true) and it's always true.
The problem with your code is that you have probably made a typo in the line:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
The condition is always evaluated as true because of ( date || 28 ).
Change it to ( date == 28 ) and everything should work as expected.
There is an error in the last condition of your first if statement.
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
should be changed to:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date == 28 ))
Previously, it was evaluating ( date || 28 ), which is true as date is a positive integer. This made the entire condition true.
In your first if statement, you have (date || 28) which should have been (date == 28) instead:
if (( date == 7 ) || ( date == 14 ) || ( date == 21 ) || ( date || 28 ))
{
printf("sunday! ");
}
The reason that it always runs only the first if statement is that else if only gets checked if the above if statement is not true. In your case, the first if statement is always true, because ( date || 28 ) expression is always true.

Calculate the date after X days with a user input date

First and foremost, I am new so I am sorry for any incorrect terms I use for explaining. While I do speak English quite well I don't learn C in English,so I'll do my best to translate and understand correctly.
I am trying to write a program where the program asks a user to input a date (0<= year <=9999) and the programs then validates if that is a logical date (including leap years). if it's not logical the program asks to input a date again, until it is. When it's a logical date the program then prints it and asks the user to input a number (-1000000 <= N <= 1000000) of days and then print the date after\before the N amount of days.
I have the Date code pretty much set up,though I can't seem to get the while loop to return to the beginning when the date is wrong.
I also am trying to figure out a an algorithm to calculate the date after\before N days.
sorry for going on so long and thank you for all helpers/insights.FYI because this is a first assignment I am not allowed to use arrays or strings, so I understand the code will be bulky
void main()
{//d=day m=month y=year
int dd, mm, yy, flag = 0;
printf("Enter day month and year:\n");
scanf("%d\n%d\n%d", &dd, &mm, &yy);
//year check
while (flag==0)
{
if (yy >= 0 && yy <= 9999 && mm >= 1 && mm <= 12 && dd >= 1 && dd <= 31);
{
//month check
if (mm >= 1 && mm <= 12);
{
//day check
if ((dd >= 1 && dd <= 31) && (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12))
printf("\n%d\n%d\n%d", dd, mm, yy);
else
if ((dd >= 1 && dd <= 30) && (mm == 34 || mm == 6 || mm == 9 || mm == 1))
printf("\n%d\n%d\n%d", dd, mm, yy);
//leap year check
if ((dd >= 1 && dd <= 28) && mm == 2)
printf("\n%d\n%d\n%d", dd, mm, yy);
else
if ((dd == 29) && mm == 2 && (yy % 400 == 0 || yy % 100 != 0 && yy % 4 == 0))
printf("\n%d\n%d\n%d", dd, mm, yy);
}
}
}(flag = 1);
}

Resources