-
April 29th, 2013, 11:02 AM
#1
Array swapped index
Hello,
I have to do an array where the user inputs it, and it shows it with random generated numbers, and then the user puts 2 numbers for the indexes to swap.
so far i've got this;
Code:
using System;
class MainClass
{
public static void Main (string[] args)
{
int[] randomSizedArray;
string sizeOfArray;
int convertedSizeArray = -1;
Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);
;
randomSizedArray= new int[convertedSizeArray];
Random rnd = new Random();
for (int i=0; i < convertedSizeArray; i++) {
randomSizedArray[i] = rnd.Next(1,99);
}
for (int i=0; i < convertedSizeArray; i++)
{
Console.WriteLine(randomSizedArray[i] + "");
}
string swapindex1;
string swapindex2;
int index1;
int index2;
Console.WriteLine("Please Enter Index to swap");
swapindex1 = Console.ReadLine();
index1 = Int32.Parse(swapindex1);
int temp = randomSizedArray[index1];
Console.WriteLine ("Please Enter a Second Value to swap");
swapindex2 = Console.ReadLine();
index2 = Int32.Parse(swapindex2);
randomSizedArray[index1] = randomSizedArray[index2];
randomSizedArray[index2] = temp;
Console.WriteLine(randomSizedArray[temp] + "");
}
}
I think i got the whole thing right except the printing out for the swapped indexes so if anyone can please help and guide me i would appreciate it.
Last edited by BioPhysEngr; April 29th, 2013 at 09:28 PM.
Reason: add code tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|