Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write and run a C++ program that has a 20-element database (array) called dB[ ] with the elements declared as shown below:

C++
const int NUM_ELE = 20;
char dB[NUM_ELE] = { 'x','b','A','X','w','T','M','e','n','Z','z','d','Q','L','K','j','S','v','q','R'};


Provide a function called bub_sort() that uses a bubble (exchange) sort to reorder the elements (capitals in ascending alphabetic order first, then lower-case letters in ascending alphabetic order.) This function must track the total number of moves in reordering the array and provide that number in the function "return." The function must have dB[ ] passed to it (by reference) along with the number of elements in the database (by value). AFTER the function is called in main(), display the total number of moves required and show the reordered array for each of the 20 elements (indicate the INDEX number for each element). Be sure to properly document your program and provide a descriptive display.



I got the alphabetical orders, but how do I count the total number of moves?

What I have tried:

C++
#include<iostream>                       //library needed
using namespace std;					 //Library location

int bubbleSort(char[], char);

int main()									////Driver function header
{
	const int NUM_ELE = 20; 					// variable declaration
	char dB[NUM_ELE] = { 'x','b','A','X','w','T','M','e','n','Z','z','d','Q','L','K','j','S','v','q','R'};
	int i, moves;
	char temp;


	// title display
	cout<< 	"	This program sort the the 20 elements in alphabetic order"<<endl;

	for(int i = 0; i < NUM_ELE; i++)
		for(int j = 0; j < NUM_ELE-1; j++)
		{
			if(dB[j+1] < dB[j])
			{
				temp = dB[j];
				dB[j] = dB[j+1];
				dB[j+1] = temp;
			}
		}

	for(int i = 0; i < NUM_ELE; i++)
		cout<<dB[i] <<" ";

	return 0;
}
Posted
Updated 29-Feb-16 16:37pm
v4
Comments
Patrice T 28-Feb-16 18:41pm
   
Are you saying that you can program a bubble sort but you are unable to count the moves ?
Is it your code ? It don't respect your assignment.

you have a variable called 'moves' .. ok, that needs to be initialised to 0

then think about where the action is happening in your bubble sort (swapping elements) - isnt that a good place to increment 'moves' ?

lastly you can print (or whatever) the contents of the variable 'moves' after (or before) you write the sorted array to the console
   
Comments
CPallini 29-Feb-16 12:46pm
   
My 5.
Did you write this code ?
The code don't respect the requirement of the assignment. It don't use a function nor count the moves.
How can you program a bubble sort and be unable to count the moves ? it is the easy part of the requirement.

Looks like you have a good skill at finding things on internet, but you follow the wrong track.

I recommend you to do your assignments by yourself. Solving these problems will help you in the process of learning programming. Your fails are helping your teacher to spot your weakness and apply remedial actions.
When you don't understand something, ask your teacher.

By the way the program is poorly coded and do about 2 times the amount of work necessary to do the job.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900