C# List Score Calculator: I think the Logic for my loop to add the value on the
ID: 3760382 • Letter: C
Question
C# List Score Calculator:
I think the Logic for my loop to add the value on the list and total is wrong.
Task:
Create a program that functions as a score calculator. It should save the scores the user enters into a list and display the sorted scores in a listbox. The total, count, and average of the scores should be automatically updated each time a new score is added.
Follow these steps for Lab 8:
Create a new C# project and save it as YourLastNameLab8.
The textbox and output labels should have text that is right-aligned.
Declare a class variable for a List<> that will store the scores entered by the user.
Write code in the Click event handler for the Add button:
declare the necessary variables (a total variable that is initialized to 0 may be helpful)
clear the existing items from the listbox
add the score that is entered by the user to the list
sort the list
use a loop to add each of the scores in the list to a variable that holds the total, and then add the current item in the list to the listbox
calculate the average by dividing the total by the number of elements in the list
display the total, count, and average of the scores in the list
send the focus to the Score textbox (and select the text if you would like)
The Clear button should:
clear the textbox
clear the output labels
clear the items from the listbox
clear the elements from the list
send the focus to the textbox
Test the application to be sure it works correctly.
Close your project and exit Visual Studio.
Submit the zipped program folder to the appropriate Desire2Learn Dropbox folder.
My code so far:
namespace ShresthaLab8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<double> ScoresList = new List<double>();
private void btnAdd_Click(object sender, EventArgs e)
{
double averageScore = 0;
double scoretotal = 0;
double score;
score = double.Parse(txtScore.Text);
ScoresList.Add(score);
listBoxScores.Items.Add(score);
for (double index = 0; index < score; index++)
{
scoretotal += score;
}
ScoresList.Count();
ScoresList.Sort();
averageScore = (double)scoretotal / ScoresList.Count();
lblScoreTotal.Text = scoretotal.ToString();
lblAverage.Text = averageScore.ToString();
lblScoreCount.Text = ScoresList.Count.ToString();
txtScore.Focus();
txtScore.SelectionStart = 0;
txtScore.SelectionLength = txtScore.Text.Length;
}
}
}
Explanation / Answer
Hi,
Only two correction need to done here in order to make your application run successfully
1) You have declared and intialized the variables (averagescore,scoretoatal,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.
Solution to this problem is to declare and intialize them as a class variable.
2) The for loop you are using is having a condition index < score. this should be index<scores.count
and the next line should be scoretotal += score[index].
Below is the Complete program for your reference
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 Form1 : Form
{
List<double> scores = new List<double>();
double averageScore = 0;
double scoretotal = 0;
double score;
public Form1()
{
InitializeComponent();
}
private void btnClear_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 = "";
}
private 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();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.