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.