Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Computer Science Introduction to Algorithms Algorithms in Code Linear Search Implementations

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel like that's a typo. I feel like the section marked as "C" should have been marked as "C#" given that it uses classes/objects found in the C# language. ANSI C is not object-oriented.

Hope this helps! :sparkles:

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Whoops! It was marked C# but turns out its stripped out of the resulting Markdown. Who knew!

Here is a link to a simple linear search in C#. All the logic is written in the main function (which is not advisable), but one can see what is going easier. Raw code below.

using System;


// Source: https://www.c-sharpcorner.com/blogs/linear-search-in-console-application1

namespace LinearSearch
{
    class Program
    {
        static void Main()
        {
            var numberArray = new int[100];
            Console.WriteLine("Enter the number of elements");

            string userInputElements = Console.ReadLine();
            int usersNumbers = Int32.Parse(userInputElements);

            Console.WriteLine("---------------------------");

            Console.WriteLine("\n Enter the array elements. Press enter/return after each number \n");

            for (int i = 0; i < usersNumbers; i++)
            {
                string usersInputNumbers = Console.ReadLine();
                numberArray[i] = Int32.Parse(usersInputNumbers);

            }

            Console.WriteLine("---------------------------");

            Console.WriteLine("Enter the Search element\n");

            string usersSearchElement = Console.ReadLine();
            int usersSearchNumber = Int32.Parse(usersSearchElement);

            for (int i = 0; i < usersNumbers; i++)
            {
                if (numberArray[i] == usersSearchNumber)
                {
                    Console.WriteLine("---------------------------");
                    Console.WriteLine("Search successful!");
                    Console.WriteLine("Element {0} found at location {1}\n", usersSearchNumber, i + 1);

                    Console.ReadLine();
                }
            }

            Console.WriteLine("Entered element not found.  Search unsuccesful.");
            Console.ReadLine();




        }
    }
}