Otto
Otto

Reputation: 47

Count the number of occurrences in an array with random numbers without methods or list in C#

I am trying to solve an exercise in C# as follows:

Write a program that generates 20 random integers between 0 and 9 and displays the count for each number. Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, ..., 9s.)

This is what i come up with which kind of work but i have a problem with the 0's counting 1 extra all the time.

using System.Collections.Generic;
using System.Text;

namespace ArrayExercises
{
    class TaskFive
    {
        public static void FindNumberCount()
        {

            int c0=0,c1=02,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0;
            int[] arr = new int[20];
            Random rand = new Random();
            Console.WriteLine("Numbers generated ");
            for (int i = 0; i < 19; i++)
            {
                arr[i] = rand.Next(0, 10);
                Console.WriteLine(arr[i]);
            }
            foreach(int number in arr)
            {
                 if (number == 0) { c0++; }
            else if (number == 1) { c1++; }
            else if (number == 2) { c2++; }
            else if (number == 3) { c3++; }
            else if (number == 4) { c4++; }
            else if (number == 5) { c5++; }
            else if (number == 6) { c6++; }
            else if (number == 7) { c7++; }
            else if (number == 8) { c8++; }
            else if (number == 9) { c9++; }

            }
            Console.WriteLine
            (
              $"Number of 0's: {c0} \n" +
              $"Number of 1's: {c1} \n" +
              $"Number of 2's: {c2} \n" +
              $"Number of 3's: {c3} \n" +
              $"Number of 4's: {c4} \n" +
              $"Number of 5's: {c5} \n" +
              $"Number of 6's: {c6} \n" +
              $"Number of 7's: {c7} \n" +
              $"Number of 8's: {c8} \n" +
              $"Number of 9's: {c9}"
            );
        }
    }
}

Thanks in advance :)

Upvotes: 0

Views: 742

Answers (5)

Hendrik
Hendrik

Reputation: 11

The halting condition of the first for loop should be i<20. Then your program should work.

This is how I would solve it:

 static void Main(string[] args)
 {
     Random random = new Random();

     //Fill array with random numbers
     int[] array = new int[20];
     for (int i = 0; i < array.Length; i++)
         array[i] = random.Next(0, 10);

     //Count how many times a number occurs
     int[] numberCounts = new int[10];
     for (int i = 0; i < array.Length; i++)
         numberCounts[array[i]]++;

     //Print the count of the numbers
     for(int i = 0; i < numberCounts.Length; i++)
         Console.WriteLine("Number of " + i + "'s: " + numberCounts[i]);

     //Keep the console open
     Console.ReadLine();
 }

Upvotes: 0

Ansil F
Ansil F

Reputation: 284

The issues is in this line

for (int i = 0; i < 19; i++)

You initialized an array with 20 int and set only value to 19 of them. If you don't set the value int defaults to Zero and hence you always get one extra zero Change your code as below

 for (int i = 0; i <= 19; i++)

Upvotes: 0

kaffekopp
kaffekopp

Reputation: 2619

int[] counts = new int[10];
int[] numbers = new int[20];


var random = new Random();
for (int i = 0; i < numbers.Length; i++)
{        
    // Generate random numbers
    numbers[i] = random.Next(0, 9);
    // Increment the count of the generated number
    counts[numbers[i]]++;
}

Upvotes: 1

Zoedingl
Zoedingl

Reputation: 186

the for loop you use loops only 19 times. You must change the "i < 19" to "i < 20". In your program, the for loop leaves the last int in the array at it's default value (0), this also explains, why you always have one more zero. Hope this helped.

Upvotes: 0

Silny ToJa
Silny ToJa

Reputation: 2261

You could shorten it like this

    public static void FindNumberCount()
    {
        int[] count = new int[10];
        Random rand = new Random();
        int[] arr = new int[20];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = rand.Next(0, 10);
            Console.WriteLine(arr[i]);
            count[arr[i]]++;
        }
        for (int i = 0; i < count.Length; i++)
        {
                Console.WriteLine($"Number of {i}'s: {count[i]}");

        }
       
    }

If you want draw 20 numbers you should for (int i = 0; i < 20; i++) not 19.

Upvotes: 1

Related Questions