10 Replies - 376 Views - Last Post: 29 April 2013 - 09:52 AM Rate Topic: -----

#1 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

How to swap indexes in an array?

Posted 29 April 2013 - 08:13 AM

Hello,
So im looking for help, ive looked around the internet but really couldnt find the answer to it.
I have to get an input for an array size with random generated numbers. I have done that.
I now need to get two more inputs from the user in order to change the indexes. Im just looking for help, no one needs to give me coding or anything but a guide is what im looking for. this is my coding so far.
 
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);
		string swapindex1;
		string swapindex2;
		int index1;
		Console.WriteLine("Please Enter Index to swap");
		swapindex1 = Console.ReadLine();
     	index1 =  Int32.Parse(index1);
		int temp = randomSizedArray[index1];
		randomSizedArray[index1] = randomSizedArray[index2];
		randomSizedArray[index2] = temp;	
			
		}
			for (int i=0; i < convertedSizeArray; i++) 
{
    Console.WriteLine(randomSizedArray[i] + "");
}
				
	}	
}



Can someone tell me my mistakes please?

Is This A Good Question/Topic? 0
  • +

Replies To: How to swap indexes in an array?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • Joined: 12-December 12

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:08 AM

Your mistake is that you are only asking for one index from the user, and you are getting confused between index1 and swapindex1 (and 2).

This post has been edited by andrewsw: 29 April 2013 - 09:11 AM

Was This Post Helpful? 1
  • +
  • -

#3 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:10 AM

An update: this is what i did so far: Now I get the random numbers and everything except the printing the swapped index array.
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] + "");
	}	

 
}


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • Joined: 12-December 12

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:15 AM

This line:

randomSizedArray[index1] = randomSizedArray[index2];

wipes out the value at index1. You need to store this value in temp firstly.
Was This Post Helpful? 1
  • +
  • -

#5 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:20 AM

Thank youu
it works fine now, but i got a question, how can i make it so that if the user puts something different then 1-99, it gives an error and asks for the input again in the first input?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • Joined: 12-December 12

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:33 AM

View Postmountainkewl, on 29 April 2013 - 04:20 PM, said:

Thank youu
it works fine now, but i got a question, how can i make it so that if the user puts something different then 1-99, it gives an error and asks for the input again in the first input?

You need to ask for the first input within a loop and only exit the loop if the number they enter is between 1 and 99.
Was This Post Helpful? 0
  • +
  • -

#7 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:34 AM

WOuld you give me a link or some help in coding on how to do that please?
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:38 AM

Why? So you can skip the thinking part?
andrewsw has give a good clue go look at there reply again.
Was This Post Helpful? 0
  • +
  • -

#9 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:40 AM

No Adam, im asking for some help, im not trying to get the code from him, just a guide, or help.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,352
  • Joined: 12-December 12

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:50 AM

http://www.dotnetperls.com/loop
http://www.dotnetperls.com/if
Was This Post Helpful? 0
  • +
  • -

#11 mountainkewl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 13

Re: How to swap indexes in an array?

Posted 29 April 2013 - 09:52 AM

Thank you Andrew
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1