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 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Hari Krishna93 reputationPosted Apr 16, 2015, 11:22 PM EDT
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();
Akhil GargRank #772 · 3K reputationPosted Apr 17, 2015, 9:32 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts