
I created the following code in the college that I go to(Centennial College in Toronto Canada). Here are the requirements:
âWrite a generic method, Search, that implements the linear-search algorithm. Method Search
should compare the search key with each element in its array parameter until the search key is
found or until the end of the array is reached. If the search key is found, return its location in the
array; otherwise, return -1. Write a test app that inputs and searches an int array and a double
array.â
Could you believe I got 33% (5 out of 15 for this assignment). I must be blind because my teacher swears that this code not only is not good, but she suggested that I go get help in the tutoring office. I donât see it, but could someone point out what I did wrong and if it is worth a 10 mark deduction from 15 marks.
Thanks,
Truck35
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GenericsTest
{
class MyArrayList
{
int CountInt2 { get; set;}
int CountInt1 { get; set;}
int CountDouble2 { get; set; }
int CountDouble1 { get; set; }
int[] integerArray;
double[] doubleArray;
public MyArrayList()
{
CountInt1 = 0;
CountInt2 = 0;
CountDouble1 = 0;
CountDouble2 = 0;
integerArray = new int[5];
doubleArray = new double[5];
}
//used to fill Student array
public void SetArrayInfo(int number)
{
integerArray[CountInt1] = number;
++CountInt1;
}//end SetArrayInfo<Student>
//used to fill Student array
public void SetArrayInfo(double number)
{
doubleArray[CountDouble1] = number;
++CountDouble1;
}//end SetArrayInfo<Student>
public int Search<T>(T number)
{
if (number.GetType() == typeof(int))
{
//used to find student info in student array
while (CountInt2 <= integerArray.Length)
{
if (CountInt2 < integerArray.Length)
if (integerArray[CountInt2].Equals(number))
{
Console.Clear();
Console.WriteLine("The number " + integerArray[CountInt2] + " was found in array index number: " + CountInt2);
Console.WriteLine();
CountInt2 = 0;
return -2;
}
++CountInt2;
}
return -1;
}
else if (number.GetType() == typeof(double))
{
//used to find student info in student array
while (CountDouble2 <= doubleArray.Length)
{
if (CountDouble2 < doubleArray.Length)
if (doubleArray[CountDouble2].Equals(number))
{
Console.Clear();
Console.WriteLine("The double " + doubleArray[CountDouble2] + " was found in array index number: {0}", CountDouble2);
Console.WriteLine();
CountDouble2 = 0;
return -2;
}
++CountDouble2;
}
}
return -1;
}//end GetArrayInfo<int>
}
class DriverProgram
{
static void Main(string[] args)
{
int numberInt;
double numberDouble;
int choice = 0;
MyArrayList myArrayList = new MyArrayList();
myArrayList.SetArrayInfo(8);
myArrayList.SetArrayInfo(9);
myArrayList.SetArrayInfo(5);
myArrayList.SetArrayInfo(10);
myArrayList.SetArrayInfo(21);
myArrayList.SetArrayInfo(7.7);
myArrayList.SetArrayInfo(3.30);
myArrayList.SetArrayInfo(9.80);
myArrayList.SetArrayInfo(12.3);
myArrayList.SetArrayInfo(21.58);
do
{
Console.WriteLine("1 Get Integer Array Index");
Console.WriteLine("2 Get Double Array Index");
Console.WriteLine("3 Exit");
try
{
Console.Write("Choose one: ");
choice = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException e)
{
Console.WriteLine("You have entered an invalid type");
Console.WriteLine("Error: " + e);
}
//return Integer array index
if (choice == 1)
{
try
{
Console.Clear();
Console.Write("Enter integere number: ");
numberInt = Convert.ToInt32(Console.ReadLine());
int answer = myArrayList.Search<int>(numberInt);
if (answer == -1)
Console.WriteLine("The ID number entered was not found.");
}
catch (FormatException e)
{
Console.WriteLine("You have entered an invalid type");
Console.WriteLine("Error: " + e);
}
}
else if (choice == 2)
{
try
{
Console.Clear();
Console.Write("Enter double number: ");
numberDouble = Convert.ToDouble(Console.ReadLine());
int answer = myArrayList.Search<double>(numberDouble);
if (answer == -1)
Console.WriteLine("The ID number entered was not found.");
}
catch (FormatException e)
{
Console.WriteLine("You have entered an invalid type");
Console.WriteLine("Error: " + e);
}
}
//clear screen before exit
else if (choice == 3) { Console.Clear(); }
//display error message if user chooses an invalid choice
else
{
Console.Clear();
Console.WriteLine("The choice you made is Invalid");
}
//exit program
Console.WriteLine("Press any key to continue....");
Console.ReadKey();
Console.Clear();
} while (choice != 3);//end do/while loop
}
}
}