Question

Write a program in C# to display the list of items in the array according to...

Write a program in C# to display the list of items in the array according to the length of the string in ascending order and the original order. The array should contain the following data “ROME, LONDON, NAIROBI, CALIFORNIA, ZURICH, NEW DELHI, AMSTERDAM, HOUSTON, PARIS". The output should be as follow

Here is the arranged list:                                                                                  

ROME                                                                                                       

PARIS                                                                                                      

LONDON                                                                                                       

ZURICH

HOUSTON                                                                                            

NAIROBI                                                                                                      

AMSTERDAM                                                                                                    

NEW DELHI                                                                                                    

CALIFORNIA

Here is the original list

ROME

LONDON

NAIROBI

CALIFORNIA

ZURICH

NEW DELHI

AMSTERDAM

HOUSTON

PARIS

Homework Answers

Answer #1

If you have any doubts, please give me comment...

using System;

class Excercise {

    public static void Main () {

        string[] items = { "ROME", "LONDON", "NAIROBI", "CALIFORNIA", "ZURICH", "NEW DELHI", "AMSTERDAM", "HOUSTON", "PARIS" };

        string[] sortedItems = (string[]) items.Clone ();

        for (int i = 0; i < sortedItems.Length; i++) {

            for (int j = 0; j < sortedItems.Length-i-1; j++) {

                int len1 = sortedItems[j].Length;

                int len2 = sortedItems[j+1].Length;

                if (len1 > len2 || (len1==len2 && sortedItems[j].CompareTo(sortedItems[j+1])>0)) {

                    string temp = sortedItems[j];

                    sortedItems[j] = sortedItems[j+1];

                    sortedItems[j+1] = temp;

                }

            }

        }

        Console.WriteLine ("Here is the arranged list:");

        for (int i = 0; i < sortedItems.Length; i++) {

            Console.WriteLine (sortedItems[i]);

        }

        Console.WriteLine ("Here is the original list:");

        for (int i = 0; i < items.Length; i++) {

            Console.WriteLine (items[i]);

        }

    }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
An array of characters contains a few letters. Write a complete C program that will display...
An array of characters contains a few letters. Write a complete C program that will display the output as shown below. Lets assume the array contains =”abcde”. The program should be able to work with any array size, configurable in the program. Expected output Original array = [ a b c d e] a a b a b c a b c d a b c d e
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
Write a program that reads an integer, a list of words, and a character. The integer...
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle z then...
Answer question for C# 1 of 10 When items are added or removed, a list resizes...
Answer question for C# 1 of 10 When items are added or removed, a list resizes itself. True False Question 2 of 10 What happens when you assign one list to another using the assignment operator? An error message generates. Elements copy to new memory locations. Both lists become value-type data elements. Both lists reference the same memory locations. Question 3 of 10 Which of the following is a valid example of calling a method and sending it an array...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...