how many times those two numbers occur?

Bratanov

New member
Joined
Feb 23, 2021
Messages
1
Programming Experience
Beginner
Hey guys I'm currently working on a code I have to do for school. It goes: A series of n integers is given. Write a console application that finds and displays the smallest
and the largest number in the console and how many times those two numbers occur in it.

I have managed to do the part where it shows you the smallest and biggest number in the array, entered by the user. However, the second part is not working out and I have tried many solutions but still cannot manage to do it. I don't understand how to make the program measure how many times the largest and smallest numbers occur throughout the array.

Here is what I came up with. It gives you the smallest and biggest numbers but fails to measure how many times each of them occurs. Any help would be appreciated.


C#:
using System;

class MainClass {
    public static void Main (string[] args) {
        int Count1 = 0;
        int Count2 = 0;
        string[] numbers;
        int min=int.MaxValue;
        int max=int.MinValue;
        Console.Write("How many elements are there in the array:");
        int element = int.Parse(Console.ReadLine());

        int[] intNumbers=new int[element];
        for(int i = 0; i < element; i ++)
        {
            Console.WriteLine($"The element {i} -");
            intNumbers = int.Parse(Console.ReadLine()); 
        } 

        for (int i = 0; i < element; i++) 
        { 
            if(intNumbers < min) 
            { 
                min = intNumbers; 
            } 
            if(min == intNumbers) 
            { 
                Count1++; 
            } 
        } 
        for(int j = 0; j < element; j++) 
        { 
            if(intNumbers[j] > max) 
            { 
                max = intNumbers[j]; 

            } 
            if(max == intNumbers[j]) 
            { 
                Count2++; 
            } 
        } 
        Console.WriteLine("The smallest number is {0}",min); 
        Console.WriteLine("The largest number is {0}",max); 
        Console.WriteLine("The smallest number occurs:" + Count1 + "times."); 
        Console.WriteLine("The largest number occurs:" + Count2 + "times."); 
    } 
}
 
Last edited by a moderator:
You should always be working out the logic that the code needs to be implementing before writing any code. The best way to do that is to imagine how you would accomplish the task if you had to do it manually, e.g. with pen and paper. You say that you can already do the min and max part so basically you have two numbers and a list of numbers and you need to find out how many times each of the two numbers appears in the list. How would you do that manually? I suspect that you could do it fairly easily, so you can also easily work out how you'd do it. You can then formalise the steps you would take into an algorithm and you can then implement that algorithm in code. If your code doesn't work, you can then debug it and compare it to the algorithm at every step to find out exactly where and how it doesn't behave as expected.

So, what's your algorithm? If you don't know then that means that you don't know what your code has to do, so it should not be a surprise that it doesn't work.
 
Back
Top Bottom