I did a previous program takes scores from the user and computes the total of the scores, score count, and score average.Now I am supposed to add an array that will store the scores the user inputs and display them in a message box

more detailed instructions below:

  1. Delete the class-level score total variable and replace it with a class-level array that can hold up to 20
    scores.
  2. Modify the Click event handler for the Add button so it adds the score that’s entered by the user to the
    next element in the array. To do that, you can use the class-level score count variable to refer to the
    element.
  3. Move the Clear Scores button as shown above. Then, modify the Click event handler for this button
    so it removes any scores that have been added to the array. The easiest way to do that is to create a
    new array and assign it to the array variable (e.g., scores = new int[20];).
  4. Add a Display Scores button that sorts the scores in the array, displays the scores in a dialog box, and
    moves the focus to the Score text box. Be sure that only the elements that contain scores are
    displayed.

heres what I have so far, the main thing I need help on is just getting the array to work. Then I can figure out how to get the score total and average to work. Alos the messagebox should show the scores vertically using (/n) and not horizontal like I think I have it now

 public partial class frmScoreCalculator : Form
    {

        public frmScoreCalculator()
        {
            InitializeComponent();
        }

        int[] intScoreTotalsArray = new int[20];
        int intScoreCount = 0;

        /// <summary>
        /// this event calculates the score total, score count, and average 
        /// when the user clicks the Add buttont to add more scores to the form. Also gives focus to txtScore.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {

            int intScore = 0;           
            Int32.TryParse(txtScore.Text,out intScore);

            intScoreCount++;

            txtScoreTotal.ToString(); 
            txtScoreCount.Text = intScoreCount.ToString();


            txtScore.Focus();
            txtScore.SelectAll();
        }

        /// <summary>
        /// This event clears all the textboxes amd sets the stored values 
        /// back to zero when user clicks the clear button. Also gives focus to txtScore.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearScores_Click(object sender, EventArgs e)
        {
            this.txtScore.Clear();
            this.txtScoreCount.Clear();
            this.txtAverage.Clear();
            this.txtScoreTotal.Clear();
            this.intScoreCount = 0;
            this.intScoreTotalsArray = new int[20];
            txtScore.Focus();
        }

        /// <summary>
        /// This event closes the form when the user clicks the Exit button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {

            Array.Sort(intScoreTotalsArray);

            string intScoreTotalsArray = "";
            for (int i = 0; i < intScoreTotalsArray.Length; i++)
                numbersString += intScoreTotalsArray[i] + " ";
            MessageBox.Show(intScoreTotalsArray, "Sorted Scores");

Are you able to post all of the form code so I can copy and paste it and it will work? This includes auto generated code for the controls etc.

DaveAmour thanks for replying. Here is the entire page of code, hope this is what you are looking for?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScoreCalculator
{
    public partial class frmScoreCalculator : Form
    {

        public frmScoreCalculator()
        {
            InitializeComponent();
        }

        int[] intScoreTotalsArray = new int[20];
        int intScoreCount = 0;


        private void btnAdd_Click(object sender, EventArgs e)
        {

            int intScore = 0;           
            Int32.TryParse(txtScore.Text,out intScore);

            intScoreCount++;

            txtScoreTotal.ToString(); 
            txtScoreCount.Text = intScoreCount.ToString();


            txtScore.Focus();
            txtScore.SelectAll();
        }


        private void btnClearScores_Click(object sender, EventArgs e)
        {
            this.txtScore.Clear();
            this.txtScoreCount.Clear();
            this.txtAverage.Clear();
            this.txtScoreTotal.Clear();
            this.intScoreCount = 0;
            this.intScoreTotalsArray = new int[20];


            txtScore.Focus();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {

            Array.Sort(intScoreTotalsArray);

            string numbersString = "";
            for (int i = 0; i < intScoreTotalsArray.Length; i++)
                numbersString += intScoreTotalsArray[i] + " ";
            MessageBox.Show(numbersString, "Score Array");

It cuts off at line 69 - maybe there is a size limit on the forum?

oops, this should work now.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScoreCalculator
{
    public partial class frmScoreCalculator : Form
    {

        public frmScoreCalculator()
        {
            InitializeComponent();
        }

        int[] intScoreTotalsArray = new int[20];
        int intScoreCount = 0;


        private void btnAdd_Click(object sender, EventArgs e)
        {

            int intScore = 0;           
            Int32.TryParse(txtScore.Text,out intScore);

            intScoreCount++;

            txtScoreTotal.ToString(); 
            txtScoreCount.Text = intScoreCount.ToString();


            txtScore.Focus();
            txtScore.SelectAll();
        }


        private void btnClearScores_Click(object sender, EventArgs e)
        {
            this.txtScore.Clear();
            this.txtScoreCount.Clear();
            this.txtAverage.Clear();
            this.txtScoreTotal.Clear();
            this.intScoreCount = 0;
            this.intScoreTotalsArray = new int[20];


            txtScore.Focus();
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {

            Array.Sort(intScoreTotalsArray);

            string numbersString = "";
            for (int i = 0; i < intScoreTotalsArray.Length; i++)
                numbersString += intScoreTotalsArray[i] + " ";
            MessageBox.Show(numbersString, "Score Array");




        }
    }
}

I need the autogenerated code too. Your form is a partial class. Visual Studio will also have created a partial class into which it puts the C# code to generate the form controls which you have dragged and dropped onto your form. I need this too.

Thanks

Sorry I'm still kind new to VS but I think this is what you need.

namespace ScoreCalculator
{
    partial class frmScoreCalculator
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtScore = new System.Windows.Forms.TextBox();
            this.txtScoreTotal = new System.Windows.Forms.TextBox();
            this.txtScoreCount = new System.Windows.Forms.TextBox();
            this.txtAverage = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.btnClearScores = new System.Windows.Forms.Button();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnAdd = new System.Windows.Forms.Button();
            this.btnDisplayScores = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // txtScore
            // 
            this.txtScore.Location = new System.Drawing.Point(81, 35);
            this.txtScore.Name = "txtScore";
            this.txtScore.Size = new System.Drawing.Size(53, 20);
            this.txtScore.TabIndex = 0;
            // 
            // txtScoreTotal
            // 
            this.txtScoreTotal.Location = new System.Drawing.Point(81, 65);
            this.txtScoreTotal.Name = "txtScoreTotal";
            this.txtScoreTotal.ReadOnly = true;
            this.txtScoreTotal.Size = new System.Drawing.Size(53, 20);
            this.txtScoreTotal.TabIndex = 1;
            // 
            // txtScoreCount
            // 
            this.txtScoreCount.Location = new System.Drawing.Point(81, 95);
            this.txtScoreCount.Name = "txtScoreCount";
            this.txtScoreCount.ReadOnly = true;
            this.txtScoreCount.Size = new System.Drawing.Size(53, 20);
            this.txtScoreCount.TabIndex = 2;
            // 
            // txtAverage
            // 
            this.txtAverage.Location = new System.Drawing.Point(81, 125);
            this.txtAverage.Name = "txtAverage";
            this.txtAverage.ReadOnly = true;
            this.txtAverage.Size = new System.Drawing.Size(53, 20);
            this.txtAverage.TabIndex = 3;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(37, 38);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(38, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Score:";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 68);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 13);
            this.label2.TabIndex = 5;
            this.label2.Text = "Score Total:";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(10, 98);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(69, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Score Count:";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(28, 128);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(50, 13);
            this.label4.TabIndex = 7;
            this.label4.Text = "Average:";
            // 
            // btnClearScores
            // 
            this.btnClearScores.Location = new System.Drawing.Point(145, 170);
            this.btnClearScores.Name = "btnClearScores";
            this.btnClearScores.Size = new System.Drawing.Size(90, 23);
            this.btnClearScores.TabIndex = 8;
            this.btnClearScores.Text = "Clear Scores";
            this.btnClearScores.UseVisualStyleBackColor = true;
            this.btnClearScores.Click += new System.EventHandler(this.btnClearScores_Click);
            // 
            // btnExit
            // 
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(160, 211);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 9;
            this.btnExit.Text = "Exit";
            this.btnExit.UseVisualStyleBackColor = true;
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // btnAdd
            // 
            this.btnAdd.Location = new System.Drawing.Point(160, 32);
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(75, 23);
            this.btnAdd.TabIndex = 10;
            this.btnAdd.Text = "Add";
            this.btnAdd.UseVisualStyleBackColor = true;
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnDisplayScores
            // 
            this.btnDisplayScores.Location = new System.Drawing.Point(22, 170);
            this.btnDisplayScores.Name = "btnDisplayScores";
            this.btnDisplayScores.Size = new System.Drawing.Size(102, 23);
            this.btnDisplayScores.TabIndex = 11;
            this.btnDisplayScores.Text = "Display Scores";
            this.btnDisplayScores.UseVisualStyleBackColor = true;
            this.btnDisplayScores.Click += new System.EventHandler(this.btnDisplayScores_Click);
            // 
            // frmScoreCalculator
            // 
            this.AcceptButton = this.btnAdd;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnExit;
            this.ClientSize = new System.Drawing.Size(257, 246);
            this.Controls.Add(this.btnDisplayScores);
            this.Controls.Add(this.btnAdd);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnClearScores);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtAverage);
            this.Controls.Add(this.txtScoreCount);
            this.Controls.Add(this.txtScoreTotal);
            this.Controls.Add(this.txtScore);
            this.Name = "frmScoreCalculator";
            this.Text = "Score Calculator";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtScore;
        private System.Windows.Forms.TextBox txtScoreTotal;
        private System.Windows.Forms.TextBox txtScoreCount;
        private System.Windows.Forms.TextBox txtAverage;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button btnClearScores;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.Button btnAdd;
        private System.Windows.Forms.Button btnDisplayScores;
    }
}

Ok I have this running now.

One thing at a time.

Firstly to display vertically you just need:

numbersString += intScoreTotalsArray[i] + "\n";

Next to add numbers to the array just use:

intScoreTotalsArray[intScoreCount] = intScore;

Put this in your button click event handler like this:

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int intScore = 0;

            Int32.TryParse(txtScore.Text, out intScore);

            intScoreTotalsArray[intScoreCount] = intScore;

            intScoreCount++;

            txtScoreTotal.ToString();
            txtScoreCount.Text = intScoreCount.ToString();

            txtScore.Focus();
            txtScore.SelectAll();
        }

You also need to add in some error handling.

What if they enter non numeric data?

What happens if they try and add more than 20 items?

Let me know how you get on and if you need help with any other bits?

Thanks for the help it seems to be taking the numbers and adding them into the array now.

However, when it goes to displays all these zeros still.

For example, I enter in the scores 1,2,3 and it shows in the msgbox:

0
0
0
0
0
0
0
0
0
1
2
3

Any idea how to fix this?

Sure there are many ways to fix this.

You could do it like this for example:

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            var nonZeroData = intScoreTotalsArray.Where(v => v > 0).ToArray();

            Array.Sort(nonZeroData);

            string numbersString = "";

            for (int i = 0; i < nonZeroData.Length; i++)
            {
                numbersString += nonZeroData[i] + "\n";
            }

            MessageBox.Show(numbersString, "Score Array");
        }

The important thing is that you understand why it is misbehaving and how I have fixed it.

That makes sense. We were taught about the var property but for some reason were told to try amd stay away from using it, ironically. Not really sure of the reasoning behind that. Maybe there is another way you could show me?

I notice that before it was always showing 20 zeros in the msgbox since I have the array size declared for 20. The problem was that it is not recognizing the number of elements that the user actually inputs and to only display those.

My other issue you might be able to help me with is getting the score total and average to work. I had this working before but what got me confused was in the directions it says to delete the class variable for score total. It doesn't make sense to me why you would need to do that.

You don't have to use var, you can use this instead:

int[] nonZeroData = intScoreTotalsArray.Where(v => v > 0).ToArray();

"I notice that before it was always showing 20 zeros in the msgbox since I have the array size declared for 20. The problem was that it is not recognizing the number of elements that the user actually inputs and to only display those."

I've fixed this though right or am I missing something?

"delete the class variable for score total." I never saw this variable I don't think so can't comment on that.

To do the statistics you could add the two lines below. Put them in your button click event handler.

txtAverage.Text = intScoreTotalsArray.Where(v => v > 0).Average(v => v).ToString();
txtScoreTotal.Text = intScoreTotalsArray.Where(v => v > 0).Sum(v => v).ToString();

It seems to working now. Thanks for the help!

Youre welcome.

We were taught about the var property but for some reason were told to try amd stay away from using it, ironically. Not really sure of the reasoning behind that.

var is something of a controversy. Some folks think it should never be used when optional because it hides the type of the variable. Some folks think it should always be used because it eliminates a lot of complexity and duplication of type names as well as simplifies maintenance when types change.

The middle of the road is typically to use it only when required or the type is specified on the right side of the initialization, such as

var foo = new Dictionary<int, string>();

instead of

Dictionary<int, string> foo = new Dictionary<int, string>();

whereas more opaque types would continue to specify the type as in

Dictionary<int, string> foo = LoadFoo();

rather than

var foo = LoadFoo();

My advice is to follow whatever coding standard for your organization or course, and do whatever you're most comfortable with in personal code.

On a side note, some people still believe that var is a dynamic type rather than a static type, and that's untrue. All var does is interpret the type of the initialization statement and match it when creating a variable. That type is static, so var is little more than a notational convenience in these cases.

There are situations where var is required, and that's when you're creating an anonymous type:

var foo = new { FirstName = "Jane", LastName = "Doe" };

There's no type name to use in the first place, so you must infer it from context.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.