Saturday, December 3, 2016

Number Counter in C#

Hi there in this article I would like to share with you the code that is written by a close friend of mine, a fellow software engineer and Expert C# developer Mr. Alfel Benvic G. Go. Thank you very much Sir Bon for sharing your code in your website to share the knowledge to other developers around the world.  


In this article Sir Bon share his sample program that is called Number Counter written in C# programming language. Kindly see the problem description below.

Problem

Write a program that reads in an array of type Int32. You may assume that there are 16 entries in the array. Your program determines how many entries are used. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.

Note:

• Variables are preferred to be of type Int32 for ease and convenience.
• You might need to create some helper functions to at least easily solve the problem.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com


My mobile number here in the Philippines is 09173084360.



Sample Program Output



Program Listing

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

namespace NumberCounter
{
    class Program
    {
        private static Int32[] numArr = new Int32[16];

        public static void paxNum(Int32[] x)
        {
            Int32 n = x.Length, temp;
            for (Int32 i = 1; i < n; i++)
            {
                for (int j = 0; j < n - i; j++)
                {
                    if (x[j] < x[j + 1])
                    {
                        temp = x[j];
                        x[j] = x[j + 1];
                        x[j + 1] = temp;
                    }
                }
            }
        }

        public static void countNumbers(Int32[] elements)
        {
            Int32 number, count_num;
            String compOne, compTwo;

            paxNum(elements);

            for (Int32 i = 0; i < elements.Length; i++)
            {
                number = 0;
                count_num = 1;
                compOne = "" + elements[i];
                for (int j = 0; j < elements.Length; j++)
                {
                    compTwo = "" + elements[j];
                    if (j >= i)
                    {
                        if (compOne.Equals(compTwo) && j != i)
                        {
                            count_num++;
                        }
                    }
                    else if (compOne.Equals(compTwo))
                    {
                        number = 1;
                    }
                }

                if (number != 1)
                {
                    Console.WriteLine(" " + elements[i] + "\t-    " + count_num);
                }
            }
        }


        static void Main(string[] args)
        {
            Int32 i, num;
            Console.WriteLine("Enter 16 numbers  [duplicates allowed]     ");

            for (i = 0; i < 16; i++)
            {
                Console.Write("Enter a number" + "   " + "[ " + (i + 1) + " ]:" + "  ");
                num = Convert.ToInt32(Console.ReadLine());
                numArr[i] = num;
            }
            Console.WriteLine("\n N           COUNT");
            countNumbers(numArr);
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment