Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I could use a little bit of input on creating a way to keep up with counting the number of people that have 2 scores in a text file.

then how I get it to do the math correctly.

I know it should be something simalar to

Total score += score1 + score2

But how do I make keep up with any amount of people in the text file and then divide it properly?

Looking to expand my VB.Net knowledge.

 

Jblake00

Posted

This sounds simple but you've presented your problem in a confusing way.

 

Do you mean reading a pair of numbers from a file and then adding them together?

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

Yes I do. But then I have to keep up with the number of times it does it and the figure out how to divide it.

I kind of got the idea in my head but I am missing something.

 varEveryonesGrade += Math.Round(varEveryonesGrade / 3)

That works when the text file only contains 3 people. And when you have to step through the stream reader for every line that is read. Such as.

       Start = my.ReadLine
       strStudent = Start.Substring(0, 11)
       Grade1 = Start.Substring(12, 4)
       Grade2 = Start.Substring(16, 4)
       varAverage = Math.Round((Grade1 + Grade2) / 2, 0)
       txtDsplyClsAvr.Text = "The class average is " & CStr(varEveryonesGrade)

And repeat the process for each person in the text file.

But a loop can do it by only writing the code once, But I need to get my counter down.Here is what I got so far, Also my msgBox doesn't display when the floppy isn't in the drive.

Dim my As IO.StreamReader
       my = IO.File.OpenText("A:\grades.txt")
       '
       Dim fmtStr As String = "{0,11}{1,4} {2,8}  {3,6}     {4,6}"
       Dim strStudent, Start As String, strLetterGrade As String
       Dim Grade1, Grade2, total As Double
       Dim varEveryonesGrade As Double
       Dim varAverage As Double
       varEveryonesGrade = 0
       varAverage = 0


       'Message box not showing up when file isn't in the drive.
              If IO.File.Exists("A:\grades.txt") = True Then

           Do While my.Peek <> -1
               ' strStudent = strStudent
               Start = my.ReadLine
               strStudent = Start.Substring(0, 11)
               Grade1 = Start.Substring(12, 4)
               Grade2 = Start.Substring(16, 4)
               varAverage += 1

               varAverage = Math.Round((Grade1 + Grade2) / 2, 0)


               varEveryonesGrade += varAverage + total
               varEveryonesGrade += Math.Round((Grade1 + Grade2) &  varAverage / varEveryonesGrade))


               varEveryonesGrade += 1
               txtDsplyClsAvr.Text = "The class average is " & CStr(varEveryonesGrade)


               '  Math.Round(varAverage)
               'need to use case statements with this one!
'I should be able to figure that out so no one needs to 
'bother with it unless they just so want to.. 
               If varAverage >= 90 Then
                   strLetterGrade = "A"


               ElseIf varAverage >= 80 Then
                   strLetterGrade = "B"

               ElseIf varAverage >= 70 Then
                   strLetterGrade = "C"

               ElseIf varAverage >= 60 Then
                   strLetterGrade = "D"

               ElseIf varAverage >= 50 Then
                   strLetterGrade = "F"
               End If

               lstGtGrds.Items.Add(String.Format(fmtStr, strStudent, Grade1, Grade2, varAverage, strLetterGrade))
               ''''''''''''''''''''''''
               '
           Loop

       ElseIf IO.File.Exists("A:\grades.txt") = False Then

           MsgBox("Try putting in the disk that has grades.txt on it!", , "Missing Disk")




       End If
       my.Close()

   End Sub

I hope that will clarify what I am trying to accomplish.

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

Just somethings you may have missed in there

 

varAverage += 1
varAverage = Math.Round((Grade1 + Grade2) / 2, 0)

the second line will always over write the contents of varAverage - the +1 you doing before it is not going to have any effect.

 

also in the line

varEveryonesGrade += Math.Round((Grade1 + Grade2) &  varAverage / varEveryonesGrade))

what are you trying to do - you seem to be mixing math calculations + and / with string concatenation - &. Is that really what you want?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Hmm I change a little code, but came up with a weird calculation.

The class average is 505, that should come out to be

The class average is 77. Here is what I tryed

varEveryonesGrade += varAverage + total
               total += (Grade1 + Grade2) & (varAverage / varEveryonesGrade)
               total = Math.Round(varEveryonesGrade, 2)

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

The problem could be the '&' it will calculate the (Grade1 + Grade2) bit as a number, then calculate the (varAverage / varEveryonesGrade) as a number and then concatenate the two together as if they were strings.

 

If you put a breakpoint on the line

total += (Grade1 + Grade2) & (varAverage / varEveryonesGrade)

 

what does the results of (Grade1 + Grade2) and (varAverage / varEveryonesGrade) look like and how do they compare to the calculated total?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Ok now I got

Start = my.ReadLine
               strStudent = Start.Substring(0, 11)
               Grade1 = Start.Substring(12, 4)
               Grade2 = Start.Substring(16, 4)
               varAverage = Math.Round((Grade1 + Grade2) / 2, 0)
               varAverage += varEveryonesGrade
               sum = Math.Round(varEveryonesGrade \ varAverage)
txtDsplyClsAvr.Text = "The class average is " & CStr(varEveryonesGrade)

But the result displayed in the text box is wrong. It says

"The class average is 0" It should give an average of 76 or 77?

Hmm I bet I need to be dividing this by the number of students.

That is probably what I need to be keeping the count on?

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

again

 

varAverage = Math.Round((Grade1 + Grade2) / 2, 0)
varAverage += varEveryonesGrade

 

you are asigning a value to a variable in one line and then simply overwriting it in the next. You seem to be making the problem a lot more confusing and convuleted than it really is. What does the file look like and what are you trying to do?

 

As far as I can tell you want to average a grade for a student from 2 grades and then average this value for X number of students - correct?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Well at least I accomplished one thing.

I fixed the message box problem.

All I have to do way move the line.

my = IO.File.OpenText("A:\grades.txt")

from under the Dim my IOSteamReader

to under the line If IO.File.Exists("A:\grades.txt") like this

If IO.File.Exists("A:\grades.txt") 
my = IO.File.OpenText("A:\grades.txt")

now the messagebox executes if the disk is not in the drive!

if I could just figure out the math.I know it is an algebraic type of problem or I think it is. I drew it on paper. Here is my idea of the thing

grade1 + grade2 / 2 = average>

grade1 + grade2 / 2 = average>>> =allAverages / sum

grade1 + grade2 / 2 = average>

Which in this case the sum would be 3, but I need to figure out a way to figure out how to keep track of the sum. Just in case more students are added to the text file. Sorry this post is getting so long.

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

grade1 + grade2 /2 -> average of one student.

average of all students / number of students = overall average (I think - my stats are a bit rusty)

 

you could simply add a counter to the routine and every time you read a student's details in just increment it.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Ok I am getting a lot closer I think? Here is my code now.

Private Sub btnGtGrades_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGtGrades.Click
       Dim my As IO.StreamReader      
       Dim fmtStr As String = "{0,11}{1,4} {2,8}  {3,6}     {4,6}"
       Dim strStudent, Start As String, strLetterGrade As String
       Dim Grade1, Grade2, total As Double
       Dim varEveryonesGrade As Double
       Dim varAverage As Double
       Dim sum As Integer

             varAverage = 0
                     sum = 0
               If IO.File.Exists("A:\grades.txt") Then
           my = IO.File.OpenText("A:\grades.txt")
           Do While my.Peek <> -1

               Start = my.ReadLine
               strStudent = Start.Substring(0, 11)
               Grade1 = Start.Substring(12, 4)
               Grade2 = Start.Substring(16, 4)
               varAverage = Math.Round((Grade1 + Grade2) / 2, 0)
               varEveryonesGrade += varAverage
               sum += 1
               varEveryonesGrade = Math.Round(varEveryonesGrade / sum)

               txtDsplyClsAvr.Text = "The class average is " & CStr(varEveryonesGrade)

                             
               Select Case varAverage
                   Case Is >= 90
                       strLetterGrade = "A"
                   Case Is >= 80
                       strLetterGrade = "B"
                   Case Is >= 70
                       strLetterGrade = "C"
                   Case Is >= 60
                       strLetterGrade = "D"
                   Case Is >= 50
                       strLetterGrade = "F"
               End Select
               lstGtGrds.Items.Add(String.Format(fmtStr, strStudent, Grade1, Grade2, varAverage, strLetterGrade))

           Loop

       Else

           MsgBox("Try putting in the disk that has grades.txt on it!, , Missing Disk")




       End If
       my.Close()

   End Sub
End Class

Everything works except I get an unexpected result in the text box where the class average is displayed. The txtBox displays the meassage, "The class average is 53" and it should be "77". I suspect this has something to do with counting my sum. Hmm by the way it is calculating, it is like it is being divided by 4.5 or something close to that when it should be divided by 3????

Looking to expand my VB.Net knowledge.

 

Jblake00

  • Administrators
Posted

try only doing the line

varEveryonesGrade = Math.Round(varEveryonesGrade / sum)

 

after the loop as you are currently going to be shifting the value lower by averaging it every iteration of the loop.

 

The first and second time the varEveryOnesGrade will be correct, but on the third loop you will have divided by one extra time, and again on the 4th etc.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...