1

I am trying to build a program where a user enters a range of numbers(ints) and stores them in an array, counts how many times each number was entered and returns the count of each number. I also keep track of the total number of valid values and invalid values entered, but they don't have to be sorted, only counted.

My problem lies in the fact that we are not allowed to use the group clause yet, as instructed by my professor. We are also not declaring implicit local variables yet and he doesn't want us to use var, as well as using lists.

I am able to count the number of valid entries and invalid entries, but having trouble on counting the occurrence of each element in the array.

This is the specific loop I am having trouble with, it is listing each location in the array ex. 0, 1, 2, instead of counting the number of occurrences of each number.

for (int i = 0; i < values.Length; i++)
Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Testing7._1OutAgain
{
class Program
{
    static void Main(string[] args)
    {
        string inputValue;
        int numberOfValues = 0,
            intValue,
            incorrectValues = 0;

        Console.Write("This program contains the following test data and will" +
            " return the values to you, tell you how many total values were inputted" +
            " and tell you how many times each of the values were repeated.\n\n");

        //Get array size
        Console.Write("How many numbers will you enter?\t\n");
        string stringSize = Console.ReadLine();
        int arraySize = Convert.ToInt32(stringSize);
        int[] values = new int[arraySize];


        //Fill array and count valid entry with numberOfValues++, invalid with incorrectValues
        while (numberOfValues < arraySize)
        {

            Console.WriteLine("Enter value: ");
            inputValue = Console.ReadLine();
            intValue = Convert.ToInt32(inputValue);
            if (intValue >= 1 && intValue <= 10)
            {
                values[numberOfValues] = Convert.ToInt32(inputValue);
                numberOfValues++;
            }
            else
            {
                Console.WriteLine("Incorrect value.");
                incorrectValues++;

            }
        }

        for (int i = 0; i < values.Length; i++)
            Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);

        Console.WriteLine("\n\nTotal number of values = {0}", numberOfValues);
        Console.WriteLine("\n\nTotal number of incorrect values = {0}", incorrectValues);
        Console.ReadKey();
    }
  }
}

This is my first post here and I have looked at other solutions on this same issue, and I have seen some excellent solutions using the group clause but I am not allowed to. Thanks so much for any help and I hope this question made sense, I am just trying to become a better programmer. I appreciate any help and apologize if it isn't very complicated. I am new at programming.

3 Answers 3

2

You can for example create a dictionary to store that info.Create a dictionary variable where you declare the other variables like this:

static void Main(string[] args)
{
    string inputValue;
    int numberOfValues = 0,
        intValue,
        incorrectValues = 0;

    Dictionary<int, int> ocurrences = new Dictionary<int, int>();


    //....rest of code bellow
}

Then in your while loop this:

while (numberOfValues < arraySize)
{

       Console.WriteLine("Enter value: ");
       inputValue = Console.ReadLine();
       intValue = Convert.ToInt32(inputValue);


       if (intValue >= 1 && intValue <= 10)
       {
            if (!ocurrences.ContainsKey(intValue))
            {
                ocurrences.Add(intValue, 0);
            }
            values[numberOfValues] = Convert.ToInt32(inputValue);
            numberOfValues++;

            ocurrences[intValue]++;
       }
       else
       {
             Console.WriteLine("Incorrect value.");
             incorrectValues++;

       }
}

To extract the info like this:

//Dont do this one...
//for (int i = 0; i < values.Length; i++)
     // Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);

foreach (KeyValuePair<int, int> pair in ocurrences)
{
    Console.WriteLine("Number {0} occured {1} times.",pair.Key,pair.Value);
}
Sign up to request clarification or add additional context in comments.

Comments

0
   for (int i = 0; i < values.Length; i++)
        Console.WriteLine("Value {0} was entered: {1} time(s)", values[i], i);

You're basically running over your array, and saying which element is at which position. What you seem to want is to count your items .

So, for each item, you need to iterate over the array, and count the number of times it appears. You'll have to change the last i in your for loop with another loop, or a method that will go over all the items of the array, and every time it found the item you're looking for (your original values[i]) it will increment a counter, and then instead of the i you'll have that counter.

I'll leave you to write the code for that, since it is trivial from this point, and it is your homework after all :)

Comments

0

use int.TryParse(inputValue, out intValue) rather than intValue = Convert.ToInt32(inputValue) to avoid errors

Comments

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.