Answer to Question #259278 in C# for Kayla

Question #259278

Write a program that reads data into an array of type int. Valid values are from 0 to 10. Your program should display how many were inputted as well as the number of invalid entries. Output a list of distinct valid entries and a count of how many times that use the following test data



1 7 2 4 2 3 8 4 6 4 4 7

1
Expert's answer
2021-10-31T12:01:28-0400
using System;
using System.Collections.Generic;


namespace App
{






    class Program
    {


        static void Main(string[] args)
        {
            
            int[] counterValues = new int[11];




            int n = -1;
            while (n <=0)
            {
                Console.Write("Enter number of values >0: ");
                if (!int.TryParse(Console.ReadLine(), out n) || n <= 0)
                {
                    Console.WriteLine("Wrong number.");
                    n = -1;
                }
            }
            int[] values = new int[n];
            int invalidEntries = 0;
            for (int i = 0; i < n; i++) {
                int value = -1;
                Console.Write("Enter value between 0 and 10: ");
                if (!int.TryParse(Console.ReadLine(), out value))
                {
                    Console.WriteLine("Wrong value.");
                }
                if (value < 0 || value > 10)
                {
                    invalidEntries++;
                }
                values[i] = value;
            }
            




            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] >= 0 && values[i] <= 10)
                {
                    counterValues[values[i]]++;
                }
            }
            Console.WriteLine("Total values: {0}",n);
            Console.WriteLine("The number of valid entries: {0}", (n - invalidEntries));
            Console.WriteLine("The number of invalid entries: {0}", invalidEntries);
            for (int  i = 0; i <=10; i++)
            {
                Console.WriteLine("Value {0} appears {1} times", i, counterValues[i]);
            }
            


            Console.ReadLine();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS