12 Replies - 910 Views - Last Post: 10 December 2011 - 01:55 AM Rate Topic: -----

#1 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Frequency Array How To

Posted 06 December 2011 - 11:15 PM

Want to create a frequency array where a list box display the frequency of salaries. Only problems is I dont know how to go about setting in between number. What I would like to do is have a List box display $200-299, $300-399, $400-499 etc... and the the frequency of salaries between the range.Thanks in advance.

Side note the project runs and work almost as intended. If you would like to see the whole code let me know.

-Nogard

       
        private void Form1_Load(object sender, EventArgs e)
        {
            RestartButton_Click_1(sender, e);
        }
        private void RestartButton_Click_1(object sender, EventArgs e)
        {
            Array.Clear(salesArray, 0, MAX);
            SalaryBox.Items.Clear();
            SalesTextBox.Focus();
            SalesTextBox.Clear();
            SalaryLabel.Text = "";
            for (int i = 200; i > 200 || i < 299; i++)
                FrequencyBox.Items.Add(i + "\t\t" + salaryFrequency[i]);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Frequency Array How To

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Frequency Array How To

Posted 07 December 2011 - 09:00 AM

So what exactly is your question? What do you want the volunteers here to *help* you with?

If I'm reading this right you're just wanting to count the number of times a salary is between 200-299, 300-399 and so on.

So what are you asking of the volunteers here?


I don't want code, just some ideas/help on the logic because I'm lost or don't want to have to think it through myself.
Was This Post Helpful? 0
  • +
  • -

#3 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 07 December 2011 - 04:26 PM

Apologize for being vague. I would like to know how to modify my existing code so it will display range of numbers in the frequency list box. I want the list box to display 200-299, 300-399, all the way to 1000. That list box then will also display the frequency of salaries within said range.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Frequency Array How To

Posted 07 December 2011 - 04:36 PM

Quote

I would like to know how to modify my existing code so it will display range of numbers in the frequency list box.


You've demonstrated that you know how to add items to a listbox. Just keep adding the items you want.

13 FrequencyBox.Items.Add(i + "\t\t" + salaryFrequency[i]);


Quote

That list box then will also display the frequency of salaries within said range.


Loop through all your data. If the item is >199 and <300 then increment the counter for that group. If it is >300 and <400 then increment the counter for that group.

There really isn't anything elegant or special or magical in what you want to do. Its just looping and counting.

Maybe you're looking at this as if it is tougher than it really is. I'm guilty of that some times. Often the direct straightforward easy stuff is overlooked trying to find something fancy and elegant.
Was This Post Helpful? 1
  • +
  • -

#5 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 07 December 2011 - 04:50 PM

Screen shot for further clarity.

Attached Image

Honestly I though there was some fancy line I could write that would do it. But I guess I will mess with the loop and see if I can get it to work. On a side note would you know how I can add the dollar sign to the numeric range? Thanks for the help.
Was This Post Helpful? 1
  • +
  • -

#6 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 08 December 2011 - 07:18 PM

Need help modifiying the code to match the screen shot above. Currently it only displays 200-299 for 8 lines in the list box.
for (double empSalary = 200; empSalary < 300; i++)
                FrequencyBox.Items.Add("200 - 299" + "\t\t\t" + salaryFrequency[i]);


Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Frequency Array How To

Posted 08 December 2011 - 07:30 PM

No. This is your school homework, not ours. We aren't being graded on this.

Frankly, if you can't get this one little bit given that all the logic has been laid out in front of you then your instructor needs to see that. It isn't that tough. Quit looking for single lines of code that is going to do everything for you. You just have to write it one line at a time.
Write one like like you have on line 2 for 200-300 and element[0]
Write one like for the next range for element[1]
Don't do anything fancy. Just brute force your way through the list. At least for now so you get something done and actually make an effort to do it. Because right now we are only seeing the palest of efforts to write 1 or 2 lines here and there in the hopes someone here takes pity on you and writes your project for you - if only out of frustration.

The goal is NOT to turn in a cool project for a grade.
The goal IS to actually learn to do this, and if that's not happening your instructor needs to know so you can get the tutoring you need.

This education is costing you thousands of dollars. Don't bluff your way through it, learn nothing then take years repaying the student loans for nothing.

If it were a car that cost you this much you would be wringing every last feature out of the sales person. Do the same to your teacher. make them earn their salary and TEACH you.
Was This Post Helpful? 0
  • +
  • -

#8 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 08 December 2011 - 09:20 PM

Jumping to conclusion isn't necessary. I rarely ask for help and when I do its because of frustration. I don't know what gave you the idea that I wanted the whole project done for me clearly I was asking for help on 2 lines of code out of the 200 I already wrote. I appreciate the help I get on the forum but if your not willing to help then don't. Frankly I would rather see no replies then negative remark. I have certain specifications to follow on the work if I could just brute force my way through I already would have.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Frequency Array How To

Posted 09 December 2011 - 08:52 AM

Fresh day... You're refreshed. I'm refreshed. Let's give it a second go.

Here's the total code that we see from you:

private void Form1_Load(object sender, EventArgs e)
{
    RestartButton_Click_1(sender, e);
}
private void RestartButton_Click_1(object sender, EventArgs e)
{
    Array.Clear(salesArray, 0, MAX);
    SalaryBox.Items.Clear();
    SalesTextBox.Focus();
    SalesTextBox.Clear();
    SalaryLabel.Text = "";
    for (double empSalary = 200; empSalary < 300; i++)
                FrequencyBox.Items.Add("200 - 299" + "\t\t\t" + salaryFrequency[i]);
}


From our perspective there isn't a lot of trial and error here. Suggestion: Don't delete trials that don't work. Just comment them out. That way you don't re-try on Friday what failed on Monday, but you forgot about. It also lets us see all the things you've tried. Often a failed attempt is SOOOO close to working that it just needs a little nudge.

After all the guidance given the only piece of code change anyone here sees was from this

 for (int i = 200; i > 200 || i < 299; i++)
FrequencyBox.Items.Add(i + "\t\t" + salaryFrequency[i]);

to this
for (double empSalary = 200; empSalary < 300; i++)
                FrequencyBox.Items.Add("200 - 299" + "\t\t\t" + salaryFrequency[i]);


So maybe you can see why it doesn't look like you've really tried much, from our perspective.

Let's look at the logic. (whiteboards are good for this stuff by the way)

<loop from 100 to 10,000 salary range>
  • create two temp range values from index to index+99
  • loop through your data to count all the salaries between you min and max
  • Add a line to your combo with your min-max and count
</loop>

As you can see this isn't a tough thing. Perhaps you're just making more of it in your head than it has to be. That's certainly tripped all of us up now and again.
Was This Post Helpful? 0
  • +
  • -

#10 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 09 December 2011 - 04:39 PM

Perhaps it will help my cause if I post my entire code. As you can see if have tried a few methods but I feel like I'm missing something important because everything I tried either gave me an error or didn't work as intended. The frustration at the moment is getting to me as I have been staring at this code for the past 3 days none stop. Except from sleeping. After a messing around with the code it seems as though I have done more harm than good. So I do apologize if it seems a bit messy. The attached image is as far as I got with the code still compiling and running. Thanks for still taking the time to help I appreciate it.


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 Assingment_4
{
    public partial class Form1 : Form
    {
        const int MAX = 5;
        private int[] salesArray = new int[MAX];
        private int idxSales = -1;
        //private int[] salaryFrequency200 = new int[1];
        //private int[] salaryFrequency300 = new int[1];
        //private int[] salaryFrequency400 = new int[1];
        //private int[] salaryFrequency500 = new int[1];
        //private int[] salaryFrequency600 = new int[1];
        //private int[] salaryFrequency700 = new int[1];
        //private int[] salaryFrequency800 = new int[1];
        //private int[] salaryFrequency900 = new int[1];
        //private int[] salaryFrequency1000 = new int[1];
        private int[] salaryFrequency = new int[1];


        List<string> Salaries = new List<string>();


        public Form1()
        {
            InitializeComponent();

            Salaries.Add("$200 - 299");
            Salaries.Add("$200 - 399");
            Salaries.Add("$400 - 499");
            Salaries.Add("$500 - 599");
            Salaries.Add("$600 - 699");
            Salaries.Add("$700 - 799");
            Salaries.Add("$800 - 899");
            Salaries.Add("$900 - 999");
            Salaries.Add("$1000 +");

            //FrequencyBox.DataSource = Salaries;

        }
        private void SalaryButton_Click(object sender, EventArgs e)
        {
            int number = 1;
            int sales;
            double empSalary;
            bool isValidSales;
            SalesTextBox.Focus();
            SalesTextBox.SelectAll();

            //salaryFrequency[number]++;

            if (++idxSales < MAX)
            {
                isValidSales = SetSales(out sales, out empSalary);

                if (isValidSales)
                {
                    StoreSales(sales, idxSales);
                    DisplaySales(sales, empSalary);
                }
                else
                    idxSales--;
            }
            else
            {
                idxSales--;
                MessageBox.Show("No more salaries can be accommodated -- "
                                     + "max is " + MAX);
            }
            //salaryFrequency200[number]++;
            //salaryFrequency300[number]++;
            ShowUpdatedFrequency(number);
        }
        private bool SetSales(out int sales, out double empSalary)
        {
            bool isValid = true;
            sales = -1;
            empSalary = 0;

            try
            {
                sales = int.Parse(SalesTextBox.Text);
                empSalary = (sales * 0.09) + 200;
                SalaryLabel.Text = empSalary.ToString("c");
                if (sales < 0)
                    throw new Exception("Sales cannot be negative!");
            }
            catch (FormatException)
            {
                isValid = false;
                MessageBox.Show("Sales must be an integer", "Invalid Input",
                                   MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                isValid = false;
                MessageBox.Show(ex.Message, "Invalid input", MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation);
            }
            return isValid;
        }
        private void ShowUpdatedFrequency(int number)
        {
            FrequencyBox.Items[number] = number + "\t\t\t" + salaryFrequency[number];//If I make to many changes I always get an argument out range unhandled exception error here.
            //FrequencyBox.Items[number] = number + "\t\t\t" + salaryFrequency200[number];
            //FrequencyBox.Items[number] = number + "\t\t\t" + salaryFrequency300[number];
            //for (int i = 2; i <= 12; i++)
            //FrequencyBox.Items.Add(i + "\t\t" + salaryFrequency[i]);
        }
        private void StoreSales(int s, int i)
        {
            salesArray[i] = s;
        }

        private void DisplaySales(int sales, double empSalary)
        {
            SalaryBox.Items.Add(empSalary);
            //if (empSalary > 200 && empSalary < 300)
            //{
            //    int i = 0;
            //    i ++;
            //    FrequencyBox.Items.Add("$200 - 299" + "\t\t\t" + salaryFrequency[i]);
            //}
            //else if (empSalary > 200 && empSalary < 300)
            //{
            //    int i = 0;
            //    i++;
            //    FrequencyBox.Items.Add("$200 - 299" + "\t\t\t" + salaryFrequency[i]);
            //}

        }

        private void RestartButton_Click_1(object sender, EventArgs e)
        {
            Array.Clear(salesArray, 0, MAX);
            SalaryBox.Items.Clear();
            SalesTextBox.Focus();
            SalesTextBox.Clear();
            SalaryLabel.Text = "";
            int i = 0;
            //double empSalary = SalaryLabel.Text;
            //FrequencyBox.DataSource = Enumerable.Range(200, 200).ToArray();
            //if(empSalary > 200 && empSalary < 300)
            //{
            //    int i = 0;
            //    i++;
            //    FrequencyBox.Items.Add("$200 - 299" + "\t\t\t" + salaryFrequency[i]);
            //}

            for (double empSalary = 200; empSalary < 300; i++)
                FrequencyBox.Items.Add("200 - 299" + "\t\t\t" + salaryFrequency[i]);

            //for (double empSalary = 300; empSalary < 400; i++)
            //    FrequencyBox.Items.Add(i + "\t\t\t" + salaryFrequency[i]);

            //for (int empSalary = 200; empSalary < 300; i++)
            //{
            //    FrequencyBox.Items.Add("200 - 299" + "\t\t\t" + salaryFrequency200[i]);
            //}

            //for (int empSalary = 200; empSalary < 300; i++)
            //{
            //    FrequencyBox.Items.Add("300 - 399" + "\t\t\t" + salaryFrequency300[i]);
            //}

            //for (double empSalary = 300; empSalary < 400; i++)
            //    FrequencyBox.Items.Add(i + "\t\t\t" + salaryFrequency[i]);


        }

        private void SortButton_Click(object sender, EventArgs e)
        {
            Array.Clear(salaryFrequency, 0, salaryFrequency.Length);
            Array.Sort(salesArray, 0, idxSales + 1);
            SalaryBox.Items.Clear();
            for (int i = 0; i <= idxSales; i++)
                SalaryBox.Items.Add(salesArray[i]);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RestartButton_Click_1(sender, e);
        }
    }
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5439
  • View blog
  • Posts: 11,669
  • Joined: 02-June 10

Re: Frequency Array How To

Posted 09 December 2011 - 05:28 PM

View PosttlhIn`toq, on 09 December 2011 - 09:52 AM, said:

<loop from 100 to 10,000 salary range>
  • create two temp range values from index to index+99
  • loop through your data to count all the salaries between you min and max
  • Add a line to your combo with your min-max and count
</loop>

As you can see this isn't a tough thing. Perhaps you're just making more of it in your head than it has to be. That's certainly tripped all of us up now and again.


The logic has been laid out for you. Please make an effort to code the suggested logic.
Once you have made an effort to impliment the suggestion post the new code and I/we will help you clean up any problems. But until you actually try to take the help that is given I'm afraid I don't know what else I/we can do for you. I'm not going to write the code for you and seem to resistant to writting it yourself. So tell me... Other than lay out the logic of two nested loops what else would you like me to do for you?
Was This Post Helpful? 0
  • +
  • -

#12 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 09 December 2011 - 06:07 PM

Is this what you meant because I already tried it. Gives me a unhandled exception error at line 112. I hope you font think I am playing the ignorant roll out of laziness in order to avoid doing my own work. Because that cant be further from the truth. This is my first C# class and the only other programming class I took was C++. I have just been learning by example therefore its rather difficult for me to wrap my head around

Quote

<loop from 100 to 10,000 salary range>
create two temp range values from index to index+99
loop through your data to count all the salaries between you min and max
Add a line to your combo with your min-max and count
</loop>
perhaps a sample code will go a lot further with me.

            //for (double empSalary = 300; empSalary < 400; i++)
            //    FrequencyBox.Items.Add("300 - 399" + "\t\t\t" + salaryFrequency300[i]);

            //for (int empSalary = 400; empSalary < 500; i++)
            //{
            //    FrequencyBox.Items.Add("400 - 499" + "\t\t\t" + salaryFrequency400[i]);
            //}

            //for (int empSalary = 500; empSalary < 600; i++)
            //{
            //    FrequencyBox.Items.Add("500 - 599" + "\t\t\t" + salaryFrequency500[i]);
            //}


Was This Post Helpful? 0
  • +
  • -

#13 Nogard Htrae  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 04-November 11

Re: Frequency Array How To

Posted 10 December 2011 - 01:55 AM

I got it to work more or less. However I get a few errors. Code needs a bit of cleaning up. I get an argument out range error on line 63 when I input 10000 or more. Rather tired at the moment so I might be overlooking something easy. Let me know if there anything that stands out in particular that needs changing.

-Nogard

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 Assingment_4
{
    public partial class Form1 : Form
    {
        int MAX = 5;
        List<string> Salaries = new List<string>() { "$200 - 299", "$300 - 399", "$400 - 499", "$500 - 599", "$600 - 699", "$700 - 799", "$800 - 899", "$900 - 999", "$1000 +   " };
        List<int> Frequencies = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        List<int> lst_Sales = new List<int>();
        List<double> lst_Salaries = new List<double>();

        public Form1()
        {
            InitializeComponent();
            //FrequencyBox.DataSource = Salaries;

            //FrequencyBox.DataSource = Salaries + "" + Frequencies;
            //if (RestartButton.Click)
            //{

            //}

        }

        private void SalaryButton_Click(object sender, EventArgs e)
        {
            int sales;
            double empSalary;
            bool isValidSales;

            if (lst_Sales.Count < MAX)
            {
                isValidSales = SetSales(out sales, out empSalary);
                if (isValidSales)
                {
                    StoreSales(sales);
                    DisplaySales(empSalary);
                }
            }
            else
            {
                MessageBox.Show("No more salaries can be accommodated -- max is " + MAX);
            }
            ShowUpdatedFrequency();
        }
        private void ShowUpdatedFrequency()
        {
            Frequencies = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            for (int x = 0; x < lst_Salaries.Count; x++)
            {
                //double z = lst_Salaries[x];
                //double k = Math.Floor(z / 100.0);
                //int y = Convert.ToInt16(k) - 2;
                //Frequencies[y] += 1;
                Frequencies[Convert.ToInt16(Math.Floor(lst_Salaries[x] / 100.0)) - 2] += 1;
                //FrequencyBox.DataSource = Salaries;
            }
            FrequencyBox.Items.Clear();
            for (int x = 0; x < Salaries.Count; x++)
            {
                FrequencyBox.Items.Add(Salaries[x] + "\t\t" + Frequencies[x].ToString());
            }
        }
        private void StoreSales(int sales)
        {
            lst_Sales.Add(sales);
        }
        private void DisplaySales(double empSalary)
        {
            lst_Salaries.Add(empSalary);
            SalaryBox.Items.Add(empSalary);
        }
        private bool SetSales(out int sales, out double empSalary)
        {
            bool isValid = true;
            sales = -1;
            empSalary = 0;

            try
            {
                sales = int.Parse(SalesTextBox.Text);
                empSalary = (sales * 0.09) + 200;
                SalaryLabel.Text = empSalary.ToString("c");
                if (sales < 0)
                    MessageBox.Show("Sales cannot be negative!");
            }
            catch (FormatException ex)
            {
                isValid = false;
                MessageBox.Show("Sales must be an integer", "Invalid Input",
                                   MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                isValid = false;
                MessageBox.Show(ex.Message, "Invalid input", MessageBoxButtons.OK,
                                   MessageBoxIcon.Exclamation);
            }
            return isValid;
        }
        private void RestartButton_Click_1(object sender, EventArgs e)
        {
            SalaryBox.Items.Clear();
            FrequencyBox.Items.Clear();
            SalesTextBox.Focus();
            SalesTextBox.Clear();
            SalaryLabel.Text = "";
            MAX = 0;
        }
        private void SortButton_Click(object sender, EventArgs e)
        {
            //Array.Clear(salaryFrequency, 0, salaryFrequency.Length);
            //Array.Sort(salesArray, 0, idxSales + 1);
            //SalaryBox.Items.Clear();
            //for (int i = 0; i <= idxSales; i++)
            //    SalaryBox.Items.Add(salesArray[i]);


            //int y = Convert.ToInt16(Math.Floor(lst_Salaries[x] / 100.0)) - 2;
            //Frequencies[y] += 1;
            //Frequencies[Convert.ToInt16(Math.Floor(lst_Salaries[x] / 100.0)) - 2] += 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1