How to create a program to enter 10 numbers,then calculate average and variance!

Started by fett_017, November 24, 2005, 01:07:28 PM

Previous topic - Next topic

fett_017

I have to create a program to enter 10 numbers and then calculate the mean (average) and variance for the 10 numbers.

The numbers need to be integers or real numbers.

I'm really stuck...I have a program running  using an array that creates ten random numbers and displays them. however i don't know how to use the array with entering in the numbers manually.

The program that creates the ten random numbers is below:

TITLE Read numbers into an array and print them out

INCLUDE Irvine32.Inc

.data
array   sdword 10 dup(?)      ;Array of 10 integers
i   sdword   ?      ;Loop counter
outer   sdword   ?      ;Outer loop counter
inner   sdword   ?      ;Inner Loop counter

prompt byte   "Enter a non-negative integer",0

.code

Bubble_Sort PROC         ;Bubble_Sort(int array[10])
;eax will hold the value of the outer loop counter
;ecx will hold the value of the inner loop counter

   mov eax,0      ;
   mov outer,eax      ;outer=0
   
outer_loop:         ;while(outer !=10)
   mov ebx, outer      ;{
   cmp ebx, 10      ;
   je  back_to_main   ;
   
   mov eax,0      ;
   cmp ebx, 10      ;inner=0
   mov edi,0      ;
   
inner_loop:         ;while(inner !=9)
   mov ebx, inner      ;{
   cmp ebx,9      ;
   je  end_outer_loop   ;
   
   mov eax,array[edi+4]   ;
   cmp array[edi],eax   ;
   jb  end_inner_loop   ; if(array[inner]>array[inner+1])
            ; {
   mov edx,array[edi]   ; temp = array[inner];
   mov array[edi],eax   ; array[inner]=array[inner+1];
   mov array[edi+4],edx   ; array[inner+1]=temp;
            ; }
end_inner_loop:
   inc inner      ; inner=inner+1;
   add edi,type array   ;}
   jmp inner_loop      ;
   
end_outer_loop:         ;
   inc outer      ; outer=outer+1;
   jmp outer_loop      ;
   
back_to_main:
   ret         ;}
Bubble_Sort ENDP


I'm new to this MASM stuff and would appreciate the help. Thanx.




MichaelW

Assuming you can use the procedures in the Irvine32 library, you could call the ReadInt procedure in a loop to get the numbers from the user. AFAIK all of the procedures in the library preserve all registers that are not used to return a value. The ReadInt procedure returns the number read in EAX, so you would be free to use EBX, ECX, EDX, ESI, and EDI within the loop, without having to worry about the procedure call disturbing the register values. Before you entered the loop, you would need to load a pointer to the array into one of the available registers, and for simplicity, a loop-count value into another. Within the loop you could display a prompt with a call to WriteString, call ReadInt, store the return value in the array, add 4 to the pointer (because each element is 4 bytes long), decrement the loop count, and exit the loop when the loop count reaches zero. If necessary, you could add code to the loop to check the validity of the input number, and discard invalid input by jumping to the start of the loop without storing the value, or adjusting the pointer or loop count.

To learn how to calculate the mean and variance, you could start by doing a web search for "variance". You should be able to find definitions and pseudo-code implementations. You will probably need to use the FPU for this, and to learn how see the Floating-Point Tutorial here:

http://www.website.masmforum.com/tutorials/index.htm
eschew obfuscation

raymond

And if you want to compute with floats before learning how to program the FPU, you could always have a look at the library of functions FPU.LIB available at:

http://www.ray.masmcode.com/fpu.html#fpulib

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

AeroASM

If you can't be bothered to do a loop, you could jsut call ReadInt ten times.