Function Syntax Error with Modulo - c++

I am confused why Visual Studios says that after century) right before the % 7; there needs to be a semicolon. It says I made a syntax error. But I want to take that whole thing in parentheses and then do the modulo 7 to it.
int determineDay(int month, int day, int year) {
const int HUNDRED_YEARS = 100;
int newYear = year % HUNDRED_YEARS;
int century = (year - newYear) / HUNDRED_YEARS;
if (month == 1) {
const int ONE_YEAR = 1;
month = 13;
year = year - ONE_YEAR;
int zellerNumber = (day + floor((13 * (month + 1)) / 5)) + year
+ floor(year / 4) + floor(century / 4) + 5 * century) % 7;
return zellerNumber;
}
else if (month == 2) {
const int ONE_YEAR = 1;
month = 14;
year = year - ONE_YEAR;
int zellerNumber = (day + floor((13 * (month + 1)) / 5)) + year
+ floor(year / 4) + floor(century / 4) + 5 * century) % 7;
return zellerNumber;
}
else {
int zellerNumber = (day + floor((13 * (month + 1)) / 5)) + year
+ floor(year / 4) + floor(century / 4) + 5 * century) % 7;
return zellerNumber;
}
}

As others have stated, you are missing an open parenthesis before day.
Adding to this...
This is a minor syntax error, but they will keep happening if you don't make effort to simplify your logic. Notice that the following appears in all paths of your if-else-if:
int zellerNumber = (day + floor((13 * (month + 1)) / 5)) + year
+ floor(year / 4) + floor(century / 4) + 5 * century) % 7;
Just like in math, you can "factor" this out. Since you know the logic for calculating zellerNumber is the same no matter the case (only the variables change), you can move it below your if-else-if. This also will allow you to have only one return statement. More often than not, you should aim for this.
Simplify your logic and you will see far fewer syntax errors, and the ones that you do encounter will be easier to find.

Related

Result not matching up to Set Variable

I am scratching my brain off trying to figure this one out.
Why are running backs getting 60+ carries a game with this code? I just don't get it.
The team is an array, this for loop is only supposed to run 25 times AT MOST. I've even tried setting the for loop to (int g = 1; g < 20; g++) and yet still it is coming up with 60+ carries a game for no reason. Any help?
hold3 = rand() % (10 - 15 + 1) + 15;
for(int g = 1; g < hold3; g++){
hold = rand() % (15 - team[u].elusivness[2] + 1) + team[u].elusivness[2];
if(hold == 13 || hold == 14){
team[u].carries[2]++;
hold = rand() % (17 - team[u].strength[2] + 1) + team[u].strength[2];
if(hold == 13){
team[u].rushingtouchdowns[2]++;
hold = rand() % 25 + 5;
team[u].rushingyards[2] += hold;
}else{
hold = rand() % (12 - team[u].speed[2] + 1) + team[u].speed[2];
if(hold == 12){
hold = rand() % 15 + 5;
team[u].rushingyards[2] += hold;
}else{
hold = rand() % 5 + 1;
team[u].rushingyards[2] += hold;
}
}
}
}
you are trying getting mod from deviser below 0. hold3 = rand() % (10 - 15 + 1) + 15;

Question about Operator Precedence in C++

The output is 34 but How this expression evaluated?
Could you please show with parenthesis?
Correct operator precedence is:
a += ((((((2 * i++) % 5) * 4) + (--j)) - (3 / k)) + 2);
But I think correct should be:
a += (((((2 * i++) % (5 * 4)) + (--j)) - (3 / k)) + 2);
#include <iostream>
using namespace std;
int main()
{
int a = 3, i = 12, j = 14, k = 16;
a += 2 * i++ % 5 * 4 + --j - 3 / k + 2;
cout << a;
}
The operators *, / and % have same precedence and are grouped left to right. Therefore it is ((2 * i++) % 5) * 4 and not (2 * i++) % (5 * 4)

How can I change the code for the julian to gregorian and back?

I have these two functions and I can't get the right result for conversion from julian to gregorian calendar. The problem is that if a year is divisible by 4000 it's giving me an incorrect result as it is supposed to be a leap year but according to the requirements it's not. So I tried to add the condition but it's not working 100%.Thanks.
int gregorian_to_julian(int year, int month, int day)const {
int result=0;
int cnt=0;//**added by me
if(year>=4000)//**added by me
cnt=year/4000;//**added by me
int a = (14 - month) / 12;
int m = month + 12 * a -3;
int y = year + 4800 -a;
result = day + ((153 * m + 2) /5) + (365 * y) + (y/4) - (y/100) + (y/400) - 32045;
result-=cnt;
return result;
}
int julian_to_gregorian(int sequence, int &year, int &month, int &day)const {
int result = 1;
int cnt=0;
int a = sequence + 32044;
int b = (4 * a +3) / 146097;
int c = a - (146097 * b) / 4;
int d = (4 * c +3) / 1461;
int e = c - (1461 * d) /4;
int m = (5*e +2) / 153;
day = e - ((153 *m +2) /5) +1;
month = m + 3 -12 * (m/10);
year = 100 *b + d - 4800 + (m/10);
if(year>=4000)//**added by me
cnt=year/4000;//**added by me
day+=cnt;//**added by me
return result;
}
The rule is like this:
_leap=false;
if (year% 4==0) _leap=true;
if (year%100==0) _leap=false;
if (year%400==0) _leap=true;
// here _leap holds true if it is a leap year

C++ summing multiples of 3 and 5

I just started C++ programming for three days now and I cannot figure out how to complete this exercise. Basically, I want to sum all multiples of 3 and 5 under 1000. Here is my code:
int sum3n5(int max){
int sum = 0;
for(int i = 1; i <= max; ++i){
if( i%3 == 0 && i%5 == 0 ) { sum += i;}
else if( i%3 == 0 || i%5 == 0 ) { sum +=i;}
return sum;
};
};
Sorry if it is a trivial mistake that I failed to realize.
I always get the result 0 after running this.
int sum3n5(int max){
int sum = 0;
for (int i = 1; i <= max; ++i){
if( i % 3 == 0 || i % 5 == 0 ){
sum += i;
}
}
return sum;
}
You only need the || (logical or) operator, not the && (and certainly not both!). And the return needs to be after the for loop so that the loop can complete before the function returns.
A version without loop:
int sum3n5(int max)
{
return 3 * (max / 3) * (max / 3 + 1) / 2
+ 5 * (max / 5) * (max / 5 + 1) / 2
- 15 * (max / 15) * (max / 15 + 1) / 2;
}
It uses the fact that 1 + 2 + .. + n == n * (n + 1) / 2

Partition function based on Euler's formula

I am trying to find the partitions of a number using the Euler's formula for that:
It produces results like:
P(3) = P(2) + P(1) = 3
P(4) = P(3) + P(2) = 3+ 2 = 5
P(5) = P(4) + P(3) - P(0) = 5 + 3 - 1 = 7
P(6) = P(5) + P(4) - P(1) = 7 + 5 - 1 = 11 and so on..
* P(0) = 1
It produces two positive and then two negative values and so on.
I am using recursion for that but the code goes into an infinite loop without producing any result.
long result = 0;
long counter = 0;
class Euler
{
public:
long Partition(long n)
{
int exponent = 0;
if (n < 0)
{
return 0;
}
else
{
counter = counter + 1;
exponent = pow(-1, counter - 1) ;
if (n == 0)
{
n = 1;
}
return Partition((exponent * (n - ( (counter * ( (3 * counter) - 1)) / 2)))) +
Partition(((exponent * (n - ( (counter * ( (3 * counter) + 1)) / 2)) )));
}
}
};
int main(int argc, char** argv)
{
long result= 0;
long a = 3;
Euler * obj = new Euler();
long s = obj->Partition(a);
std::cout << s;
return 0;
}
Your global counter is modified by the first call to Partition, so the second one operates on a different one; in fact, the counter changes more or less unpredictably.
Do not use globals.