Skip to content

Instantly share code, notes, and snippets.

@tolmicl
Last active February 4, 2017 01:02
Show Gist options
  • Save tolmicl/43751e4a72bcaf8ed134721a0b77992a to your computer and use it in GitHub Desktop.
Save tolmicl/43751e4a72bcaf8ed134721a0b77992a to your computer and use it in GitHub Desktop.
p812.cs
// Problem 8.12 Duplicate Elimination
// Use one-dimensional array that inputs five numbers
using System;
class p812
{
static void Main()
{
int[] numbers = new int[5];
bool duplicate = false;
int count = 0;
while (count < 5) // Number of elements to be added to the array
{
Console.Write("Enter a number between 10 and 100: "); // Prompt user to enter values as indicated
int userInput = int.Parse(Console.ReadLine());
while (userInput < 10 || userInput > 100)
{
Console.Write("INVALID INPUT. Enter a number between 10 and 100: "); // If entry is invalid, ask user to enter it again
userInput = int.Parse(Console.ReadLine());
}
duplicate = false;
for (int i = 0; i < numbers.Length; i++)
{
if (userInput == numbers[i])
{
duplicate = true;
}
}
if (!duplicate) // If user input is unique, add the input to the array
{
numbers[count] = userInput;
}
for (int i = 0; i < numbers.Length; i++)
{
if (!(numbers[i] == 0))
{
Console.Write(numbers[i] + " ");
}
}
count++;
Console.WriteLine();
}
Console.ReadLine(); // Keep the window open until user closes it to see that the program executed properly
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment