Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 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
 
Old March 23rd, 2007, 11:03 AM
Registered User
 
Join Date: Mar 2007
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());


            }
        }


 
Old March 24th, 2007, 07:31 AM
Registered User
 
Join Date: Mar 2007
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]);




 
Old March 27th, 2007, 09:20 AM
Registered User
 
Join Date: Mar 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a million for the solution. This worked

 
Old March 27th, 2007, 09:23 AM
Registered User
 
Join Date: Mar 2007
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






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





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