Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
My array is like:
11  22  33  44  55  66  77  88  99  110


and the swapping is to be done like this
22  11  44  33  66  55  88  77  110  90


ASP.NET C# console application,
so can anyone please help me for this

C#
class Program
    {
        static void Main()
        {
            int i,n;

            //Enter Number of elements in an array
            Console.WriteLine("Enter no array elements : ");
            n = Convert.ToInt32(Console.ReadLine());



            // Enter elements to an array
            int[] arr = new int[n];           
            Console.WriteLine("Enter the array elements : ");
            for(i=0; i<arr.length;>            {
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }


            //Display Array Elements
            Console.WriteLine("Array Elements are :");
            for (i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i] + "\t");
            }


            //Logic Starts From Here
            if (arr.Length == 1)
            {
                Console.WriteLine("Too Les Items to be shorted");
            }
            else
            {
                int start, end, mid;
                start = arr[0];
                end = arr[n-1];
                Console.WriteLine("Start and End are " + start + "\t  and \t" + end);

                for (i = 0; i < n - 1; i++)
                {
                    arr[n - 1] = arr[i];
                }

                Console.WriteLine(" NEWArray Elements are :");
                for (i = 0; i < arr.Length; i++)
                {
                    Console.WriteLine(arr[i] + "\t");
                }
                ;
            }

            Console.ReadKey();

        }
    }


What I have tried:

ASP.NET C# console application,
so can anyone please help me for this
Posted
Updated 29-Jun-16 14:10pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-16 20:30pm    
What have you tried so far?
—SA
Patrice T 28-Jun-16 21:10pm    
How 99 become 90 after swap ?
Dave Kreskowiak 29-Jun-16 20:24pm    
Nobody has said this yet, but there is no such thing as a "ASP.NET Console" application.

Those are two entirely different application types. ASP.NET applications run on a web server and server up web pages. Console applications run in a CMD window. You're talking about a Console application.

Try this
C#
// swap
int tmp;
for (i = 0; i < arr.Length- 1; i+=2)
{
    tmp= arr[i + 1];
    arr[i + 1] = arr[i];
    arr[i]= tmp;
}
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 30-Jun-16 2:22am    
what is n ?
could you post a complete solution.

btw, congrats Polymorphe for 50K
Karthik_Mahalingam 30-Jun-16 2:22am    
.
Patrice T 30-Jun-16 3:49am    
Ooops, correction done.
Thank you :)
Karthik_Mahalingam 30-Jun-16 3:53am    
5 for simpler solution.
Patrice T 30-Jun-16 3:55am    
Thank You.
int[] arr = new int[10];
int i;
Console.WriteLine("Enter the array elements : ");
for(i=0; i<arr.length;> {
Console.WriteLine(arr[i] + "\t");
}
====================

then after
start = a[0];
end = a[n-1];

for(i=0; i<n-1;>{
a[n-1] = a[i];
}

is it the right way..?
 
Share this answer
 
Comments
Patrice T 28-Jun-16 21:37pm    
Is this a solution or not ?
Use Improve question to update your question.
try this

C#
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;


class Program
{
    static void Main(string[] args)
    {
        int[] input = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 110 };
        int[] output = new int[10];
        int count = 0;
        for (int i = 0; i < input.Length; i++)
        {
            count++;
            if (count == 2)
            {
                output[i - 1] = input[i];
                output[i] = input[i - 1];
                count = 0;
            }
        }

        Console.WriteLine(" Output  :");
        for (int i = 0; i < output.Length; i++)
            Console.WriteLine(output[i] + "\t");

        Console.ReadKey();
    }
}
 
Share this answer
 
Comments
Patrice T 29-Jun-16 20:13pm    
Your code for swapping look pretty complicated, see Solution 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900