Question

Exercise 1: Write a C++ program that asks the user to input an integer n followed...

Exercise 1: Write a C++ program that asks the user to input an integer n followed by n other characters that will be stored in an array. We suppose here that the user will input only lowercase characters between ‘a’ and ‘z’. We also suppose that the user will input different characters. The program will then sort these characters according to an increasing order of their alphabetical order. In this exercise, feel free to use selection sort, insertion sort, or bubble sort. However, say which algorithm you will use.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

To do the sorting of characters in alphabetic order I have used Bubble Sort Algorithm

C++ Code to sort the character array

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int n;
cout<<"Enter the number of character you want to sort: ";
cin>>n;
// declare the array of size n to store the characters
char char_array [n];
cout<<"Enter the character "<<endl;
for(int i=0;i<n;i++)
{
cin>>char_array[i];
}
cout<<"Characters before sorting: ";
for(int i=0;i<n;i++)
{
cout<<char_array[i]<<" ";
}

//using bubble sort to sort the characters
for (int i = 0; i < n; i++)
   {
for (int j = i+1; j < n; j++)
   {
  
//if previous has bigger ascii value than next,
if (char_array[i] > char_array[j])
   {
     
//swapping the prev and next characters
char temp = char_array[i];
char_array[i] = char_array[j];
char_array[j] = temp;
}
}
}
cout<<"\nCharacters after sorting: ";
for(int i=0;i<n;i++)
{
cout<<char_array[i]<<" ";
}
return 0;
}

Code Screenshots

#include <iostream> #include <string> using namespace std; int main() { int n; cout<<Enter the number of character you want

35 36 } cout<<\nCharacters after sorting: ; for(int i=0;i<n;i++) 37 { 38 39 cout<<char_array[i]<< ; w 40 } return 0; 41 4

Output

C:\Users\Sudhanshu Goel\Desktop\ces3 c programing code.exe Enter the number of character you want to sort: 10 Enter the chara

This is how you can sort the characters in increasing order of their alphabetical order.

If u like the answer then give it a thumbs up and have any doubt comment it

Add a comment
Know the answer?
Add Answer to:
Exercise 1: Write a C++ program that asks the user to input an integer n followed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Write a C++ program that asks the user to input a decimal integer (i) and an...

    Write a C++ program that asks the user to input a decimal integer (i) and an integer radix from 2 to 16 (r) and outputs the value of iin radix r(base r). (HINT: use repeated division by base r) NOTE: show output screenshot

  • Exercise Write a program which asks the user to enter an integer n. It will print...

    Exercise Write a program which asks the user to enter an integer n. It will print a diamond shape if n is negative, AA AAAA AAAAAA AAAA AA a triangle otherwise. AA AMA AAAA AAAAA

  • Write a C program that prompts the user for an input string with all lowercase letters....

    Write a C program that prompts the user for an input string with all lowercase letters. Your program should then sort the characters in the string and print the sorted string. Assume that the string contains no spaces and has at most 30 lowercase characters. Your program should loop continually until the user enters “q” to quit.

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Write a C++ program that asks the user for 50 integer numbers and an integer n....

    Write a C++ program that asks the user for 50 integer numbers and an integer n. The program will show then the pairs of numbers (a,b) where a=nb.

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • The program defined in insertionSort.cpp gets a list of numbers as input from the user and...

    The program defined in insertionSort.cpp gets a list of numbers as input from the user and calls the insertion sort algorithm to sort them. It also displays the list before and after the sorting. The insertion sort algorithm should be defined in the InsertionSort.h file, but this definition has been omitted and it is your task to provide it. The appropriate insertion sort function is to be defined in the InsertionSort.h file. The InsertionSort.h file performs an include of Swap.h,...

  • Write a program that asks the user to input a 4-digit integer and then prints the...

    Write a program that asks the user to input a 4-digit integer and then prints the integer in reverse. Your program needs to implement a function reverse that will take in as input the 4-digit number and print it out in reverse. Your code will consist of two files: main.s and reverse.s. Main.s will ask for the user input number and call the function reverse. For ARM/Data Structure needs to be able to run on Pi Reverse.s will take in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT