VKS-LEARNING HUB

Home » Solved Structure Based Question

Solved Structure Based Question

void bubblesortroll(student arr[], int n)

{

Function to sort an array of student on roll using bubble sort.

for(int x=1; x<n; x++)

for(int k=0; k<n-x; k++)

if (arr[k].roll>arr[k+1].roll)

{

student t=arr[k];

arr[k]=arr[k+1];

arr[k+1]=t;

}

}

Function to sort an array of student on roll using selection sort.

void selectionsortroll(student arr[], int n)

{

for(int x=0; x<n-1; x++)

{

student min=arr[x];

int pos=x;

for(int k=x+1; k<n; k++)

if (arr[k].roll<min.roll)

{

min=arr[k];

pos=k;

}

arr[pos]=arr[x];

arr[x]=min;

}

}

Function to sort an array of student on roll using insertion sort.

void insertionsortroll(student arr[], int n)

{

for(int x=1; x<n; x++)

{

student t=arr[x];

int k=x-1;

while(k>=0 && t.roll<arr[k].roll)

{

arr[k+1]=arr[k];

k–;

}

arr[k+1]=t;

}

}

Function to locate for a roll in an array of student using linear search. Return value of the function is int. If search is successful function returns value 1 otherwise 0.

int linearsearchroll(student arr[], int n, int roll)

{

int x=0, found=0;

while (x<n && found==0)

if (roll==arr[x].roll)

found=1;

else

x++;

return found;

}

Function to locate for a roll in an array of student using linear search. Return value of the function is void. Status of search is displayed in the function.

void linearsearchroll(student arr[], int n, int roll)

{

int x=0, found=0;

while (x<n && found==0)

if (roll ==arr[x].roll)

found=1;

else

x++;

if (found==1)

cout<<roll<<” Found in the array\n”;

else

cout<<roll<<” Does not Exist in the array\n”;

}

Function to locate for a roll in a sorted array of student using binary search. Return value of the function is int. If search is successful function returns value 1 otherwise 0.

int binarysearchroll(student arr[], int n, int roll)

{

int lb=0, ub=n-1;

int found=0, mid;

while (lb<=ub && found==0)

{

mid=(ub+lb)/2;

if (roll<arr[mid].roll)

ub=mid-1;

else

if (roll>arr[mid].roll)

lb=mid+1;

else

found=1;

}

return found;

}

void binarysearchroll(student arr[], int n, int roll)

Function to locate for a roll in a sorted array of student using binary search. Return value of the function is void. Status of search is displayed in the function.

{

int lb=0, ub=n-1;

int found=0, mid;

while (lb<=ub && found==0)

{

mid=(ub+lb)/2;

if (roll<arr[mid].roll)

ub=mid-1;

else

if (roll>arr[mid].roll)

lb=mid+1;

else

found=1;

}

if (found==1)

cout<<roll<<” Found in the array\n”;

else

cout<<roll<<” Does not Exist in the array\n”;

}

void bubblesortname(student arr[], int n)

{

for(int x=1; x<n; x++)

for(int k=0; k<n-x; k++)

if (strcmp(arr[k].name, arr[k+1].name)>0)

Function to sort an array of student on name using bubble sort.

{

student t=arr[k];

arr[k]=arr[k+1];

arr[k+1]=t;

}

}

void selectionsortname(student arr[], int n)

Function to sort an array of student on name using selection sort.

{

for(int x=0; x<n-1; x++)

{

student min=arr[x];

int pos=x;

for(int k=x+1; k<n; k++)

if (strcmp(arr[k].name, min.name)<0)

{

min=arr[k];

pos=k;

}

arr[pos]=arr[x];

arr[x]=min;

}

}

void insertionsortname(student arr[], int n)

Function to sort an array of student on name using insertion sort.

{

for(int x=1; x<n; x++)

{

student t=arr[x];

int k=x-1;

while(k>=0 && strcmp(t.name, arr[k].name)<0)

{

arr[k+1]=arr[k];

k–;

}

arr[k+1]=t;

}

}

Function to locate for a roll in an array of student using linear search. Return value of the function is int. If search is successful function returns value 1 otherwise 0.

int linearsearchroll(student arr[], int n, int roll)

{

int x=0, found=0;

while (x<n && found==0)

if (arr[x].roll==roll)

found=1;

else

x++;

return found;

}

Function to locate for a name in an array of student using linear search. Return value of the function is void. Status of search is displayed in the function.

void linearsearchname(student arr[], int n, char name[])

{

int x=0, found=0;

while (x<n && found==0)

if (strcmp(name, arr[x].name)==0)

found=1;

else

x++;

if (found==1)

cout<<name<<” Found in the array\n”;

else

cout<<name<<” Does not Exist in the array\n”;

}

Function to locate for a roll in a sorted array of student using binary search. Return value of the function is int. If search is successful function returns value 1 otherwise 0.

int binarysearchroll(student arr[], int n, int roll)

{

int lb=0, ub=n-1, found=0, mid;

while (lb<=ub && found==0)

{

mid=(ub+lb)/2;

if (arr[mid].roll>roll)

ub=mid-1;

else

if (arr[mid].roll<roll)

lb=mid+1;

else

found=1;

}

return found;

}

Function to locate for a name in a sorted array of student using binary search. Return value of the function is void. Status of search is displayed in the function.

void binarysearchname(student arr[], int n, char name[])

{

int lb=0, ub=n-1, found=0, mid;

while (lb<=ub && found==0)

{

mid=(ub+lb)/2;

if (strcmp(name, arr[mid].name)<0)

ub=mid-1;

else

if (strcmp(name, arr[mid].name)>0)

lb=mid+1;

else

found=1;

}

if (found==1)

cout<<name<<” Found in the array\n”;

else

cout<<name<<” Does not Exist in the array\n”;

}

Function to merge two arrays of student sorted on roll to obtain the third array also sorted on roll. All three arrays are sorted in ascending order on roll.

void mergeroll(student a[],student b[],student c[],int n1,int n2)

{

int i=0, j=0, k=0;

while (i<n1 && j<n2)

if (a[i].roll<b[j].roll)

c[k++]=a[i++];

else

c[k++]=b[j++];

while (i<n1)

c[k++]=a[i++];

while (j<n2)

c[k++]=b[j++];

}

Function to merge two arrays of student sorted on name to obtain the third array also sorted on name. All three arrays are sorted in ascending order on name.

void mergename(student a[],student b[],student c[],int n1,int n2)

{

int i=0, j=0, k=0;

while (i<n1 && j<n2)

if (strcmp(a[i].name,b[j].name)<0)

c[k++]=a[i++];

else

c[k++]=b[j++];

while (i<n1)

c[k++]=a[i++];

while (j<n2)

c[k++]=b[j++];

}

Function to insert student in an array of student. Constant MAX is the size of the array. Array name, number of elements currently in the array, position for insertion and the student that is to be inserted are passed as parameters to the function.

void arrinsert(student arr[], int& n, int pos, student item)

{

if (n==MAX)

cout<<“Overflow\n”;

else

{

for (int x=n-1; x>=pos; x–)

arr[x+1]=arr[x];

arr[pos]=item;

n++;

cout<<item.roll<<‘,'<<item.name<<‘,'<<item.mark;

cout<<” inserted in the array\n”;

}

}

Function to delete from an array of student. Array name, number of elements currently in the array and position for deletion are passed as parameters to the function.

void arrdelete(student arr[], int& n, int pos)

{

if (n==0)

cout<<“Underflow\n”;

else

{

student item=arr[pos];

for (int x=pos+1; x<n; x++)

arr[x-1]=arr[x];

n–;

cout<<item.roll<<‘,'<<item.name<<‘,'<<item.mark;

cout<<” deleted from the array\n”;

}

}


Leave a comment