Thread: Practice...
View Single Post
Old 02-11-2005, 10:32 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
if ((array[j].compare(array[j + 1]) > 0))
            swap(array[j], array[j + 1]);
We are not allowed to use STL. We build everything up from scratch.

1) Don't pass by pointer if not needed. Pass by reference.
2) Observe how I cleaned up main() by adding a print() function.
3) Observe how to calculate the size of an array:
the size of the whole array divided by the size of a single element results in the total number of elements.
We don't want unneeded local (or global) variables laying around
4) Do not use #define var xxx. Next time if needed do this:
const unsigned myConstVariableName = 12 //whatever;
5) Pass by const when appropriate.
6) It is not "Sorted list" (in print code). We have no list. We have an array. There is a distinct difference between them. We will take notice about arrays only.

In general, observe how I cleaned up the code. This is the code we are going to use.

Next 2 tasks:
- Make the Bubble sorter a template so we can sort any array.
- Make it so I can choose between sorting by ascending order or sorting by descending order.
You need to demonstrate how to use your features the way you implemented it.
In general, me - as a client - will buy the library you are making. I need to know how to use your library.
Yes, pretend you are making a product and I am a potential client who wishes to code with the libraries that you built.


This is in order of difficulty. The template task is the easy one. The other is the first step of our actual (learning) project.
Remember, coding is communicating. Make sure any stranger - who doesn't have a clue about how you think - can understand the code. Use proper names. Think about them first. Change it later if you're not happy with the name.
In a few hundred lines of code it doesn't matter. In 80,000 per person in a project, it does!

Failing is an option. That is the whole purpose. So do not be affraid to say you can't do it. I do this for a living. So relax and see how you can do this.

Code:
#include <iostream>
#include <string>

using namespace std;

void bubbleSort(string* array, const size_t size);
void swap(string&, string&);
void print_array(string* array, const size_t size);

int main(int argc, char *argv[])
{
  string  sLeaderTypes[] =
  { "Emperor","King","Tyrant","President","Warlord","Prince",
    "Baron","Queen","Dictator"
  };

  const size_t arrSize = sizeof sLeaderTypes/ sizeof(*sLeaderTypes);
  cout << endl << "Data items in original order:" << endl;
  print_array(sLeaderTypes, arrSize);
  cout<<endl;

  bubbleSort(sLeaderTypes, arrSize );

  cout <<"Sorted Array:";
  print_array(sLeaderTypes, arrSize);
  cout<<endl;

  return 0;
}

//------------------------------------------------- void bubbleSort(string* array, const size_t size)
{
  for(int i = size; i>=0; --i)
  {
    for(int j= 0 ; j<size-1 ; j++)
    {
      if (array[j] > array[j + 1] )
      swap(array[j], array[j + 1]);
    }
  }
}

//------------------------------------------------- void swap(string& lhs, string& rhs)
{
   string temp;
   temp = lhs;
   lhs = rhs;
   rhs = temp;
}

//------------------------------------------------- void print_array(string* array, const size_t size)
{
  for (int i = 0; i < size; i++)
  {
    cout <<array[i] << endl;
  }
}
__________________

Last edited by Valmont; 02-16-2005 at 08:38 PM.
Valmont is offline   Reply With Quote