-
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.
-
Re: Array swapped index
I don't clearly understand what you want to do here. You want to display the indices the user specified? Or you want to show what's at the array after the swap? Or you want to show the whole array again after the swap?
Also, welcome to the forum. In the future, if you add [code] and [/code] tags around your code, it will preserve the formatting (and increase the probability of a reply). I'd made the modification for you to your first post.