Results 1 to 11 of 11

Thread: adding to my array(some help plz :)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    adding to my array(some help plz :)

    Code:
    Dim intx As Integer
            Dim intmark As Integer
            Dim inttotalmarks As Integer
            Dim intstudents As Integer
            Dim inttotal As Integer
            Dim dblpercent As Double
            Dim chargrade(30) As Char
            Dim strgrade As String
    
    
            intstudents = Convert.ToInt32(txtnoofstudents.Text)
    
            For intx = 0 To (intstudents - 1)
                intmark = InputBox("Please enter the students mark")
    
                inttotal = Convert.ToInt32(txtmarksavailable.Text)
    
                dblpercent = intmark / inttotal * 100
    
    
                If Me.radday.Checked Then
    
                    If dblpercent > 89 Then
                        strgrade = "A"
                    ElseIf dblpercent > 79 Then
                        strgrade = "B"
                    ElseIf dblpercent > 69 Then
                        strgrade = "C"
                    ElseIf dblpercent > 59 Then
                        strgrade = "D"
                    ElseIf dblpercent > 1 Then
                        strgrade = "F"
                    End If
    
                Else
    
                    If dblpercent > 84 Then
                        strgrade = "A"
                    ElseIf dblpercent > 74 Then
                        strgrade = "B"
                    ElseIf dblpercent > 64 Then
                        strgrade = "C"
                    ElseIf dblpercent > 49 Then
                        strgrade = "D"
                    ElseIf dblpercent > 1 Then
                        strgrade = "F"
    
    
                    End If
    
                End If
    
                chargrade(intx) = Convert.ToChar(strgrade)
    
    next
    ok well thats my code and the problem i am having is with the last line, what i need is that after each grade is calculated to be passed into the array chargrade for futher processing later on....any suggestions on how to make that line work?

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: adding to my array(some help plz :)

    You code looks ok to me, what is the problem exactly?

    If you put MessageBox.Show(chargrade(intx).ToString) directly after the last line you should see that each value is being correctly added to the array. Is that not the case when you run it?

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: adding to my array(some help plz :)

    your are setting the calculated value to an locally declared array, if you need to have those values outside this Sub, you have to declare the array in the general section. Also you have declared a fixed length array, are the 30 entries enough?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Re: adding to my array(some help plz :)

    ok thanks for replyin...but the line i had the problem with has fixed itself some how...what i need now is for the line to display each grade below each other this is the line i have currently in which displays them beside each other

    Code:
    Label1.Text = Label1.Text & chargrade & ControlChars.NewLine

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: adding to my array(some help plz :)

    vb Code:
    1. Me.Label1.Text &= charGrade & Environment.NewLine
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Re: adding to my array(some help plz :)

    unfortunately that code also brings the grades up on the same line

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: adding to my array(some help plz :)

    Looking above your last post i see charGrade is actually an array so just referring to it by charGrade doesn't really make sense.

    Do you want them displayed at the end once they have all entered their grades or while they are inputting them.

    For example this works fine:
    vb Code:
    1. charGrade(0) = "A"c
    2.         charGrade(1) = "B"c
    3.         charGrade(2) = "C"c
    4.         charGrade(3) = "D"c
    5.  
    6.         For i As Integer = 0 To 3
    7.  
    8.             Me.Label1.Text &= charGrade(i) & Environment.NewLine
    9.         Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Re: adding to my array(some help plz :)

    what i need is once all the marks are input they are then transferred into grades using my if statements. i am not all that great with programming but i think i put it outside the loop so it would display after everything is input...looking at that code it would display them as i input the marks i think

  9. #9
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: adding to my array(some help plz :)

    If you add this after everything has been completed (all marks have been entered and converted) then it will work (obviously rename the controls or variables as you need).
    vb Code:
    1. Me.Label1.Text = String.Empty
    2.  
    3.         For i As Integer = 0 To charGrade.Length - 1
    4.  
    5.             Me.Label1.Text &= charGrade(i) & Environment.NewLine
    6.  
    7.         Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    15

    Re: adding to my array(some help plz :)

    ok ty v much that works...they next stage of the program is for me to count the amount of each grade so if i have b,b,b,c,d,c i need to display that i have 3 bs, 2 cs and 1 d anyone have any ideas how to do that?

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: adding to my array(some help plz :)

    Well you could have 5 counters for each of the grades. Loop through each character in the gradeChar array and increase the counter by 1 if found at that index. Display them all at the end.

    Although JMC posted something last night which was used to count the number of occurences of a letter using a dictionary. Something like this:
    vb Code:
    1. Dim gradeCount As New Dictionary(Of Char, Integer)
    2.         For Each c As Char In charGrade
    3.             If gradeCount.ContainsKey(c) Then
    4.                 gradeCount(c) += 1
    5.             Else
    6.                 gradeCount.Add(c, 1)
    7.             End If
    8.         Next
    9.  
    10.         For Each itm As KeyValuePair(Of Char, Integer) In gradeCount
    11.             MessageBox.Show("Grade " & itm.Key.ToString & " : " & itm.Value.ToString & " times")
    12.         Next
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width