I am writing a program that takes in data for student info with a student ID # field, student first name field and student last name field. The user will input the data for each student (up to 20 students) or until the user enters '999' for the student ID field. Next I would like to sort the student info into two separate buckets based on the last name field.
I am having an issue with the buckets separating correctly. I will use the CompareTo method to compare the last name strings for the student last name array but when I print the buckets they are mixed up. For example, the last names starting with A-K should go into a 'low values' bucket and last names starting with J-Z should go into a 'high values' bucket.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _123_Assignment2
{
using System;
using static System.Console;
class Program
{
struct student
{
public int studentId;
public string firstName;
public string lastName;
};
static void Main(string[] args)
{
student[] studentInfo = new student[20];
string[] bucketLow = new string[20];
string[] bucketHigh = new string [20];
int x = 0;
int y = 0;
int z = 1;
WriteLine("Enter student ID number:");
studentInfo[x].studentId = Convert.ToInt32(ReadLine());
while (studentInfo[x].studentId != 999)
{
WriteLine("Enter first name:");
studentInfo[x].firstName = ReadLine();
WriteLine("Enter last name:");
studentInfo[x].lastName = ReadLine();
x++;
WriteLine("Enter student ID number:");
studentInfo[x].studentId = Convert.ToInt32(ReadLine());
}
for (int j = 0; j < x; j++)
{
if(studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0)
bucketLow[y] = studentInfo[j].lastName;
else
bucketHigh[y] = studentInfo[j].lastName;
y++;
z++;
}
WriteLine("Unsorted Table:");
for (int j = 0; j < studentInfo.Length; j++)
{
WriteLine("{0}{1}{2}",studentInfo[j].studentId,studentInfo[j].firstName,
studentInfo[j].lastName);
}
WriteLine("Bucket 1:");
for (int j = 0; j < x; j++)
{
WriteLine(bucketLow[j]);
}
WriteLine("Bucket 2:");
for (int j = 0; j < x; j++)
{
WriteLine(bucketHigh[j]);
}
}
}
}
I believe I am not writing the CompareTo method correctly, I tried sorting from both the beginning and end of the array respectively and keep getting the same results?
I do not think that you sorting works (the j and z comparisons):
studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0
Try to simplify your bucket sort - if you know that your buckets are A-K and J-Z maybe you should replace the part of:
for (int j = 0; j < x; j++)
{
if(studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0)
bucketLow[y] = studentInfo[j].lastName;
else
bucketHigh[y] = studentInfo[j].lastName;
y++;
z++;
}
}
Try something like:
for (var i = 0; i < studentInfo.Length; i++)
{
if (studentInfo[i].lastName[0] <= 'K')
bucketLow[y] = studentInfo[i].lastName;
else
bucketHigh[y] = studentInfo[i].lastName;
y++;
}
(And of course add some check that you have a valid input of at least 1 character and so on...)