10 Replies - 769 Views - Last Post: 19 February 2013 - 06:21 AM Rate Topic: -----

#1 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 02:15 AM

Hey guys ok so im now making a code i posted a few days ago it was dumped because they thought i was asking for the code when i simply needed to be pushed in the right direction but any way that doesnt matter i will now break down my code into segments trying to explain it the best to my ability ok so here we go


 //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ",Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ",Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

The array data now this is where most of it happens now when i get to the first line it was generating random numbers but it suppose to display the array from what im told and i have run it multiple times but once it gets to this part it just begins to generate phenomenal numbers which is the main issue with this code

Moving on

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max] ;
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();


This is the main void with the values within the code


            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
            Display.ArrayData(SearchArray);
            }
            //end initialize array


This is where the array initializes

            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }

Now the Search this is where i search for the sequential search

            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");


Now this is where the bubble sort this is where it displays alot of its issues with displaying random numbers which i am not to sure why

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 10 - 50)");
                target = int.Parse(Console.ReadLine());
                if (target >= 10 && target <= 50)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************

now this is for the Binary search this seems to run fine for the time

            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = ReadLine();
                if(ChangElement >0 && ChangeElement <= 10);
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while(ok = false)
                Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end

This is where the Element change begins and for some reason i am having issues with
else
statements if anyone could help with that would be greatly apprectiated

            while (first <= last)
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}

This is the Binary search which is having a few issues i hope to fix




This is the entire code as one
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
 * Name: NAME
 * Date: 13 Feb 2013
 * ID: ID
 */
namespace ConsoleApplication15
{
    class Program
    {
        //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ",Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ",Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max] ;
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();

            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
            Display.ArrayData(SearchArray);
            }
            //end initialize array
            //*******************************************************************************
            
            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }
            //*******************************************************************************
            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 10 - 50)");
                target = int.Parse(Console.ReadLine());
                if (target >= 10 && target <= 50)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************
            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = ReadLine();
                if(ChangElement >0 && ChangeElement <= 10);
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while(ok = false)
                Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end
            //Binary Search
            // search sorted array with ascending values
            while (first <= last)
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}


If anyone could help with my issues that would be great

This post has been edited by LawlessBaron: 19 February 2013 - 02:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort and Initialization not displaying

#2 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 03:27 AM

            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = ReadLine();
                if(ChangElement >0 && ChangeElement <= 10);
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while(ok = false)
                Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end
There's a ChangeElement in there missing an "e" on line 113. On the same line there shouldn't be a semicolon at the end.

On lines 112 and 118 you have to use:
int.parse(Console.ReadLine());
On line 117 you should use == instead of =.

On line 109 you have a do while loop. The while should be at the end of the loop, not in the middle. You could use while(true) and break out of it if you want it to end in the middle.

Also i don't understand why your ArrayData method does the exact same thing three times?
Was This Post Helpful? 0
  • +
  • -

#3 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 03:33 AM

Thats what i am trying to figure out at the moment thanks for that

and what does it mean invalid expression Else?
Was This Post Helpful? 0
  • +
  • -

#4 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 03:43 AM

I suppose it's because the if statement is invalid as well. It has a semicolon at the end.
Was This Post Helpful? 0
  • +
  • -

#5 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 03:58 AM

View PostWitchking, on 19 February 2013 - 06:43 PM, said:

I suppose it's because the if statement is invalid as well. It has a semicolon at the end.

Edited code im new to C# i don't fully understand the entire concept is why i am asking for a bit of assistance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication15
{
    class Program
    {
        //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ", Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ", Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max];
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();

            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
                Display.ArrayData(SearchArray);
            }
            //end initialize array
            //*******************************************************************************

            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }
            //*******************************************************************************
            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 0 - 10)");
                target = int.Parse(Console.ReadLine());
                if (target >= 0 && target <= 10)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************
            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = int.Parse(Console.ReadLine());
                if (ChangeElement > 0 && ChangeElement <= 10)
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while (ok == false)
                    Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end
            //Binary Search
            // search sorted array with ascending values
            while (first <= last);
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 04:13 AM

View PostLawlessBaron, on 19 February 2013 - 12:58 PM, said:

View PostWitchking, on 19 February 2013 - 06:43 PM, said:

I suppose it's because the if statement is invalid as well. It has a semicolon at the end.

Edited code im new to C# i don't fully understand the entire concept is why i am asking for a bit of assistance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication15
{
    class Program
    {
        //Display Array
        void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.Write("- {0} ", Numbers[i]);
            }
            Console.WriteLine();
            for (int z = 0; z < Numbers.Length; z++)
            {
                Console.Write("- {0} ", Numbers[z]);
            }

            for (int x = 0; x < Numbers.Length; x++)
            {
                Console.Write("- {0} ", Numbers[x]);
            }
            Console.WriteLine();
        }// end of array data

        static void Main(string[] args)
        {
            const int max = 10;
            int[] SearchArray = new int[max];
            int target = 0; //value to find
            int midpoint = 0;
            int first = 0; //first value in array
            int last = SearchArray.Length - 1; // last value in array
            bool ok = false;
            Program Display = new Program();
            Random GetRandom = new Random();

            //Initialize Array
            for (int x = 0; x < SearchArray.Length; x++)
            {
                SearchArray[x] = GetRandom.Next();
                Display.ArrayData(SearchArray);
            }
            //end initialize array
            //*******************************************************************************

            //sequential search
            Display.ArrayData(SearchArray);
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 1 - 9)");
                target = int.Parse(Console.ReadLine());
                if (target >= 1 && target <= 9)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            bool found = false;
            for (int z = 0; z < SearchArray.Length; z++)
            {
                if (SearchArray[target] == SearchArray[z])
                {
                    Console.WriteLine("Variable Found");
                    found = true;
                    break;
                }
            }
            //*******************************************************************************
            int i = 0, j = 0, temp = 0;
            //sorting: bubble sort
            Console.WriteLine("Bubble Sort initialized");
            Display.ArrayData(SearchArray);
            for (i = 0; i < SearchArray.Length; i++)
            {
                for (j = i + 1; j < SearchArray.Length; j++)
                {
                    if (SearchArray[i] > SearchArray[j])
                    {
                        temp = SearchArray[i];
                        SearchArray[i] = SearchArray[j];
                        SearchArray[j] = temp;
                    }
                    //Console.WriteLine("J={0}", j);
                }
                //Console.WriteLine("I={0}", i);
            }
            Display.ArrayData(SearchArray);
            Console.WriteLine("Bubble Sort completed");

            //*******************************************************************************
            do //get target integer from keyboard
            {
                Console.WriteLine("please enter search target (between 0 - 10)");
                target = int.Parse(Console.ReadLine());
                if (target >= 0 && target <= 10)
                    ok = true;
            }
            while (!ok); //end of Do While loop
            //*******************************************************************************
            //change element begin
            int ChangeElement = 0;
            do
            {
                Console.WriteLine("What Element do you want to change?[0 - 10]");
                ChangeElement = int.Parse(Console.ReadLine());
                if (ChangeElement > 0 && ChangeElement <= 10)
                    ok = true;
                else
                    Console.WriteLine("The value is not between 0 and 10");
                while (ok == false)
                    Console.WriteLine("Enter the new value for element");
                SearchArray[ChangeElement] = GetInt();
            }

            //change element end
            //Binary Search
            // search sorted array with ascending values
            while (first <= last);
            {
                midpoint = (first + last) / 2; //find midpoint of array
                if (target == SearchArray[midpoint])
                {
                    Console.WriteLine("The target has been found at {0} with a value of {1}", midpoint, SearchArray[midpoint]);
                    Display.ArrayData(SearchArray);
                    break;
                }
                else if (target < SearchArray[midpoint])
                    last = midpoint - 1;
                else
                    first = midpoint + 1;
            } //end of while loop
        }
    }
}
I already explained all the mistakes in your code in my first post, but here goes again.

On line 115 you try to use a GetInt method. I think what you want to use is
int.Parse(Console.ReadLine())
like you did previously.

On line 105 you start a do while loop. Then in the middle of it on line you have the while statement. You should move it to the end of the loop like so.
do
{
//do stuff here
}
while(condition)
If you want to break the loop in the middle you could use something like this this.
while (true)
{
//do stuff here
if (condition) break;
}
Once you've fixed this part, VS will no longer say that line 121 is missing a semicolon.
Was This Post Helpful? 1
  • +
  • -

#7 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 04:24 AM

Thankyou sorry for my misunderstanding for one line i get the error
on line 113
changeElement = int.Parse(Console.ReadLine());


"Int Does not contain a definition for "Parse"

and this is my output which is not right i need to correct this issue

http://gyazo.com/162...45db4d547b69d62

This post has been edited by LawlessBaron: 19 February 2013 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#8 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 04:39 AM

View PostLawlessBaron, on 19 February 2013 - 01:24 PM, said:

Thankyou sorry for my misunderstanding for one line i get the error
on line 113
changeElement = int.Parse(Console.ReadLine());


"Int Does not contain a definition for "Parse"

and this is my output which is not right i need to correct this issue

http://gyazo.com/162...45db4d547b69d62

Are you sure the P in Parse is uppercase?

As for the output, the program is only doing what you told it to do. Try changing your ArrayData method. Something like this might have the desired effect.
void ArrayData(int[] Numbers)
        {
            for (int i = 0; i < Numbers.Length; i++)
            {
                Console.WriteLine(Numbers[i]);
            }
            Console.WriteLine();
        }

Also you're printing out your entire array every time you generate a random value in the initialization loop. You might want to comment out that line.
Was This Post Helpful? 0
  • +
  • -

#9 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 04:48 AM

We were requested to do so within the array and Bubble Sort
Was This Post Helpful? 0
  • +
  • -

#10 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 188
  • Joined: 17-February 13

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 04:55 AM

View PostLawlessBaron, on 19 February 2013 - 01:48 PM, said:

We were requested to do so within the array and Bubble Sort


Alright, well at least you might want to change your ArrayData method like i said.

Here's what i get when i do so.

1000437646
0
0
0
0
0
0
0
0
0

1000437646
2070389898
0
0
0
0
0
0
0
0

1000437646
2070389898
1783643966
0
0
0
0
0
0
0

1000437646
2070389898
1783643966
1113406577
0
0
0
0
0
0

1000437646
2070389898
1783643966
1113406577
912306062
0
0
0
0
0

1000437646
2070389898
1783643966
1113406577
912306062
1916504833
0
0
0
0

1000437646
2070389898
1783643966
1113406577
912306062
1916504833
1017403483
0
0
0

1000437646
2070389898
1783643966
1113406577
912306062
1916504833
1017403483
145629170
0
0

1000437646
2070389898
1783643966
1113406577
912306062
1916504833
1017403483
145629170
595207813
0

1000437646
2070389898
1783643966
1113406577
912306062
1916504833
1017403483
145629170
595207813
1503484628

Bubble Sort initialized
1000437646
2070389898
1783643966
1113406577
912306062
1916504833
1017403483
145629170
595207813
1503484628

145629170
595207813
912306062
1000437646
1017403483
1113406577
1503484628
1783643966
1916504833
2070389898

Bubble Sort completed
please enter search target (between 10 - 50)


I commented out the sequential search part because that didn't seem to do anything at all.
Was This Post Helpful? 0
  • +
  • -

#11 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: Bubble Sort and Initialization not displaying

Posted 19 February 2013 - 06:21 AM

The method is int32.Parse(), not int.Parse. There's also an int16.Parse(). And really, especially reading from the command line, you should be using int32.TryParse(), which will catch invalid entries.

EDIT: I can't get the MSDN int32.TryParse() Method link to work, so here's the url:
msdn.microsoft.com/en-us/library/f02979c7.aspx

This post has been edited by h4nnib4l: 19 February 2013 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1