reverse array and the index

how can be reversed the array and the index

Page 1 of 1

6 Replies - 1281 Views - Last Post: 04 July 2009 - 09:01 AM Rate Topic: -----

#1 feliang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 09

reverse array and the index

Post icon  Posted 03 July 2009 - 07:46 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace try_again
{
	class Program
	{
		static void Main(string[] args)
		{
			int[] myArray = { 2, 4, 6, 8 };
			Console.WriteLine("original values of myArray");
			showArray(myArray);
			Console.WriteLine("Calling reverse array");
			reverseArray(myArray);
			Console.WriteLine("reverse my array");
			showArray(myArray);
			Console.ReadKey();
		}
		public static void reverseArray(int[] array) 
		{
			//for (int x = array.Length - 1; x >= 0; x--)

			//	Console.WriteLine(myArray[x]);
			
		}
		public static void showArray(int[] array) 
		{
			foreach (int i in array)
				Console.WriteLine(i);
		}
		public static int ReverseArray(int[] array) 
		{
			int[] newArray = new int[array.Length];
			int i = 0;

			for (int x = array.Length - 1; x >= 0; x--) 
			{
				newArray[i] = array[x];
				i++;
			}
			array = newArray;
			return myArray;
		}
		   
	}
}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: reverse array and the index

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: reverse array and the index

Posted 03 July 2009 - 10:35 AM

First and foremost welcome to Dream.In.Code, hope you enjoy your stay with us.

In the future please post your programming questions in the proper language forum (In this case it's C#, I will move this one for you). Also
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: reverse array and the index

Posted 03 July 2009 - 11:25 AM

Try a temporary array for that. And then something like this:

 int[] myArray = { 2, 4, 6, 8 };
			int[] temparray = new int[myArray.Length];
			for (int i = 0; i < myArray.Length; i++)
			{
				temparray[(myArray.Length-1) - i] = myArray[i]; //the -1 is to convert lenght to index
			}


After that you can use a similar process to copy the temp array back to the original array.

But I guess if you wanted to be lazy you could also use the array.reverse method. :P Just look it up on msdn, it can help you a lot in the future to use this as a reference. To help you out now, the usage is Array.Reverse(myArray);

This post has been edited by Renagado: 03 July 2009 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#4 feliang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 09

Re: reverse array and the index

Posted 04 July 2009 - 07:42 AM

View PostRenagado, on 3 Jul, 2009 - 10:25 AM, said:

Try a temporary array for that. And then something like this:

 int[] myArray = { 2, 4, 6, 8 };
			int[] temparray = new int[myArray.Length];
			for (int i = 0; i < myArray.Length; i++)
			{
				temparray[(myArray.Length-1) - i] = myArray[i]; //the -1 is to convert lenght to index
			}


After that you can use a similar process to copy the temp array back to the original array.

But I guess if you wanted to be lazy you could also use the array.reverse method. :P Just look it up on msdn, it can help you a lot in the future to use this as a reference. To help you out now, the usage is Array.Reverse(myArray);

Was This Post Helpful? 0
  • +
  • -

#5 feliang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-June 09

Re: reverse array and the index

Post icon  Posted 04 July 2009 - 07:45 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace try_again
{
	class Program
	{
		static void Main(string[] args)
		{
			int[] myArray = { 2, 4, 6, 8 };
			Console.WriteLine("original array values");
			showArray(myArray);
			Console.WriteLine("calling reverse my array");
			reverseArray(myArray);
			Console.WriteLine("reverse array");
			showArray(myArray);
			Console.ReadKey();

		   

		 }public static void reverseArray(int[]array)
		 {
			 int []newArray=new int[array.Length];
			 int i=0;
			 for (int x=array.Length-1;x>=0;x--)
			 {
				 newArray[i]=array[x];
				 i++;

				
			 }
			 array = newArray;
			 

			


		 }public static void modifiedArray(int[] myArray) 
		 {
			 int[] temparray = new int[myArray.Length];
			 for (int i = 0; i < myArray.Length; i++)
			 {
				 temparray[(myArray.Length - 1) - i] = myArray[i]; //the -1 is to convert lenght to index
				 Console.ReadKey();
			 }
		 }
	  

		public static void showArray(int[]array)
		{
			foreach(int i in array)
				Console.WriteLine(i);

		}
		
		   
	}
}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: reverse array and the index

Posted 04 July 2009 - 08:31 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: reverse array and the index

Posted 04 July 2009 - 09:01 AM

Topics merged, please do not create duplicate topics :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1