Sir plzz help me to solve this error, The below code is work fine but when i replace
for(i=0; i
for(i=1; i<=size; i++)
it shows exception .why?
using System;
public class ArrName
{
public static void Main()
{
string min,temp;
Console.Write("Enter the no. of Application: ");
int size = Convert.ToInt32(Console.ReadLine());
int loc,i,j;
string[] arr = new string[size];
Console.WriteLine("Enter proper name of each Student: ");
for(i = 0; i
{
Console.Write("{0}. ",i);
arr[i] = Console.ReadLine();
}
Console.WriteLine();
Console.ReadLine();
Console.WriteLine("Name After Arrange them in Alphabetical order: ");
for(i = 0; i
{
min = arr[i];
loc = i;
for(j = i+1; j
{
if((arr[j].CompareTo(min))<0 )
{
min = arr[j];
loc = j;
}
if(loc != i)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(i = 0; i
{
Console.WriteLine(" {0}", arr[i]);
}
Console.ReadKey();
}
}
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akhil GargPosted Apr 17, 2015, 1:32 PM
Hari KrishnaPosted Apr 17, 2015, 3:22 AM
Arrays are using for store similar data types grouping as a single unit. We can access Array elements by its numeric index. The array indexes start at zero. The default value of numeric array elements are set to zero, and reference elements are set to null .
see the follwing link:
http://csharp.net-informations.com/collection/csharp-array.htm
Code:
string min="", temp="";
Console.Write("Enter the no. of Application: ");
int size = Convert.ToInt32(Console.ReadLine());
int loc, i, j;
string[] arr = new string[size];
Console.WriteLine("Enter proper name of each Student: ");
for (i = 0; i < size; i++)
{
Console.Write("{0}. ", (i+1));
arr[i] = Console.ReadLine();
}
Console.WriteLine();
//Console.ReadLine();
Console.WriteLine("Name After Arrange them in Alphabetical order: ");
for (i = 0; i < size; i++)
{
min = arr[i];
loc = i;
for (j = i + 1; j < size; j++)
{
if ((arr[j].CompareTo(min)) < 0)
{
min = arr[j];
loc = j;
// Write this logic inside if statement
if (loc != i)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// End
}
}
}
for (i = 0; i < size; i++)
{
Console.WriteLine(" {0}", arr[i]);
}
Console.ReadKey();