Articles

Solved Harmonic average in cmd

November 27, 2012 at 10:47:47
Specs: Windows 7

I WANT TO DO A HARMONIC AVERAGE CMD CALCULATOR IN CMD. IF SOMEBODY COULD HELP ME...
FORMULA: N / 1/X + 1/Y
N = 2 IN THIS CASE BECAUSE THERE'S JUST 2 VARIABLE NUMBERS, X AND Y. I WANT IT IN THE MAX OF 3 NUMBER.
TNX GUYS

See More: Harmonic average in cmd

Report •


#1
November 27, 2012 at 12:06:24
✔ Best Answer

you did not say where you want inputs to be drawn from (commndline or textfile), nor where you want outputs to go, so i just used command.
This would be a major pain to try with batchscript, so I won't attempt it. Maybe someone else who is patient and doesn't mind being frustrated. This solution is vbscript:
'===== begin vbscript harmavg.vbs
n=wscript.arguments.count
if n=0 then err("At least one argument is required")
for i=0 to n-1
if wscript.arguments(i)=0 or isnumeric(wscript.arguments(i))=0 then
err("No zeros allowed for any argument")
end if
z=z+1/wscript.arguments(i)
next
if z=0 then err("Negative arguments have resulted in zero")
wscript.echo n/z

sub err(msg)
wscript.echo msg & ", division by zero is not do-able!"
wscript.echo "Usage: Harmavg[.VBS] n1 [n2..nn]"
wscript.echo "where: n1, n2 etc are any NONZERO values."
wscript.quit
end sub
'======= end vbscritp
usage: HARMAVG number1 [number2 number3]...


Report •

#2
November 27, 2012 at 12:42:35

@echo off
::theoretically using the formula
::h= 2x1x2/x1+X2
::which I would think would give you the correct answer
::you could do this.

set /p xone="please enter first variable:  "
set /p xtwo="please enter second variable:  "
set /a xone=xone
set /a xtwo=xtwo

set /a first=(2*xone*xtwo)
set /a second=(xone+xtwo)

set /a answer=(first/second)

echo note that batch only uses whole numbers
echo your answer is  %answer%
pause
cls

::in case you have to do it the other way
::batch is a real pain for decimals..:)


:: mike


Report •

#3
November 28, 2012 at 04:00:37

Tnx Mike. So the problem is that in batch file we cannot do decimals?
Is there another way to make a program easy like batch that opens in all windows xp and higher that make harmonica in a easy way like batch??
Rly tnx bro.
Since already, Antonio.

Report •

Related Solutions

#4
November 28, 2012 at 04:14:55

Because I need it to do decimals...

Report •

#5
November 28, 2012 at 05:59:58

The above vbs script is fairly simple, don't know if that's something that is workable for you? If you wanted to use it you would run the command interpreter, then navigate to the file you have the script in, and then type harmavg.vbs number1 number2.

I would agree with nbrane, that working through decimals is much much easier in vbs. The only way I could think to do it is through changing where the decimal point lies in the original entries, and then changing them back, the only issue is that even this will fail the mathematical precision you are asking for. If that works better, don't forget to change the best answer to nbrane, that way people see the better answer first.

Thanks for replying so fast, sorry that there isn't an easy answer in batch.

:: mike


Report •

#6
November 29, 2012 at 14:45:32

Nbrane, hello, how r u bro? I just wanna know how to use this vbs formula...
Idk how to use that...
Tnx bro

Report •

#7
November 29, 2012 at 15:00:01

Beasure idk how to use that vbscript nbrane gave...
Tnx.

Report •

#8
November 29, 2012 at 19:15:48

Hello: save the script (simple ascii-text, like batch) as something.VBS. (i called it "harmavg", so for example, saved as harmavg.vbs, to use it:)
HARMAVG number1 [number2 number3 ...]
(you can add the extension if you want: HARMAGE.VBS number1.....)
or, on older (pre-xp) systems, you need cscript to run it:
cscript /nologo harmavg.vbs number1.....

Report •

#9
November 29, 2012 at 19:34:10

My windows is 7. So what I need to do is, open notepad and text this:
'===== begin vbscript harmavg.vbs
n=wscript.arguments.count
for i=0 to n-1
z=z+1/wscript.arguments(i)
next
wscript.echo n/z
'==== end vbscript

save as .vbs

and then? About windows xp what do I need to do? I didn't understood.
Rly tnx bro, I'll give u best answer.


Report •

#10
November 29, 2012 at 19:40:31

When I save that script in notepad as .vbs and execute I have this error:
http://s11.postimage.org/x0rqmmwqr/...

Tnx bro.


Report •

#11
November 29, 2012 at 21:09:12

I cannot duplicate that error, on either xp or seven ("ultimate"). I saved with notepad, with no filename (".vbs"), and ran it with no errors. No clue where the http-linked error comes from..."post-image" no-how related to running scripts. Sounds like a local-machine issue. Try it on a "clean" xp or seven machine, is all I can suggest, sounds like yours might be compromised or local environment/machine security settings are in effect. Also might try the "cscript" version: cscript .vbs n1 n2 n3 ...

Report •

#12
November 29, 2012 at 23:04:01

Hmm... so what I did was correct?
How do I try csript?
tnx

Report •

#13
November 29, 2012 at 23:04:46

I have to put only this onde the notepad right?
'===== begin vbscript harmavg.vbs
n=wscript.arguments.count
for i=0 to n-1
z=z+1/wscript.arguments(i)
next
wscript.echo n/z
'==== end vbscript

Report •

#14
November 29, 2012 at 23:05:05

save .vbs and just doble click on it?

Report •

#15
November 30, 2012 at 05:35:17

Actual error:
a.vbs(6, 1) Microsoft VBScript runtime error: Overflow

Hint: What's the value of z if WScript.Arguments.Count is zero?
Hint 2: InputBox() might be a better choice for certain audiences.

How To Ask Questions The Smart Way


Report •

#16
November 30, 2012 at 13:00:49

select run from the start menu and type

c:\folderwhere\harmavg.vbs 400 800

IF you want to change the numbers go to the two numbers you wanted, type whatever you want them to be. Also change the directory/folder to where the file is.

:: mike


Report •

#17
November 30, 2012 at 18:59:43

Thanks, Mike and Razor. I forgot to code for the safety-catch on zero args, and neglected to offer various means of executing the script. You helped cover my omissions. I edited the posted script to try and trap some of the "div-by-zero" errors ("overflow") in case of future encounters with my woefully inadequate code.

Report •

#18
December 1, 2012 at 08:09:43

'this is compiled to ease your life
'credit to Razor2.3, and nbrane
varX =Inputbox("Enter X variable","Numerical Input Required")
if varX=0 then err("a zero has been entered")
varY =Inputbox("Enter Y variable","Numerical Input Required")
if varY=0 then err("a zero has been entered")

z=2/(1/varX+1/varY)
if z=0 then err("Negative arguments have resulted in zero")
wscript.echo z


sub err(msg)
wscript.echo msg & ", division by zero is not do-able!"
wscript.echo "where: varX, varY etc are any NONZERO values."
wscript.quit
end sub

wscript.quit

edit: paste into notepad and save as a .vbs, then double click.


Report •

#19
December 4, 2012 at 03:50:31

Really, Really tnx guuys...
Now its done.
I'd like to say TNX again. PERFECT!!!!
Tnx to Nbrane, razor and mikelinux.

Report •


Ask Question