Hey everyone,
I am in an introduction to programming course, and I am tryin to solve the following problem using C#. I know that I am to use arrays, however I am a little lost and was hoping that somebody could give me some assistance.


Here is the code that I have so far:

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 Soenenyoung_lab5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int scoreTotal = 0;
        int scoreCount = 0;
        int averageScore = 0;

        int[] scores = new int[6];

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //take the values off the form and store them in our scores array
            scores[0] = Convert.ToInt32(txtScore.Text);
            scores[1] = Convert.ToInt32(txtScore.Text);
            scores[2] = Convert.ToInt32(txtScore.Text);
            scores[3] = Convert.ToInt32(txtScore.Text);
            scores[4] = Convert.ToInt32(txtScore.Text);
            scores[5] = Convert.ToInt32(txtScore.Text);
  
        }
    }
}

Recommended Answers

All 7 Replies

Every time you click the add button, you got to
- fill in the array(I guess you have to use arrays) at index scorecount(check for out of range!)
- increment your scorecount
- update your score total
- calculate the average
- fill in the appropriate textboxes
Success!

now when you click on Add button just add for loop for that scores like this

int total=0;
        for(int i=0;i<scores.Length;i++)
        {
            total = total + scores[i];
        }
        MessageBox.Show(total.ToString());

I did your homework, and you owe me a beer:

public partial class Form1 : Form
    {
        int[] scores;
        public Form1()
        {
            InitializeComponent();
            TextBox[] tbs = new TextBox[] { textBox2, textBox3, textBox4 };
            foreach (TextBox tb in tbs)
                tb.Enabled = false;
            textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            textBox1.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (Char)Keys.Back && !Char.IsNumber(e.KeyChar))
                e.Handled = true;
            if (e.KeyChar == (Char)Keys.Enter)
                AddingNumbers();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddingNumbers();      
        }

        private void AddingNumbers()
        {
            if (textBox1.Text != "")
            {
                Array.Resize(ref scores, ((scores == null ? 1 : scores.Length + 1)));
                scores[scores.Length - 1] = int.Parse(textBox1.Text);

                //math operations for other textBoxes:
                int _total = 0;
                int _count = 0;
                float _avg = 0;
                for (int i = 0; i < scores.Length; i++)
                {
                    _total += scores[i];
                    _count++;
                }
                _avg = _total / scores.Length;

                //show all the results:
                textBox2.Text = _total.ToString();
                textBox3.Text = _count.ToString();
                textBox4.Text = _avg.ToString();
            }
        }

        private void buttonDiusplay_Click(object sender, EventArgs e)
        {
            Array.Sort(scores);
            StringBuilder sb = new StringBuilder();
            foreach (int score in scores)
                sb.AppendLine(score.ToString());
            MessageBox.Show(sb.ToString(), "Sorted scores");
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            scores = null;
            TextBox[] tbs = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
            foreach (TextBox tb in tbs)
                tb.Text = String.Empty;
            textBox1.Focus();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }

Thanks dude...you're the man!!

the only thing I have left to do on it is make sure that the user enters a score between 1 and 100, and clear the Score text box when they hit the Add button...how would i do that?

If score is greater than or equal to one and smaller than or equal to 100
Translate this sentence to an if-statement and you're done.

To clear a textbox you could set its text property to empty: MyTxbx.Text = string.Empty;

none of my buttons function - why is this? here is my code -

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 Score_Calculator
{
    public partial class Form1 : Form
    {
        List<int> scores = new List<int>();
        private int Total = 0;
        private int Average = 0;

        public Form1 ()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            int ScoreInput; 
            if (string.IsNullOrEmpty (txtScore.Text) || !int.TryParse(txtScore.Text, out ScoreInput))
            {
                txtScore.Focus();
                MessageBox.Show("Please enter a valid score", "Input Error");
                txtScore.Focus(); 
                return;
            }
            else if ((ScoreInput < 0) || (ScoreInput > 100))
            {
                txtScore.Focus();
                MessageBox.Show("The score must be between 0 and 100", "Input Error");
                txtScore.Focus(); 
                return;
            }

            scores.Add(20); 
            int sum = 0;
            for (int i = 0; i < scores.Count; i++)
            {
                int score = scores[i];
                sum += score;
            }

            Total = ScoreInput + Total;
            Average = Total / scores.Count;
            txtScoreTotal.Text = Total.ToString();
            txtAverage.Text = Average.ToString(string.Empty);
            txtScoreCount.Text = scores.Count.ToString(string.Empty);
            txtScore.Text = string.Empty;
            txtScore.Focus();
        }

        private void btnDisplayScores_Click(object sender, EventArgs e)
        {
            scores.Sort();
            StringBuilder sb = new StringBuilder();
            for (int i1 = 0; i1 < scores.Count; i1++)
                sb.AppendLine(i1.ToString());
            MessageBox.Show(sb.ToString().Trim(), "Sorted Scores");
        }

        private void btnClearScores_Click(object sender, EventArgs e)
        {
            scores.Clear();
            txtScoreTotal.Text = "0";
            txtScoreCount.Text = "0";
            txtAverage.Text = "0";
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
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.