1

I'm looking for a way to somehow count amount of steps:

public static int Calculate0(int end, int init, int lim, int bon) { return end <= 0 ? 0 : Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end); } 

I guess my problem kind of two fold:

  1. I dont understand the the special :? operator in C# and
  2. I don't know where to input some kind of variable into the Calculate0 method.

Been trying to read about the :? operator through Microsoft's guide but I still struggle with understanding what happens inside Calculate0.

My code is currently looking like this. Can this be correct?

using System; namespace TestProject { internal class Program { private int calc0Steps = 0; public static void Main() { var calc0 = Program.Calculate0(1, 0, 1, 2); Console.WriteLine("Calculate: {0} | Steps: {1}", calc, calc0Steps); } public static int Calculate0(int end, int init, int lim, int bon) { return end <= 0 ? 0 : calc0Steps; Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end); } } } 

Update

I'm sorry for being confusing. I'll try to narrow it down: How can I put a counter into Calculate0?

The main scope of my assignment is to do a full test coverage of the method fhcimolin provided and compare this method to Calculate0. A small side task is to count the computational steps. But I had no idea how I implemented a counter in Calculate0 in the first place.

I'm having another version of Calculate0 looking like fhcimolin's answer, where I can put in a counter. And I need to count how many computation steps there are in both.

9
  • We don't know what you are trying to calculate, so maybe you need to explain that. Include expected and actual outcome for the given values Commented Mar 12, 2019 at 16:19
  • If you find the conditional operator (?:) confusing, then use plain if/else statements to perfect your calculation Commented Mar 12, 2019 at 16:20
  • The return statement seems to have two statements? First the ternary return end <= 0 ? 0 : calc0Steps and second, the Math.Min part? Won't it accuse of unreachable code? Commented Mar 12, 2019 at 16:21
  • 1
    To answer your question 1, the ? is the ternary operator. you may be familar with IIF from VB6, it's similar in function. the statement bon == 0 ? init : init + (2 * bon - lim / bon) * end can be read as: If this bon == 0 is true ? use this value init otherwise : use this init + (2 * bon - lim / bon) * end Commented Mar 12, 2019 at 16:24
  • I think the point is to count computational steps manually. Not with a counter. Commented Mar 12, 2019 at 20:11

1 Answer 1

1

If I got your logic right, you might want your Calculate0 method to look like this:

public static int Calculate0(int end, int init, int lim, int bon) { return end <= 0 ? calc0Steps : Math.Min(2 * lim, bon == 0 ? init : init + (2 * bon - lim / bon) * end); } 

Which would the be equivalent of:

public static int Calculate0(int end, int init, int lim, int bon) { if (end <= 0) { return calc0Steps; } else { int aux; if (bon == 0) { aux = init; } else { aux = init + (2 * bon - lim / bon) * end; } return Math.Min(2 * lim, aux); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I've edited my question to be more clear. I thank you for the time you spent and I'm sorry for wasting your time by being unprecise. Amazing how close you are to the actual other method we are testing against. We have an assignment (datascience first year) where we need to compare these two methods and count the computation steps. This is just a small part of the assignment, the biggest part is to do a 100% test coverage! Again thank you
@jubibanna I accidentally overlooked that aspect of the question. I focused instead in solving your syntax problem. Now you're talking about complexity, and I suggest you get a read on this. I don't think the complexity is supposed to be affected (between an if/else block and a ternary operator), but some folks put it to the test, and it's technically slower.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.