Simple Array Problem ( but cant find the solution )

Oct 12 2004 3:10 PM
Question : Use an array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100 inclusive. As each number is read, print it only (you can use Window.Form or Console Window to display result) if it is not a duplicate of a number already read. In your code, you should consider the worse case (in which all number are different). I am trying to solve this problem but soemhow cant print the error when the same number is been entered and it doesnt shows error that a dupicate of a number already read.. Please provide me a solution The way i have tried to solve it is : using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ArrayProblem { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label; private System.Windows.Forms.TextBox inputtextBox; private System.Windows.Forms.Button EnterButton; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.label = new System.Windows.Forms.Label(); this.inputtextBox = new System.Windows.Forms.TextBox(); this.EnterButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label // this.label.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label.Location = new System.Drawing.Point(16, 24); this.label.Name = "label"; this.label.Size = new System.Drawing.Size(192, 32); this.label.TabIndex = 0; this.label.Text = "Enter any Number between 10 and 100 inclusive"; this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // textBox // this.inputtextBox.Location = new System.Drawing.Point(232, 32); this.inputtextBox.Name = "textBox"; this.inputtextBox.Size = new System.Drawing.Size(184, 20); this.inputtextBox.TabIndex = 1; this.inputtextBox.Text = ""; // // EnterButton // this.EnterButton.Location = new System.Drawing.Point(176, 88); this.EnterButton.Name = "EnterButton"; this.EnterButton.Size = new System.Drawing.Size(112, 32); this.EnterButton.TabIndex = 2; this.EnterButton.Text = "Enter"; this.EnterButton.Click += new System.EventHandler(this.EnterButton_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(448, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.EnterButton, this.inputtextBox, this.label}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void EnterButton_Click(object sender, System.EventArgs e) { int[] number = new int[20]; int n = Int32.Parse( inputtextBox.Text ); int m = 0; if ( n >= 10 && n <= 100 ) //while ( n >= 10 && n <= 100 ) { int i = LinearSearch( number,n ); if ( i != -1 ) { MessageBox.Show( "This number has already been entered.Please enter some other number"); } MessageBox.Show("The number is:" +n, "Number Display",MessageBoxButtons.OK,MessageBoxIcon.Information ); number[m] = n; m++; } else { MessageBox.Show( "Please enter any number from 10 to 100" ); } } public int LinearSearch ( int [] array, int key ) { for ( int k=0; k < array.Length; k++ ) { if ( array[k] == key) return k; } return -1; } } }

Answers (3)