C# exercises and solutions-find record index


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;

}



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.