I am trying to calculate the average between all the numbers in a text file (every number is on a different line), no matter how many numbers there are. (Using VBScript)
Here is an example of the text file that I am trying to find the average between its numbers:
1
9
4
4
2
Also, sorry if I didn't word this well, it's my first question.
Can you try this script (I suppose that numbers are integer):
filename = "myfile.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
i = 0 'Elements number
sum = 0 'Sum of values
Do Until f.AtEndOfStream
sum = sum + CInt(trim(f.ReadLine))
i = i + 1
Loop
avg = sum / i 'Calculate the average
f.Close
Thank you
Related
I have a text box with characters got from a calculation in my code . the textbox specifically contains only integers... Is there a way I can sum up the integers in the text box? Eg. If my textbox has 123456, the code should find the sum of 1+2+3+4+5+6 and then display the sum in another text box. Thank you in advance
VB6
Public Sub Calculate()
Dim i As Integer
Dim sum As Integer
Dim length As Integer
i = 1
sum = 0
length = Len(TextBox1.Text)
While i <= length
sum = sum + Mid(TextBox1.Text, i, 1)
i = i + 1
Wend
TextBox2.Text = sum
End Sub
Use Mid$() and Len() functions to retrieve number by number and add it (+) to the Total (sum).
EDIT:
Dim total As Integer, i As Integer
For i = 1 To Len(Trim$(Text1.Text))
total = total + CInt(Mid$(Text1.Text, i, 1))
Next i
Text2.Text = total
I have been trying to create this sequence:
2,7,17,37,67,...
I have to print first 10 numbers of the series.
To do this, I have created the following:
option explicit
Dim m,n,i,str,d
m=2
d=10
n=7
For i=0 to 10
n=n+d
d=d+10
str=str&n&vbcrlf
msgbox str
next
I am unable to print first two numbers, 2 and 7, as they are declared before the for loop. Even if I store them in a variable called str, they get printed after every execution. Is there a way to add these two and print them only once.
You could add your initial value of m to your string before you start running your sequence. Then, append the value of n to your string at the start of your loop instead of the end so that you capture n's initial value. For example:
m=2: d=10: n=7
str = m & vbCrLf ' Capture initial value of m
For i = 1 to 9
str = str & n & vbCrLf ' Capture initial value of n
n = n + d
d = d + 10
Next
MsgBox str
Note that you're only looping 9 times now, since you've already captured the first number in your sequence (m) prior to your loop.
I also moved MsgBox outside your loop so that it only appears once, after the full 10-number sequence has been generated.
I'm working with vb6 and I want to generate multiple randum numbers (the range from to is detirmend by user and also the number of generated answers) and send them to a listbox
But I don't want to duplicate generated numbers So..
I want before sending the generated number to the listbox to check if it already exists in the lisbox. if it already exists then generate another number if it does't then send it the the listbox
here is what I have till now
max and min are the range to chose numbers between
answers is the number of generated numbers
Randomize
For i = 1 To answers Step 1
generated = CInt(Int((max - min + 1) * Rnd() + min))
For n = 0 To List1.ListCount
If List1.List(n) <> gen Then
List1.AddItem (gen)
Else
If List1.List = gen Then
'I don't know what to do from here
'(how to go back to generate another number)
Next n
Next i
Thank you in advance
keep in minde I need to keep things simple
Thank you soo much
Use a boolean value to keep the result if same value generated is in list.
Private Sub AddRandomNumbers()
Dim blnIfFound As Boolean
Dim max As Integer
Dim min As Integer
Dim answers As Integer
max = 10
min = 1
answers = 5
Randomize
Do While List1.ListCount < answers
generated = CInt(Int((max - min + 1) * Rnd() + min))
blnIfFound = False
For n = 0 To List1.ListCount
If List1.List(n) = generated Then
blnIfFound = True
Exit For
End If
Next n
If blnIfFound = False Then List1.AddItem (generated)
Loop
End Sub
I'm working on a program in Visual Basic that takes user input as an integer from 1 to 99 and then tells the use how many quarters, dimes, Nickles, and pennies you need to supplement for that amount. My problem is completely symantical, and my algorithm isn't working like I thought it would. Here is the code that does the actual math, where the variables used are already declared
Select Case Input
Case 25 to 99
numQuarters = Input / 25
remainder = Input Mod 25
Select Case remainder
Case 10 to 24
numDimes = remainder / 10
remainder = remainder mod 10
numNickles = remainder / 5
remainder = remainder mod 5
numPennies = remainder
I'm going to stop there, because that's the only part of the code that's giving me trouble. When I enter a number from 88 to 99 (which is handled by that part of the code) the numbers come out strange. For example, 88 gives me 4 quarters, 1 dime, 1 Nickle, and 3 pennies. I'm not quite sure what's happening, but if someone could help me, I would appreciate it.
I reckon your problem was that it was storing the fractional part of the division, when it should be dropping it and keeping only whole multiples as the fractional portion of each coin is reflected in the remainder.
So after each division, truncate it. E.g.:
numQuarters = Int(Input / 25)
remainder = Input Mod 25
'or since you aren't working with fractional currencies you could use integral division operator '\':
numQuarters = Input \ 25
With 88 as the input it will, after these lines, have numQuarters = 3 and remainder = 18
Anyway, pehaps a more flexible way that doesn't rely on hard-coded orders of precedence and can handle whatever units you like (cents, fractional dollars, whatever) is like:
Sub exampleUsage()
Dim denominations, change
Dim i As Long, txt
'basic UK coins, replace with whatever
'you can of course use pence as the unit, rather than pounds
denominations = Array(1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01)
change = ChaChing(3.78, denominations)
For i = 0 To UBound(denominations)
txt = txt & Format(denominations(i), "£0.00") & " x " & change(i) & " = " & Format(denominations(i) * change(i), "£0.00") & vbCrLf
Next i
MsgBox (txt)
End Sub
'denominations is a Vector of valid denominations of the amount
'the return is a Vector, corresponding to denominations, of the amount of each denomination
Public Function ChaChing(ByVal money As Double, denominations)
Dim change, i As Long
ReDim change(LBound(denominations) To UBound(denominations))
For i = LBound(denominations) To UBound(denominations)
If money = 0 Then Exit For 'short-circuit
change(i) = Int(money / denominations(i))
money = money - change(i) * denominations(i)
Next i
ChaChing = change
End Function
The issue is with the bottom of my code where I have to get the program to recite the entered values in reverse order. I think it might be something to do with the index?
Option Explicit On
Option Strict On
'Author: Murray Spears
'Date: October 12th 2012
'Write a program that accepts five input values and stores them into an array.
'Then display numbers in reverse order.
'Then display the average number, and all numbers that are are above average.
Imports System
Module Values
Sub Main()
Dim Number(4) as Integer
Dim Average as Double = 0
Dim Index as integer
'
For Index = 0 to 4
Console.Write("Enter number: ")
Number(Index)=Convert.ToInt32(Console.Readline())
Next Index
'Figure out the average for all the entered values.
Average = (Number(0)+Number(1)+Number(2)+Number(3)+Number(4))/5
Console.Writeline("The average of the numbers is: " &Average)
Console.Write("Numbers that are greater than the average: ")
Index = 4
Do until Index = 0
If Number(Index) > Average then
Console.Writeline(Number(Index))
End If
Index -=1
Loop
Console.Writeline("Numbers in reverse order: ")
Index = 4
Do while Number(index) > 0
Console.Writeline(Number(Index))
Number(index) -= 1
Loop
End Sub
End Module
Use Step -1 to step backwards.
For Index = 4 To 0 Step -1
' do your thing
Next
Imo, the simplest way is to just use a loop as you did when you entered the numbers but as you wrote yourself make the indexes go in reverse.
This is what the "Step -1" is for.
For Index As Integer = 4 To 0 Step -1
Console.Writeline(Number(Index))
Next