Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this exercise, you’ll create a form that accepts one or more scores from the

ID: 3890661 • Letter: I

Question

In this exercise, you’ll create a form that accepts one or more scores from the user. Each time a score is added, the score total, score count, and average score are calculated and displayed.

USING C#

Start a new project named ScoreCalculator.

Add labels, text boxes, and buttons to the default form and set the properties of the form and its controls so they appear as shown above. When the user presses the Enter key, the Click event of the Add button should fire. When the user presses the Esc key, the Click event of the Exit button should fire.

Declare two class variables to store the score total and the score count.

Create an event handler for the Add button Click event. This event handler should get the score the user enters, calculate and display the score total, score count, and average score, and reset the focus to the Score text box. You can assume that the user will enter valid integer values and that they will be positive.

Create an event handler for the Click event of the Clear Scores button. This event handler should set the two class variables to zero, clear the text boxes on the form, and move the focus to the Score text box.

Create an event handler for the Click event of the Exit button that closes the form.

Test the application to be sure it works correctly.

Now we are going to modify what has been done so far to store the entered data in an array so that it can be sorted. We will modify the form slightly and following the sort produce a dialog box containing the sorted data. (see below)

Go ahead and declare a class variable myData for an array that can hold up to 20 scores.

Modify the Click event handler for the Add button so it inserts each score that is entered by the user into the next element in the array. To do that, you can use the score count variable to refer to the next element.

If you have not done so already, add a Display Scores button that with a Click event that sorts the scores in the array (using a separate method), displays the scores in a dialog box (such as the one shown below), and moves the focus to the Score text box. Be sure that only the array elements that contain scores are displayed.

Test the application to be sure it works correctly.

Finally: we are going to modify what has been done so far to store the entered data in list. We do this in order to emphasize some of the differences between a list and an array.

Replace the declaration for the array variable with a declaration for a List<int> object, and delete the class variable for the score count. This count is not needed anymore, why not?

Modify the Click event handler for the Add button so it adds to the list the score that’s entered by the user. In addition, delete the statement that increments the score count variable you just deleted.

Modify the Click event handler for the Display Scores button so it sorts the scores in the list and then displays them in a dialog box. Modify the dialog box so that it shows the count of the number of elements in the list. How can you determine the count of these elements given that we are no longer tracking this count.

Test the application to be sure it works correctly.

Explanation / Answer

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace ShresthaLab8

{

public partial class From1 :Form;

{

List<double>scores = new List<double>();

double averageScore = 0;

double scoretotal = 0;

double score;

public Form1()

{

initializeComponent();

}

private voidbtnClear_Click(object sender,EventArgs e)

{

listScore.items.Clear();

txtScore.Text="";

txtScore.Focus();

Scores.Clear();

averageScore = 0;

Scoretotal = 0;

Score = 0;

lblAverageScore.Text = "";

lblScoreCount.Text="";

lblTotalScore.text = "";

}

public void btnAdd_Click(object sender,EventArgs e)

{

score = double.Parse(txtScore.Text);

scores.Add(score);

listScore.items.Add(score);

for(int i =0;i<scores.Count;i++)

{

scoretotal +=scores[i];

}

averageScore = scoretotal /scores.Count;

lblAverageScore.text=averageScore.ToString();

lblScoreCount.Text = Scores.Count.ToString();

lblTotalScore.Text = Scoretotal.ToString();

txtScore.Text="";

txtScore.Focus();

}

}

}

Only two correction need to done here to make your application run

1: you have decleared and intialized the variables averagescore,scoretotal,scorecount)inside btn click method because of which every time you are clicking on button they are becoming zero hence you are not able to get correct average score,total score and score count

2:the for loop you are using having a condition index<score this should be index<scores.count

and the next line should be scoretotal+=score[index]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote