Result not matching up to Set Variable - c++

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;

Related

CPP won't generate text?

I have some CPP code to generate Lorem Ipsum style text. It works when I ask it for one sentence at a time, but when I tell it to mass generate sentences it generates tons of sentences that are just spaces and then periods. Here's the code (modified for confidentiality):
srand(time(NULL));
string a[9327] = {"Str1", "Str2", "Str3" . . .};
int loop_1 = 0;
int loop_2 = 0;
while (loop_2 <= 100000) {
while (loop_1 <= (rand() % 38) + 2) {
int value = rand() % (9327 - (rand() % (9327 - (rand() % (9327 - (rand() % 9327))))));
cout << a[value] << " ";
loop_1 = loop_1 + 1;}
cout << "\b. ";
loop_2 = loop_2 + 1;
}
I'm sorry if this is an incompetent question. I'm a conlanger/composer normally but I had to throw together some code for a project––so I'm still just barely learning C++.
Okay, I mean, this code doesn't make a lot of sense but to answer the question, note that the only way the inner loop can not issue random strings is if it never runs and it will not run if loop_1 is greater than (rand() % 38) + 2 which is a random number from 2 to 40. Once loop_1 is greater than 40 the inner loop can never run, because loop_1 only increases.
But anyway, before that occurs, if you want the inner loop to definitely run then test that it does ... Also might as well get rid of loop_2 because it isn't doing anything once loop_1 is greater than 40.
Replacing 9327 with 7, I get
int main() {
srand(time(NULL));
string a[7] = { "aaaaaaaaaa ", "bbbbbbbb ", "ccccccccccccc ", "dddddddd ", "eeeeeeeeeee ", "ffffffffff ", "ggggggg "};
int loop_1 = 0;
while (loop_1 < 40) {
auto num = (rand() % 38) + 2;
if (loop_1 > num) {
continue;
}
while (loop_1 <= num) {
int value = rand() % (7 - (rand() % (7 - (rand() % (7 - (rand() % 7))))));
cout << a[value] << " ";
loop_1 = loop_1 + 1;
}
cout << "\b. ";
}
}

Function Syntax Error with Modulo

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.

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

Project Euler #27 [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm challenging myself in Project Euler but currently stuck on problem 27, in which the problem states:
Euler published the remarkable quadratic formula:
n² + n + 41
It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.
Using computers, the incredible formula n² 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, 79 and 1601, is 126479.
Considering quadratics of the form:
n² + an + b, where |a| 1000 and |b| 1000
where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces > the maximum number of primes for consecutive values of n, starting with n = 0.
I wrote the following code, which gives me the answers pretty quick but it is wrong (it spits me (-951) * (-705) = 670455). Can somebody check my code to see where is/are my mistake(s)?
#include <iostream>
#include <vector>
#include <cmath>
#include <time.h>
using namespace std;
bool isprime(unsigned int n, int d[339]);
int main()
{
clock_t t = clock();
int c[] = {13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311};
int result[4];
result[3] = 0;
for (int a = -999; a < 1000; a+=2)
{
for (int b = -999; b < 1000; b+=2)
{
bool prime;
int n = 0, count = 0;
do
{
prime = isprime(n*n + a*n + b, c);
n++;
count++;
} while (prime);
count--;
n--;
if (count > result[3])
{
result[0] = a;
result[1] = b;
result[2] = n;
result[3] = count;
}
}
if ((a+1) % 100 == 0)
cout << a+1 << endl;
}
cout << result[0] << endl << result[1] << endl << result[2] << endl << result[3] << endl << clock()-t;
cin >> result[0];
return 0;
}
bool isprime(unsigned int n, int d[339])
{
int j = 0, l;
if ((n == 2) || (n == 3) || (n == 5) || (n == 7) || (n == 11))
return 1;
if ((n % 2 == 0) || (n % 3 == 0) || (n % 5 == 0) || (n % 7 == 0) || (n % 11 == 0))
return 0;
while (j <= int (sqrt(n) / 2310))
{
for (int k = 0; k < 339; k++)
{
l = 2310 * j + d[k];
if (n % l == 0)
return 0;
}
j++;
}
return 1;
}
There's a bug in isprime function.
In your function, you check all 2310 * j + d[k] where j < int (sqrt(n) / 2310)) to ensure the target n is a prime number. However, an additional condition that l < sqrt(n) is also required, or you will over-exclude some prime numbers.
For example, when a = 1, b = 41 and n = 0, your function will check whether 41 is a prime number starting from j = 0. So whether 41 can be divisible by 2310 * 0 + d[7] = 41 is also verified, which leads to a false return.
This version should be correct:
bool isprime(unsigned int n, int d[])
{
int j = 0, l;
if ((n == 2) || (n == 3) || (n == 5) || (n == 7) || (n == 11))
return 1;
if ((n % 2 == 0) || (n % 3 == 0) || (n % 5 == 0) || (n % 7 == 0) || (n % 11 == 0))
return 0;
double root = sqrt(n);
while (j <= int (root / 2310))
{
for (int k = 0; k < 339; k++)
{
l = 2310 * j + d[k];
if (l < root && n % l == 0)
return 0;
}
j++;
}
return 1;
}

C++ Faulty Random Numbers [duplicate]

This question already has answers here:
How unique is rand() in C?
(4 answers)
Closed 8 years ago.
I'm creating a machine that generates a shotgun with random components that effect the overall quality of the gun. I'm having a problem generating the random parts that will comprise the shotgun. There are 4 parts that have to be generated. When I created each of these functions, I tested them individually and they all work but when I try to put them together with the createChromo() function, the numbers are individually random. I should be getting results like 2131 and 1332, but I keep getting 1111 or 1112 or 2221 or 2222....Here is the code:
int generateButt()
{
srand(unsigned(time(NULL)));
int buttType = rand() % 3 + 1;
if(buttType == 1)
{
accuracy = rand() % ((5 - 2) + 2) / 10.0;
fireRate = fireRate - 0.3;
}
if(buttType == 2)
{
accuracy = rand() % ((8 + 5) + 5)/ 10.0;
fireRate = fireRate - 0.2;
}
if(buttType == 3)
{
accuracy = rand() % ((11 + 8) + 8) / 10.0;
fireRate = fireRate - 0.1;
}
return buttType;
}
int generateBarrel()
{
srand(unsigned(time(NULL)));
int barrelType = rand() % 3 + 1;
if(barrelType == 1)
{
range = rand() % (16 - 5) + 5;
power = power + 3;
}
if(barrelType == 2)
{
range = rand() % (21 - 16) + 16;
power = power + 1;
}
if(barrelType == 3)
{
range = rand() % (26 + 21) + 21;
power = power - 1;
}
return barrelType;
}
int generateBullet()
{
srand(unsigned(time(NULL)));
int bulletType = rand() % 3 + 1;
if(bulletType == 1)
{
power = rand() % (16 - 10) + 10;
range = range + 5;
}
if(bulletType == 2)
{
power = rand() % (26 - 16) + 16;
range = range + 1;
}
if(bulletType == 3)
{
power = rand() % (35 - 26) + 26;
range = range - 2;
}
return bulletType;
}
int generateAction()
{
srand(unsigned(time(NULL)));
int actionType = rand() % 2 + 1;
if(actionType == 1)
{
fireRate = 1.5;
accuracy = accuracy + 0.2;
}
if(actionType == 2)
{
fireRate = 2.0;
accuracy = accuracy - 0.1;
}
return actionType;
}
void createChromo(int a, int b, int c, int d)
{
cout <<a<<b<<c<<d<<"\n";
}
int main()
{
for(int i = 0; i < popSize; i++)
createChromo(generateButt(), generateBarrel(), generateBullet(), generateAction());
system("pause");
return 0;
}
You're calling srand each time you call either function. What srand does is seed the generator for a new string of random numbers. You base that seed off of the current second, so if it's called in the same second, the seed will be the same as last time, and thus the sequence of random numbers obtained from rand() will be as well.
Call srand(time(NULL)); once at the beginning of your program to just have one sequence, and keep using the next number in that one sequence instead of starting the same sequence over.
If you have access to C++11, you might consider using the <random> header as well.
the concept of random does not exist in the entire computation world
the function srand() is used to set a seed for the rand() function
if you set 2 different seed your generator of pseudo-random numbers will behave in 2 different ways