Click here to Skip to main content
15,887,485 members
Home / Discussions / C#
   

C#

 
GeneralRe: Delete an edited answer Pin
agent_kruger18-Jan-14 18:18
professionalagent_kruger18-Jan-14 18:18 
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 
I have tried to write a program that takes in an array size, fills an array with random elements with each element being used only once. Elements must also be greater than 1 but below the array size. However, when I set the array size as 5, the console only shows me 4 elements!

This happens with all sizes entered, it always shows me 1 element too few! The shuffling is working correctly, however. Can anyone tell me how I can get it to display as many elements as the array size is set to?

Example, if the user enters 5 as the array size, the console should enter 5 random numbers (2,3,5,6,1), but instead enters 4(5,1,2,3)




C#
private 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");      //ask user to input an integer for the size of the array
        arraySize = Convert.ToInt16(Console.ReadLine());        //read in user's input

        a = new int[arraySize];

        Console.WriteLine("\nThe array will now be displayed\n");

        for (int i = 0; i < a.Length; i++)                      //use a for loop to fill in the array
        {

            a[i] = i + 1;
        }

        Shuffle(a);
}

public void Shuffle(int[] a)
{
    Random rndNumber = new Random();
    int n = a.Length();
    while (n > 1)
    {
        n--;
        int i = rndNumber.Next(1, n);
        int temp = a[i];
        a[i] = a[n];
        a[n] = temp;

        Console.WriteLine(a[n]);
    }

}

AnswerRe: Problem with shuffling array elements and displaying them Pin
Eddy Vluggen16-Jan-14 3:03
professionalEddy Vluggen16-Jan-14 3:03 
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 

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.