Koenvc
Koenvc

Reputation: 3

Show how many times a specific number is found in an array

The first part of my code is where the input numbers are put into an array, this part works fine. In the second part you need to read in a searchterm and search for the searchterm in the array. After searching for it I need to show how many times the searchterm is found in the array. The second part is what i have tried but it doesn't work.

int[] number = new int[20];
int i = 0;
int notnull = 1;
while ( ( i < 20 ) && ( notnull != 0 ) )
{
  Console.Write("Give a number <0 = stop> : ");
  number[i] = int.Parse(Console.ReadLine());
  notnull = number[i];
  i++;
}

Console.WriteLine("Give searchterm (number): ");
int searchterm = int.Parse(Console.ReadLine());
int count = 1;
if ( searchterm == number[i] )
  count++;

Console.WriteLine("The number {0} shows up {1} times", searchterm, count);

Upvotes: 0

Views: 240

Answers (3)

user12031933
user12031933

Reputation:

You can try this:

using System.Linq;
using System.Collections.Generic;

static void Test()
{
  int capacity = 20;
  List<int> numbers = new List<int>(capacity);

  Console.WriteLine($"Enter up to {capacity} numbers (0 or not an integer to stop): ");
  int value = 0;
  do
  {
    int.TryParse(Console.ReadLine(), out value);
    if ( value != 0 )
      numbers.Add(value);
  }
  while ( numbers.Count < capacity && value != 0 );

  if ( numbers.Count > 0 )
  {
    Console.WriteLine();
    Console.WriteLine("Enter a number to search occurences: ");
    int.TryParse(Console.ReadLine(), out value);
    if ( value > 0 )
    {
      Console.WriteLine();
      Console.WriteLine("The number {0} shows up {1} times",
                        value, 
                        numbers.Count(n => n == value));
    }
  }
}

Sample:

Enter up to a number (0 or not an integer to stop):
1
2
3
1
5
6
7
3
2
0

Enter un number to search occurences:
3

The number 3 shows up 2 times

Upvotes: 0

SomeBody
SomeBody

Reputation: 8743

You can easily count items in an Array or an IEnumerable<T> in general, that have to fulfill a certain condition, with LINQ's Count():

Console.WriteLine("Give searchterm (number): ");
int searchterm = int.Parse(Console.ReadLine());
int count = number.Count(i => i == searchterm);
Console.WriteLine("The number {0} shows up {1} times", searchterm, count);

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

First, let's get rid of magic numbers - 20 in your case; let's change collecrtion's type: array doesn't have Add which List<T> provides:

   List<int> number = new List<int>();

Then let's be cafeful with user input: what if user put bla-bla-bla? In order to avoid crash we can use TryParse instead of Parse:

   while (true) {
     // 0 is not a good choice; let it be Q (Quit) instead
     Console.WriteLine("Give a number or Q to stop : ");

     // Trim: let's be nice and tolerate trailing/leading spaces 
     string input = Console.ReadLine().Trim(); 

     // Let's accept Q, q, quit, etc.
     if (input.StartsWith("Q", StringComparison.OrdinalIgnoreCase))
       break;           

     if (int.TryParse(input, out int value)) 
       number.Add(value);  
     else
       Console.WriteLine("Sorry, invalid input");
   }

Now we have number to work with. Let's obtain searchterm with the technique (TryParse)

   int searchterm = -1; // initialization, let compiler be happy

   while (true) {    
     Console.WriteLine("Give searchterm (number): ");

     if (int.TryParse( Console.ReadLine(), out searchterm))
       break;

     Console.WriteLine("Sorry, invalid input");         
   }

Finally, let's compute count; usually we put Linq for this:

   int count = number.Count(searchterm);

However, if you want good old loops

   int count = 0;

   foreach (var item in number)
     if (item == searchterm) 
       count += 1;

Final suggestion is to use string interpolation as more readable then formatting:

   Console.WriteLine($"The number {searchterm} shows up {count} times");  

Upvotes: 0

Related Questions