[go: up one dir, main page]

0% found this document useful (0 votes)
24 views3 pages

Code

The document contains a C# Windows Forms application for managing student rankings. It allows users to add students with their marks, rank them based on average marks, and clear the student list. The application includes a Student class to store student information and methods for adding, ranking, and clearing students in the Form1 class.

Uploaded by

buck29875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Code

The document contains a C# Windows Forms application for managing student rankings. It allows users to add students with their marks, rank them based on average marks, and clear the student list. The application includes a Student class to store student information and methods for adding, ranking, and clearing students in the Form1 class.

Uploaded by

buck29875
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Form 1.

cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace StudentRankingApp
{
public partial class Form1 : Form
{
private List<Student> students = new List<Student>();

public Form1()
{
InitializeComponent();
UpdateStudentCount();
}

// Add a student to the list


private void btnAddStudent_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string surname = txtSurname.Text.Trim();
int numberOfSubjects = (int)nudSubjects.Value;
List<int> marks = new List<int>();

// Validate input
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(surname))
{
MessageBox.Show("Please enter both name and surname.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

// Collect marks for each subject


for (int i = 1; i <= numberOfSubjects; i++)
{
string markInput =
Microsoft.VisualBasic.Interaction.InputBox(string.Format("Enter mark for subject
{0}:", i), "Enter Marks", "0");
int mark;
if (int.TryParse(markInput, out mark))
{
marks.Add(mark);
}
else
{
MessageBox.Show("Invalid mark entered. Please try again.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}

// Create a new student and add to the list


Student student = new Student(name, surname, numberOfSubjects, marks);
students.Add(student);

// Clear input fields


txtName.Clear();
txtSurname.Clear();
nudSubjects.Value = 1;
// Update the student count
UpdateStudentCount();

MessageBox.Show("Student added successfully!", "Success",


MessageBoxButtons.OK, MessageBoxIcon.Information);
}

// Rank students and display the top N


private void btnRankStudents_Click(object sender, EventArgs e)
{
try
{
students.Sort((a, b) => b.AverageMark.CompareTo(a.AverageMark));
int topN = (int)nudTopN.Value;
lstRankedStudents.Items.Clear();
for (int i = 0; i < topN && i < students.Count; i++)
{
lstRankedStudents.Items.Add(string.Format("{0} {1} - Average:
{2:F2}", students[i].Name, students[i].Surname, students[i].AverageMark));
}
lblStudentCount.Text = string.Format("Number of Students: {0}",
students.Count);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message, "Error");
}
}

// Clear all students from the system


private void btnClearStudents_Click(object sender, EventArgs e)
{
students.Clear();
lstRankedStudents.Items.Clear();
UpdateStudentCount();

MessageBox.Show("All students have been cleared.", "Success",


MessageBoxButtons.OK, MessageBoxIcon.Information);
}

// Update the student count label


private void UpdateStudentCount()
{
lblStudentCount.Text = string.Format("Number of Students: {0}",
students.Count);
}

// Quick Sort Algorithm


private List<Student> QuickSort(List<Student> list)
{
if (list.Count <= 1) return list;
int pivotIndex = list.Count / 2;
double pivot = list[pivotIndex].AverageMark;
List<Student> less = new List<Student>();
List<Student> greater = new List<Student>();

for (int i = 0; i < list.Count; i++)


{
if (i == pivotIndex) continue;
if (list[i].AverageMark >= pivot)
{
greater.Add(list[i]);
}
else
{
less.Add(list[i]);
}
}

List<Student> sorted = QuickSort(greater);


sorted.Add(list[pivotIndex]);
sorted.AddRange(QuickSort(less));

return sorted;
}
}
}

Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentRankingApp
{
public class Student
{
public string Name { get; set; }
public string Surname { get; set; }
public int NumberOfSubjects { get; set; }
public List<int> Marks { get; set; }
public double AverageMark { get; set; }

public Student(string name, string surname, int numberOfSubjects, List<int>


marks)
{
Name = name;
Surname = surname;
NumberOfSubjects = numberOfSubjects;
Marks = marks;
AverageMark = marks.Average();
}
}
}

You might also like