Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C# help


  • Please log in to reply

#1
Garlet01

Garlet01

    Member

  • Member
  • PipPip
  • 42 posts
I am having trouble doing this in C# anyone want to help me please?

Your mission: 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

Tips

Best practices: 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
  • 0

Advertisements


#2
Garlet01

Garlet01

    Member

  • Topic Starter
  • Member
  • PipPip
  • 42 posts
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);
}
  • 0

#3
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP

Your program will store the names in an ArrayList object.


This is the requirement? :confused: When is this from? earlier 2000's? lol. If this is a modern course they should really update the material, because this would never be suggested anymore. The better alternative is the generic List<T> collection.

In the code that you put in your second post however, is this the solution that you came up with? This is solved?

Console.WriteLine(
"\n{0} names entered."
+ "\nNames in ascending Order\n");

I just want to point out that you're not using that string formatter here... And you're using a List<string> which is good; it's not the ArrayList. That Console.Writeline() should look like this though:
Console.WriteLine(
"\n{0} names entered."
+ "\nNames in ascending Order\n", names.Count);

Edited by AceInfinity, 05 May 2013 - 10:19 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP