3
Answers

Simple Array Problem ( but cant find the solution )

Photo of r.saksena

r.saksena

20y
2.1k
1
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)

0
Photo of Andrzej Wegierski
NA 705 0 20y
Simple sort this array and next ignore the values identical with prior
0
Photo of bilnaad
NA 686 0 20y
I thinks it's better practice with this sort of small programs to work with the Console. It's a cleaner environment to code in :) Copy/Paste this and look how it works. using System; namespace ConsoleApplication2 { public class ArrayProblem { public static void Main() { int[] values = new int[] { 10,25,34,67,42, 14,98,76,28,10, 99,82,13,25,76 }; Console.WriteLine(" -----------------------\n"); for(int i = 0; i < values.Length; i++) { int temp = values[i]; bool uni = false; for(int j = 0; j < values.Length; j++) { if (i==j){break;} uni = (temp == values[j]); if (uni){break;} } if (!uni) { Console.WriteLine(string.Format(" {0} is unique" ,temp)); } else { Console.WriteLine(string.Format(" ({0} is duplicate)" ,temp)); } } Console.WriteLine("\n -----------------------"); Console.ReadLine(); } } }
0
Photo of shane
NA 26 0 20y
You are creating a new empty array each time you click on the enter button. You are not holding on to values after the click event of the enter button has terminated. If you make the array a form level variable it will hold values for the life of the form. Hope this helps.