CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2007
    Posts
    3

    Smile Much Needed Help With A Program

    I am suppose to write a program in C that basically uses a few functions: Here is what the program is suppose to do. I am not sure how to put this one together. Any help would be much appreciated.

    I need to write a program that will generate 100 random numbers between 1 and 50. Using these numbers, generate a list that will tell the number of random numbers that fell between 1-5, 6-10, 11-15, 16-20, ... , and 46-50. Print out the results as a histogram (a line representing the number of values). Your output might look like the example below:

    1-5 (11) ***********
    6-10 (8) ********
    11-15 (12) ************
    16-20 (9) *********
    21-25 (10) **********
    26-30 (11) ***********
    31-35 (7) *******
    36-40 (8) ********
    41-45 (13) *************
    46-50 (11) ***********

    If anyone knows how or what I should do to get started, please help....

  2. #2

    Re: Much Needed Help With A Program

    I would start by getting a pencil and a piece of paper. I'd do on PAPER, by hand what I needed the program to do. Then I'd write code to do what I did by hand. That's always the first step.

    Homework assignments (usually) aren't about writing code. They're about applying code.

  3. #3
    Join Date
    May 2002
    Posts
    10,943

    Re: Much Needed Help With A Program

    [ moved ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4

    Re: Much Needed Help With A Program

    Problems like this are intruiging, but rewarding when you get them right.

    I won't give you the answer, but one of the main keys is figuring out how you can handle all the data, and in this case if you think about it rationally it helps.

    You have 10 sets, for each random number generated you need to test which set it belongs to and then increment a counter for that set.

    So you need,

    1 random number generator (obviously only producing no's. in the range)
    10 conditional If statements testing the range of the numbers i.e. if (x>=0 && x <= 5) { count1++; }
    10 count variables, count1, count2, count3...count10 (think how an array might be useful to you here)

    Wrap this up in a loop, and then after that, create another loop, iterate through it, printing the asterix's for each of the counts, you might need a loop in a loop here, 1 to iterate through the counts, 1 to print an asterix for each increment.

    And that should do the trick.

    Hope that wasn't too preacherish, and hope I didn't lose you.

    good luck.

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