Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 23rd, 2007, 11:03 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get and Display Array

I am trying to get and display array..but look like there is problem
when it asks for Enter the numbers it inserts "0" at everyline why it is happening? help me . Thanks

 public static void getArray(int[] arr)
        {

                int i;
                for(i = 0; i < arr.Length; i++)
                {
                   Console.Write("Enter the numbers to be sorted:");
                    Console.ReadLine();
                    Console.Write(arr[i]);
                    arr[i]++;
                    Console.WriteLine();

                }

            }


        public static void displayArr(int[] arr)
        {

            foreach (Object j in arr)
            {

                Console.Write("Sorted:" + j.ToString());


            }
        }


Reply With Quote
  #2 (permalink)  
Old March 24th, 2007, 07:31 AM
Registered User
 
Join Date: Mar 2007
Location: , , Netherlands.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When you call: Console.Write(arr[i]); you write the value of 'arr' at point 'i' on your console application .
if 'arr' is a new array of ints, all the values are 0. when using Console.Write(), you don't assing values to a point in an array.


try this:

int[] arr = new int[10];

public static void getArray()
{

     int i = 0
     Console.WriteLine("Enter the numbers to be sorted:");

     while (i < arr.Lenght)
     {
          int val = Console.ReadLine();
          arr[i] = val;
          Console.WriteLine("");
          i += 1;
     }

}

public static void displayArr()
{

     foreach(int j in arr)
     {
          Console.Write("Sorted: + j.ToString());
     }

}


if this isn't what you mean, and you only want to get rid of the '0' every time, remove the line:
Console.Write(arr[i]);




Reply With Quote
  #3 (permalink)  
Old March 27th, 2007, 09:20 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a million for the solution. This worked

Reply With Quote
  #4 (permalink)  
Old March 27th, 2007, 09:23 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now i have another problem when i get an array using

getArray(int [] arr)
for (i = 0; i < arr.Length; i++)
                {

                    String val = Console.ReadLine();
                    arr[i] = Convert.ToInt32(val);
}

 public static void quicks(int lo, int hi, int[] arr)
        {
            int i = lo;
            hi = arr.Length;
            int j = hi;
            int pivot;
            pivot = arr[(lo + hi) / 2];

            if (lo >= hi)
            {
                return;
            }
            Console.WriteLine(pivot);

                do
                {
                    while (arr[i] < pivot)
                    i++;
                    while (arr[j] > pivot) // when it comes here it says Array
                                          // out of bound problem
                    j--;

                    if (i < j)
                    {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                    }

            } while(i < j ); // end of do condition

            if (lo < j) quicks(lo, j,arr);
            if (i < hi) quicks(i, hi,arr);
Please help me in correcting quicksort function

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
div element in array won't display onload linus9 BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 5 November 6th, 2008 02:08 AM
display byte array of pdf in jsp mahendra.w JSP Basics 2 September 23rd, 2007 11:23 PM
display my database in an array knightneo PHP Databases 2 December 3rd, 2006 01:25 AM
Display of 2 dimensional array numeric data oldrocketdog Visual Basic 2005 Basics 0 December 6th, 2005 10:39 AM



All times are GMT -4. The time now is 03:48 AM.


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