CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Feb 2007
    Posts
    21

    Help with arrays in C#

    I am new to C# programming and have been working on this project for a week. Here's the logic:

    1. Output total number of salary ranges:
    200 - 299, 300-399, 400-499, 500-599, 600-699, 700-799, 800-899,
    1000+
    2. Input gross sales.
    3. If quit (-1) go to step 8
    4. Calculate the salary =$200 + 9% * gross sales (entered by user)
    5. Determine the salary range that the calculated salary falls into.
    6. Increment a counter for the salary range that was determined.
    7. Repeat steps 2 thru 6.
    8. Display the number of salaries falling into the salary ranges as indicated.
    9. End.

    Any help would be greatly appreciated.

    Code:
     static void Main(string[] args)
          {
    
             int grossSales = 0;
    
             while (grossSales != -1)
             {
                Console.Write("Enter in the sales for the salesperson (-1 to quit): ");
                grossSales = Int32.Parse(Console.ReadLine());
    
                //store grossSales in each salary range array
                int[] frequency = new int[9];
    
                //calculate the salary for each item stored in salary range array
                for (int counter = 0; counter < frequency; counter++)
                frequency[counter] = 200 + ((9/10)* grossSales);
             
                int[] getSalary = new int[];// array of salary of the salespeople
    
                //for each salary, increment the appropriate frequency
                foreach (int salary in getSalary)
                ++frequency[salary / 10];
    
                Console.WriteLine("{0,20}{1,30}", "Salary Range", "Number of Salespeople");
    
                //for each salary, print number
                for (int salary = 0; salary < frequency.Length; salary++)
                {
                if (salary == 10)
                   Console.Write("1000 and over: ");
                else
                   Console.Write("{0:D2}{1:D2}: ", salary * 10, salary * 10 + 9);
    
             }
           
             }
          }
       }
    }
    Last edited by apricotsun; February 16th, 2007 at 03:50 PM.

  2. #2
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    Re: Help with arrays in C#

    And did you have a question?

    There are numerous problems in your code, I think you need to spend some time reading.

    Major issues:
    * Your variables are going out of scope each iteration of your while loop
    * You setup arrays, but assign no values
    * Your final output is being repeated each iteration of the while loop

  3. #3
    Join Date
    Feb 2007
    Posts
    21

    Re: Help with arrays in C#

    Sorry. My question was what is wrong with my code. Thank you for responding. I'm trying to figure out your answers. What do you mean out of scope? How do I fix it?

  4. #4
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    Re: Help with arrays in C#

    Quote Originally Posted by apricotsun
    Sorry. My question was what is wrong with my code. Thank you for responding. I'm trying to figure out your answers. What do you mean out of scope? How do I fix it?
    There really is too much wrong with your code to even bother trying to teach it all to you here.


    Code:
    static void Main(string[] args)
            {
    
                int[] sales = new int[9];
                int[] frequency = new int[9];
    
                 for(int counter = 0; counter < 9; counter++)
                 {
                    Console.Write("Enter in the sales for the salesperson: ");
                    sales[counter] = Int32.Parse(Console.ReadLine());
                    
                 }
    
                 for (int counter = 0; counter < 9; counter++)
                 {
    
                     frequency[(Convert.ToInt32((sales[counter] * .09)) - 1)] = (frequency[(Convert.ToInt32((sales[counter] * .09)) - 1)] + 1);
                     
                 }
    
                 int ranges = 200; 
                 for (int counter = 0; counter < 9; counter++)
                 {
                     Console.WriteLine(ranges.ToString() + "-" + Convert.ToString(ranges + 99) + ": " + frequency[counter].ToString());
                     ranges = ranges + 100;
                 }
                 Console.ReadLine();
            }

    That should give you close to what you want, i'll have to charge you for any further customization

  5. #5
    Join Date
    Dec 2006
    Location
    Wisconsin
    Posts
    93

    Re: Help with arrays in C#

    Quote Originally Posted by crackersixx
    i'll have to charge you for any further customization
    ... I'm glad my paycheck doesn't come from that solution.

    On a serious note,
    If you're not sure what "scope" means I think you should start off with some books on programming in C# before you jump into creating that type of program.

    Don't get me wrong one of the best ways to learn is by learning as you go, but you should have a small amount of background before you start playing with salary calculations. Unless this is just a sample solution.

    You can look at this thread, I've made several book recommendations in it.
    Kenneth Jones, .Net Framework BCL/CLR Guru
    Join the fight, refuse to respond to posts that contain code outside of [ code ] tags

  6. #6
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Help with arrays in C#

    Quote Originally Posted by apricotsun
    I am new to C# programming and have been working on this project for a week.
    Are you taking a college computer programming course? If so, what is your major? Just curious.

  7. #7
    Join Date
    Feb 2007
    Posts
    21

    Re: Help with arrays in C#

    Thank you guys. Any help is deeply appreciated. I'm using C# for dummies. I guess I need a dummier version cause that book does not make much sense. Thank you for the book suggestions. I will buy them - as I really do need help.

  8. #8
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Help with arrays in C#

    Quote Originally Posted by apricotsun
    Thank you guys. Any help is deeply appreciated. I'm using C# for dummies. I guess I need a dummier version cause that book does not make much sense.
    LOL! Keep on banging away. I predict that you will experience that "AHA!!" sensation in a week or two when it all starts making sense. Don't give up. Then the fun really begins.

  9. #9
    Join Date
    Dec 2006
    Location
    Wisconsin
    Posts
    93

    Re: Help with arrays in C#

    You said you were new to C# programming... I assume you ment this was your first programming language then?

    Sometimes those dummy books are not very good. I think they are written by dummies more then half the time. But see the recommendations in that thread, I explained the difference between those types of books, and what type of books you should try to get instead.

    Good Luck.
    Kenneth Jones, .Net Framework BCL/CLR Guru
    Join the fight, refuse to respond to posts that contain code outside of [ code ] tags

  10. #10
    Join Date
    Feb 2007
    Posts
    21

    Re: Help with arrays in C#

    Yes. I am new to programming. That is why I am still trying to grasp the concepts that everyone assumes is second nature. I still have a lot of questions but am too embarrassed to ask. So I will have to get those books

  11. #11
    Join Date
    Feb 2007
    Posts
    21

    Re: Help with arrays in C#

    Can anyone explain to me what the following code means?

    item = item > 9 ? 9 : item - 1;

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Help with arrays in C#

    Please use code tags!

    Code:
    item = item > 9 ? 9 : item - 1;
    is the same as
    Code:
       if (item > 9)
          item = 9;
       else
         item = item - 1;
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Feb 2007
    Posts
    21

    Re: Help with arrays in C#

    Sorry for forgetting the code tags. Thank you for explaining that to me. Really appreciate it.

  14. #14
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Help with arrays in C#

    apricotsun,

    If you have time, please go thro' the video courses available in the following websites. To me, video course are the best training materials to get me started pretty quick in any programming language.

    Biginners Video Series on C## [Free] http://msdn.microsoft.com/vstudio/ex...g/default.aspx

    Video Game development, Learn to write C# the fun way [Free]
    http://www.microsoft.com/events/seri...nvideodev.mspx

    [EDITED]

    Finally, welcome to programming world and Good luck.
    Last edited by poochi; February 19th, 2007 at 02:13 PM.

  15. #15
    Join Date
    Dec 2006
    Location
    Wisconsin
    Posts
    93

    Re: Help with arrays in C#

    Quote Originally Posted by poochi
    If you are rich enough and have 100US$ to spend on programming materials, I would suggest you to become a life time member of www.learnvisualstudio.net.
    I sure hope they put that $100.00 to good use, like say, for example, fixing their home page.... its ridiculous

    Example attached.
    Attached Images Attached Images
    Kenneth Jones, .Net Framework BCL/CLR Guru
    Join the fight, refuse to respond to posts that contain code outside of [ code ] tags

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured