Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
I need some help with working out averages of values entered!!!
Please help if you can 
Thanks
|
James
Member
Registered: 1st Jun 02
Location: Surrey
User status: Offline
|
Yes I do it for a living - what language?
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
averages of values entered?
average = ((value1 + value2 + value3) / 3)

p.s. Got any more detail? how many values? how are they inputted? what language?
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
Hi guys,
Im trying to upload my code onto this page but the bloody program won't open in the library 
College shite
Basically its a "Car Voting Program" the user will select a car (in a listbox) and a picture is displayed...
They will then vote on it using a scrollbar (value 1-10) they then need to submit the value (which is then stored using the write file command & a counter is used to show the amount of votes)
The sum is something like:
score1 = score1 + val(txtrating.text) (value of rating)
counter1 = txtvotes.text
counter1 = counter1 + 1
average = score1 / counter1
I would like to have an average score for each car (which is in a list) so when they click on the next car the current values are stored and boxes are reset (cleared)
I will send the code as soon as I can thanks
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
this in vb.net?
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
^^ Yes mate
Im using Visual Studio 2005
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
I don't 100% get what you're trying to do. Do you have to use text files to hold the 'data'? Can't you use a database back end?
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
The data is stored to a text file, using the Steamwriter and writefile command (sorry mate I can't remember off the top of my head)
I will post up the code asap but the bloody program isn't loaded on these machines
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
Just what i'm saying is, a file will essentially be a list of sequential data. So you can load that data from the text file into say a list...
So a text file containing names of cars, could be loaded and then displayed in a list.
but then when you click say the 'ferrari' in the list, there will be no method of fetching the data relative to the ferrari. i.e. to fetch the ferrari's previous average score etc.
With a database, you could have a table of cars and data relative to the car on number of votes and total vote amount.
So when the application loads, it calls to the database, loads the contents of 'car' table into a list - giving you a list of cars. Then you click a car, it queries the data base and fetches for that car, a count and total vote value.
Then you can do the sums from that easily and display it to the user.
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
So for example. you end up with something like this (v.basic example)

Basically it's gathered data from db to populate the car list. You click the car, it fetches car picture / details, votes and average. Then you click submit and it updates the vote / count value back in the database.
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
Well done that man, lol nice picture yea thats a good example of what I have done.
I shall ask my tutor if I am able to use a database to store the data (not entirely sure on this yet)
How long did that just take you btw?
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
2 minutes 
hmm... it could work with files, but you'd probably have to have multiple files...
So you'd have for instance.
carlist.txt = list of cars (for listbox).
nameofcar1.txt
nameofcar2.txt
nameofcar3.txt
nameofcar4.txt
etc
So in my example above you'd have
carlist.txt =
"
Ferrari_355
106_Gti
Porsche_911
"
then you'd have files
Ferrari_355.txt
106_Gti.txt
Porsche_911.txt
Then in each individual car file - like Ferrari_355.txt you could have number for vote total, number of votes, image name?
Then you just do something which goes
when clicking on item in list, open file name of (listbox1.SelectedItems.Item + ".txt") ... read in each into a variable (seperated by commas or whatever).
so you inport ferrari_355.txt which contains
104,11,Ferrari.jpg
score = 104
counter = 11
image = Ferrari.jpg
and go from there.
Then when you click submit, it takes the currently loaded information adds the new count to counter and new vote to score ... writes it all back to a file (overwriting any file existing or creating a new one if it doesn't exist).
That'd work, but it's not very good or scalable. A database would make your life much easier.
[Edited on 09-05-2008 by Paul_J]
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
Cheers Paul, im useless at programming
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
no prob. Find out what constraints you have to how you can do it. I hate how school / college can sometimes put annoying constraints on how you should do something which makes it harder.
If you have to use files as import, a xml file may be better than just a .txt file, at least then there's some structure to the data.
|
James
Member
Registered: 1st Jun 02
Location: Surrey
User status: Offline
|
XML FTW!
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
Hmm... I suppose if it's a really small example, just to get it to load and save to 1 single file. You could use a big multi dimensional array. 
not the ideal solution, but would work.
So like cars[10,4] for 10 cars.
It reads the data from the file seperated by commas...
for each line of the file increment y, for each comma increment x
cars[y,x] = whatever
cars[0,0] = carname
cars[0,1] = votes
cars[0,2] = score
cars[0,3] = imagesrc
new line of file increment incremenet y
cars[1,0] = car name
cars[1,1] = votes
cars[1,2] = counter
etc etc
Then you basically compile a list for all elements [0,0], [1,0], [2,0], [3,0], [4,0] etc
that'd give you a list of the car names.
Then when they click on the list, get the index value of that list item, and use it to access the rest of the car details.
so if you've clicked on the 4th car
[4,0] = name
[4,1] = votes
[4,2] = counter
etc...
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
quote: Originally posted by James
XML FTW!
yeh I have a feeling though, they won't be allowed to use xml, databases or anything like that.
I've got a feeling they will have to read and write plain text, to a text file
|
James
Member
Registered: 1st Jun 02
Location: Surrey
User status: Offline
|
.txt FTW!
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
When I learned stuff like this first time round I was only allowed to use a flat file system. Bit of an arse because there are so many better ways of doing it...
|
ed
Member
Registered: 10th Sep 03
User status: Offline
|
.dat FTW...
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
Here is the code for the button which I need to work (for calculating the average scores)
Private Sub cmdAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAverage.Click
Dim carfile As New System.IO.StreamWriter("H:\ITP\Assignment 3\Car Voting (assignment 3)\test.txt")
carfile.WriteLine(score1)
carfile.WriteLine(score2)
carfile.WriteLine(score3)
carfile.WriteLine(score4)
carfile.WriteLine(score5)
score1 = score1 + Val(txtrating.Text)
counter1 = counter1 + 1
txtvotes.Text = counter1
lstAverage.Text = score1 / counter1
End Sub
Should this work? if this is not enough code I will add more thanks 
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
well where's counter value coming from?
|
Ash_EP3
Member
Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
|
Public Class frmCarVoting
Dim counter As Integer
Dim counter1 As Integer
Dim counter2 As Integer
Dim counter3 As Integer
Dim counter4 As Integer
Dim counter5 As Integer
Dim cars(4) As String
Dim score1 As Integer
Dim score2 As Integer
Dim score3 As Integer
Dim score4 As Integer
Dim score5 As Integer
Dim car1 As String
Dim average As Integer
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
txtrating.Text = HScrollBar1.Value
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Image = My.Resources.ariel_atom
End Sub
Private Sub cmdRegister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRegister.Click
counter = counter + 1
txtvotes.Text = counter
HScrollBar1.Value = 1
txtrating.Text = ""
End Sub
Private Sub txtrating_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtrating.TextChanged
txtrating.Text = HScrollBar1.Value
End Sub
Private Sub lstCars_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCars.SelectedIndexChanged
If lstCars.SelectedIndex = 0 Then
PictureBox1.Image = My.Resources.ariel_atom
lblName.Text = lstCars.SelectedItem
End If
If lstCars.SelectedIndex = 1 Then
PictureBox1.Image = My.Resources.civic_type_r
lblName.Text = lstCars.SelectedItem
End If
If lstCars.SelectedIndex = 2 Then
PictureBox1.Image = My.Resources.golf_w12_650
lblName.Text = lstCars.SelectedItem
End If
If lstCars.SelectedIndex = 3 Then
PictureBox1.Image = My.Resources.lambo_gallardo
lblName.Text = lstCars.SelectedItem
End If
If lstCars.SelectedIndex = 4 Then
PictureBox1.Image = My.Resources.porsche_carrera_gt
lblName.Text = lstCars.SelectedItem
End If
End Sub
Private Sub cmdAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAverage.Click
Dim carfile As New System.IO.StreamWriter("H:\ITP\Assignment 3\Car Voting (assignment 3)\test.txt")
carfile.WriteLine(score1)
carfile.WriteLine(score2)
carfile.WriteLine(score3)
carfile.WriteLine(score4)
carfile.WriteLine(score5)
score1 = score1 + Val(txtrating.Text)
counter1 = counter1 + 1
txtvotes.Text = counter1
lstAverage.Text = score1 / counter1
End Sub
End Class
This is my full code, counter value is being calculated on how many times someone presses the "register rating" button
|