Reference: chapter sections 8.3 and 8.4 (sorting) Chapter sections 8.1 and 8.2 (
ID: 3885624 • Letter: R
Question
Reference: chapter sections 8.3 and 8.4 (sorting) Chapter sections 8.1 and 8.2 (Searching)
USE C++ for this code
A. Create three array of 15+ students records including ID’s, the corresponding e-mail addresses, and the corresponding GPA’s –student ID’s not sorted in order.
B. Sort the above data by student ID’s using bubble sorting.
C. Sequential Search one ID which is from somewhere in the middle of the sorted array and a second ID which is from near the end of the sorted array and one ID is not from the sorted array.
D. Binary Search one ID which is from somewhere in the middle of the sorted array and a second ID which is from near the end of the sorted array and one ID is not from the array.
E. Execution and output:
1. Display the original unsorted array of student records.
2. Display the sorted array result of student records.
3. Display the sequential search result of student records.
4. Display the binary search result of student records.
Explanation / Answer
#include<iostream>
using namespace std;
//structure to store student records
//each record cantains id,email and gpa
int n=5;
struct student
{
int id;
string email;
float gpa;
}arr[5];//this arr will cantain 5 records of the students
//if you want more records,then please increase n value
//Dispaly() function to display the records in the array
void Display(struct student arr[])
{
for(int i=0;i<n;i++)
{
cout<<arr[i].id<<" ";
cout<<arr[i].email<<" ";
cout<<arr[i].gpa<<" ";
cout<<" ";
}
cout<<" ";
}
//Sequential search function
void Sequential(int target,struct student arr[])
{
int found=0;
//iterating from 0 to n
for(int i=0;i<n;i++)
{
if(arr[i].id==target){
cout<<"Found ";
found=1;
break;
}
}
if(found==0)
cout<<"Not found ";
}
//Binary search function
void Binary(int target,struct student arr[])
{
int first=0;
int last=n-1;
int middle=(first+last)/2;
while(first<=last)
{
if(arr[middle].id<target)
first=middle+1;
else if(arr[middle].id==target){
cout<<"Found ";
break;
}
else
last=middle-1;
middle=(first+last)/2;
}
if(first>last)
cout<<"Not found ";
}
//Main method
int main()
{
int target;
//Storing student records
cout<<"Enter Student details: ";
for(int i=0;i<n;i++)
{
cin>>arr[i].id;
cin>>arr[i].email;
cin>>arr[i].gpa;
}
//1.Displaying Original unsorted array
cout<<"Before Sorting ";
Display(arr);
//Sorting using Bubble sort
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[j].id<arr[i].id);
{
//swap id
int temp=arr[j].id;
arr[j].id=arr[i].id;
arr[i].id=temp;
//swap email
string temp1=arr[j].email;
arr[j].email=arr[i].email;
arr[i].email=temp1;
//swap gpa
float temp2=arr[j].gpa;
arr[j].gpa=arr[i].gpa;
arr[i].gpa=temp2;
}
}
}
//2.Displaying the sorted array results
cout<<"After Sorting ";
Display(arr);
//3.Sequential Search results
cout<<" Sequential Search Results: ";
cout<<"Enter ID [Somewhere in the middle] ";
cin>>target;
Sequential(target,arr);
cout<<"Enter ID [Near to end of the array] ";
cin>>target;
Sequential(target,arr);
cout<<"Enter ID [Not from the array] ";
cin>>target;
Sequential(target,arr);
//4.Binary Search results
cout<<" Binary search Results: ";
cout<<"Enter ID [Somewhere in the middle] ";
cin>>target;
Binary(target,arr);
cout<<"Enter ID [Near to end of the array] ";
cin>>target;
Binary(target,arr);
cout<<"Enter ID [Not from the array] ";
cin>>target;
Binary(target,arr);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.