For C# need to make these changes to this programm, http://pastebin.com/nBsBETd8
For this assignment, rewrite the CS7 Array to use a string typed List.
Review the CS7 Arrays program.
The main changes to make to CS7 Arrays to complete your assignment include:
The function names may stay the same.
Change the declaration of cstrName[] to List cstrName = new List( );
The variable cintNumberOfNames will not be needed because cstrName.Count can be used.
In CS7Form_Load:
Delete int i = 0;
The if-else statement to check if the array size has been exceeded is not needed since lists can
grow.
Use the Add method to load the names in the list,
cstrName.Add(nameStreamReader.ReadLine());
The names still need to be displayed in the listbox after the list is loaded.
In displayNames( ) change cintNumberOfNames to cstrName.Count.
In btnSortByName, all of the logic can be replaced with a call to Sort, cstrName.Sort(); The
names have to be redisplayed after the sort.
In btnSearchByName:
Declare a varible to store the result of the search, int intIndex;
Call btnSortByName to sort and redisplay the array just in case it hasn\'t be sorted.
Use the BinarySearch method to conduct the search,
intIndex = cstrName.BinarySearch(txtSearchName.Text);
A negative value is returned if the value is not found, else the zero-based index of the element is
returned. In the statement above, the result is stored in intIndex. intIndex could then be checked
if the result is >= zero, such as if (intIndex >= 0 )
Use the result to display a message if the name was not found, else select the matching entry in
listbox and display a message indicating the name was found.
In btnDelete use the RemoveAt method to delete the name at the top of the list. Before calling
RemoveAt, check that Count is greater than zero to avoid a run-time exception. The names have
to be redisplayed after removing a name.
here is the code,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Project: CS7 Array Processing
//Programmer: Daryl Austin
//Description: Declare and load an array with a list of names.
// The user can sort the list by name and search by name.
// Names can be deleted from the top of the list.
using System.IO; // FileStream and StreamReader
namespace CS7
{
public partial class CS7Form : Form
{
public CS7Form()
{
InitializeComponent();
}
//Declare class-level arrays
//Array can hold up to 10 names.
string[] cstrName = new string[9];
//Use cintNumberOfCustomers to process array because array
//may be partially loaded. Count set in CS7Form_Load
int cintNumberOfNames;
private void CS7Form_Load(object sender, EventArgs e)
{
int i = 0; // subscript initialized to zero
try
{
//Load the array with the data in the file
FileStream nameFile = new FileStream(\"cs7.txt\", FileMode.Open);
StreamReader nameStreamReader = new StreamReader(nameFile);
while (nameStreamReader.Peek() != -1)
{
if (i < c.
For C# need to make these changes to this programm, httppastebin..pdf
1. For C# need to make these changes to this programm, http://pastebin.com/nBsBETd8
For this assignment, rewrite the CS7 Array to use a string typed List.
Review the CS7 Arrays program.
The main changes to make to CS7 Arrays to complete your assignment include:
The function names may stay the same.
Change the declaration of cstrName[] to List cstrName = new List( );
The variable cintNumberOfNames will not be needed because cstrName.Count can be used.
In CS7Form_Load:
Delete int i = 0;
The if-else statement to check if the array size has been exceeded is not needed since lists can
grow.
Use the Add method to load the names in the list,
cstrName.Add(nameStreamReader.ReadLine());
The names still need to be displayed in the listbox after the list is loaded.
In displayNames( ) change cintNumberOfNames to cstrName.Count.
In btnSortByName, all of the logic can be replaced with a call to Sort, cstrName.Sort(); The
names have to be redisplayed after the sort.
In btnSearchByName:
Declare a varible to store the result of the search, int intIndex;
Call btnSortByName to sort and redisplay the array just in case it hasn't be sorted.
Use the BinarySearch method to conduct the search,
intIndex = cstrName.BinarySearch(txtSearchName.Text);
A negative value is returned if the value is not found, else the zero-based index of the element is
returned. In the statement above, the result is stored in intIndex. intIndex could then be checked
if the result is >= zero, such as if (intIndex >= 0 )
Use the result to display a message if the name was not found, else select the matching entry in
listbox and display a message indicating the name was found.
In btnDelete use the RemoveAt method to delete the name at the top of the list. Before calling
RemoveAt, check that Count is greater than zero to avoid a run-time exception. The names have
to be redisplayed after removing a name.
here is the code,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
2. using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Project: CS7 Array Processing
//Programmer: Daryl Austin
//Description: Declare and load an array with a list of names.
// The user can sort the list by name and search by name.
// Names can be deleted from the top of the list.
using System.IO; // FileStream and StreamReader
namespace CS7
{
public partial class CS7Form : Form
{
public CS7Form()
{
InitializeComponent();
}
//Declare class-level arrays
//Array can hold up to 10 names.
string[] cstrName = new string[9];
//Use cintNumberOfCustomers to process array because array
//may be partially loaded. Count set in CS7Form_Load
int cintNumberOfNames;
private void CS7Form_Load(object sender, EventArgs e)
{
int i = 0; // subscript initialized to zero
try
{
//Load the array with the data in the file
FileStream nameFile = new FileStream("cs7.txt", FileMode.Open);
StreamReader nameStreamReader = new StreamReader(nameFile);
while (nameStreamReader.Peek() != -1)
{
if (i < cstrName.Length)
{
cstrName[i] = nameStreamReader.ReadLine();
3. i += 1; //Increment subscript by one
}
else
{
MessageBox.Show
("Error: Notify Programmer Array Size Exceeded. ",
"Array Size Exceeded", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
break; //Get of out of loop; Arrays are full.
}//End If
}//End Loop
nameFile.Close(); //Close file
}
catch (Exception ex)
{
MessageBox.Show("Error Opening File. Data not loaded " + ex.Message,
"File Not Found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
cintNumberOfNames = i; //Save how many students were loaded
displayNames(); //display names in list box
}
void displayNames()
{
int i;
//Listboxes need to be cleared because this procedure is also
//called to redisplayed the data
lstName.Items.Clear();
//Use cintNumberOfnames to process a partially filled array
//cintNumberOfnames is like Length, so we need to subtract one
//to get the subscript value of the last entry.
for (i = 0; i < cintNumberOfNames; i++)
{
lstName.Items.Add(cstrName[i]);
}//next i - name
}
4. private void btnSortByName_Click(object sender, EventArgs e)
{
//Sort by name using a Simple Selection sort
int i;
int i2;
string strHoldValue;
string strMinName;
int intMinSubscript;
//outer loop keeps track of where the next value
//should be placed.
for (i = 0; i < cintNumberOfNames - 1; i++)
{
intMinSubscript = i;
strMinName = cstrName[i];
//inner loop finds the lowest value to move up
for (i2 = i + 1; i2 < cintNumberOfNames; i2++)
{
//Only == and != can be used with strings, use CompareTo
if (cstrName[i2].CompareTo(strMinName) < 0)
{
//save the new low value found
strMinName = cstrName[i2];
intMinSubscript = i2;
}
}//next i2
strHoldValue = cstrName[i];
cstrName[i] = cstrName[intMinSubscript];
cstrName[intMinSubscript] = strHoldValue;
}//next i
//display names in list box
displayNames();
}
private void btnSearchByName_Click(object sender, EventArgs e)
{
int i;
bool blnNameFound = false;
5. //array must be sorted ascending for early exit logic to work
btnSortByName_Click(sender, e);
for (i = 0; i < cintNumberOfNames; i++)
{
//case sensitive comparison
if (txtSearchName.Text == cstrName[i])
{
blnNameFound = true;
txtSearchResults.Text = "Matching name is selected in list box.";
lstName.SelectedIndex = i;
break; //exit for loop
}
}//next i
if (blnNameFound == false)
{
txtSearchResults.Text = "Match not found - Reached end of array";
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i;
int i2;
for (i = 0; i < cintNumberOfNames; i++)
{
//move names up one posistion
i2 = i + 1;
cstrName[i] = cstrName[i2];
}
//If the count is > 0 subtract one and redisplay names in list box
if (cintNumberOfNames > 0)
{
//substract one from the count
cintNumberOfNames -= 1;
//display names in list box
displayNames();
}
6. }
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}//end of class
}//end of namespace
Solution
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Project: CS7 Array Processing
//Programmer: Daryl Austin
//Description: Declare and load an array with a list of names.
// The user can sort the list by name and search by name.
// Names can be deleted from the top of the list.
using System.IO; // FileStream and StreamReader
namespace CS7
{
public partial class CS7Form : Form
{
public CS7Form()
{
InitializeComponent();
7. }
//Declare class-level arrays
//Array can hold up to 10 names.
string[] cstrName = new string[9];
//Use cintNumberOfCustomers to process array because array
//may be partially loaded. Count set in CS7Form_Load
int cintNumberOfNames;
private void CS7Form_Load(object sender, EventArgs e)
{
int i = 0; // subscript initialized to zero
try
{
//Load the array with the data in the file
FileStream nameFile = new FileStream("cs7.txt", FileMode.Open);
StreamReader nameStreamReader = new StreamReader(nameFile);
while (nameStreamReader.Peek() != -1)
{
if (i < cstrName.Length)
{
cstrName[i] = nameStreamReader.ReadLine();
i += 1; //Increment subscript by one
}
else
{
MessageBox.Show
("Error: Notify Programmer Array Size Exceeded. ",
"Array Size Exceeded", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
break; //Get of out of loop; Arrays are full.
}//End If
}//End Loop
8. nameFile.Close(); //Close file
}
catch (Exception ex)
{
MessageBox.Show("Error Opening File. Data not loaded " + ex.Message,
"File Not Found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
cintNumberOfNames = i; //Save how many students were loaded
displayNames(); //display names in list box
}
void displayNames()
{
int i;
//Listboxes need to be cleared because this procedure is also
//called to redisplayed the data
lstName.Items.Clear();
//Use cintNumberOfnames to process a partially filled array
//cintNumberOfnames is like Length, so we need to subtract one
//to get the subscript value of the last entry.
for (i = 0; i < cintNumberOfNames; i++)
{
lstName.Items.Add(cstrName[i]);
}//next i - name
}
private void btnSortByName_Click(object sender, EventArgs e)
{
9. //Sort by name using a Simple Selection sort
int i;
int i2;
string strHoldValue;
string strMinName;
int intMinSubscript;
//outer loop keeps track of where the next value
//should be placed.
for (i = 0; i < cintNumberOfNames - 1; i++)
{
intMinSubscript = i;
strMinName = cstrName[i];
//inner loop finds the lowest value to move up
for (i2 = i + 1; i2 < cintNumberOfNames; i2++)
{
//Only == and != can be used with strings, use CompareTo
if (cstrName[i2].CompareTo(strMinName) < 0)
{
//save the new low value found
strMinName = cstrName[i2];
intMinSubscript = i2;
}
}
strHoldValue = cstrName[i];
cstrName[i] = cstrName[intMinSubscript];
cstrName[intMinSubscript] = strHoldValue;
}
//display names in list box
displayNames();
}
10. private void btnSearchByName_Click(object sender, EventArgs e)
{
int i;
bool blnNameFound = false;
//array must be sorted ascending for early exit logic to work
btnSortByName_Click(sender, e);
for (i = 0; i < cintNumberOfNames; i++)
{
//case sensitive comparison
if (txtSearchName.Text == cstrName[i])
{
blnNameFound = true;
txtSearchResults.Text = "Matching name is selected in list box.";
lstName.SelectedIndex = i;
break; //exit for loop
}
}//next i
if (blnNameFound == false)
{
txtSearchResults.Text = "Match not found - Reached end of array";
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
int i;
int i2;
for (i = 0; i < cintNumberOfNames; i++)
{
//move names up one posistion
11. i2 = i + 1;
cstrName[i] = cstrName[i2];
}
//If the count is > 0 subtract one and redisplay names in list box
if (cintNumberOfNames > 0)
{
//substract one from the count
cintNumberOfNames -= 1;
//display names in list box
displayNames();
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}//end of class
}//end of namespace