Hi I’m fairly new to c# programing so please bear with me. I’m currently working on a “simple” little program which allows the user to enter 5 values into the same text box and once this has been done I want to be able to display the maximum and minimum values and also the average.

Although I’m not completely sure how to do arrays I realise I need to use one for this to work. Im really getting confused how I can get the first inputted value to store into the array indexed 0 then once its stored here move to the next one and store that into index [1] and so on upto 5.

Any help will be much appreciated as ive hit a brick wall

Cheers

When the user is entering the 5 values are they entering one value, clicks save and then entering the next value ? if they are entering them in the same box at the same time (e.g. 12 34 56) then you may need to tokenise the input.

Also have a read though these links they may be of some use to you:

http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
http://www.switchonthecode.com/tutorials/csharp-basic-array-tutorial
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11142005060354AM/WorkingWithArrays.aspx

No they enter the first value click an add button then that gets stored in the array as index[0] then the textbox is cleared and they enter the second number and that is stored in index[1] once the user has entered the 5 values then you work with the 5 values that are stored in the array to find out which is the highest lowest then find the average.

Thanks for your response

If you are not limited to the use of arrays this can also be done using a List<int> NumberList = new List<int>();

This will allow you to use NumberList.Add(); to add the numbers from your text box. The list also allows for-each looping through its contents which I would prefer to use over for loops (personal preference) when calculating the outputs.

Im using the data type double because the values the user is entering is the shop takings for a week. this is the program spec

a company has five stores design and program an interface with a single text box to accept the weekly sales for each store

display the store with the maximum sales, the one with the minimum sales and display the sum of takings

and display the average weekly sales for the five stores

Ok.

One question before this goes any further.

Is this some sort of college assignment/homework? As it sounds like one (not an issue if it is just helps to know)

Yeah its for an assignment im not on here looking for answers im just looking for help to point me in the right direction! If you give me all the answers then i will never learn and im keen to learn and understand the code i use FULLY.

you need to define your array and the int value that will be used to count the number of array indexes. Arrays start at 0 (so if you want 5 indexes your array index numbers will go 0,1,2,3,4).

An array is defined as follows:

int index = 0; //This will be used to itterate through the array
datatype[] myArray = new datatype[number of indexes required];

button1_Clicked()
{
    if (textbox1.text != string.Empty && index < maxNumberOfVals)
    {
        myArray[index] = //convert text box value to double
        index++;
    }
}

You will need a reset button to reset all values.

Thanks for you help chris ill see if i can use what u have said to solve my problem. I fully understand that an array starts at 0 therefore if you assig 100 values it will be 0-99! Ive been reading books but just cant figure out how to store the numnber that has been entered into the array then move to the next number that needs to be entered !

This is where i got to before i got out of my depth!

      private void btnAdd_Click(object sender, EventArgs e)
    {
        bool flag;
        double userInput;
        double[] salesCollection = new double[5];

        for (int i = 0; i < 5; i++)
        {
            flag = double.TryParse(tbxUserInput.Text, out userInput);

            if (flag == false)
            {
                MessageBox.Show("Please enter a valid numeric value"); // show a message box with the following error message ""
                tbxUserInput.Clear(); // Clear the contents of this object (A Text Box)
                tbxUserInput.Focus(); // Set the focus onto this object (A Text Box)
                return; // Drop out of the program/Event and RETURN to the Main Frame
            }

            salesCollectionList.Add(userInput);
            tbxUserInput.Clear();
            tbxUserInput.Focus();
        }

I what is salesCollectionList ?

You don't want to use a for loop if values can only be added one at a time.

Take the for loop out and replace it with a simple if

Change salesCollectionList.Add() which is used to add to lists to:

salesCollection[indexNumber] = userInput;

so it should look more like:

double[] salesCollection = new double[5];
int index = 0;

private void btnAdd_Click(object sender, EventArgs e)
{
    bool flag;
    double userInput;

    if (index <= 5) //Only executes if maximum number of values has NOT been reached (max is array size)
    {
        flag = double.TryParse(tbxUserInput.Text, out userInput);
        if (flag == false)
        {
            MessageBox.Show("Please enter a valid numeric value"); 
            tbxUserInput.Clear(); // Clear the contents of this object (A Text Box)
            tbxUserInput.Focus(); // Set the focus onto this object (A Text Box)
            return; // Drop out of the program/Event and RETURN to the Main Frame
        }
        else
        {
            salesCollection[index] = userInput; //Stores userInput in array index specified by index
            tbxUserInput.Clear();
            tbxUserInput.Focus();
            index++; //Will increase every time to move to next index
        }
    }
    else
    {
        messageBox.Show("too many values entered");
    }
}

Defining you array inside the event handler means it will be reinitialised every time the button is clicked and you'll lose previously entered vals

Ah right, o sorry i left the salesCollectionList in from when i was trying to do it by using a list so i could try use the add method to "add the values into my array"

Thanks for your time chris ill try again now

Here to help, arrays are better if you want to access a specific piece of data without having to itterate through the entire data set or to store data in a table like format but lists are quick and best from pulling large amount of data to be displayed (i.e. customer records to data grid view).

Edit: A list may actually be best for your app, may make it more efficient.

i Have the code in now and using debug and stepping through my code i can actually see that its storing my 5 values in the array list but once i try and enter a sixth value the program crashes and says i have an un handled exception. I think after you help im going to go back and read the chapter on loops again! Like i say im really new to this but really aprechiate the help off you guys! and just want to stress im here to learn and not just to get answers put the code in and not understand what i have done :)

Thanks

that is because when you defined the array you defined it with a maximum of 5 indexes so you can only store 5 values. A list is probably the best solution to your problem.

If you're interested in how to stop that error from appearing and you want to add as many values as you like define you array like this:

double[] myArray = new double[0];
int index = 0;

and when you add a new value:

myArray = new double[index] = userInput;
index++;

this should let you keep adding to your array by increasing it's size every time the user wants to add a new val

Thanks a bunch Chris your helps has been great enough for one day tho ill be back to it tomorrow now ;)

Thanks again

;)

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.