Answer to Question #273710 in C# for chi

Question #273710

3.3. Write a program that produces the given sequence numbers (in alternate arrangement and reverse order using the three looping statements: 5, 1, 4, 2, 3, 3, 2, 4, 1, 5,


1st Solution – using for loop

2nd Solution – using while loop

3rd Solution-­‐ using do while


1
Expert's answer
2021-11-30T18:40:54-0500
using System;
using System.Text;

namespace ConsoleAppProduceSiquense
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 5;
            int[] siquense;
            int[] resultArray;
            siquense = ProduceSiquenseUsingForLoop(n);
            resultArray = AddReflectionToArray(siquense);
            Console.WriteLine($"The sequence using a 'for' loop is {ArrayToCommaSeparatedString(resultArray)}.");

            siquense = ProduceSiquenseUsingWhileLoop(n);
            resultArray = AddReflectionToArray(siquense);
            Console.WriteLine($"The sequence using a 'while' loop is {ArrayToCommaSeparatedString(resultArray)}.");

            siquense = ProduceSiquenseUsingDoWhileLoop(n);
            resultArray = AddReflectionToArray(siquense);
            Console.WriteLine($"The sequence using a 'do while' loop is {ArrayToCommaSeparatedString(resultArray)}.");

            Console.ReadKey();
        }
        static int[] AddReflectionToArray(int[] siquense)
        {
            int[] reverseSiquense = new int[siquense.Length];
            siquense.CopyTo(reverseSiquense, 0);
            Array.Reverse(reverseSiquense);
            int[] resultArray = new int[siquense.Length + reverseSiquense.Length];
            Array.Copy(siquense, resultArray, siquense.Length);
            Array.Copy(reverseSiquense, 0, resultArray, siquense.Length, reverseSiquense.Length);
            return resultArray;
        }
        static string ArrayToCommaSeparatedString(int[] siquense)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (int i in siquense)
            {
                stringBuilder.Append(i + ",");
            }
            stringBuilder.Remove(stringBuilder.Length - 1, 1);
            return stringBuilder.ToString();
        }
        static int[] ProduceSiquenseUsingForLoop(int n)
        {
            int[] siquense = new int[n];
            int currentReverse = n;
            int currentDirect = 1;
            for (int i = 0; i < n; i++)
            {
                if (i % 2 == 0)
                {
                    siquense[i] = currentReverse--;
                }
                else
                {
                    siquense[i] = currentDirect++;
                }
            }
            return siquense;
        }
        static int[] ProduceSiquenseUsingWhileLoop(int n)
        {
            int[] siquense = new int[n];
            int currentReverse = n;
            int currentDirect = 1;
            int i = 0;
            while (i < n)
            {
                if (i % 2 == 0)
                {
                    siquense[i] = currentReverse--;
                }
                else
                {
                    siquense[i] = currentDirect++;
                }
                i++;
            }
            return siquense;
        }
        static int[] ProduceSiquenseUsingDoWhileLoop(int n)
        {
            int[] siquense = new int[n];
            int currentReverse = n;
            int currentDirect = 1;
            int i = 0;
            do
            {
                if (i % 2 == 0)
                {
                    siquense[i] = currentReverse--;
                }
                else
                {
                    siquense[i] = currentDirect++;
                }
                i++;
            }
            while (i < n);
            return siquense;
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS