0

I have homework. I have to make a program which can input the length of an array with the value in it. After I click the "process" button, the program will make an output of index and value with the result sum and average from the array.

codeArray

I'm stuck and couldn't print the index and the value to the multiple textbox below process button.

I'm expecting the output will look like this:

captureCode2

Here's the code which I'd been successful write so far:

namespace ArrayProcess
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void process_Click(object sender, EventArgs e)
        {

            int sum = 0;
            string ind = "index";
            string message;
            int count = Convert.ToInt32(inputArray.Text);
            int[] varray = new int[count];
            for (int i=1; i <= count; i++)
            {
                varray[i] = Convert.ToInt32(Interaction.InputBox(message="enter the value of array number "+i));
                sum += varray[i];
            }
            boxSum.Text = Convert.ToString(sum);
        }
    }
}

Please, help me.

Dilshod K
  • 2,924
  • 1
  • 13
  • 46
Jericho
  • 71
  • 2
  • 8

2 Answers2

0

Here is the code for you (boxAvg is for average)

namespace ArrayProcess
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void process_Click(object sender, EventArgs e)
        {

            int sum = 0;
            string ind = "index";
            string message;
            int count = Convert.ToInt32(inputArray.Text);
            int[] varray = new int[count];
            for (int i=1; i <= count; i++)
            {
                varray[i-1] = Convert.ToInt32(Interaction.InputBox(message="enter the value of array number "+i));
                sum += varray[i-1];
            }
            //Refer your list box here to add newly added values to the list
            boxSum.Text = Convert.ToString(sum);
            boxAvg.Text = Convert.ToString(sum / count);  //calculate the average
        }
    }
}
Sandeep Bhutani
  • 589
  • 7
  • 23
0

Given that the array already has data :

private void Display()
{
    var columnHeader1 = "Index    Value\n";
    multilineTextBox1.Text = 
        columnHeader1 +
        string.Join("\n", vArray.Select((x,i)=> $"{i+1,5}    {x,5}"));


    boxSum.Text = vArray.Sum(); 
    boxAvg.Text = vArray.Average();


    var columnHeader2 = "Index    Value    Histogram\n";
    multilineTextBox2.Text = 
        columnHeader2 + 
        string.Join("\n", vArray.Select((x,i)=> $"{i+1,5}    {x,5}    {new String('*', x)}"));
}

Console demo

xdtTransform
  • 1,986
  • 14
  • 34