Provide me with the code and a screenshot that it works. Please give small steps
ID: 3887768 • Letter: P
Question
Provide me with the code and a screenshot that it works.Please give small steps/explanations as to why you came to the conclusion, do not copy and past off google.
I will provide instructions and steps, please read carefully.
Input must make a large array To store the characters, COUNT it. Enter a string of data, as you read each character in , get a character BUBBLE SoRT dio 2817 Projects Bubble Sort 1Bubble Sort Bubble Sont.cpp 1 #includedostream 2 #define SIZE20 3 using namespace std; 5 //Prints the array to screen. 6 void printArray (char array[1) ou will het- fostrerm in th; for (int i-e; i
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
//This function prints the array
void printArray(char array[])
{
int i;
//Loop for iterating through the array
for( i=0;i<SIZE;i++)
{
printf("%c ",array[i]);
if((i+1)%10 == 0)
{
printf(" ");
}
}
//This function is used for displaying the output at the standard output.
//This is used in place if cout of c++
printf(" ");
}
//Function for performing the bubble sort operation on the given array
void bubbleSort(char array[])
{
int i,j;
for(i=0;i<SIZE-1;i++)
{
for(j=0;j<SIZE;j++)
{
//comparing the jth and the (j+1)th element
if(array[j]>array[j+1]) //just change the sign to < if you want to sort in descending order
{
//performing swapping
char temp = array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
int main()
{
char array[SIZE];
int i;
printf("Enter the %d digits : ",SIZE);
//loop for accepting the input from the user
for(i=0;i<SIZE;i++)
{
//This function is used for accepting the input from the standard input.
//This function is used in place of cin
scanf("%c",&array[i]);
fflush(stdin);
}
//prints the unsorted array on the screen
printf("This is the original data, unsorted: ");
printArray(array);
//calls the method to sort the array
bubbleSort(array);
//prints the sorted data to the screen
printf("This is the sorted data: ");
printArray(array);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.