Jump to content
Sign in to follow this  
NetRepTodd

Array Reversal

Recommended Posts

NetRepTodd    200

I'm writing in c# using VS2012 as my IDE.  

 

My goal is to write a function that essentially would reverse an array in place.  Sure, I could use a library or built in function, however I'm studying for an exam and one thing I should know how to do is to write one without using libraries, built in functions, or a second array.  

 

When compiled, my console displays the following:

[spoiler].dnuorg a ni gniyalp si eH

woooooo woooooo woooo

 

System.Int32[][/spoiler]

 

I want to actually display the new output of the array reversed.  Here is the code i've written:

[spoiler]

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

namespace ConsoleApplication1
{
    class ReverseString
    {
        public static void Main(string[] args)
        {
            //This code reverses the following text
            //
            string Name = "He is playing in a ground.";
            char[] characters = Name.ToCharArray();
            StringBuilder sb = new StringBuilder();
            for (int i = Name.Length - 1; i >= 0; --i)
            {
                sb.Append(characters[i]);
            }
            /****/

            //This value array is what I want to display in reverse order
            //
            int[] value = { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            
            //This calls the function I wrote to reverse the array.
            Reverse(value);

            /****/

            //This code below tries to reverse the following text.
            //This code works fine.
            //
            string RicFlairSaysWoo = "oooow oooooow oooooow";
            char[] flairSyndrome = RicFlairSaysWoo.ToCharArray();
            StringBuilder stringBuild = new StringBuilder();
            for (int i = RicFlairSaysWoo.Length - 1; i >= 0; --i)
            {
                stringBuild.Append(flairSyndrome[i]);
            }
            
            //The following below displays my results to the command line.
            Console.Write(sb.ToString() + "\n" + stringBuild.ToString() + "\n" + "\n");
            Console.Write(value);
            Console.Read();
        }

        //This code should be reversing my array.
        static void Reverse(int[] value)
        {
            int length = value.Length;

            for (int i = 0; i < length; i++)
            {
                int tempVar = value[i]; 
                value[i] = value[length - i - 1]; 
                value[length - i - 1] = tempVar;
            }

        }
    }
}

 

[/spoiler]

 

 

If this is difficult to read, I can break it up into sections for you. Thanks in advance for any help given.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×