Using C++ the sample BUBBLESORT program with CHARACTERS attached above: Enter 5
ID: 656084 • Letter: U
Question
Using C++
the sample BUBBLESORT program with CHARACTERS attached above:
Enter 5 words (does not have to be a phrase or sentence..you SHOULD use some digits, caps, and special characters like punctuation. Have each of these words including punctuation entered into a character array. See my sample bubblechar output. Have the array take up to 80 characters.
Print out the original unsorted data (sentence or phrase)
Print out the data sorted (ascending)
Print out the data sorted (descending)
Print out the Largest and Smallest input value by ASCII code
Print out all characters that appear more than once and give their frequency.
ALSO separately print out the character(s) that occur the most!
If NO characters appear more than once indicate that in a message.
Sample output:
suppose input is: This class will be fun!
Original Unsorted Data: This class will be fun!
Sorted List: !Tabcefhiilllnsssuw (ascending)
Sorted List: wusssnllliihfecbaT! (descending)
Largest: w
Smallest: !
FREQUENCY
i 2
l 3
s 3
l appears the most with 3 occurrences
s appears the most with 3 occurrences
Explanation / Answer
#include <iostream.h>
void input(char ulist[26], int& n);
void Bubblesort(char ulist[26],char slist[26], int n);
void DBubblesort(char ulist[26],char slist[26], int n);
void print(char list[26], int n);
void Largest(char unlist[26],int n)
{
int i,j, temp=unlist[0];
for(i=1;i<=n;i++)
if(unlist[i]>temp)
{
temp=unlist[i];
}
cout<<" largest:"<<temp;
}
void Smallest(char unlist[26],int n)
{
int i,j, temp=unlist[0];
for(i=1;i<=n;i++)
if(unlist[i]<temp)
{
temp=unlist[i];
}
cout<<" smallest:"<<temp;
}
void count(char s[], int counts[], int n)
{
for (int i = 0; i < 26; i++)
{
counts[i] = 0;
}
for (int i = 0; i < n; i++)
{
{
s[i] = tolower(s[i]);
counts[s[i] - 'a'] ++;
}
}
cout<<" No.of OCcurences of each character in the input ";
for (int i = 0; i < 26; i++)
{
if (counts[i]!=0)
cout << counts[i] << " " << static_cast<char>(i + 'a') << endl;
else
cout << "Zero occurences "<< static_cast<char>(i + 'a') << endl;
}
}
void main()
{
char ulist[26],slist[26];
int counts[26];
input(ulist,n);
cout<<"Unsorted data";
print(ulist,n);
cout<<" Sorted data in Ascending";
Bubblesort(ulist,slist,n);
print(slist,n);
cout<<" Sorted data in descending";
DBubblesort(ulist,slist,n);
print(slist,n);
Largest(ulist,n);
Smallest(ulist,n);
count(ulist, counts,n);
}
void input(char ulist[26], int &n)
{
int i=0;
char value;
cout<<"Enter The characters:";
cin>>value;
while(i<25 && value!='#')
{
i++;
ulist[i]=value;
if(i<25)
cin>>value;
}
n=i;
}
void DBubblesort(char unlist[26], char sortlist[26],int n)
{
int i,j, temp;
for(i=1;i<=n;i++)
sortlist[i]=unlist[i];
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(sortlist[i]<sortlist[j])
{
temp=sortlist[i];
sortlist[i]=sortlist[j];
sortlist[j]=temp;
}
}
void Bubblesort(char unlist[26], char sortlist[26],int n)
{
int i,j, temp;
for(i=1;i<=n;i++)
sortlist[i]=unlist[i];
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
if(sortlist[i]>sortlist[j])
{
temp=sortlist[i];
sortlist[i]=sortlist[j];
sortlist[j]=temp;
}
}
void print(char list[26], int n)
{
int i;
cout<<"list of charcters in ASCII mode";
for(i=1;i<=n;i++)
cout<<list[i]<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.