Using Using Using Using Using Using Using Using Namespace Public Partial Class
Using Using Using Using Using Using Using Using Namespace Public Partial Class
{ public frm1DArrays() { InitializeComponent(); } int[] num = new int[6]; int ind = 0; private void btnAdd_Click(object sender, EventArgs e) { if (ind < 6) { num[ind] = Convert.ToInt32(txtData.Text); ind++; MessageBox.Show("One Element Added Successfully"); txtData.Text = ""; txtData.Focus(); } else MessageBox.Show("Array Full"); } private void btnAddAtLoc_Click(object sender, EventArgs e) { int loc=0; if ((txtData.Text == "") && (txtLoc.Text == "")) MessageBox.Show("Please Input Data or Location"); else
{ loc = Convert.ToInt32(txtLoc.Text); if ((loc>=0)&&(loc < ind)) { for (int i = ind - 1; i >= loc; i--) { num[i + 1] = num[i]; } num[loc] = Convert.ToInt32(txtData.Text); MessageBox.Show("One Element Added Successfully"); ind++; } else MessageBox.Show("Please enter Location Between 0 and " + (ind-1).ToString()); } } private void btnShow_Click(object sender, EventArgs e) { for(int i=0;i<ind;i++) MessageBox.Show("num["+i.ToString()+"] = " + num[i].ToString()); } private void btnSort_Click(object sender, EventArgs e)
{ int temp = 0; for (int i = 0; i < ind; i++) { for (int j = 0; j < ind - i - 1; j++) { if (num[j] > num[j + 1]) { temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } MessageBox.Show("Sorting Complete, Press Show Button to display array elements."); } private void btnSearch_Click(object sender, EventArgs e) { int loc = 0, data = 0; if ((txtData.Text == "") && (txtLoc.Text == "")) { MessageBox.Show("Please Enter either Data or Location to search"); } else if ((txtData.Text != "") &&(txtLoc.Text !="")) {
Location");
data = Convert.ToInt32(txtData.Text); loc = Convert.ToInt32(txtLoc.Text); if (num[loc] == data) MessageBox.Show("Data Found at " + loc.ToString() + " else MessageBox.Show("Data not Available in array");
} else if (txtLoc.Text == "") { int flag = 0; data = Convert.ToInt32(txtData.Text); for (int i = 0; i < ind; i++) { if (num[i] == data) { MessageBox.Show("Data Found at " + i.ToString() + " Location"); } flag = 1; } if(flag!=1) MessageBox.Show("Data not Available in array"); } else if(txtData.Text =="") { loc=Convert.ToInt32 (txtLoc.Text); if((loc>=0) &&(loc<6))
MessageBox.Show("InValid Location and Data field empty. Input data and Location"); txtData.Focus (); } } } private void btnTextbox_Click(object sender, EventArgs e) { TextBox objtb = new TextBox(); objtb.Location = new Point(200, 200); objtb.Size = new Size(50, 50); this.Controls.Add(objtb); } }