Extend from 3 hour slot to N days - c++

I want to set up params.arrival_rate every 3 hours of sim_time. This comprises a day but I would like to extend to N days. So, each time slot of 3 hours (in the code below is in minutes) of this N day, params.arrival_rate takes a value. Any optimal function to do so?
My actual code looks like this:
if(sim_time >= 0 && sim_time <= 180) //00:00 - 03:00
{
params.arrival_rate = 10;
}
else if(sim_time > 180 && sim_time <= 360) //03:00-06:00
{
params.arrival_rate = 9;
}
else if(sim_time > 360 && sim_time <= 540) //06:00-09:00
{
params.arrival_rate = 10.5;
}
else if(sim_time > 540 && sim_time <= 720) //09:00-12:00
{
params.arrival_rate = 12;
}
else if(sim_time > 720 && sim_time <= 900) //12:00-15:00
{
params.arrival_rate = 11.5;
}
else if(sim_time > 900 && sim_time <= 1080) //15:00-18:00
{
params.arrival_rate = 11;
}
else if(sim_time > 1080 && sim_time <= 1260) //18:00-21:00
{
params.arrival_rate = 10.5;
}
else if(sim_time > 1260 && sim_time <= 1440) //21:00-24:00
{
params.arrival_rate = 9;
}
else //by default
{
params.arrival_rate = 9;
}
Many thanks.

Assuming you want the same pattern every day, it suffices to take the time modulo 1440 minutes:
int day_time = sim_time % 1440;
int day_no = sim_time / 1440;
Now you can use day_time to get the time in the current day and use that to calculate params.arrival_rate. day_no will contain the day number (starting from 0).
As a further enhancement, consider using an array for your arrival rates instead of a long if-elseif chain:
const double arrival_rates[] = { 10, 9, 10.5, 12, 11.5, 11, 10.5, 9 };
params.arrival_rate = arrival_rates[day_time / (3 * 60)];
The calculation inside the brackets will round down the day_time to the start of the nearest three-hour block.

Related

C++ program sometimes giving wrong answer

I have this program, which should give me the smallest original price of the item based on the input from the user.
There are some conditions, for example, if the quantity of the mask is more than 9 the price will be discounted by 10%, 15% if its more than 30 and 20% if it's more than 50. The result should give the answer Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int mprice; //price input
int mquantity; //quantity input
int n; //first input
int fee = 2000; //const fee
float finalprice;
float maskCalc(int price, int quantity) {
float holder = (float)(price - fee) / (float)(quantity);
if (quantity > 0) {
finalprice = holder;
}
//if between 10 and 30
else if (quantity > 9) {
finalprice = holder / 0.9;
}
//between 30 and 49
else if (quantity > 30) {
finalprice = holder / 0.85;
}
//more than 50
else if (quantity > 49) {
finalprice = holder / 0.8;
}
//less than ten
else {
finalprice = holder;
}
return finalprice;
}
int main()
{
cin >> n;
float arr[n];
// Input oruulah loop
for (int i = 0; i < n; i++) {
cin >> mprice >> mquantity;
x = maskCalc(mprice, mquantity);
arr[i] = x;
}
for (int i = 1; i < n; i++) {
if (arr[0] > arr[i]) {
arr[0] = arr[i];
}
}
printf("%.2f", arr[0]);
return 0;
}
I gave the input
3
5000 3
7000 10
3000 1
the answer was 555.56 which is correct, but when I give something like
3
2500 1
7000 10
3000 1
it is giving me 0.00 while I was expecting this to give me 500.00. Any help will be appreciated.
You need to check for the highest quantity first in your if-else switch, otherwise you always fall into the default (<10) case.
//more than 50
if (quantity >= 50) {
finalprice = holder / 0.8;
}
//between 30 and 49
else if (quantity >= 30) {
finalprice = holder / 0.85;
}
//if between 10 and 30
else if (quantity >= 10) {
finalprice = holder / 0.9;
}
//less than ten
else {
finalprice = holder;
}

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

Angle Hit Comparision: Fails to detect hit

I am attempting to write a function that will tell me if an angle lies within 2 other angles. When say 'if an angle lies within 2 other angles' I mean for example, if I have the 2 angles 0 and 90 then 45 would lie between those angles but -20(or 99) would not.
My Problem: My function doesn't seem to be detecting when 2 angles lie within 2 angles when it should. I'm not sure if my function works for negative angles aswell?
What do I need to change to get my function working correctly?
bool is_angle_between(int target, int angle1, int angle2)
{
// Post: Return true if target lies between the 2 angles
int iTarget = (360 + (target % 360)) % 360;
int iAngle1 = (3600000 + angle1) % 360;
int iAngle2 = (3600000 + angle2) % 360;
if (iAngle1 < iAngle2)
if (iAngle1 <= iTarget && iTarget <= iAngle2)
return true;
else if (iAngle1 <= iTarget || iTarget <= iAngle2)
return true;
return false;
}
This question only has to do with testing whether an integer lies between within the range of two other integers. Since we do not know whether angle1 or angle2 is the larger value, I would do something like this:
bool is_angle_between(int target, int angle1, int angle2)
{
return (target > angle1 && target < angle2) ||
(target > angle2 && target < angle1);
}
A good way to do this is to rotate the interval so that all numbers you compare are positive:
int rTarget = ((target - angle1) % 360 + 360) % 360;
int rAngle2 = ((angle2 - angle1) % 360 + 360) % 360;
return 0 <= rAngle1 && rAngle1 <= rAngle2;
Otherwise you will get into trouble near 0 = 360 degrees.
BTW, you should avoid unnecessary if statements, as branching can be expensive.
Seems like this should work:
if (iAngle1 < iAngle2) {
if (iAngle1 <= iTarget && iTarget <= iAngle2) {
return true;
}
}
else {
if (iAngle2 <= iTarget && iTarget <= iAngle1) {
return true;
}
}
return false;
Note that the braces here help ensure that the else matches the if you intend it to. Your original code is indented as you want it to match, but that isn't how it's parsed.

infinite loop while counting dates

I'm trying to solve problem that asks to find how many times 13th day occurs at each weekday in period of 1990+N-1 years.
int weekDay = 1;
int week[] = {0,0,0,0,0,0,0};
N = 20;
for (int year = 1990; year <= 1990+N-1; year++){
for (int month = 1; month <= 12; month++){
int days = numberOfDays(year,month);
for (int day = 1; day <= days; day++){
if (day == 13)
week[weekDay] += 1;
weekDay += 1;
if (weekDay > 7)
weekDay = 1;
}
}
}
Here's my solution, however I get stuck in an infinite loop in year and can't seem to fix it.
EDIT : numberOfDays function.
int numberOfDays(int year, int month)
{
if (month == 2 && leapYear(year))
return 29;
else if (month == 2)
return 28;
if (month == 9 || month == 4 || month == 6 || month == 11)
return 30;
return 31;
}
You are using weekdays in the range 1..7 but your histogram array week[] is indexed 0..6.
One possible solution - change:
week[weekDay] += 1;
to:
week[weekDay - 1] += 1;
Another solution - make week[] one element bigger and don't use element 0, i.e. change:
int week[] = {0,0,0,0,0,0,0};
to:
int week[] = {0,0,0,0,0,0,0,0};
A third possible solution - use week days in the range 0..6, i.e. change:
int weekDay = 1;
to:
int weekDay = 0;
and change:
if (weekDay > 7)
weekDay = 1;
to:
if (weekDay > 6)
weekDay = 0;
Off-by-one. Your week[] array has 7 elements, indexed 0..6. You write to week[7], which overwrites something you didn't intend, such as e.g. the year variable.

Smallest number that is evenly divisible by all of the numbers from 1 to 20?

I did this problem [Project Euler problem 5], but very bad manner of programming, see the code in c++,
#include<iostream>
using namespace std;
// to find lowest divisble number till 20
int main()
{
int num = 20, flag = 0;
while(flag == 0)
{
if ((num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0
&& (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) ==0
&& (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18)==0
&& (num%19) == 0 && (num%20) == 0)
{
flag = 1;
cout<< " lowest divisible number upto 20 is "<< num<<endl;
}
num++;
}
}
i was solving this in c++ and stuck in a loop, how would one solve this step......
consider num = 20 and divide it by numbers from 1 to 20
check whether all remainders are zero,
if yes, quit and show output num
or else num++
i din't know how to use control structures, so did this step
if ((num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0
&& (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) ==0
&& (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18)==0
&& (num%19) == 0 && (num%20) == 0) `
how to code this in proper manner?
answer for this problem is:
abhilash#abhilash:~$ ./a.out
lowest divisible number upto 20 is 232792560
The smallest number that is divisible by two numbers is the LCM of those two numbers. Actually, the smallest number divisible by a set of N numbers x1..xN is the LCM of those numbers. It is easy to compute the LCM of two numbers (see the wikipedia article), and you can extend to N numbers by exploiting the fact that
LCM(x0,x1,x2) = LCM(x0,LCM(x1,x2))
Note: Beware of overflows.
Code (in Python):
def gcd(a,b):
return gcd(b,a%b) if b else a
def lcm(a,b):
return a/gcd(a,b)*b
print reduce(lcm,range(2,21))
Factor all the integers from 1 to 20 into their prime factorizations. For example, factor 18 as 18 = 3^2 * 2. Now, for each prime number p that appears in the prime factorization of some integer in the range 1 to 20, find the maximum exponent that it has among all those prime factorizations. For example, the prime 3 will have exponent 2 because it appears in the factorization of 18 as 3^2 and if it appeared in any prime factorization with an exponent of 3 (i.e., 3^3), that number would have to be at least as large as 3^3 = 27 which it outside of the range 1 to 20. Now collect all of these primes with their corresponding exponent and you have the answer.
So, as example, let's find the smallest number evenly divisible by all the numbers from 1 to 4.
2 = 2^1
3 = 3^1
4 = 2^2
The primes that appear are 2 and 3. We note that the maximum exponent of 2 is 2 and the maximum exponent of 3 is 1. Thus, the smallest number that is evenly divisible by all the numbers from 1 to 4 is 2^2 * 3 = 12.
Here's a relatively straightforward implementation.
#include <iostream>
#include <vector>
std::vector<int> GetPrimes(int);
std::vector<int> Factor(int, const std::vector<int> &);
int main() {
int n;
std::cout << "Enter an integer: ";
std::cin >> n;
std::vector<int> primes = GetPrimes(n);
std::vector<int> exponents(primes.size(), 0);
for(int i = 2; i <= n; i++) {
std::vector<int> factors = Factor(i, primes);
for(int i = 0; i < exponents.size(); i++) {
if(factors[i] > exponents[i]) exponents[i] = factors[i];
}
}
int p = 1;
for(int i = 0; i < primes.size(); i++) {
for(int j = 0; j < exponents[i]; j++) {
p *= primes[i];
}
}
std::cout << "Answer: " << p << std::endl;
}
std::vector<int> GetPrimes(int max) {
bool *isPrime = new bool[max + 1];
for(int i = 0; i <= max; i++) {
isPrime[i] = true;
}
isPrime[0] = isPrime[1] = false;
int p = 2;
while(p <= max) {
if(isPrime[p]) {
for(int j = 2; p * j <= max; j++) {
isPrime[p * j] = false;
}
}
p++;
}
std::vector<int> primes;
for(int i = 0; i <= max; i++) {
if(isPrime[i]) primes.push_back(i);
}
delete []isPrime;
return primes;
}
std::vector<int> Factor(int n, const std::vector<int> &primes) {
std::vector<int> exponents(primes.size(), 0);
while(n > 1) {
for(int i = 0; i < primes.size(); i++) {
if(n % primes[i] == 0) {
exponents[i]++;
n /= primes[i];
break;
}
}
}
return exponents;
}
Sample output:
Enter an integer: 20
Answer: 232792560
There is a faster way to answer the problem, using number theory. Other answers contain indications how to do this. This answer is only about a better way to write the if condition in your original code.
If you only want to replace the long condition, you can express it more nicely in a for loop:
if ((num%2) == 0 && (num%3) == 0 && (num%4) == 0 && (num%5) == 0 && (num%6) == 0
&& (num%7) == 0 && (num%8) == 0 && (num%9) == 0 && (num%10) == 0 && (num%11) == 0 && (num%12) ==0
&& (num%13) == 0 && (num%14) == 0 && (num%15) == 0 && (num%16) == 0 && (num%17) == 0 && (num%18)==0
&& (num%19) == 0 && (num%20) == 0)
{ ... }
becomes:
{
int divisor;
for (divisor=2; divisor<=20; divisor++)
if (num%divisor != 0)
break;
if (divisor != 21)
{ ...}
}
The style is not great but I think this is what you were looking for.
See http://en.wikipedia.org/wiki/Greatest_common_divisor
Given two numbers a and b you can compute gcd(a, b) and the smallest number divisible by both is a * b / gcd(a, b). The obvious thing then to do is to keep a sort of running total of this and add in the numbers you care about one by one: you have an answer so far A and you add in the next number X_i to consider by putting
A' = A * X_i / (gcd(A, X_i))
You can see that this actually works by considering what you get if you factorise everything and write them out as products of primes. This should pretty much allow you to work out the answer by hand.
Hint:
instead of incrementing num by 1 at each step you could increment it by 20 (will work alot faster). Of course there may be other improvements too, ill think about it later if i have time. Hope i helped you a little bit.
The number in question is the least common multiple of the numbers 1 through 20.
Because I'm lazy, let ** represent exponentiation. Let kapow(x,y) represent the integer part of the log to the base x of y. (For example, kapow(2,8) = 3, kapow(2,9) = 3, kapow(3,9) = 2.
The primes less than or equal to 20 are 2, 3, 5, 7, 11, 13, and 17. The LCM is,
Because sqrt(20) < 5, we know that kapow(i,20) for i >= 5 is 1. By inspection, the LCM is
LCM = 2kapow(2,20) * 3kapow(3,20)
* 5 * 7 * 11 * 13 * 17 * 19
which is
LCM = 24 * 32 * 5 * 7 * 11 * 13 *
17 * 19
or
LCM = 16 * 9 * 5 * 7 * 11 * 13 * 17 *
19
Here is a C# version of #MAK's answer, there might be List reduce method in C#, I found something online but no quick examples so I just used a for loop in place of Python's reduce:
static void Main(string[] args)
{
const int min = 2;
const int max = 20;
var accum = min;
for (var i = min; i <= max; i++)
{
accum = lcm(accum, i);
}
Console.WriteLine(accum);
Console.ReadLine();
}
private static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
private static int lcm(int a, int b)
{
return a/gcd(a, b)*b;
}
Code in JavaScript:
var i=1,j=1;
for (i = 1; ; i++) {
for (j = 1; j <= 20; j++) {
if (i % j != 0) {
break;
}
if (i % j == 0 && j == 20) {
console.log('printval' + i)
break;
}
}
}
This can help you
http://www.mathwarehouse.com/arithmetic/numbers/prime-number/prime-factorization.php?number=232792560
The prime factorization of 232,792,560
2^4 • 3^2 • 5 • 7 • 11 • 13 • 17 • 19
Ruby Cheat:
require 'rational'
def lcmFinder(a = 1, b=2)
if b <=20
lcm = a.lcm b
lcmFinder(lcm, b+1)
end
puts a
end
lcmFinder()
this is written in c
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,flag=0;
for(a=1; ; a++)
{
for(b=1; b<=20; b++)
{
if (a%b==0)
{
flag++;
}
}
if (flag==20)
{
printf("The least num divisible by 1 to 20 is = %d",a);
break;
}
flag=0;
}
getch();
}
#include<vector>
using std::vector;
unsigned int Pow(unsigned int base, unsigned int index);
unsigned int minDiv(unsigned int n)
{
vector<unsigned int> index(n,0);
for(unsigned int i = 2; i <= n; ++i)
{
unsigned int test = i;
for(unsigned int j = 2; j <= i; ++j)
{
unsigned int tempNum = 0;
while( test%j == 0)
{
test /= j;
tempNum++;
}
if(index[j-1] < tempNum)
index[j-1] = tempNum;
}
}
unsigned int res =1;
for(unsigned int i = 2; i <= n; ++i)
{
res *= Pow( i, index[i-1]);
}
return res;
}
unsigned int Pow(unsigned int base, unsigned int index)
{
if(base == 0)
return 0;
if(index == 0)
return 1;
unsigned int res = 1;
while(index)
{
res *= base;
index--;
}
return res;
}
The vector is used for storing the factors of the smallest number.
This is why you would benefit from writing a function like this:
long long getSmallestDivNum(long long n)
{
long long ans = 1;
if( n == 0)
{
return 0;
}
for (long long i = 1; i <= n; i++)
ans = (ans * i)/(__gcd(ans, i));
return ans;
}
Given the maximum n, you want to return the smallest number that is dividable by 1 through 20.
Let's look at the set of 1 to 20. First off, it contains a number of prime numbers, namely:
2
3
5
7
11
13
17
19
So, because it's has to be dividable by 19, you can only check multiples of 19, because 19 is a prime number. After that, you check if it can be divided by the one below that, etc. If the number can be divided by all the prime numbers successfully, it can be divided by the numbers 1 through 20.
float primenumbers[] = { 19, 17, 13, 11, 7, 5, 3, 2; };
float num = 20;
while (1)
{
bool dividable = true;
for (int i = 0; i < 8; i++)
{
if (num % primenumbers[i] != 0)
{
dividable = false;
break;
}
}
if (dividable) { break; }
num += 1;
}
std::cout << "The smallest number dividable by 1 through 20 is " << num << std::endl;