Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
QuestionLINQ Query Grouping Pin
eddieangel17-Jan-14 13:20
eddieangel17-Jan-14 13:20 
AnswerRe: LINQ Query Grouping Pin
OriginalGriff17-Jan-14 20:23
mveOriginalGriff17-Jan-14 20:23 
GeneralRe: LINQ Query Grouping Pin
eddieangel20-Jan-14 6:23
eddieangel20-Jan-14 6:23 
QuestionC# tamir sharp ssh, scp incomplete file transfer Pin
Michael Gaida16-Jan-14 6:03
Michael Gaida16-Jan-14 6:03 
AnswerRe: C# tamir sharp ssh, scp incomplete file transfer Pin
Eddy Vluggen18-Jan-14 0:58
professionalEddy Vluggen18-Jan-14 0:58 
GeneralRe: C# tamir sharp ssh, scp incomplete file transfer Pin
Michael Gaida27-Jan-14 2:23
Michael Gaida27-Jan-14 2:23 
QuestionProblem with shuffling array elements and displaying them Pin
Member 1052812316-Jan-14 2:36
Member 1052812316-Jan-14 2:36 
AnswerRe: Problem with shuffling array elements and displaying them Pin
Eddy Vluggen16-Jan-14 3:03
professionalEddy Vluggen16-Jan-14 3:03 
Your Shuffle-loop is incorrect; it starts at (n-1), where n=count. Meaning it will skip one!

Try something like below;
C#
class Program
{
    static void Main(string[] args)
    {
        RunQuestionTwo();
        Console.ReadKey();
    }

    private static void RunQuestionTwo()
    {
        int arraySize;    //declare variables
        int[] a;

        Console.WriteLine("\nTask 2 \nPlease enter an integer to determine the size of the random array\n");

        // what happens when I, as a user, enter the string "one"?
        arraySize = Convert.ToInt16(Console.ReadLine());

        a = new int[arraySize];
        Console.WriteLine("\nThe array will now be displayed\n");

        for (int i = 0; i < a.Length; i++) 
        {
            a[i] = i + 1;
        }

        Shuffle(a);
    }

    public static void Shuffle(int[] a)
    {
        Random rndNumber = new Random();
        int n = a.Length;
        Console.WriteLine(string.Format("n = {0}", n));

        for (int r = 0; r < n; r++)
        {
            int i = rndNumber.Next(1, n);
            int temp = a[i];
            a[i] = a[r];
            a[r] = temp;

            Console.WriteLine(a[r]);
        }
    }
}
You'd also like to verify the users' input and prevent the app from blowing up on 'unexpected' input. I made the methods static, since that's preferred for methods that don't change (local object) state.
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]

GeneralRe: Problem with shuffling array elements and displaying them Pin
Member 1052812317-Jan-14 3:53
Member 1052812317-Jan-14 3:53 
AnswerRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 4:03
professionalBillWoodruff16-Jan-14 4:03 
GeneralRe: Problem with shuffling array elements and displaying them Pin
CPallini16-Jan-14 21:43
mveCPallini16-Jan-14 21:43 
AnswerRe: Problem with shuffling array elements and displaying them Pin
OriginalGriff16-Jan-14 8:31
mveOriginalGriff16-Jan-14 8:31 
GeneralRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 17:33
professionalBillWoodruff16-Jan-14 17:33 
GeneralRe: Problem with shuffling array elements and displaying them Pin
OriginalGriff16-Jan-14 19:51
mveOriginalGriff16-Jan-14 19:51 
AnswerRe: Problem with shuffling array elements and displaying them Pin
BillWoodruff16-Jan-14 19:41
professionalBillWoodruff16-Jan-14 19:41 
AnswerMessage Closed Pin
16-Jan-14 22:43
harold aptroot16-Jan-14 22:43 
GeneralRe: All of the above is actually wrong Pin
CPallini16-Jan-14 23:38
mveCPallini16-Jan-14 23:38 
QuestionDIfference between Q/A and Discussion? Pin
agent_kruger16-Jan-14 1:43
professionalagent_kruger16-Jan-14 1:43 
AnswerRe: DIfference between Q/A and Discussion? Pin
Richard MacCutchan16-Jan-14 2:10
mveRichard MacCutchan16-Jan-14 2:10 
AnswerRe: DIfference between Q/A and Discussion? PinPopular
BillWoodruff16-Jan-14 6:02
professionalBillWoodruff16-Jan-14 6:02 
GeneralRe: DIfference between Q/A and Discussion? Pin
agent_kruger16-Jan-14 23:38
professionalagent_kruger16-Jan-14 23:38 
GeneralRe: DIfference between Q/A and Discussion? Pin
Pete O'Hanlon16-Jan-14 23:55
mvePete O'Hanlon16-Jan-14 23:55 
QuestionHow to use SQL store procedure in LINQ in C#? Pin
Brijesh Kumar Prajapati16-Jan-14 0:09
Brijesh Kumar Prajapati16-Jan-14 0:09 
AnswerRe: How to use SQL store procedure in LINQ in C#? Pin
Eddy Vluggen16-Jan-14 0:17
professionalEddy Vluggen16-Jan-14 0:17 
QuestionApplication requires Microsoft.Vbe.Interop Ver .... installed in GAC Pin
emma.sun.sts15-Jan-14 22:35
emma.sun.sts15-Jan-14 22:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.