Wrox Programmer Forums
|
Forum and Wrox.com Feedback Post your suggestions for improving the Forums or Wrox.com or questions for the staff here. "Where can I find the code for my book?"
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Forum and Wrox.com Feedback section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 12th, 2015, 10:39 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Angry College C# Programming Assignment

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
        }
    }
}
  #2 (permalink)  
Old February 16th, 2015, 01:03 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

Well she would be right. You really have not implemented anything generic here. You have define a wrapper class around 2 strongly typed arrays and defined a generic function for which you did not leverage any of the benefits of generics.

Check out this link to get a good understanding and how generics is used https://msdn.microsoft.com/en-us/lib...(v=vs.80).aspx

If you have more questions after giving this a good read feel free to ask.
  #3 (permalink)  
Old February 16th, 2015, 04:16 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Smile Thanks for your comment

Hi,
Thanks for your comment. Although I agree that I did not make use of the features associated with generic Namespace, I disagree that the method I used is not generic. My method has one argument that is of type T. Type T’s value is determined at run time by the user’s input. If the method was not generic, I could not have use one argument that was a variable but, would have to use either a method with two parameters and two different types or, two different methods each with different types and overload them. I don’t know. I may be new at this but, no one has convinced me that I deserved to fail this section of my assignment nor that my method is not a generic method. However, I agree that my algorithm could have utilized as much of the features that are provided by generics as possible. But, according to what my professor taught and what I read, I think my mark should be higher than 33%. That’s just me. I’m still learning and maybe I’ll see what people are saying.

Bless,
Truck35
  #4 (permalink)  
Old February 16th, 2015, 05:33 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

The method although defined in a manor to be generic is not the proper use of generics. Had you taken a look and read the link l supplied you might have realized it, an yes you could have implemented your code using a object as the type parameter and you have accomplished the same thing.


A true sln for a generic method would have been something like

Code:
public int Search<T>(T value)
{
     var rVal = -1;

      for( int i = 0; genericArray.Length; i++ )
      {
            if( genericArray[I].CompareTo(value) == 0 )
                 rVal = i;
       }

      return rVal;
}

Where of course the genericArray is define a generic like T[] genericArray

You did define the your method with generic notation but you did not implement any generic about you class. Which is where you get the 5 point.

Try read the link that posted.
  #5 (permalink)  
Old February 16th, 2015, 07:32 PM
Authorized User
 
Join Date: Nov 2012
Posts: 25
Thanks: 3
Thanked 0 Times in 0 Posts
Smile Thanks again for the comments

So, you say that the syntax was of the generic type but the use of my method was not generic. So, what makes a generic method, is it syntax or the way it is use? This is not a rhetorical question; I am actually asking you. Because if the algorithm makes a generic a generic method then I understand where my professor is coming from. According to the way the lecture was given and from the books I read, a generic method has a special syntax that allows you to give the arguments a variable type. I did not know that the definition was based on what type of algorithm you used. I obviously did not use an ordinary method because I used generic syntax. In addition, you could not have done what I did with just an ordinary method. What would you call the method you used in the example you posted for me. Does this style of method not have a name because it was not used correctly or would you say that it is a generic method because of its syntax? The requirements asked for one generic method called “Search” that searches an integer and a double array and returns -1 and the index were the key was found. I did that. I still say that even though I did use the best algorithm. I should have gotten more than 33%. Nothing less than 50%.
Let me know what you think.

Bless,
Kevin
  #6 (permalink)  
Old February 16th, 2015, 08:26 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

Generics is a mixture of generics notation and implementation. I can use generics notation to define a class and methods but if I dont implement the entire class in a generic manor using generic data structures then it does no good.

Thing about the generics classes that are already defined in the system.colloectio.generics name space.

List<string> List<int> List<double> List<MyClass> are all created from the same generic class List<T>


The class does not contain a data structure for every single type under the sun, that is just not possible. In stead it use generic to define a type safe data (such as T[] m_GenericArray) structure with an interface to access it underlying data in a type safe manor. Which by the way is the goal of generics.

You can do the same thing with an object array but the problem with an object array is that it is not type safe you can store any object and many types of objects in a object array, but a generic type safe array you can only store object of Type T in it and the type of data to store passed in the angle brackets.

Looking at the your last post I would not even go so far as to define a generic class that implements a search method. I would have just defined a single method that searches a generic IEnumerable which is an interface that all .net collections implement.

Or just a single methods that search a generic array i.e.

public static class GenericMethods
{
// Performs a linear search on an array of type T
// and return the index of the element if found.
// returns -1 otherwise.
public static int LinearSearch<T>(T[] genericArray, T findValue )
{


}
}

If you implement this method properly it will satisfy all your requirements with out having to define your own custom generic class.

Give the link i mention a good read and see if you van finish the above method.

I will also impletemnt my own version so we can post them for comparison.

Last edited by mmorgan30; February 16th, 2015 at 09:20 PM..
  #7 (permalink)  
Old February 16th, 2015, 10:03 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

I have my sln done, when your sln is done post it and I will look over it and then ill post mine so you can compare it t0 my sln of your requirements.
  #8 (permalink)  
Old February 20th, 2015, 04:01 AM
Registered User
 
Join Date: Feb 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mmorgan30 View Post
I have my sln done, when your sln is done post it and I will look over it and then ill post mine so you can compare it t0 my sln of your requirements.
tks mmorgan
  #9 (permalink)  
Old February 20th, 2015, 04:04 AM
Registered User
 
Join Date: Feb 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mmorgan30 View Post
I have my sln done, when your sln is done post it and I will look over it and then ill post mine so you can compare it t0 my sln of your requirements.
tks mmorgan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Using this text in college course imvandyman BOOK: Ivor Horton's Beginning Visual C++ 2012 1 June 18th, 2013 12:24 PM
Wrox book piracy by Citytech College, NYC cstudent Forum and Wrox.com Feedback 2 December 15th, 2012 12:46 AM
Need a little help on college project limpep Excel VBA 6 February 1st, 2007 09:26 AM
assignment pooja85jain SQL Language 1 September 2nd, 2006 11:09 AM
Programming assignment. Neeko C++ Programming 16 December 18th, 2005 09:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.