Help - Search - Members - Calendar
Full Version: Visual Basic help
Pixel2Life Forum > Help Section > Desktop Programming
N-sane Noob
My question is how do I make an ongoing average. For example I have a list and and everytime I enter a number the number will appear on the list one under another. How do I make an average of those number. But lets say i find the average of those numbers but when i enter another number, the new average should appear.

Another question:

I know this sounds pretty easy but how do I write a list to file and also read it.
EclipsE
VB... my speciality smile.gif
Ok, now, we go from the start. Make a Listbox, textbox, commandbutton and a label, and give them some names like lstNumbers, txtNumber, cmdAdd, lblAverage. From the names you should see what is what. Now, in cmdAdd_Click procedure, add this:

CODE
If IsNumeric(txtNumber.Text) Then
    lstNumbers.AddItem txtNumber.Text
    txtNumber.Text = ""
    CalculateAverage
Else
    MsgBox "Only numbers are allowed!"
End If


The first line checks if only numbers are present in the textbox. If it's true, then add the number to the list, empty the textbox and call the sub CalculateAverage, see down... If not, display a message box with info "Only numbers are allowed!". Easy. Moving on...

CODE
Private Sub CalculateAverage()
Dim i As Integer
Dim averagenum As Double

For i = 0 to lstNumbers.ListCount - 1

    averagenum = averagenum + CDbl(lstNumbers.List(i))

Next i

averagenum = averagenum / lstNumbers.ListCount
lblAverage.Caption = "Average Number is: " & averagenum

End Sub


Easy, ain't it? Hehe...

Sorry, in the next code i did not capitalise some words and did some spaces but i want to hurry a little bit...
As for file input and output, see this:
For output:
CODE
Dim i as integer, ff as integer
ff=freefile   'get a free file

open "c:\list.lst" for output as #ff   'open the file for ouput

    for i = 0 to lstNumbers.ListCount-1  'from the first to the last in the list
        print #ff, lstNumbers.List(i)         'write it down
    next i     'next

close #ff   'close the file


For input:
CODE
Dim ff as integer, aline as string
ff=freefile   'get a free file

if not dir("c:\list.lst") then exit sub   'if the file does not exist exit the sub

open "c:\list.lst" for input as #ff    'open file

    while not eof(ff)   'while we are not at the end of the file
    
        line input #ff, aline   'read a line
        lstnumbers.additem aline  'add to the list

    wend

    'we can calculate the average now
    CalculateAverage

close #ff   'close file


If you're stuck somewhere, say it!
N-sane Noob
thanks a lot. Know I have another problem. I am doing a who wants to be a millionaire game and I am trying to randomize the questions but a good way I found is ARRAYS but I know very little about arrays.

Is there an easier way?
EclipsE
The easiest thing is - arrays smile.gif
It isn't complicated at all. Array 101 - starting with arrays

So, what is an array? An array is a variable of a type(String, Integer, Long etc...) that has many members. I can't explain it well. But, here is a sample:

CODE
Dim strQuestions(10) As String

strQuestions(0) = "How much money does Bill Gates have?"
strQuestions(1) = "When was the first computer invented?"
etc etc...


The first line declares an array with 10 members, the type of string. But the lower bound is 0 so it means we have 11 members. We could do this:

CODE
Dim strQuestions(1 To 10) As String

And now we have 10 members starting from 1 to 10.

Now, the other lines in the example.
CODE
strQuestions(0) = "How much money does Bill Gates have?"

and the other one
CODE
strQuestions(1) = "When was the first computer invented?"


We can assign a value to an array member just like to some variables.

CODE
ArrayName(MemberIndex) = Value


That simple smile.gif

Now, you say you were doing a game like millionare. A easy way is to do it with arrays of user types. Check this:

CODE
Private Type TQuestion
    strQuestion As String
    strAnswer As String
    strAnswers(1 To 4) As String
End Type

Dim arrQuestion(1 To 10) As TQuestion


You can manage every value of the arrQuestions array. Like this:

CODE
With arrQuestions(1)
    .strQuestion = "How much money does Bill Gates have?"
    .strAnswer = "Noone knows!"
    .strAnswers(1) = "10 billion dollars"
    .strAnswers(2) = "20 billion dollars"
    .strAnswers(3) = "50 billion dollars"
    .strAnswers(4) = "Noone knows!"
End With

With arrQuestions(2)
     .strQuestion = "A question"
     .strAnswer = "The right answer"
     .strAnswers(1) = "Wrong answer"
     .strAnswers(2) = "Wrong answer"
     .strAnswers(3) = "The right answer"
     .strAnswers(4) = "Wrong answer"
End With


And the rest you got to figure out! If you need help or if you want me to make the quiz (i can do it very fast) just say! I'll do anything to help people!

wink.gif
N-sane Noob
Yes please. If you have time...I would appreciate 100% and when you will ever need help I'll be the first one to offer smile.gif
EclipsE
cool.gif I even made it with a cool GUI!

Usually, i would recommend to store questions and answers in a seperate file crypted but still, it's your choice! (if you want to do that i can help too rolleyes.gif )

If you need anything, say! I'm always here to help

btw, The project is commented, but still if you don't understand something, i'm here...
N-sane Noob
lol...thanks a lot...but the thing is this is to professional for me.....u even used GUI ...lol...all i wanted was some command buttons and random question by using arrays smile.gif
EclipsE
Well... Just, think of the labels as command buttons! If the labels were command buttons and there was no GUI, the code would be... the same...
N-sane Noob
lol sorry i am really bad at this..sad.gif
EclipsE
You're not the first and not the last... It reminds me when i was a begginer, except i needed to figure out everything all by myself... Man, i am programming for like 7 years in VB so, you're a lucky man having someone to help you on your way to success! shades.gif
N-sane Noob
lol...to tell u the secret....this is for a school project....im getting most of the stuff.....but arrays is for a higher grade...
EclipsE
smile.gif

Anything i could do for you?
N-sane Noob
I wish you could make me...only if u want....a new one with only command buttons and just the label with questions......using arrays,,,,thank you..please please smile.gif....like....newbish looking...lol
EclipsE
Here you go! No user defined types, only arrays. And one 2-dimensional array, the one with the answers.

Oh, and remove the comments if you are going to use it for a school project... smile.gif
N-sane Noob
lol....man..ur too good......even tho this is newbish for u ...its professional for what i learn....lol....theres stuff like init question....and a lot that i didnt learn....what i needed was this.....4 command buttons and random questions and the money......lol....thats really pro.....i just wanted something so simple.....the code is very advanced for me....lol
EclipsE
That's simple! You got 4 command buttons and random questions, and the money

InitQuestions:

In this sub you init the questions, give them answers and what is the right answer. An example:

CODE
strQuestion(1) = "What's my creator's display name on p2l?"
strAnswers(1, 1) = "Daredevil"
strAnswers(1, 2) = "EclipsE"
strAnswers(1, 3) = "DaCr00k"
strAnswers(1, 4) = "ZeroCool"
strAnswerIndex(1) = 2


The first line is the question you want. It'll be the first question because between ( and ) is the number 1. To init the second question you would write:

CODE
strQuestion(2) = "My question"


and so on and so on...

Beware that the number of questions is limited, there can only be 10 questions in this example because of this:

CODE
Private strQuestion(1 To 10) As String
Private strAnswerIndex(1 To 10) As Integer
Private strAnswers(1 To 10, 1 To 4) As String


You see that everywhere says 1 To 10. To make more questions change the 10 in every line to the number of questions you want to have.

Next, if you make more questions be sure to change the following lines:

CODE
currentQuestion = Int(Rnd * 11)
If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 10 Then currentQuestion = 10


to

CODE
currentQuestion = Int(Rnd * (NumberOfQuestions + 1))
If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > NumberOfQuestions Then currentQuestion = NumberOfQuestions


replace NumberOfQuestions with how many questions do you have.

and replace this

CODE
For i = 1 To 10


with

CODE
For i = 1 To NumberOfQuesions


Again, the numberofquestions is a number that represents how much questions do you have.

Ok, let's go back to the init routine:

CODE
strAnswers(1, 1) = "Daredevil"
strAnswers(1, 2) = "EclipsE"
strAnswers(1, 3) = "DaCr00k"
strAnswers(1, 4) = "ZeroCool"


That are the possible answers to the question, and one of them is the right answer. To set some possible answers type this:

CODE
strAnswers(numQ, 1) = "answer1"
  strAnswers(numQ, 2) = "answer2"
  strAnswers(numQ, 3) = "answer3"
  strAnswers(numQ, 4) = "answer4"


Where numQ is the question number to who you want to set the possible answers. numbers after the question number, can be 1, 2, 3 or 4. They represent the A, B, C and D answer on the command buttons. The text in the "" is the answer.

and the last thing:

CODE
strAnswerIndex(1) = 2


This means that the right answer to the question 1 is the second answer, and if you see the lines up you'll see that the second answer is EclipsE. Look at my display name... It says it all victory.gif
To use the AnswerIndex

CODE
strAnswerIndex(numQ) = TheRightAnswer


numQ - the question number
TheRightAnswer - the number of the answer that is correct, can be 1, 2, 3 or 4

Ok, that's the init questions sub.

In RandomizeQuestions we do the following:

Randomize Everything with the command Randomize, this means that the program will start differently every time it is executed. Without this, the questions would be random, but always in the same order every time the program has been started.
Then we make a random number from 0 to 1 and using Rnd and then multiplying it with 11, and rounding it to a whole number using Int.
Then we check if the number is smaller than 1 and if it is, make it 1.
If the number is larger than 10, we make it 10.

And then comes the tricky part. You see, every time you give the correct answer i put the question number in a string so we would not come to that question again. Example, if i answered on questions 3, 6 and 7 the string would be like this: "|3||6||7|"
Now we check if the random number we got is a question that already was answered from the program start using InStr. We check if there is "|" the question number "|" in the string. If not, we got our next question!
But if we got it.... Well, we'll use a trick.

We'll loop from 1 to the last question and see if the question is free(not in the string). If it's not in the string we take it, set the currentQuestion and Exit For. If it's in the string we set currentQuestion to 0.

Eventually, we would go answer all questions. That's why we use currentQuestion and set it to 0. If it's 0 after the For - Next, that means no questions are left.... And you are a millionare!

I maybe left something and maybe you'll not understand everything but hey.......

rolleyes.gif
N-sane Noob
Okay....i have changed most of the stufff....but im not getting this...

CODE
If currentQuestion <> 0 Then DoneQuestions = DoneQuestions & "|" & currentQuestion & "|"


and this

CODE
If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 20 Then currentQuestion = 20
EclipsE
Easy.

CODE
If currentQuestion <> 0 Then DoneQuestions = DoneQuestions & "|" & currentQuestion & "|"


If the current question isn't 0 we add it to the done questions string so we know later that the question has been used.
But if it's 0 nothing will happen cuz that will mean that the game is finished.

CODE
If currentQuestion < 1 Then currentQuestion = 1
If currentQuestion > 20 Then currentQuestion = 20


We check for the boundaries. Sometimes the current question after randomizing can be 0. Then we check if it's less than 1, and if it is we set it to 1.
If the question after randomizing is larger than the max questions, we set it to 20(in your case).
. Adam .
Man, EclipsE you should write some tutorials smile.gif

- Adam bigwink.gif
EclipsE
I did but all on serbian... I'm toooooo lazy to translate them smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.