Thread: array of counters

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    array of counters

    another question from my book, i would like to know if you think my answer is correct please

    question: use a single subscripted array to solve the following problem: A company pays its salespeople on a commision basis. The salespeople receive £200/week, plus 9% of thier gross sales for that week. For example, a salesperson who grosses £5000 in sales in a week receives £200 plus 9% of £5000, or a total of £650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson's salary is truncated to an integer amount).
    a) "£200-£299"
    b) "£300-£399"
    c) "£400-£499"
    d) "£500-£599"
    e) "£600-£699"
    f)"£700-£799"
    g)"£800-£899"
    h)"£900-£999"
    i)"£1000 and over"

    here is what i came up with
    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Ex7_4
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.TextBox textBox1;
    		private System.ComponentModel.Container components = null;
    		Random ranNum = new Random();
    		string[] salary = {"£200-£299", "£300-£399", "£400-£499", "£500-£599",
    						   "£600-£699", "£700-£799", "£800-£899", "£900-£999",
    							"£1000 and over"};
    		int[] frequency = new int[ 10 ];
    
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (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.button1 = new System.Windows.Forms.Button();
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.SuspendLayout();
    			// 
    			// button1
    			// 
    			this.button1.Location = new System.Drawing.Point(104, 24);
    			this.button1.Name = "button1";
    			this.button1.Size = new System.Drawing.Size(104, 23);
    			this.button1.TabIndex = 0;
    			this.button1.Text = "Compute Salary";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(16, 88);
    			this.textBox1.Multiline = true;
    			this.textBox1.Name = "textBox1";
    			this.textBox1.Size = new System.Drawing.Size(288, 176);
    			this.textBox1.TabIndex = 1;
    			this.textBox1.Text = "";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(320, 273);
    			this.Controls.Add(this.textBox1);
    			this.Controls.Add(this.button1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			textBox1.Text = "salary\t\t\tfrequency\r\n";
     
    			for(int i=1; i<frequency.Length; i++)
    			{
    				int wages = ranNum.Next(1, 10);//generate random number
    				frequency[wages]++;//update array of counters
    			}
    
    			for(int i=0; i<salary.Length; i++)//display output
    			{
    				textBox1.Text += salary[i] + "\t\t" + frequency[i + 1] + "\r\n";
    			}
    		}
    	}
    }
    im asking for help here because i think the part of the question that mentions 9% etc is a red herring, does my solution answer the question correctly?

    thanks in advance

    luigi
    You will never know unless you try!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That code is getting beyond my C# knowledge, but:
    Code:
    textBox1.Text = "salary\t\t\tfrequency\r\n";
    You probably don't need a \r there, since you have a \n.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You need to involve that "9%"
    Code:
    mySalary = baseSalary + 0.09 * grossSale;
    To check whether your program gives the correct answer, just compare its result with that if you do it by hand.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    Wink

    Quote Originally Posted by alphaoide
    You need to involve that "9%"
    Code:
    mySalary = baseSalary + 0.09 * grossSale;
    To check whether your program gives the correct answer, just compare its result with that if you do it by hand.
    yes thats what i was thinking, i have hardcoded the input because the question is ambiguous as where the input is coming from and so i followed an example in the chapter on the same topic.

    You probably don't need a \r there, since you have a \n.
    actually you do with textbox's in C#, i tried for hours trying just using "/n" which caused a unprintable character to appear. "\n" does work in RTF type textbox's

    thanks people
    You will never know unless you try!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. array of counters
    By dantestwin in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2004, 11:14 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM