Reputation: 23
I'm trying to find the AVG of this array : 1742,1065,-67,-2988,-796,-1000,31,-67,-100,1180 I belive my method to change the number from POS to NEG is worng. my prog need's to calculate the above array then print massege if the AVG is NEG or POS, and it always print that the AVG is POS even though it's not. here is my code so far:
; lab56.asm ; .MODEL SMALL .STACK 100h .DATA AVG_NEG DB 'THE AVG IS NEG',13,10,'$' AVG_POS DB 'THE AVG IS POS',13,10,'$' INDEX DB 'Numbers that are larger than the average are in indexes:',13,10,'$' RES DB ' ','$' ARR DW 1742,1065,-67,-2988,-796,-1000,31,-67,-100,1180 Ten DW 10 AVG DW 0 temprint DB ' ','$' ;Program start here: .CODE MOV AX,@DATA ; DS can be written to only through a register MOV DS,AX ; Set DS to point to data segment LEA SI, ARR ; ; SUMUP MOV CX,10 ;10 variables in array Sum: MOV AX,[SI] CMP AX,0 JG Pos_label XOR AX,0000000000000000b ADD AX,0000000000000001b Pos_label: ADD AVG,AX ADD SI,2 ;move to the next number LOOP Sum ; Divided by 10 to get the AVG CWD ; AX -> DX:AX IDIV Ten MOV AVG,AX ; print ; Check if NEG or POS CMP AVG,0 JG Avg_label MOV AH,9 ; Set print option for int 21h MOV DX,OFFSET AVG_NEG ; Set DS:DX to point to AVG_NEG INT 21h JMP continue Avg_label: MOV AH,9 ; Set print option for int 21h MOV DX,OFFSET AVG_POS ; Set DS:DX to point to AVG_POS INT 21h continue: ; ;Program end's here: MOV AH,4Ch ; Set terminate option for int 21h INT 21h ; Return to DOS (terminate program) END
Upvotes: 1
Views: 256
Reputation: 163357
If you change the sign of all your inputs before you add them together, then you'll obviously always get that same sign for the sum (barring overflow), and thus for the average as well. Furthermore, the computed average will be wrong because you'll be averaging the magnitudes instead of the actual values.
Your method of changing the signs is indeed wrong, so don't do that. Just compute the average the same way you learned in grade school: Add the numbers and divide by the count.
Upvotes: 1