Construct a program that sorts a list numbers or strings the

Construct a program that sorts a list (numbers or strings) then searches for an element in the list. You can ask the user to enter elements for your list or create the list within your program. Also, allow the user to enter an item to search for. (Please use comments)

Solution

#include<iostream>
using namespace std;
int main( )
{

int list1[10],size,i,j,temp,element;
int flag=0;

cout<<\"Enter the no. of elements:\"<<endl;
cin>> size;
cout<<\"Enter the array elements:\"<<endl;
for(i=0; i< size; i++) //for loop to enter the numbers
cin>>list1[i];
cout<<\"The array entered before sort is\"<<endl;
for(i=0;i<size;i++)
cout<<list1[i]<<\" \";
//code to sort the array
for( i=0; i< size; i++)
{
for(j=i; j< size-1; j++)
{
if(list1[i]> list1[j+1] )
{
temp= list1[i];
list1[i]= list1[j+1];
list1[j+1]= temp;
}
}
}

cout<<\"The array after sort is \"<<endl;
for(i=0;i<size;i++)
cout<<list1[i]<<\" \";
cout<<\"Enter the element to be searched in the array\"<<endl;
cin>>element;
//search code
for(i=0;i<size;i++)
{
if(list1[i]==element)
flag=1; //changing the value of flag to 1 if the element is found
break;
}
if(flag==1)
cout<<\"the element is found in the array\"<<endl;
return 0;
}

Construct a program that sorts a list (numbers or strings) then searches for an element in the list. You can ask the user to enter elements for your list or cre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site