1

I wanted to create an array of increment_Num[], something like this:[1.60,1.62,1.64,1.66,1.68,1.70]

  //First step I converted the string to decimal value here:
  decimal Start_Num = decimal.Parse("1.60");
  decimal Stop_Num = decimal.Parse("1.70");
  decimal Steps_Num = decimal.Parse("0.02");
  
  //Second step I calculated the total number of points and converted the decimal value to int here:    
  decimal steps = (Stop_Num - Start_Num) /Steps_Num;
  int steps_int=(int)decimal.Ceiling(steps);
  
  //Third step I tried to create a for loop which will create an array       
  decimal[] increment_Num = new decimal[steps_int+1];
  for (decimal f=0; f<steps_int+1; f+=Steps_Num)
  {
  increment_Num[f] = Start_Num + f * Steps_Num;
  }

The following code gives me this error at the step-3 in the second last line for increment_Num[f]:

Error CS0266 Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

Am I doing something wrong in declaration?

3
  • 3
    increment_Num[f] u use f as an index, arrays are indexed by int and f is a decimal Commented Aug 25, 2021 at 10:47
  • The moment I introduce int the for loop: for (int f=0; f<steps_int+1; f+=Steps_Num), it says that Steps_Num is decimal value. Which 0.02 in my case. Commented Aug 25, 2021 at 10:54
  • 1
    You aren't meant to increment by Steps_Num. You should increment by 1 as usual. Commented Aug 25, 2021 at 10:59

1 Answer 1

2

Posted my comment as answer :

increment_Num[f] u use f as an index, arrays are indexed by int and f is a decimal

The code should be

for (int index =0; index<=steps_int;  index++)
{   
    increment_Num[index] = Start_Num + index * Steps_Num;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.