C exercises and solutions: delete record

C structure: delete record

Step 6: Defining the delete(student st[], int *itemcount) function 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 wants 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(struct 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 continues 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.

//function to delete record
void delete_rec(student st[], int *itemcount){
char id[10];
int index,i;
if (*itemcount > 0)
printf("Enter student's ID:");
index = search(st, id,*itemcount);

if ((index!=-1) && (*itemcount != 0))
if (index == (*itemcount-1)) //delete the last record

clean(st, index);

printf("The record was deleted.\n");
else //delete the first or middle record
for (i = index; i < *itemcount-1; i++)
st[i] = st[i + 1];
clean(st, *itemcount);
--(*itemcount) ;


else printf("The record doesn't exist.Check the ID and try again.\n");

else printf("No record to delete\n");
//function to clean deleted record
void clean(student st[],int index)

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;



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.