Iterate with threshold stored in database [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a variable number of transactions in every month saved in the database. I have to calculate a payment according a structure like as 0-100 transaction, 1 € for every transaction, 101-200 2€ for each, 201 to 300 3€ for each, etc. The first 100 transactions I’ll pay 100 €, the next 100 transactions I’ll pay 200 € and in this way. Until here is easy for me, the problem become because the number of thresholds (in the earlier example was 3), can be variable, sometimes is 3, other times could be 2, 4, 5 or whatever. These thresholds are stored in a table in the database. Please, can someone help me. Thanks in advance.

For each threshold in the database, you will sore a LOWER and UPPER bound and Price (0,100,1),(101,200,2),(201,300,3).
Load the thresholds into a List with each member containing the lower, upper, and price
List<int[]> thresholds = new List<int[]>
thresholds.Add(new int[]{0,100,1});
thresholds.Add(new int[]{101,200,2);//etc.
private void calculateTotal(int numberSold){
int total =0;
foreach(int[] bound in thresholds){
if(numberSold>=bound[0]&&numberSold<=bound[1]{
total+= bound[2]*numberSold;
}
}
return total;
}
That's about the best I can do for you based on your limited question. Play with this and if you have a problem, ask a specific question based on the code you create and the specific problem you are having.

Related

best method to if/else potentially large condition [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Working on an MVC application. I have hundreds of users. Currently I'm trying to give some of our top users (maybe 10) a discount if they log-in through their assigned work-place e-mails. The price break is shown in the Search result, Product page, Shopping Cart, and Checkout page. I had to work on this quickly to hack it for the holiday season, so currently the way I am doing this is something like this:
ProductDetails.aspx
if (user == "at#at.com") {
Product. Price * 20
}
else {
Product.Price
}
As you can see, this works for now, however I'd have to do this for all 4 pages, and as our discounted users increase, this may become too long and mundane. I'm looking at a way to go around having such a long if/else statement, and was wondering if it makes sense to use a stored procedure instead or a method?
I would suggest to add a discount field to the user table or any appropriate table in the database and save the discount info there. You can also create a new table that holds the discount information tied up to the different users and manage to return the discount value instead of going through an if/else statement.

what is the best way to store Numbers of a Bingo Card in database table [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I wanna create a web-based bingo game using MVC and EF, I want to store 24 numbers for each card in the database table as a record,I'm not sure how to create my Bingo cards table,one way that comes to my mind is design a table with 24 integer field for each cell's number this way when I fetch the record in business logic I can't traverse the numbers because they are in the separate fields(actually I can but I think this way is not the right way to do)the other way is I concatenate all numbers together and separate it with comma and store it as a string so when I fetch the record I can split the string of comma and have all numbers in an array or something
so what is the best solution for this situation ?
how can I design my table for storing numbers of a bingo card ?
Good question!
You're right, storing it in 24 different integer fields is definitely a bad way to go. Concatenating them with commas might seem like a reasonable compromise, but it's inflexible, as you're treating them as text, not integers, so later on if you wanted to run queries such as SELECT MAX(CardNumber) FROM BingoCardNumbers WHERE CardID = 1 you couldn't do anything like that.
My suggestion would be to have two different tables, BingoCards and BingoCardNumbers. The BingoCards table would have a CardID, as well as other information relevant to the card, such as who the card belongs to, so maybe you'd want a UserID column in there too. No card numbers are stored in this table.
Then, in the BingoCardNumbers table, that would have three columns, CardID, which is the primary key from the first table, NumberIndex (1-24, which is the index of the number in the card), and finally CardNumber, which is the actual number itself. Then, if you want to get all the numbers for a particular card, you can simply do:
SELECT CardNumber FROM BingoCardNumbers WHERE CardID = 1 ORDER BY NumberIndex

How do I take an indefinitely large set of user inputs and save them into an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a program (in school for intro to computer programming(dont worry, this is all extra credit stuff, not asking for cheat answers)) that will take a user decided set of user inputs and save them all to do math (more specifically, finding the median, but I want to figure that out myself) on them. I am fairly sure that I need an array to do this (even my teacher hinted that that is what you needed to do).
My plan is to have a variable x that will decide the amount of separate numbers in the array (will not only be the number in the array, but also the number to check how many times I want to run the loop for asking for numbers), and then to have that many user inputted numbers inputting by the user, and then to be able to take those numbers and find the median of them (I will probably have to check if the number is even or odd first, then sort the numbers (somehow), then find the middlemost number.)
Thanks!
Instead of using array which has fixed size, try using List.
List allows you to add indefinitely many (until it fits into memory) elements.
So, in your for loop you could read user input and add it to the list.
After that you can sort entire list using sort method and return middle element, which is a median.
It could look like that:
int n = //TODO: read number of user inputs
List<int> elems = new List<int> ();
for (int i = 0; i < n; i++) {
int x = // get input
elems.Add(x);
}
Console.WriteLine(elems[elems.Length / 2]);
You can take a look at the List<T> class here for storing the numbers.
For reading input, see Console.ReadLine.
For converting string to int, take a look at int.TryParse.
I gave you the tools, now start coding! :D

Regarding logical equation on my C programming [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.

How to Program 'Chance'? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have an idea of something but i don't have a clue how to do it :D
basically just like in the games for example, every 6 hours you can open a chest where you can grab some item for free. Now i want to have a chest that the user can open every 6 hours, i have the items written in mysql server where every item gives experience points to the user. but how to do it?
lets say the database have 15 items, now every item have its chance value for example
nothing = 75% of getting it
sword_1 = 15% of getting it
sword_2 = 30% of getting it
any idea how to do it?
Define a range of % to get an item
Example - sword_1 = 0-15%, sword_2 = 15-45%
Note that the numbers above don't add up to 100%, so unless you really want return more than one time, sometimes.. the above ranges takes care of getting nothing.
If you want multiple rewards on a roll, simply adjust the ranges accordingly.
Generate a random number between 0 and 100. For the items where generated number falls within the range, those are your rewards won for that round.
You could assign a numeric value to each of your items in the table, the percentage of getting it would be the difference between it and its next lowest number.
Nothing, 60 (60% chance)
sword_1, 70 (10% chance)
sword_2, 100 (30% chance)
Make sure that your max item is 100.
You can then do a select from that table with a random number:
SELECT * FROM ChestItems WHERE ChanceValue < FLOOR(RAND() * 100) ORDER BY ChanceValue DESC LIMIT 1;
There are several (and much better) ways you can do this. This is just a quick example.

Categories