1

So yeah, current assignment was to make a program that asks the user to input numbers. When they quit, display a histogram of the number of times they entered numbers

Requirements Numbers are 0-10. User can quit any time Make it that the user can enter as many numbers as they want. Display it like If they entered 1,0,0,0,2,2,3,4,4,4,5 0*** 1* 2** 3* 4*** 5*

So i had worked on this, and got a working version of it before finding out i had misread the program. I thought I only needed to have 10 entries, and display that. Got that working. Then i found out it should be unlimited entries basically. We should be using two arrays. One for the numbers 0-10 and one for the counts. Should be in a for loop.

I think that since i got what i thought was what i needed done, my brain is blanking on how to do this.

My assumptions is something like

array[11]{0, 1,2,3,4,5,6,7,8,9,10) count[11]{0,0,0,0,0,0,0,0,0,0,0}

Then ask for user input, check the number, if its a 0, then increase the index 0 value of count by 1. If its a 1, increase the index of count by 1 and up its value by 1. Etc

I dont want to make a bunch of if statements, and I cannot for the life of me think of how i would call the index for a value for displaying. If i want to see what the value of Index+3 (so, how many times 3 was entered) how do i display it.

This is my old code, i do have a class for the things i call to. GetInput and OutputMessage are just methods using the console read and writeline code.

I cannot use dictionaries, or lists. I dont think 2 arrays is required.

Thank you for any help given.

    static public void Myhistogram()
    {
        Histogram myInstanceofDisplayNumbers = new Histogram();
        AverageArray myInstanceofHighLowAverage = new AverageArray();
        ArrayInputAndSort myInstanceofArrayInputAndSort = new ArrayInputAndSort();

        try
        {
            InputOutput.OutputMessage("Enter 10 numbers from 0-10");

            int[] numbers = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            int[] counts = new int[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int count = 0;
            bool entrycompleted = false;
            string input = "a";

            while (!entrycompleted)
            {

                input = InputOutput.GetInput(string.Format("Entry {0}", count + 1));
                int result;
                if (int.TryParse(input, out result) == false && input.ToLower() != "q" || int.Parse(input) < 0)
                {
                    InputOutput.OutputMessage("Please enter a number from 0-9");

                }
                else
                {
                    if (input == String.Empty)
                    {
                        InputOutput.OutputMessage("Entry was blank");
                    }
                    else if (input == "q")
                    {
                        InputOutput.OutputMessage("Sorry no quitting until all 10 numbers entered");

                    }





                    else
                    {
                        if (int.TryParse(input, out result))
                            //result is the number inputted by the user, it will be asked if its greater or equal to 0, and less than or equal to 10. 
                            // if it is, follow through, otherwise, invalid entry. 
                        {
                            if (result >= 0 && result <= 10)
                            {
                                // numbers is my inputted array
                                numbers[count] = result;



                                if (count > 10)
                                {
                                    entrycompleted = true;
                                }
                            }
                            else
                            {
                                InputOutput.OutputMessage("Invalid entry. Try again.");
                            }

                        }
                        else
                        {
                            entrycompleted = true;
                        }

                    }
                }
            }
            InputOutput.OutputMessage("");

            myInstanceofArrayInputAndSort.SortNumbers(numbers.Length, numbers);
            myInstanceofDisplayNumbers.DisplayHistogram(numbers, count, counts);

        }
        catch (Exception ex)
        {
            //OutputMessage is a static method of the class
            //InputOutput therefore you DO NOT need your own
            //instance; INSTEAD reference using the classname
            InputOutput.OutputMessage(ex.Message);
        }

    }

    public void DisplayHistogram(int[] numbers, int count, int[] counts)
    {

       // int[] frequencyofintegers = new int[10];


        for (int i = 0; i < count; i++)
        {
            counts[numbers[i]]++;
        }

        for (int i = 0; i <= 10; i++)
        {
            if (counts[i] == 0)
            {

            }
            else
            {

                InputOutput.OutputSameLine(string.Format("{0}: ", i));

                for (int j = 0; j < counts[i]; j++)
                {
                    InputOutput.OutputSameLine("*");
                }

                InputOutput.OutputMessage("");
            }
        }

        InputOutput.OutputMessage("");
    }
3
  • google search would help but this should give you an idea stackoverflow.com/questions/16527810/… Commented Oct 23, 2014 at 21:46
  • @DJKRAZE That would help if he stored the input before counting. You could also just count in real-time Commented Oct 23, 2014 at 21:47
  • that's true as well BradleyDotNet Commented Oct 23, 2014 at 21:53

1 Answer 1

1

Since the requirements allow 0-10, you already have your indexes! You don't need an second array, dictionary or anything else.

So your input read looks like (simplified):

counts[int.Parse(Console.ReadLine())]++; //Probably should use TryParse!

To print them out, just loop:

for (int i = 0; i < 11; i++)
   Console.WriteLine("{0}, {1}", i, counts[i]);

To get a specific count, its just counts[i]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I appreciate this, it clicked after looking at this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.