Click here to Skip to main content
15,907,913 members
Home / Discussions / C#
   

C#

 
AnswerRe: Repeater Pin
ZurdoDev9-Feb-15 2:50
professionalZurdoDev9-Feb-15 2:50 
QuestionThe grade of this programming Pin
Member 112935288-Feb-15 20:08
Member 112935288-Feb-15 20:08 
Hi guys,

I'm taking C# class, and I got the mark "F" for my assignment.
However, I don't know whether my work was the waste as much as get the zero.

My prof. said me my solution does not satisfy the requirements, but I can't agree with her. I think it was not zero even if I made some mistakes. So, I wanna you guys check my solution whether it was really zero. If you guys also said me I was wrong, I can accept it. but, if otherwise, I'm gonna apply grade appeal.


Here is her direction:

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.


Here is my code as test code including main():

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GenericSearchLib;

namespace GenericSearch
{
    class GenericTest
    {
        private static GenericStack<int> intStack;
        private static GenericStack<double> doubleStack;
        private static int[] intArray;
        private static double[] doubleArray;
        private static int n;

        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number of arraies what you want : "); //promt for user input
            string arrN = Console.ReadLine();
            n = Convert.ToInt32(arrN);

            if (n == 0)
                Console.WriteLine("Please enter the number is bigger than 0");
            else
                intStack = new GenericStack<int>(n);
                doubleStack = new GenericStack<double>(n);
                intArray = new int[n];
                doubleArray = new double[n];


                for ( int i = 0; i < intArray.Length; i++ )
                {
                    Console.WriteLine("Enter a integer for int array, one number as 1 : ");
                    string value = Console.ReadLine();
                    intArray[i] = Convert.ToInt32(value);
                }
                TestPush("intStack",intStack,intArray);
                Console.WriteLine();
                Console.WriteLine("-----------------Complete pusing the array-------------------");
                Console.WriteLine();

                for (int i = 0; i < doubleArray.Length; i++)
                {
                    Console.WriteLine("Enter a integer for double array, one double number as 2.00 : ");
                    string value = Console.ReadLine();
                    doubleArray[i] = Convert.ToDouble(value);
                }
                TestPush("doubleStack",doubleStack,doubleArray);
                Console.WriteLine();
                Console.WriteLine("-----------------Complete pusing the array-------------------");
                Console.WriteLine();

                Console.WriteLine();
                Console.WriteLine("-----------------Start searcing the array-------------------");
                Console.WriteLine();

                Console.WriteLine("Enter the name of array what you want to search, int or double : ");
                string arrName = Console.ReadLine();

                Console.WriteLine("Enter the keyword what you want to search : ");
                string keyword = Console.ReadLine();

                if(arrName == "int")
                    TestSearch(keyword, "Int Array", intStack);
                else
                    TestSearch(keyword, "Double Array", doubleStack);

        }

        private static void TestPush<T>(string name, GenericStack<T> stack, IEnumerable<T> elements)
        {
            // push elements onto stack
            try
            {
                Console.WriteLine("\nPushing elements onto " + name);

                // push elements onto stack
                foreach (var element in elements)
                {
                    Console.Write("{0} ", element);
                    stack.Push(element); // push onto stack
                } // end foreach
            } // end try
            catch (FullStackException exception)
            {
                Console.Error.WriteLine();
                Console.Error.WriteLine("Message: " + exception.Message);
                Console.Error.WriteLine(exception.StackTrace);
            } // end catch
        } // end method TestPush

        public static T To<T>(this string text)
        {
            return (T)Convert.ChangeType(text, typeof(T));
        }

        private static void TestSearch<T>(string searcKey, string name, GenericStack<T> stack)
        {
            int result=0;
            int savePosition=-1;
            try
            {
                for (int i = 0; i < n; i++)
                {
                    result = stack.Search(searcKey, i);
                    if (result != -1)
                        savePosition = result;
                }

                if (savePosition == -1)
                {
                    Console.WriteLine("Searching fail. Your keyword is not matched from array. -1");
                }
                else
                {
                    Console.WriteLine("Searching success. Keyword : {0} . Index(location) of array : {1}", searcKey, savePosition);
                }
            }
            catch (EmptyStackException exception)
            {
                Console.Error.WriteLine();
                Console.Error.WriteLine("Message: " + exception.Message);
                Console.Error.WriteLine(exception.StackTrace);
            }
        }

        // test Pop method
        private static void TestPop<T>(string name, Stack<T> stack)
        {
            // pop elements from stack
            try
            {
                Console.WriteLine("\nPopping elements from " + name);

                T popValue; // store element removed from stack

                // remove all elements from stack
                while (true)
                {
                    popValue = stack.Pop(); // pop from stack
                    Console.Write("{0} ", popValue);
                } // end while
            } // end try
            catch (EmptyStackException exception)
            {
                Console.Error.WriteLine();
                Console.Error.WriteLine("Message: " + exception.Message);
                Console.Error.WriteLine(exception.StackTrace);
            } // end catch
        } // end TestPop
    }
}



This is GenericStack.cs :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericSearchLib
{
    public class GenericStack<T>
    {
        private int top; //location of the top element
        private int position=0; //location of searching in array
        private T[] elements;

        public GenericStack()
            : this( 10 )
        {
            //empty constructor;
        }

        public GenericStack(int stackSize)
        {
            if (stackSize > 0) //check validate stackSize
                elements = new T[stackSize]; //create stackSize elements
            else
                throw new ArgumentException("Stack size must be positive.");

            top = -1; //stack initially empty
            position = 0; //default postion is zero as beginning of array
        }

        public void Push(T pushValue)
        {
            if (top == elements.Length - 1) //stack is full
                throw new FullStackException(String.Format("Stack is full, cannot push {0}", pushValue));

            ++top;
            elements[top] = pushValue;
        }

        public T Pop()
        {
            if (top == -1)
                throw new EmptyStackException("Stack is empty, cannot pop");

            --top;
            return elements[top + 1];
        }

        public int Search(string searckyKey, int position)
        {
            int reVal=-1;
            if (top == -1)
                throw new EmptyStackException("Stack is empty, cannot search");

            if (searckyKey.Equals(elements[position].ToString()))
            {
                reVal = position;
            }
            //++position;
                return reVal;
        }


        public void Display(string arrName)
        {
            Console.WriteLine( arrName + " ArrayList : " );

            int arrN = elements.Length - 1;
            foreach(var value in elements)
            {
                Console.WriteLine( "Location : {0}, Value : {1}", arrN, value);
                --arrN;
            }
        }
    }
}



She pointed out me 2 things.
1. I used stack instead of array.
2. I didn't provide Generic method.

Guys, please let me know, I had to get the zero.
Please show me whether this mark was just her opinion.
If it's unfair, please tell me how I can professionally say it to prof. lol

modified 9-Feb-15 2:18am.

AnswerRe: The grade of this programming Pin
OriginalGriff8-Feb-15 20:45
mveOriginalGriff8-Feb-15 20:45 
GeneralRe: The grade of this programming Pin
Member 112935288-Feb-15 20:57
Member 112935288-Feb-15 20:57 
GeneralRe: The grade of this programming Pin
OriginalGriff8-Feb-15 21:28
mveOriginalGriff8-Feb-15 21:28 
GeneralRe: The grade of this programming Pin
OriginalGriff8-Feb-15 22:35
mveOriginalGriff8-Feb-15 22:35 
AnswerRe: The grade of this programming Pin
V.8-Feb-15 20:52
professionalV.8-Feb-15 20:52 
GeneralRe: The grade of this programming Pin
Member 112935288-Feb-15 21:10
Member 112935288-Feb-15 21:10 
GeneralRe: The grade of this programming PinPopular
V.8-Feb-15 21:19
professionalV.8-Feb-15 21:19 
GeneralRe: The grade of this programming Pin
OriginalGriff8-Feb-15 21:39
mveOriginalGriff8-Feb-15 21:39 
GeneralRe: The grade of this programming Pin
harold aptroot8-Feb-15 22:31
harold aptroot8-Feb-15 22:31 
AnswerRe: The grade of this programming Pin
BillWoodruff8-Feb-15 23:47
professionalBillWoodruff8-Feb-15 23:47 
AnswerRe: The grade of this programming Pin
Dave Kreskowiak9-Feb-15 3:52
mveDave Kreskowiak9-Feb-15 3:52 
AnswerRe: The grade of this programming Pin
Member 112935289-Feb-15 4:06
Member 112935289-Feb-15 4:06 
GeneralRe: The grade of this programming Pin
OriginalGriff9-Feb-15 4:49
mveOriginalGriff9-Feb-15 4:49 
Questionelliptic curve cryptography (ECC). Pin
Member 114284388-Feb-15 19:48
Member 114284388-Feb-15 19:48 
AnswerRe: elliptic curve cryptography (ECC). Pin
OriginalGriff8-Feb-15 20:50
mveOriginalGriff8-Feb-15 20:50 
AnswerRe: elliptic curve cryptography (ECC). Pin
Pete O'Hanlon8-Feb-15 21:22
mvePete O'Hanlon8-Feb-15 21:22 
GeneralRe: elliptic curve cryptography (ECC). Pin
OriginalGriff8-Feb-15 21:29
mveOriginalGriff8-Feb-15 21:29 
AnswerRe: elliptic curve cryptography (ECC). Pin
Kenneth Haugland8-Feb-15 21:36
mvaKenneth Haugland8-Feb-15 21:36 
QuestionTouchscreenKeyboard.dll file Required Pin
Md Rafique Alam8-Feb-15 18:24
Md Rafique Alam8-Feb-15 18:24 
AnswerRe: TouchscreenKeyboard.dll file Required Pin
Richard MacCutchan8-Feb-15 21:42
mveRichard MacCutchan8-Feb-15 21:42 
QuestionHeap in C# Pin
Member 111616258-Feb-15 17:16
Member 111616258-Feb-15 17:16 
AnswerRe: Heap in C# Pin
PIEBALDconsult8-Feb-15 17:31
mvePIEBALDconsult8-Feb-15 17:31 
AnswerRe: Heap in C# Pin
Richard MacCutchan8-Feb-15 21:40
mveRichard MacCutchan8-Feb-15 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.