C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

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!




reverse array and the index

 

reverse array and the index, how can be reversed the array and the index

feliang

3 Jul, 2009 - 06:46 AM
Post #1

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 3

CODE

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.gif

Thanks,
PsychoCoder smile.gif

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Reverse Array And The Index

3 Jul, 2009 - 09:35 AM
Post #2

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 15,047



Thanked: 523 times
Dream Kudos: 11700
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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


User is offlineProfile CardPM
+Quote Post

Renagado

RE: Reverse Array And The Index

3 Jul, 2009 - 10:25 AM
Post #3

D.I.C Head
Group Icon

Joined: 14 Jun, 2009
Posts: 190



Thanked: 17 times
Dream Kudos: 100
My Contributions
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. tongue.gif 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
User is offlineProfile CardPM
+Quote Post

feliang

RE: Reverse Array And The Index

4 Jul, 2009 - 06:42 AM
Post #4

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 3

QUOTE(Renagado @ 3 Jul, 2009 - 10:25 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. tongue.gif 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);


User is offlineProfile CardPM
+Quote Post

feliang

RE: Reverse Array And The Index

4 Jul, 2009 - 06:45 AM
Post #5

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 3

CODE

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.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Reverse Array And The Index

4 Jul, 2009 - 07:31 AM
Post #6

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 15,047



Thanked: 523 times
Dream Kudos: 11700
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
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

User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Reverse Array And The Index

4 Jul, 2009 - 08:01 AM
Post #7

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 15,047



Thanked: 523 times
Dream Kudos: 11700
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Topics merged, please do not create duplicate topics smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/26/09 09:39AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month