Jump to content

Programming Code:

Aaaaaaaaa
 Write a program to alphabetize a list of last names. The user will input an undetermined number of last names. The program will display the number of names entered and alphabetized lists of the names in ascending (A-Z) and descending (Z-A) order.
 
Your program will store the names in an ArrayList object. It will use various ArrayList properties and methods to implement the program requirements.
Sample output:
 
Enter a last name: Roberts
Keep going? (Y/N): y
Enter a last name: DeLay
Keep going? (Y/N): y
Enter a last name: Foreman
Keep going? (Y/N): y
Enter a last name: Ganguly
Keep going? (Y/N): n
 
4 last names entered
 
Names in Ascending Order
 
DeLay
Foreman
Ganguly
 
Names in Descending Order
 
Roberts
Ganguly
Foreman
DeLay
 
 
Don't try to write too much at a time! First, write an outline in comments based on the requirements and the pseudocode. Then, work on instantiating an ArrayList object, followed by implementing the loop that will get user input. As always, keep things simple and implement incrementally. Review the list of ArrayList methods and properties in the textbook and experiment with the ones you think you'll need.
Pseudocode
 
Main function
Instantiate ArrayList object
Loop to get last names, until user wants to quit
Add each last name to the ArrayList
Display the count of the last names
Sort the ArrayList
Loop to display the names
Reverse the order of the ArrayList
Loop to display the names
 
 
Above is the question^
 
 
 
 
Below is what I have started working on but the names wont process in ascending and descending order. Please help me???
 
 
 
 
            var names = new List<String>();
 
            do
            {
                Console.Write("\nEnter name: ");
                names.Add(Console.ReadLine());
                Console.Write("Keep going? (y/n)");
            } while (Console.ReadKey().KeyChar == 'y');
 
            Console.WriteLine(
                  "\n{0} names entered."
                + "\nNames in ascending Order\n");
 
 
            names.Sort();
            foreach (var name in names)
            {
                Console.WriteLine(name);
            }
 
            Console.WriteLine("\nNames in descending Order\n");
 
            names.Reverse();
            foreach (var name in names)
            {
                Console.WriteLine(name);
 
 

 

Link to comment
Share on other sites

Link to post
Share on other sites

<marquee> master race :D

The Beast: CPU: i7 4790K GPU: EVGA 1080 SC Cooling: Dual NZXT Kraken x61 RAM: HyperX Fury 1866MHz Storage: SSD: 500GB Samsung EVO 840 + HDD: 1TB WD MOBO: Asus Z97 - a PSU: RM850x Case: H440 green-black Setup: Link PCPP: Link Evolution: Link 

Gear: PS4 with custom skin // Astro A50 Xbox edition to fit colour scheme // Oppo PM-3 Planar Magnetic Closed Back Headphones // OnePlus One 64GB sandstone

Other stuffs: Acer aspire 128GB SSD 10GB RAM // MacBook Pro 13" 500GB SSD 16GB RAM // A 2009 iMac 21"

 

Link to comment
Share on other sites

Link to post
Share on other sites

Try this instead of Sort() :

names = names.OrderBy(x => x).ToList();

// TODO: Update signature to include PC buid.

Link to comment
Share on other sites

Link to post
Share on other sites

//Initialize names listvar names = new List<String>();do{    //Get new name    Console.Write("\nEnter name: ");    names.Add(Console.ReadLine());    //Ask to continue    Console.Write("Keep going? (y/n)");}while(Console.ReadKey().KeyChar == 'y'); //Diaplay name countConsole.WriteLine("\n\n\n{0} names entered.", names.Count); //Sort names ascendingnames.Sort();//Loop through list and display namesConsole.WriteLine("\n\nNames in ascending Order: ");foreach(var name in names){    Console.WriteLine(name);} //Reverse the listnames.Reverse();//Loop through list and display namesConsole.WriteLine("\n\nNames in descending Order: ");foreach(var name in names){    Console.WriteLine(name);}//Pause console while user displays resultsConsole.ReadKey();

Thank You So Much!

Link to comment
Share on other sites

Link to post
Share on other sites

You had it all done perfectly fine with just some minor errors.

Ohh Okay! I knew I was on the right track, just was not sure.

Link to comment
Share on other sites

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

×