Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Creat a project Read a file with names into an array. Sort the array using the m

ID: 3554703 • Letter: C

Question

Creat a project

Read a file with names into an array. Sort the array using the most efficient algorithm you have at your disposal. Your program needs to give the user 2 options, print the sorted list of names, or allow tell the user whether a specified name exists in the list. Once again, use the most efficient search algorithm you have at your disposal. Allow the user to specify multiple operations (lookups, sorted list prints) on the array before exiting the program.

Here's a sample file:

https://cluster16-files.instructure.com/courses/1806~119/files/1806~2488/course%20files/Source/Data/People_8.txt?download=1&inline=1&sf_verifier=68bedfbde6df49ba0d2a2a77ac3ae52c&ts=1397499295&user_id=18060000000000186

Look at the first line of the data file. It doesn't look like a name. It's actually the field name for the name field. The field name is part of the file header, which we will talk more about later. For now, just skip the field header, we don't want to include it in our list of names.

Explanation / Answer

Using bubble sort algorithm to sort the array and print the elements in array

#include<iostream>
using namespace std;
class bubble
{
public :
//Array of integers to hold values
int arr[20];
//Number of elements in array
int n;
//Function to accept array elements
void read()
{
while(1)
{
cout<<" Enter the number of elements in the array:";
cin>>n;
if(n<=20)
break;
else
cout<<" Array can have maximum 20 elements ";
}
//display the header
cout<<" ";
cout<<"---------------------- ";
cout<<"Enter array elements ";
cout<<"---------------------- ";
//Get array elements
for( int i=0; i<n ;i++ )
{
cout<<"<"<<i+1<<"> ";
cin>>arr[i];
}
}
//Bubble sort function
void bubblesort()
{
for( int i=1;i<n ;i++ ) //for n-1 passes

{

//In pass i,compare the first n-i elements
//with their next elements
for( int j=0; j<n-1; j++)
{
if(arr[j] > arr[j+1])
{
int temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void display()
{
cout<<endl;
cout<<"---------------------- ";
cout<<"Sorted array elements ";
cout<<"---------------------- ";
for( int j=0; j<n; j++)
cout<<arr[j]<<endl;
}
};
int main()
{
//Instantiate an instance of class

bubble list;

// Function call to accept array elements

list.read();

// Function call to sort array

list.bubblesort();

//Function call to display the sorted array

list.display();
return 0;
}