C exercises and solutions: view records in list


C structure: view records in list

Step 4: Defining the search(struct st[], char id[], int itemcount) function to search for the index of a target record. This function 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 exist before we allow the record for deleting or updating. If the target element is found, the function returns the index of this element. It return -1, if the target element is not found in the array.


//function to find the location of the record
int search(student st[], char id[],int itemcount){
  int found =-1,i;

  for (i = 0; i < itemcount && found==-1; i++)
  {

    if (strcmp(st[i].stnumber,id)==0) found=i;

    else found=-1 ;
  }

return found;
}

Step 5: Defining the viewall(student st[], int itemcount) function 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.



//function to view all records
void viewall(student st[], int itemcount){
 int i=0;
 displayheading();
 while(i<itemcount){
   if(st[i].stnumber!=""){
    printf("%-5s",st[i].stnumber);
    printf("%-17s",st[i].stname);
    printf("%-5c",st[i].sex);
    printf("%-6.1f",st[i].quizz1);
    printf("%-6.1f",st[i].quizz2);
    printf("%-6.1f",st[i].assigment);
    printf("%-6.1f",st[i].midterm);
    printf("%-7.1f",st[i]. final);
    printf("%-4.1f",st[i].total);
    printf("\n");
   }
   i=i+1;

}


}

void displayheading(){
   printf("ID NAME SEX Q1 Q2 As Mi Fi TOTAL \n");
   printf("==========================================n");
}


}


Comments




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.