2 Replies - 681 Views - Last Post: 11 November 2015 - 09:49 AM Rate Topic: -----

#1 afinkel30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-November 15

C# One dimensional array program

Posted 10 November 2015 - 03:58 PM

Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their 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 an application (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
The application will accept sales amount until the user enters -1 to end (as shown in figure 2.1 below). At this point, the application calculates the salary and updates the appropriate counter of the array. For instance, if the salesperson gets a total salary of $565, you will update the counter that is counting totals between the $500-$599 range.

Figure 2.1. Receiving input from user

Note that you do NOT need to store the individual salary of the salesperson. Summarize the results in tabular format (see figure 2.2 below). Use only a for loop to display the output.


Figure 2.2. Final output showing all ranges and the number of agents that received commission in that range


Below is my code. I am having trouble getting the results I am looking for.



SalesReport:
using System;

public class SalesReport
{
    public static void Main(string[] args)
    {
        double[] salary = { 0, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200 };
        int[] arrayValue = new int[10];


        Console.Write("Please enter the salesperson's salary or -1 to exit: ");
        double arrayV = Convert.ToInt32(Console.ReadLine());

        while (arrayValue != -1)
        {
            for (int i = 0; i < (salary.Length); i++)
            {
                int sal = (int)(salary[i] * (0.09)) + 200;
                int range = sal / 100;
                arrayValue[range] = arrayValue[range] + 1;
            }
        }

            Console.Write("Range                    Number\n");

        for (int i = 2; i < arrayValue.Length; i++)
        {
            int lowerValue = i * 100;
            int upperValue = (i + 1) * 100 - 1;
            Console.Write("{0} - {1}               {2}", lowerValue, upperValue, arrayValue[i]);
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}


This post has been edited by Skydiver: 11 November 2015 - 06:46 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.


Is This A Good Question/Topic? 0
  • +

Replies To: C# One dimensional array program

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13659
  • View blog
  • Posts: 54,529
  • Joined: 12-June 08

Re: C# One dimensional array program

Posted 10 November 2015 - 04:45 PM

Quote

I am having trouble getting the results I am looking for

Please explain this.
Was This Post Helpful? 0
  • +
  • -

#3 tknielsen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-November 15

Re: C# One dimensional array program

Posted 11 November 2015 - 09:49 AM

Quote

I am having trouble getting the results I am looking for


Hi. There is lots of errors in your code. Line 14 you say "while (arrayV != -1)" but you cant do that because arrayV is a variable that holds reference to your array in Heap memory. When you print result out why do you want to start on position 3 in array?
I have a very basic and simple solution if you need inspiration, theres no tjeck for negative numbers and string input just to show you.

Best regards Torben K Nielsen.
using System;

namespace ConsoleApplication11
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            double [] arrayValue = new double[9];

            Console.Write("Please enter the salesperson's salary or -1 to exit: ");
            double inputValue = Convert.ToDouble(Console.ReadLine());

            while (inputValue != -1)
            {
                inputValue = (inputValue * (0.09)) + 200;

                if (inputValue < 300)
                {
                    arrayValue[0] = arrayValue[0] + 1;
                }
                else if (inputValue < 400)
                {
                    arrayValue[1] = arrayValue[1] + 1;
                }
                else if (inputValue < 500)
                {
                    arrayValue[2] = arrayValue[2] + 1;
                }
                else if (inputValue < 600)
                {
                    arrayValue[3] = arrayValue[3] + 1;
                }
                else if (inputValue < 700)
                {
                    arrayValue[4] = arrayValue[4] + 1;
                }
                else if (inputValue < 800)
                {
                    arrayValue[5] = arrayValue[5] + 1;
                }
                else if (inputValue < 900)
                {
                    arrayValue[6] = arrayValue[6] + 1;
                }
                else if (inputValue < 1000)
                {
                    arrayValue[7] = arrayValue[7] + 1;
                }
                else
                {
                    arrayValue[8] = arrayValue[8] + 1;
                }

                Console.Write("Please enter the salesperson's salary or -1 to exit: ");
                inputValue = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < arrayValue.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        Console.WriteLine("Range $200-299 :" + arrayValue[i]);
                        break;

                    case 1:
                        Console.WriteLine("Range $300-399 :" + arrayValue[i]);
                        break;

                    case 2:
                        Console.WriteLine("Range $400-499 :" + arrayValue[i]);
                        break;

                    case 3:
                        Console.WriteLine("Range $500-599 :" + arrayValue[i]);
                        break;

                    case 4:
                        Console.WriteLine("Range $600-699 :" + arrayValue[i]);
                        break;

                    case 5:
                        Console.WriteLine("Range $700-799 :" + arrayValue[i]);
                        break;

                    case 6:
                        Console.WriteLine("Range $800-899 :" + arrayValue[i]);
                        break;

                    case 7:
                        Console.WriteLine("Range $900-999 :" + arrayValue[i]);
                        break;

                    case 8:
                        Console.WriteLine("Range $1000 and over :" + arrayValue[i]);
                        break;
                }
            }
            Console.ReadLine();
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1