Visky2212
Visky2212

Reputation: 13

Save array of decimal values for using for loop

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?

Upvotes: 1

Views: 301

Answers (1)

Connor Stoop
Connor Stoop

Reputation: 1632

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;
}

Upvotes: 2

Related Questions