Exercise:
Write a C# program to keep
records and perform statistical analysis for a class of 20 students. The
information of each student contains ID, Name, Sex, quizzes Scores (2 quizzes
per semester), mid-term score, final score, and total score.The program will prompt the user to choose the operation of records from a menu as shown below:
========================================================
MENU
========================================================
1. Add student records
2. Delete student records
3. Update student records
4. View all student records
5. Calculate an average of a selected student’s scores
6. Show student who gets the max total score
7. Show student who gets the min total score
8. Find student by ID
9. Sort records by total scores
Enter your choice:1
Note: All students records are stored in an array of structures
Solution:
To make this solution simple and easy to follow, we divide this solution in to different steps:
Step1: Declaring a structure called student to store the records
struct student
{
public string stnumber;
public string stname;
public string sex;
public float quizz1;
public float quizz2;
public float assigment;
public float midterm;
public float final;
public float total;
};
C# structure: student
records application
Menu of choices
Step2: Defining the displaymenu() method to
display the menu. The simple menu provides nine choices from 1 to 9 to work
with the records.static void displaymenu(){
Console.WriteLine("=====================================================");
Console.WriteLine(" MENU ");
Console.WriteLine("=====================================================");
Console.WriteLine(" 1.Add student records");
Console.WriteLine(" 2.Delete student records");
Console.WriteLine(" 3.Update student records");
Console.WriteLine(" 4.View all student records");
Console.WriteLine(" 5.Calculate an average of a selected student's scores");
Console.WriteLine(" 6.Show student who get the max total score");
Console.WriteLine(" 7.Show student who get the min total score");
Console.WriteLine(" 8.Find a student by ID");
Console.WriteLine(" 9.Sort students by TOTAL");
}
C# structure: student
records application
Append record to list
Step3: defining the add(student[] st, ref int
itemcount) method to add a new record to the the array of student objects. This
method takes two arguments. The first argument is the array of student
objects(st) and the second argument is the number of items in the array. The
two arguments are passed by references. For an array, we don't need to use the
ref keyword when we want to pass it by reference. However, we need to use the
ref keyword when we want to pass an argument of primitive type such as int,
float, dobule,etc. When the new item is added the value itemcount variable
increases by 1 that means the number of records in the list increases.//method add/append a new record
static void add(student[] st,ref int itemcount){
Again:
Console.WriteLine();
Console.Write("Enter student's ID:");
st[itemcount].stnumber=Console.ReadLine().ToString() ;
//making sure the record to be added doesn't already exist
if(search(st,st[itemcount].stnumber,itemcount)!=-1){
Console.WriteLine("This ID already exists.");
goto Again;
}
Console.Write("Enter student's Name:");
st[itemcount].stname=Console.ReadLine ().ToString();
Console.Write("Enter student's Sex(F or M):");
st[itemcount].sex=Console.ReadLine().ToString();
Console.Write("Enter student's quizz1 score:");
st[itemcount].quizz1=float.Parse(Console.ReadLine());
Console.Write("Enter student's quizz2 score:");
st[itemcount].quizz2=float.Parse(Console.ReadLine());
Console.Write("Enter student's assigment score:");
st[itemcount].assigment=float.Parse(Console.ReadLine());
Console.Write("Enter student's mid term score:");
st[itemcount].midterm=float.Parse(Console.ReadLine());
Console.Write("Enter student's final score:");
st[itemcount].final=float.Parse(Console.ReadLine());
st[itemcount].total=st[itemcount].quizz1+st[itemcount].quizz2+st[itemcount].assigment+st[itemcount].midterm+st[itemcount].final;
++itemcount; //increase the number of items by one
}
C# structure: student
records application
Show all records in list
Step4: Defining the viewall(student[] st, int
itemcount) method to display the list of all records in the set. To display all
records, we need a while loop to traverse through the array of student objects.
static void
viewall(student[] st,int itemcount)
{
int i = 0;
Console.WriteLine("{0,-5}{1,-20}{2,-5}{3,-5}{4,-5}{5,-5}{6,-5}{7,-5}{8}(column index)", "0", "1", "2", "3", "4", "5", "6", "7", "8");
Console.WriteLine("{0,-5}{1,-20}{2,-5}{3,-5}{4,-5}{5,-5}{6,-5}{7,-5}{8,-5}", "ID", "NAME", "SEX", "Q1", "Q2", "As", "Mi", "Fi", "TOTAL");
Console.WriteLine("=====================================================");
while (i < itemcount)
{
if (st[i].stnumber !=null )
{
Console.Write("{0,-5}{1,-20}{2,-5}", st[i].stnumber, st[i].stname, st[i].sex);
Console.Write("{0,-5}{1,-5}{2,-5}",st[i].quizz1,st[i].quizz2,st[i].assigment);
Console.Write("{0,-5}{1,-5}{2,-5}",st[i].midterm,st[i].final,st[i].total);
Console.Write("\n");
}
i = i + 1;
}
}
{
int i = 0;
Console.WriteLine("{0,-5}{1,-20}{2,-5}{3,-5}{4,-5}{5,-5}{6,-5}{7,-5}{8}(column index)", "0", "1", "2", "3", "4", "5", "6", "7", "8");
Console.WriteLine("{0,-5}{1,-20}{2,-5}{3,-5}{4,-5}{5,-5}{6,-5}{7,-5}{8,-5}", "ID", "NAME", "SEX", "Q1", "Q2", "As", "Mi", "Fi", "TOTAL");
Console.WriteLine("=====================================================");
while (i < itemcount)
{
if (st[i].stnumber !=null )
{
Console.Write("{0,-5}{1,-20}{2,-5}", st[i].stnumber, st[i].stname, st[i].sex);
Console.Write("{0,-5}{1,-5}{2,-5}",st[i].quizz1,st[i].quizz2,st[i].assigment);
Console.Write("{0,-5}{1,-5}{2,-5}",st[i].midterm,st[i].final,st[i].total);
Console.Write("\n");
}
i = i + 1;
}
}
C# structure: student
records application
Find record index
Step5: Defining the search(student[] st, int
itemcount) method to search for the index of a target record. This method is
useful as we need it to find the location of the target record in the array of
student objects. It can help us to make sure the record does exit before we
allow the record for deletion or updating. If the target element is found, the
method returns the index of this element. It return -1, if the target element
is not found in the array.
static int
search(student[] st, string id,int itemcount){
int found =-1;
for (int i = 0; i < itemcount && found==-1; i++)
{
if (st[i].stnumber == id) found=i;
else found=-1 ;
}
return found;
}
int found =-1;
for (int i = 0; i < itemcount && found==-1; i++)
{
if (st[i].stnumber == id) found=i;
else found=-1 ;
}
return found;
}
C# structure: student
records application
Delete record
Step6: Defining the delete(student[] st, ref int
itemcount) method to delete a target record from the array of student objects.
The user will be prompted to enter the id of student record that his/her want
to delete. Then this id will be checked to make sure it does exist in the list.
If the target record or element really exists, the deletion process can be
made. The deletion process starts by checking whether the target record is the
last record, beginning or middle record. If the target record is the last
record in the list, we simply delete the record by supplying it to the
clean(student[] st, int index) method. The last record is the record that has
it index equal to itemcount subtracted by 1. If the target record stays at the
beginning or in the middle of the list, we need to use a loop to allow the
previous element to take over the next element. This process continue until it
reaches the end of the list(itemcount-1). Then the clean() method is called to
clean the last element of the list that should not exit. After the element is
cleaned, the itemcount variable decreases by 1. This means that the number of
elements in the list decreases.
static void
delete(student[] st, ref int itemcount)
{
string id;
int index;
if (itemcount > 0)
{
Console.Write("Enter student's ID:");
id = Console.ReadLine();
index = search(st, id.ToString(),itemcount);
if ((index!=-1) && (itemcount != 0))
{
if (index == (itemcount-1)) //delete the last record
{
clean(st, index);
--itemcount;
Console.WriteLine("The record was deleted.");
}
else //delete the first or middle record
{
for (int i = index; i < itemcount-1; i++)
{
st[i] = st[i + 1];
clean(st, itemcount);
--itemcount ;
}
}
}
else Console.WriteLine("The record doesn't exist. Check the ID and try again.");
}
else Console.WriteLine("No record to delete");
}
{
string id;
int index;
if (itemcount > 0)
{
Console.Write("Enter student's ID:");
id = Console.ReadLine();
index = search(st, id.ToString(),itemcount);
if ((index!=-1) && (itemcount != 0))
{
if (index == (itemcount-1)) //delete the last record
{
clean(st, index);
--itemcount;
Console.WriteLine("The record was deleted.");
}
else //delete the first or middle record
{
for (int i = index; i < itemcount-1; i++)
{
st[i] = st[i + 1];
clean(st, itemcount);
--itemcount ;
}
}
}
else Console.WriteLine("The record doesn't exist. Check the ID and try again.");
}
else Console.WriteLine("No record to delete");
}
static void
clean(student[] st,int index)
{
st[index].stnumber = null;
st[index].stname = null;
st[index].sex = null;
st[index].quizz1 = 0;
st[index].quizz2 = 0;
st[index].assigment = 0;
st[index].midterm = 0;
st[index].final = 0;
st[index].total = 0;
}
{
st[index].stnumber = null;
st[index].stname = null;
st[index].sex = null;
st[index].quizz1 = 0;
st[index].quizz2 = 0;
st[index].assigment = 0;
st[index].midterm = 0;
st[index].final = 0;
st[index].total = 0;
}
C# structure: student
records application
Update record
Step7: Defining the update_rec(struct student
st[], int itemcount) method to update a specified record. The update process
starts by asking the user to input the id of the record to be changed. The id
value is check to make sure it really exists. If it exits the change to the
target record can be made after asking the user to input the new value of the
field that need change.
static void
update(student[] st, int itemcount)
{
string id;
int column_index;
Console.Write("Enter student's ID:");
id=Console.ReadLine();
Console.Write("Which field you want to update(1-7)?:");
column_index=int.Parse(Console.ReadLine());
int index = search(st, id.ToString(),itemcount);
if ((index != -1) && (itemcount != 0))
{
if (column_index == 1)
{
Console.Write("Enter student's Name:");
st[index].stname = Console.ReadLine().ToString();
}
else if (column_index == 2)
{
Console.Write("Enter student's Sex(F or M):");
st[index].sex = Console.ReadLine().ToString();
}
else if (column_index == 3)
{
Console.Write("Enter student's quizz1 score:");
st[index].quizz1 = float.Parse(Console.ReadLine());
}
else if (column_index == 4)
{
Console.Write("Enter student's quizz2 score:");
st[index].quizz2 = float.Parse(Console.ReadLine());
}
else if (column_index == 5)
{
Console.Write("Enter student's assigment score:");
st[index].assigment = float.Parse(Console.ReadLine());
}
else if (column_index == 6)
{
Console.Write("Enter student's mid term score:");
st[index].midterm = float.Parse(Console.ReadLine());
}
else if (column_index == 7)
{
Console.Write("Enter student's final score:");
st[index].final = float.Parse(Console.ReadLine());
}
else Console.WriteLine("Invalid column index");
st[index].total = st[index].quizz1 + st[index].quizz2 + st[index].assigment + st[index].midterm + st[index].final;
}
else Console.WriteLine("The record deosn't exits.Check the ID and try again.");
}
{
string id;
int column_index;
Console.Write("Enter student's ID:");
id=Console.ReadLine();
Console.Write("Which field you want to update(1-7)?:");
column_index=int.Parse(Console.ReadLine());
int index = search(st, id.ToString(),itemcount);
if ((index != -1) && (itemcount != 0))
{
if (column_index == 1)
{
Console.Write("Enter student's Name:");
st[index].stname = Console.ReadLine().ToString();
}
else if (column_index == 2)
{
Console.Write("Enter student's Sex(F or M):");
st[index].sex = Console.ReadLine().ToString();
}
else if (column_index == 3)
{
Console.Write("Enter student's quizz1 score:");
st[index].quizz1 = float.Parse(Console.ReadLine());
}
else if (column_index == 4)
{
Console.Write("Enter student's quizz2 score:");
st[index].quizz2 = float.Parse(Console.ReadLine());
}
else if (column_index == 5)
{
Console.Write("Enter student's assigment score:");
st[index].assigment = float.Parse(Console.ReadLine());
}
else if (column_index == 6)
{
Console.Write("Enter student's mid term score:");
st[index].midterm = float.Parse(Console.ReadLine());
}
else if (column_index == 7)
{
Console.Write("Enter student's final score:");
st[index].final = float.Parse(Console.ReadLine());
}
else Console.WriteLine("Invalid column index");
st[index].total = st[index].quizz1 + st[index].quizz2 + st[index].assigment + st[index].midterm + st[index].final;
}
else Console.WriteLine("The record deosn't exits.Check the ID and try again.");
}
C# structure: student
records application
Average score
Step8: Defining the average(student[] st, int
itemcount) method to calculate the average score of a selected student. The
method alo starts by asking the user to input the id of the target student.
This id is checked to make sure it really exist. The average score can be
calculated by dividing the sum of quizz1 score, quizz2 score, assignment score,
mid-term score, and final score by 5.static void average(student[] st, int itemcount)
{
string id;
float avg=0;
Console.Write("Enter students'ID:");
id = Console.ReadLine();
int index = search(st, id.ToString(),itemcount);
if (index != -1 && itemcount>0)
{
st[index].total = st[index].quizz1 + st[index].quizz2 + st[index].assigment + st[index].midterm + st[index].final;
avg = st[index].total /5;
}
Console.WriteLine("The average score is {0}.", avg);
}
C# structure: student
records application
Min and Max scores
Step9: Defining the showmax(student[] st, int
itemcount) and showmin(student[] st, int itemcount) methods show about the
student who gets the maximum score and the student who gets the minimum score.
To find the highest total core or lowest total core, we need to compare every
total score of each element.static void showmax(student[] st, int itemcount)
{
float max = st[0].total;
int index=0;
Console.WriteLine(itemcount);
if (itemcount >= 2)
{
for (int j = 0; j < itemcount-1; ++j)
if (max < st[j+1].total) {
max = st[j+1].total;
index = j+1;
}
}
else if (itemcount == 1)
{
index = 0;
max = st[0].total;
}
else Console.WriteLine("Not record found!");
if (index != -1) Console.WriteLine("The student with ID:{0} gets the highest score {1}.", st[index].stnumber, max);
}
\
static void
showmin(student[] st, int itemcount)
{
float min = st[0].total;
int index = 0;
if (itemcount >= 2)
{
for (int j = 0; j < itemcount-1; ++j)
if (min > st[j+1].total)
{
min = st[j+1].total;
index = j+1;
}
}
else if (itemcount == 1)
{
index = 0;
min = st[0].total;
}
else Console.WriteLine("No record found!");
if (index != -1) Console.WriteLine("The student with ID:{0} gets the lowest score {1}.", st[index].stnumber, min);
}
//method to find record
static void find(student[] st, int itemcount)
{
string id;
Console.Write("Enter student's ID:");
id=Console.ReadLine();
int index=search(st,id.ToString(),itemcount);
if (index != -1)
{
Console.Write("{0,-5}{1,-20}{2,-5}", st[index].stnumber, st[index].stname, st[index].sex);
Console.Write("{0,-5}{1,-5}{2,-5}", st[index].quizz1, st[index].quizz2, st[index].assigment);
Console.Write("{0,-5}{1,-5}{2,-5}", st[index].midterm, st[index].final, st[index].total);
Console.WriteLine();
}
else Console.WriteLine("The record deosn't exits.");
}
{
float min = st[0].total;
int index = 0;
if (itemcount >= 2)
{
for (int j = 0; j < itemcount-1; ++j)
if (min > st[j+1].total)
{
min = st[j+1].total;
index = j+1;
}
}
else if (itemcount == 1)
{
index = 0;
min = st[0].total;
}
else Console.WriteLine("No record found!");
if (index != -1) Console.WriteLine("The student with ID:{0} gets the lowest score {1}.", st[index].stnumber, min);
}
//method to find record
static void find(student[] st, int itemcount)
{
string id;
Console.Write("Enter student's ID:");
id=Console.ReadLine();
int index=search(st,id.ToString(),itemcount);
if (index != -1)
{
Console.Write("{0,-5}{1,-20}{2,-5}", st[index].stnumber, st[index].stname, st[index].sex);
Console.Write("{0,-5}{1,-5}{2,-5}", st[index].quizz1, st[index].quizz2, st[index].assigment);
Console.Write("{0,-5}{1,-5}{2,-5}", st[index].midterm, st[index].final, st[index].total);
Console.WriteLine();
}
else Console.WriteLine("The record deosn't exits.");
}
No comments:
Post a Comment