ASSIGNMENT
A teacher has six students and wants you to create an application that stores their grade data in a file and prints a grade report. The application should have a structure that stores the following student data: Name (a string), Test Scores (an array of five Doubles), and Average (a Double). Because the teacher has six students, the application should use an array of six structure variables.
The application should allow the user to enter data for each student, and calculate the average test score.
The user should be abled to save the data to a file, read the data from the file, and print a report showing each student's test scores and average score. The form shows a meny system. You may you buttons instead if you prefer.
Input validation: Do not accept test scores less that zero or greater than 100.
]
my understanding of how it should be structured
For the Moment I don't understand that in the FOR EACH loop I can not accumulate total it saying that I am not allowed to use + . I am trying to get scores from txtScore1Std1 (For example) assign it to dblTestScoreArray and using for each loop to find sum of those 5 score and when find average and output it to lbl average for student number 1.
Code Module:
Module StudentTestScoresModule
Const intMAX_SUBSCRIPT_STUDENT As Integer = 6
Const intMAX_SUBSCRIPT_STUDENT_SCORES As Integer = 5
'create structure
Public Structure StudentData
Dim strName As String
Dim dblTestScoresArray() As Double
Dim dblAverage As Double
End Structure
Dim dblTotalStd1 As Double
Dim dblScore As Double
Dim StudentsArray(intMAX_SUBSCRIPT_STUDENT) As StudentData
Sub StudentNameDataInput()
StudentsArray(0).strName = MainForm.txtStdName1.Text
StudentsArray(1).strName = MainForm.txtStdName2.Text
StudentsArray(2).strName = MainForm.txtStdName3.Text
StudentsArray(3).strName = MainForm.txtStdName4.Text
StudentsArray(4).strName = MainForm.txtStdName5.Text
StudentsArray(5).strName = MainForm.txtStdName6.Text
End Sub
Sub StudentScoreDataInput()
For intIndex = 0 To intMAX_SUBSCRIPT_STUDENT
ReDim StudentsArray(intIndex).dblTestScoresArray(4)
Next
'test scores for first student
StudentsArray(0).dblTestScoresArray(0) = CDbl(MainForm.txtScore1Std1.Text)
StudentsArray(1).dblTestScoresArray(1) = CDbl(MainForm.txtScore2Std1.Text)
StudentsArray(2).dblTestScoresArray(2) = CDbl(MainForm.txtScore3Std1.Text)
StudentsArray(3).dblTestScoresArray(3) = CDbl(MainForm.txtScore4Std1.Text)
StudentsArray(4).dblTestScoresArray(4) = CDbl(MainForm.txtScore5Std1.Text)
For Each i As StudentData In StudentsArray
dblTotalStd1 += i
Next
dblAverage = dblTotalStd1 / intMAX_SUBSCRIPT_STUDENT_SCORES
MainForm.lblAvgStd1.Text = (dblAverage.ToString)
End Sub
Sub CalculateAverage()
End Sub
End Module
Code Main Form:
Public Class MainForm
Private Sub mnuHelpAbout_Click(sender As Object, e As EventArgs) Handles mnuHelpAbout.Click
'about program
MessageBox.Show("Student test score calculator version 0.1")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close(program)
Me.Close()
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
StudentScoreDataInput()
End Sub
End Class
just by looking, without testing, what you need to do is;
'untested code
For Each i As StudentData In StudentsArray
For Each S as Double in i.dblTestScoresArray
dblTotalStd1 += s
Next
Next
you cannot do += on a structure, you need to do it on the member and since its an array, you need to loop through it
Related
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim EvenNum, EvenNumCount, EvenNumAverage, Number, Result As Integer
Calculations(EvenNum, EvenNumCount)
GetInput(Number)
Output(Result)
End Sub
Sub GetInput(ByRef Number)
Number = txtInput.Text
End Sub
Sub Calculations(ByRef EvenNum, ByRef EvenNumCount)
Dim ListedNumbers, lstOutputSize As Integer
GetInput(lstOutputSize)
For i As Integer = 0 To lstOutputSize - 1
ListedNumbers = InputBox("Enter Numbers", "Input")
lstOutput.Items.Add(ListedNumbers)
Next
For i As Integer = 0 To lstOutput.Items.Count - 1
If (CInt(lstOutput.Items(i)) Mod 2 = 0) Then
EvenNum += lstOutput.Items(i)
EvenNumCount += 1
End If
Next
End Sub
Function Average(ByRef EvenNumAverage As Integer) As Integer
Dim EvenNum, EvenNumCount As Integer
Calculations(EvenNum, EvenNumCount)
EvenNumAverage = EvenNum / EvenNumCount
Return EvenNumAverage
End Function
Sub Output(ByRef EvenNumAverage)
lstOutput.Items.Add(Average(EvenNumAverage))
End Sub
The program is supposed to get input from a textbox for a desired number of numbers to be entered into a listbox from inputboxes.
It is then supposed to get the average of only the even numbers and then display that average into the listbox.
In it's current state the program will do what it is intended to do, it just repeats the calculation code. This only happens when I add the Output call statement under the button procedure.
You're calling Calculations twice
From btnCalculate_Click
From Average which is called by Output
I'm making a quiz and it requires a highscore function as said before. When I run the program everything is fine except that the highest score won't always be the highest score.
If I retake it, a lower score will still replace the highest. scores aren't showing in the 2nd and 3rd highest brackets either which I assume is with the same problem..
Here's the code-- (With each question answered correctly there is score += 1, then calling score update at the end of last question. )
Public score As Integer = 0
'best score
Dim highest As Integer
'second best
Dim scoreuno As Integer
'third best
Dim scoredos As Integer
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub scoreupdate()
highScore.Text = highest.ToString
score2.Text = scoreuno.ToString
score3.Text = scoredos.ToString
End Sub
Public Sub highscores()
If score > highest Then
scoredos = scoreuno
scoreuno = highest
highest = score
ElseIf score > scoreuno Then
scoredos = scoreuno
scoreuno = score
ElseIf score > scoredos Then
scoredos = score
End If
scoreupdate()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
score = 0
Form1.Show()
Me.Close()
End Sub
You could simplify your code a little by using a list of integers like this
Dim HighScores As New List(Of Integer)
Then, instead of using your current HighScores sub using the sub below, just add a score to the list which is sorted in to highest value first, and then the lowest value is removed from the end of the list.
This way, if you decide to have more than 3 high scores, it should work just fine. just change the maxHighscores variable instead of having to edit your If statement and add increasing amounts of code. Then just add code into your update sub to show the additional scores.
Private Sub AddScoreIfHighEnough(score As Integer, maxScoresCount As Integer)
'Adds score to list
HighScores.Add(score)
'Sorts list into ascending order and reverses the order
HighScores.Sort()
HighScores.Reverse()
'if the number of scores is greater than the maximum allowed
'number remove the extra scores
While maxScoresCount < HighScores.Count
HighScores.RemoveAt(HighScores.Count - 1)
End While
End Sub
To use it just add replace each time you use
highScores()
with
AddScoreIfHighEnough(score,3)
Finally to assign your scores, just use
Private Sub scoreupdate()
highScore.Text = HighScores(0).ToString
score2.Text = HighScores(1).ToString
score3.Text = HighScores(2).ToString
End Sub
Hello i am trying to make a texas hold'em style game and im at the point where i filled an array with numbers 1-52 randomly assorted. I first pull the value from the first index of the array and have the corresponding card picture be set to a picturebox value. I have saved the 52 card .png files in my resources as well. These pictures are also saved with their names as 1.png, 2.png, 3.png.... etc depending the suit and value.
I am sorting the values as 1-13 spades (2-ace), 14-26 hearts, 27-39 diamonds, 40-52 clubs.
I also just saw i should probably use a global counter to keep track of the deck position.
Public Class Form1
Dim Deal As MsgBoxResult
Dim CardDeck As New Random
Dim Counter As Integer = 1
Dim CardCount(52) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Deal = MessageBox.Show("Would you like to start a Game?", "Texas Holde'em", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
ShuffleDeck()
CalculateFirst3Cards()
End Sub
Private Sub ShuffleDeck()
If Deal = MsgBoxResult.Yes Then
For num As Integer = 1 To CardCount.Length - 1
Dim DeckValue As Integer = CardDeck.Next(1, 52)
CardCount(Counter) = DeckValue
Counter += 1
Next
End If
End Sub
Private Sub CalculateFirst3Cards()
Dim counter As Integer = 1
For num As Integer = 1 To 3
Dim hold As Integer = CardCount(counter)
Dim hold1 As String = Convert.ToString(hold)
River1.Image = My.Resources.
counter += 1
Next
End Sub
End Class
Ok, so I am super new to visual basic and was not planning on taking it as a class either until they made it a requirement for me to get my technicians certificate at my community college. Literally understand the chapters I have read so far to a T, and then first homework assignment comes up and for the past few days I have been scratching my head as to why on earth it is not working. Here is what the Prof is asking.
Write a program that calculates average daily temperatures and summary statistics. The user will be prompted to enter a Fahrenheit temperature as a value with one decimal place and to select the name of the technician entering the temperature. The user will have the option to see the Celsius equivalent of the entered Fahrenheit temperature. The program will display the average temperature of all entered temperatures. The results are displayed when the user hits ENTER, uses the access key or clicks the Calculate button. The user will be given the opportunity to enter another temperature when the user hits ESC (Clear is the Cancel Button), uses the access key or clicks the Clear button. The user will exit the program by clicking the Exit button or using its access key. The Exit button will also display the summary statistics: 1) the number of temperatures entered by each technician and 2) the average temperature of all entered temperatures. Calculations should only be done if a numeric value between 32.0 and 80.0 (inclusive) degrees for temperature is entered and a technician has been selected.
The graphical side is a breeze with dragging and dropping, then naming the labels, radio buttons, etc... But now that I have assembled my code. Nothing is working. I'm frustrated, confused, and let down. I had no idea this class would be this hard. Here is what I came up with so far code wise. No error messages at all, just not getting any output pretty much.
Option Strict On
Option Strict On
Public Class Form1
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear App
txtTemp.Clear()
lblAverageTemp.Text = String.Empty
lblCelsius.Text = String.Empty
radDave.Checked = False
radJoe.Checked = False
chkCelsiusTemp.Checked = False
'New Temp Focus
txtTemp.Focus()
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
'End app with display
MessageBox.Show("Dave entered intEntriesDave entries." & ControlChars.CrLf & "Joe entered intEntriesJoe entries." & _
ControlChars.CrLf & "The average temperature is _.", "Status")
Me.Close()
End Sub
Public Sub chkCelsiusTemp_CheckedChanged(sender As Object, e As EventArgs) Handles chkCelsiusTemp.CheckedChanged
'Convert entered Fahrenheit temp to Celsius
Dim dblCelsius As Double
dblCelsius = (CDbl(txtTemp.Text) - 32) * 5 / 9
lblCelsius.Text = CStr(dblCelsius)
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim intEntriesDave As Integer = 0
Dim intEntriesJoe As Integer = 0
If radDave.Checked = True Then
intEntriesDave = +1
End If
If radJoe.Checked = True Then
intEntriesJoe = +1
End If
Dim dblAvg As Double
dblAvg = CDbl(txtTemp.Text) / intEntriesDave + intEntriesJoe
lblAverageTemp.Text = CStr(dblAvg)
End Sub
End Class
Hope I figure this out or I can get some help with it. I procrastinated of course, like the idiot I am, and it is due in 11 hours :\
Thanks in advance!
I would use a dictionary to store names along with temperatures.
Place a numericupdown control on your form for the temperature input (numTemp) and a textbox for names tbName and a label lblCelsius for output:
Public Class Form1
Dim temps As New Dictionary(Of String, List(Of Double))
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
If numTemp.Value < 32 OrElse numTemp.Value > 80 OrElse tbName.Text = "" Then Exit Sub 'Invalid input
If temps.ContainsKey(tbName.Text) = False Then 'Name is new, create a new list entry
temps.Add(tbName.Text, New List(Of Double))
End If
temps(tbName.Text).Add(numTemp.Value) 'Append the entered temperature
lblCelsius.Text = "In celsius: " & CStr(numTemp.Value - 32) * 5 / 9 'Output the Celsius value
End Sub
Private Sub btnStats_Click(sender As Object, e As EventArgs) Handles btnStats.Click
Dim sb As New System.Text.StringBuilder 'Create the output
For Each k As String In temps.Keys
sb.AppendLine(k & ": " & temps(k).Average)
Next
lblCelsius.Text = sb.ToString
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
temps.Clear() 'Clear the database
End Sub
End Class
Basically everytime you click btnEnter you check your dictionary if the name already entered a value. If not a new entry is created with a new list and the new temperature is just added to the list.
Creating the output is then straightforward with the .Average method of the list.
I am trying to keep some skills by writing a application during my semester break at school and have found some issues I don't know the answer to.
I am trying to get this code to generate 10 results and concatenate them to a ListBox named lstPhoneNumbers. Here is what I've tried:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Calculate Random Prefix based upon selected city
If lstBoxCity.SelectedItem.ToString = "Bethany" Then
' Initialize the random-number generator.
Randomize()
Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}
' Generate random value between 1 and then length of your Bethany array
Dim randomBethany As String = Bethany(CInt((Bethany.Count * Rnd()) + 1))
MsgBox(randomBethany.ToString)
End If
* Edited for Steven Doggart**
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Calculate Random Prefix based upon selected city
If lstBoxCity.SelectedItem.ToString = "Bethany" Then
Dim Bethany As String() = {"298", "342", "443", "644", "712", "755", "759", "777", "779", "847"}
For i As Integer = 0 To 9
lstPhoneNumbers.Items.Add(Bethany(RandomPrefix.Next(0, Bethany.Count - 1)))
Next
End If
End Sub
You really should be using the Random class rather than the old-VB6-style Rnd method. Even if you are using Rnd, you should, ideally, only be calling Randomize once, when the program starts, not every time the button is clicked. To do this with the Random class, first you should create a Random object as a private field on your form, like this:
Public Class MyForm
Private rand As New Random()
' ...
Then, in your button click event handler, you need to create a For loop which generates 10 random numbers, adding each one to the ListBox control as it does so:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'...
For i As Integer = 0 to 9
lstPhoneNumbers.Items.Add(Bethany(rand.Next(0, Bethany.Count - 1)))
Next
End Sub