Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 18 Introducing 2 dimensional arrays Using radio buttons Revisit student grades application Problem definition.

Similar presentations


Presentation on theme: "Chapter 18 Introducing 2 dimensional arrays Using radio buttons Revisit student grades application Problem definition."— Presentation transcript:

1 Chapter 18 Introducing 2 dimensional arrays Using radio buttons Revisit student grades application Problem definition

2 A teacher issues three tests to a class of 10 students. The grades on these tests are integers in the range from 0 to 100. The teacher has asked you to develop an application to keep track of each student’s average and the average of the class as a whole. The teacher has also asked that there be a choice to view the grades as either numbers or letters. Letter grades should be calculated according to the grading system: 90–100A 80–89B 70–79C 60–69D Below 60F The application should allow a user to input the student’s name and three test grades, then compute each student’s average and the class average. The application should display number grades by default.

3 One dimensional Array MondayTuesdayWednesdayThursdayFridaySaturdaySunday 0123456 SEVEN ELEMENTS First ElementSeventh Element Position 0Position6 (n-1) Position numbers Name of Array

4 Two-Dimensional Rectangular Arrays

5 2D Arrays Two dimensional arrays are initialized just like one dimensional arrays Dim numbers As Integer (, ) = New Integer (0 to 1, 0 to 1) { } numbers (0,0)=1 numbers (0,1)=2 numbers (1,0)=3 numbers (1,1)=4 (,) indicates 2D array Alternate forms to declare –Dim numbers As Integer (, ) = New Integer (, ) {{1,2}, {3,4}} –Dim numbers As Integer (, ) = New Integer (1,1) { } Array Position 01 012 134

6 Radio Buttons Come on --- you didn’t think there would be a slide on this. You all know what a radio button is?

7 Developing Application Add the 2 radio buttons –Rename 1 st as numericRadioButton and set its text property to Numeric –Rename 2 nd as letterRadioButton and set its text property as letter Create a 10 X 2 array under the public class. Remember 10 X 2 means (0 To 9, and 0 to 1)

8 This is how the array will look like (x,0) Name(x,1) Average (0,y)Jack (0,0)60.3 (0,1) (1,y)John (1,0)90.6 (1,1) (2,y)Mike (2,0)91.3 (2,1) (3,y)Brad (3,0)78.9 (3,1) (4,y)Amy (4,0)90.9 (4,1) (5,y)Cami (5,0)89.9 (5,1) (6,y)Tami (6,0)98.9 (6,1) (7,y)Laura (7,0)76.6 (7,1) (8,y)Matt (8,0)80.9 (8,1) (9,y)Liz (9,0)99.9 (9,1)

9 Logic Declare array to store student name and average With each click of submit button add student name to first column and average to second column of the array. To do this you will have to calculate the average of the three scores entered. Calculate the running total of all the scores in the class for the class average –Running total = running total + elements of second column Now based on which radio button is selected, display the results in the list box Now create an event for radio button changed so that the values displayed can be changed as per the users requirements

10 Developing Application Declaring and Initializing the Array Dim grades As String(,) = New String(0 To 9, 0 To 1) {} Dim studentCount As Integer = 0 Write a function to calculate Grade Function LetterGrade(ByVal grade As Double) As String Select Case (grade) Case Is >= 90 Return "A" Case Is >= 80 Return "B" Case Is >= 70 Return "C" Case Is >= 60 Return "D" Case Else Return "F" End Select End Function

11 Developing Application Create a function testAverage(A,B,C) Function TestAverage (ByVal One As String, ByVal Two As String, ByVal Three As String) Return (Val(One)+ Val(Two) + Val (Three))/3 End Function

12 Adding event handlers Create button click event handler Dim counter As Integer = 0 Dim gradeTotal As Double = 0 ‘ add student name and test average

13 Adding event handlers grades(studentCount, 0) = nameTextBox.Text grades(studentCount, 1) = String.Format("{0:F}", TestAverage(test1TextBox.Text, test2TextBox.Text, test3TextBox.Text)) studentCount += 1 Do While counter < studentCount gradeTotal += Convert.ToDouble(grades(counter, 1)) counter += 1 Loop If letterRadioButton.Checked = True Then gradesListBox.Items.Add(grades(studentCount - 1, 0) & ControlChars.Tab & ControlChars.Tab & LetterGrade(Convert.ToDouble(grades(studentCount - 1, 1)))) averageLabel.Text = LetterGrade(gradeTotal / studentCount) Else gradesListBox.Items.Add(grades(studentCount - 1, 0) & ControlChars.Tab & ControlChars.Tab & grades(studentCount - 1, 1)) averageLabel.Text = String.Format("{0:f}", gradeTotal / studentCount) End If nameTextBox.Clear() test1TextBox.Clear() test2TextBox.Clear() test3TextBox.Clear() If studentCount = 10 Then submitButton.Enabled = False End If

14 Adding event handlers – checked changed radio button You have all the information that you need already stored in the arrays. You need to reformat it and display it. On numeric radio button selected –Clear the list box –Total the average and display in the class average text box –Display the name and the score in the listbox

15 Adding event handlers – checked changed radio button Dim counter As Integer = 0 Dim gradeTotal As Double = 0 gradesListBox.Items.Clear() Do While counter < studentCount gradesListBox.Items.Add(grades(counter, 0) & grades(counter, 1)) counter += 1 Loop If studentCount <> 0 Then averageLabel.Text = String.Format("{0:f}", gradeTotal / studentCount) End If

16 Repeat for the letter grade Dim counter As Integer = 0 Dim gradeTotal As Double = 0 gradesListBox.Items.Clear() Do While counter < studentCount gradesListBox.Items.Add(grades(counter, 0) & LetterGrade(Convert.ToDouble(grades(counter, 1)))) counter += 1 Loop If studentCount <> 0 Then averageLabel.Text = LetterGrade(Val(averageLabel.Text)) End If


Download ppt "Chapter 18 Introducing 2 dimensional arrays Using radio buttons Revisit student grades application Problem definition."

Similar presentations


Ads by Google