CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2013

    Array swapped index

    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;
    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

  2. #2
    Join Date
    Feb 2011
    United States

    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.
    Best Regards,

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

Click Here to Expand Forum to Full Width