So I’m trying to create a buy and sell program and in there, I also coded the range where a random price out of a certain range will pop out. Now the problem is I can’t figure out a way on how to display a “not available today” on a 15% chance. so basically the price list will only show the price of the item OR a “not available today” note.
this is how the code looks like now. i only inserted the price range and an srand function.
srand(time(NULL));
item1 = rand() % (1000 - 500 + 1) + 500;
item2 = rand() % (5000 - 1500 + 1) + 1500;
item3 = rand() % ( 8000 - 5000 + 1 ) + 5000;
printf("The Price of Item1 is %dG\n", item1);
printf("The Price of Item2 is %dG\n", item2);
printf("The Price of Item3 is %dG\n", item3);
if(rand() % 100 > 15)
{
/* more than 15 */
}
else
{
/* less or equal 15 */
}
if ((rand()%(100)+1) <= 15 ) {
printf("Item not available today\n");
} else {
printf("The Price of Item1 is %dG\n", item1);
}
Could this be a simple implementation of what you are trying to do?
Edit: fixed rand()
Related
I'm trying to create a program which will calculate the same date in three different ways. I'm currently stuck on calculating the day of the week, as I need this to calculate the ISO week day. I've got an algorithm that I can use, and it is the one which I've got in my code, with the only difference being that the % sign in my code is replaced by the word "mod" in the algorithm.
When I run this, I get an error saying "Expected expression before % token". I've looked this up but didn't find any results. I've also tried to look at other ways of doing it, and found the Sakomoto Algorithm, but I don't exactly understand how that works. For a possible solution, I was thinking that I maybe need to create a function called mod, but I'm not entirely sure what I would need to put in there.
int day_of_the_week(int year)
{
int week_day;
week_day = %(1+5 * %(year - 1, 4) + 4 * %(year - 1, 100) + 6 * %(year-1,
400), 7);
printf("The day of the week is %d\n", week_day);
return 0;
}
Gauss'
R(1 + 5R(A - 1, 4) + 4R(A - 1, 100) + 6R(A - 1, 400), 7)
should be equivalent to
int week_day = (1 + 5 * (year - 1) % 4) + 4 * ((year - 1) % 100) + 6 * ((year - 1) % 400) % 7;
I have an array which is used to compute a score in a game:
a = {1,7,5,1,2,6,2,3,4,5,5,6,7,7,7}
All numbers should be simply added, except when a number appears several times, for instance 7 (which appears 4 times) it should be added as such:
1*7 + 2*7 + 3*7 + 4*7
So, altogether, array "a" should give this score:
score = (1*1 + 2*1) + (1*2 + 2*2) + (1*3) + (1*4) + (1*5 + 2*5 + 3*5) + (1*6 + 2*6) + (1*7 + 2*7 + 3*7 + 4*7)
I wouldn't know where to start doing this. What's a good method for this kind of calculation?
Any help is appreciated.
You can keep track of the current multiplier for each number in another table:
function calculateScore(a)
local multipliers = {}
local score = 0
for i,number in ipairs(a) do
local multiplier = multipliers[number] or 1
multipliers[number] = multiplier + 1
score = score + number * multiplier
end
return score
end
local a = {1,7,5,1,2,6,2,3,4,5,5,6,7,7,7}
local score = calculateScore(a)
I realized a flaw on my coding for picking cards from my structure group arrays this morning for a tabletop card game I'm converting to computer.
Currently the code is set up to randomly pick an array of a specific card, but if quantity of cards in the group is < 1, then the group is skipped. For my deck this isn't being truly random because each card has its own quantity. Some cards have 10 cards, others 6, most 4. How can I code it to where it looks at all these quantity numbers as a whole and picks a random card out of these numbers.
If GameSize >= 3 Then
For StartHands = 10 To 14
Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)
'Cardslots Player3
If CardTypeArray(StartHands) = "" Then
If DeckGroup(Number).QuantityInteger > 0 Then
DeckGroup(Number).QuantityInteger -= 1
Player1HandGroup(Number).QuantityInteger3 += 1
CardTypeArray(StartHands) = Player1HandGroup(Number).CardType
Me.NumberArray(StartHands) = Number
Else
'Recall Procedure if Generated Random Number is not allowed
'due to QuantityInteger <= 0
Call StartButton_Click(sender, e)
End If
End If
Next StartHands
End If
when playing a card, and drawing a new card, I use this coding scheme, to prevent stackoverflow, but is virtual the same.
Dim temp As IEnumerable(Of LunchMoneyGame.LunchMoneyMainForm.Group) = From r In DeckGroup Where r.QuantityInteger > 0 Select r
If temp IsNot Nothing AndAlso temp.count > 0 Then
Number = (temp(Rnd.Next(0, temp.Count)).ID)
DeckGroup(Number).QuantityInteger -= 1
'Select the Player depending value of T
Select Case T
Case 0
Player1HandGroup(Number).QuantityInteger += 1
Case 1
Player1HandGroup(Number).QuantityInteger2 += 1
Case 2
Player1HandGroup(Number).QuantityInteger3 += 1
Case 3
Player1HandGroup(Number).QuantityInteger4 += 1
Case 4
Player1HandGroup(Number).QuantityInteger5 += 1
End Select
Edit:
Improved question:
How can I weight the probably of drawing a card based on how many of a particular card are left in the decks quantity integer for each card?
The idea of my solution is to generate a random number over the total number of availables cards in all deck groups. Finally we find the deck group this number targets within a loop:
Dim totalNumerOfCards As Integer = DeckGroup.Sum(Function(d) d.QuantityInteger)
Dim Number As Integer = Rnd.Next(totalNumerOfCards)
Dim numCards As Integer = 0
Dim groupIndex As Integer = 0
Dim cardIndex As Integer = 0
Dim i As Integer = 0
While i < DeckGroup.Length AndAlso numCards <= Number
groupIndex = i
cardIndex = Number - numCards
numCards += DeckGroup(i).QuantityInteger
i += 1
End While
Console.WriteLine("Your card is in DeckGroup({0}), card index {1}",
groupIndex, cardIndex);
I have the following code
B=[1 2 3; 10 20 30 ; 100 200 300 ; 500 600 800];
A=[100; 500; 300 ; 425];
SA = sum(A);
V={}; % number of rows for cell V = num of combinations -- column = 1
n = 1;
arr = zeros(1,length(B))
for k = 1:length(B)
for idx = nchoosek(1:numel(B), k)'
rows = mod(idx, length(B))
if ~isequal(rows, unique(rows)) %if rows not equal to unique(rows)
continue %combination possibility valid
end %Ignore the combination if there are two elements from the same row
B_subset = B(idx)
if (SA + sum(B_subset) <= 3000) %if sum of A + (combination) < 3000
rows( rows==0 )=4
arr(rows) = B_subset(:)
V(n,1) = {arr}
n = n + 1
arr = zeros(1,length(B))
end
end
end
The combinations are supposed to be valid if the sum of A and some values of B are less than 3000.
Problem with my code is, the last value of B, B(3,3), is only accounted for once in the code.
If you run the code, you will notice one cell of V containing [0;0;0;800] at row 12. But there are other combinations possible as well such as [1;0;0;800]. Where SA + (1 + 800) < 3000 , however the code does not print this possibility.
I cannot figure out why, can someone please help me debug this and find out why some combinations are skipped ? especially B(3,3) ?
I suspect that this line is not doing exactly what you intended:
if ~isequal(rows, unique(rows))
Instead, try this:
if ~isequal(length(rows), length(unique(rows)))
In C , how do I make 1200 / 500 = 3.
I'm doing a homework assignment.
Shipping Calculator: Speedy Shipping company will ship your package based on how much it weighs and how far you are sending the package. They will only ship small packages up to 10 pounds. You need to have a program that will help you determine how much they will charge. The charges are based on each 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same charge as 900 miles.
Here is the table they gave you:
Package Weight--------------------------Rate per 500 miles shipped
2 pounds or less------------------------$1.50
More than 2 but not more than 6---------$3.70
More than 6 but not more than 10--------$5.25
Here is one test case.
Test Case Data:
Weight: 5.6 pounds
Miles: 1200 miles
Expected results:
Your shipping charge is $11.10
My answer keeps coming out to 7.40
Are you trying to round up? Before dividing, you could add 499 to the number that is being divided.
(0 + 499) / 500 -> 0
(1 + 499) / 500 -> 1
(1200 + 499) / 500 -> 3
This will round up.
Say you want to get a ceiling division a by b (in your example a = 1200 b = 500).
You can do it in integer arithmetic like this.
result = (a + b - 1) / b;
Or you could use floating point numbers and do it like this (probably a bad idea)
result = (int) ceil( (double) a / b );
The thing is that as this is a homework, you could just make it up in small steps:
if( a % b == 0 ) {
result = a / b;
} else {
result = a / b + 1;
}
Another advantage of this code is that it actually doesn't overflow for too big as, but this is not relevant in this case, I guess.
I'd suggest using the mod and truncate functions. If mod comes out zero, it's fine, otherwise truncate and add 1.
You have to use the ceiling of the division. This will round the quotient up to the next integer.
So when you are trying to find the number of 500-mile increments, you have to round the quotient up to the next integer.
Alternatively, (and inefficiently), you could increment the number of miles by 1, until it is divisible by 500...that is, while ( (q = x_miles++%500) != 0 ) {} . Then multipy q by the rate to get your answer (That is also assuming you will have an integer number of miles).
You could also use the stdlib div function. This might be nice if you only wanted integer math and specifically wanted to avoid floating point math.
http://www.cplusplus.com/reference/clibrary/cstdlib/div/
#include <stdlib.h>
int foo(void)
{
div_t result = div(1200, 500);
return result.quot + (0 < result.rem);
}
[EDIT1]
From your code you would implement this part as follows:
if ( weight <= 5.6 )
{
int multiplier = (int) miles / 500;
if( ((int)miles % 500) > 0)
multiplier++;
rate370 = (double)multiplier * 3.7;
printf("Your total cost : %.2lf\n", rate370);
}
[ORIGINAL]
In "integer land" 1200 / 3 should equal to 2.
for what it "seems" you want try this:
int multFiveHundreds = (int)totalWeight / 500;
if(multFiveHundreds % 500 > 0)
multFiveHundreds++;