Join 309,245 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,644 people online right now. Registration is fast and FREE... Join Now!
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 =>
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
Try a temporary array for that. And then something like this:
CODE
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. 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: 3 Jul, 2009 - 10:31 AM
Try a temporary array for that. And then something like this:
CODE
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. 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);
}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 =>
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