Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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