How can I find the elements of a numeric array by adding a given step size?

63 Views Asked by At

We have an array of numbers, a starting point, and a specified step size, like this:

arr:real64[10]:=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0];
x:real64:=0;
h:real64:=0.05;

We are asked to go forward step by step by adding the step size to the starting point, and if the result is the same as one of the elements of the array, print that element separately.

But, when I run the program, some points are not printed as expected.

The output of my program

As you can see, contrary to expectations, points 0.4, 0.5, 0.7, 0.8, 0.9, and 1.0, are not printed. While I expected all the numbers in the array to be displayed in order.

program  practice;
#include( "stdlib.hhf" );
  static
     arr:real64[10]:=[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0];//our array
     x:real64:=0;//starting point
     h:real64:=0.05;//step size
begin practice;
     mov(0,ebx); // repeat loop counter
 repeat//start repeat loop
     fld(x);//put x on the st1
     fld(h); //put h on the st0 
     faddp();//adds h to the x (  x+h  is now placed on the stack.  x+h ->st0)
     fstp(x);//stores x+h into x so x has gained new value 
     stdout.put("x:",x ,"  ",nl);//prints new value of x in each step
 for(mov(0,ecx);ecx<10;inc(ecx))do //starting for loop to check if  the value of x 
                   // is exist in the array
     fld(x);//puts x on the stack. x -> (st1)
     fld(arr[ecx*8]);//puts each elements on the st0
     fcompp();//compares st0 and st1 
     fstsw(ax);//stores status register into ax 
     sahf();//stores ah into L.O byte of EFLAGS register
       if(@z)then//if two numbers are equal(zero flag is set)
          stdout.put("           Available in array:",arr[ecx*8],nl);
       endif;
  endfor; 

    inc(ebx);//Increase the loop counter.
  until(ebx=20);
end  practice;//end of our program.

Why does this happen? And how can I fix it?

0

There are 0 best solutions below