|
 |
|
 |
02-11-2005, 04:07 PM
|
#1 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
Practice...
Can anyone present to me some non-trivial ideas on a project I can undergo (console only) to better familiarize myself with C++ (standard).
I've clocked in about 500+ hours in the last year or so with C++, and am just looking on something to further my understanding of C++'s capabilities and features at a more accelerated pace than my OOP class is offering right now.
Thanks in advance to anyone who would be willing to give me some advice/ideas.
|
|
|
02-11-2005, 04:34 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
A little OOD practice:
- Provide an interface to sort an array of std::strings of such nature that:
a) the user is able to sort using BubbleSort or a more advanced sorting routine.
b) the user is able to choose between sorting by ascending order or descending order.
- Use the Template Method to implement the sorting order interface.
- Use the Strategy Pattern to implement the choice between the two sorting algorithms.
- Use the Command Design pattern to create a "Console GUI" so the user can choose with menu's and submenus:
1) BubbleSort -> [ 1) Ascending order, 2) Descending order 3) Back to main menu ]
2) CombSort-> [ 1) Ascending order, 2) Descending order 3) Back to main menu ]
3) Exit
- In main(), just create an array of std::string with leadertypes. E.g.
string sLeaderTypes[] = {"Emperor", "King", "Warlord", "President", ...etcetera};
Use this as a test case.
- Use header and source files.
If you don't know how, we'll do it bit by bit illustrating the problems and its solution.
__________________
|
|
|
02-11-2005, 05:12 PM
|
#3 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
Quote:
|
Originally Posted by Valmont
- Use the Template Method to implement the sorting order interface.
- Use the Strategy Pattern to implement the choice between the two sorting algorithms.
- Use the Command Design pattern to create a "Console GUI" so the user can choose with menu's and submenus:
|
I need clarification on those 3 models, in the meantime I'll get started on what I do know.
|
|
|
02-11-2005, 06:19 PM
|
#4 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
No don't! There is a lesson to be learned.
First write a BubbleSort program. A simple one will do. Then post it before we move on.
__________________
|
|
|
02-11-2005, 07:56 PM
|
#5 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
Here's a basic bubble sort of the sLeaderTypes you wanted, I added a few more to make sure my sort was working correctly. Which it seems to be....
Code:
#include <string>
#include <iostream>
using namespace std;
#define SIZE 100
void bubbleSort(string array[8]);
void swap(string *, string *);
int main()
{
int i=0, count=9;
string sLeaderTypes[] = {"Emperor","King","Tyrant","President","Warlord","Prince","Baron","Queen","Dictator"};
cout << endl << "Data items in original order" << endl;
for (i = 0; i < count; i++)
cout <<sLeaderTypes[i] << endl;
bubbleSort(sLeaderTypes); // sort the array
cout <<"Sorted list:";
for (i = 0; i < count; i++)
cout <<endl << sLeaderTypes[i];
return 0;
}
void bubbleSort(string array[8])
{
int pass, size = 9;
for (pass = 1; pass <= size - 1; pass++)
for (int j = 0; j <= size - 2; j++)
if ((array[j].compare(array[j + 1]) > 0))
swap(array[j], array[j + 1]);
}
void swap(string *element1Ptr, string *element2Ptr)
{
string temp;
temp = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = temp;
}
|
|
|
02-11-2005, 10:32 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
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.
|
|
|
02-12-2005, 01:26 PM
|
#7 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
I'll get started on the template and the two sorts as soon as I can, I have to work most weekend and start an assignment for school. I have a quick question though,
Code:
void bubbleSort(string* array, const size_t size);
void print_array(string* array, const size_t size);
What is the usage of size_t? excluding the size (leaving just const size_t) asks for a definition of size_t, but I have never seen anyone use this before. Thanks Valmont,
|
|
|
02-12-2005, 03:28 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
On my system std::size_t is defined as an unsigned long int.
__________________
|
|
|
02-12-2005, 04:57 PM
|
#9 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
Thanks.
|
|
|
02-16-2005, 02:44 PM
|
#10 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
Stuck
I've implemented a class (kind of) to deal with the BubbleSort. I have a few problems which I need addressed.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//String sort class
class stringarray
{
private:
string temp; //used by swap
string lhs; //used by swap
string rhs; //used by swap
public:
stringarray(){temp = "";lhs = "";rhs = "";} //Constructor
~stringarray() {} //Destructor
void bubbleSort(string* array, const size_t size); //Sort method
void swap(string& lhs, string& rhs){ temp=lhs; lhs = rhs; rhs = temp; } //Swap method
void printArray(string* array, const size_t size); //Print method
}sortthem;
//Main
void main()
{
string terminate = "q";
int i;//Iterator
//Declare initial string size
string RandomSize[50];
const size_t arrSize = sizeof RandomSize/ sizeof(*RandomSize);
cout <<"Enter string or press 'q' to quit" << endl;
//Prompt for input until user quits
for (i=0;i<50;i++)
{
cout <<"String" << i+1 << ": ";
cin >> RandomSize[i];
if (RandomSize[i].length() == 1 && RandomSize[i].compare(terminate))
{
break;
system("pause");
}
}
//Sort array and Print to screen
cout << endl << "Bubble Sorted:" << endl;
sortthem.bubbleSort(RandomSize, arrSize);
sortthem.printArray(RandomSize, arrSize);
}
//BubbleSort
void stringarray::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]);
}
}
}
//PrintArray
void stringarray::printArray(string* array, const size_t size)
{
for (int i = 0; i < size; i++)
{
while(array[i] == "")
i++;
cout <<array[i] << endl;
}
}
The things I could not figure out were:
1) I have declared an array of 50 strings, inputting until Q is reached (only capital right now). After I hit Q to terminate the strings, it sorts Q as if it were an inputted string.
I will get started on the sort by ascending or descending as soon as I get a chance. My lab class is over...
|
|
|
02-16-2005, 05:06 PM
|
#11 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Wait a minute...
I've asked you to do two things:
1) Change my Sorter into a template. E.g.:
template<typename T>
void bubble_sort(....) { }
2) Implement a feature so I (as a programmer) can choose between ascending or descending bubble sort.
And one restriction:
1) Use my code as the base code.
I never asked for a string array. I never asked for a "void main()". I never asked for user input. In fact I explicitly said you should use the std::string sLeaderArray! Don't go beyond what is asked.
I am trying to demonstrate something. I want to do it in such a way, where you can be proud on your own achievements. That means that you will build the things I want, the way I want. I let you think. You will build, and most likely fail. I let you think again. And you will learn and re-build. After two weeks of hard work, you will never forget this course and you will be able to operate like a mature individual when it comes to design and C++. After that, we can have a laugh and analyze the good and the bad things we encountered on our way. But for now, the goal is to let you "see". So stay on it.
Pretend this course costs you or your employer $300 per day...
- Val -
__________________
Last edited by Valmont; 02-16-2005 at 05:35 PM.
|
|
|
02-16-2005, 08:36 PM
|
#12 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
ALF_Bass, send me the solution(s) by pm or to my email adddy. This is to prevent cheating with fp_unit. I will post (by some timing) your solution(s) and then we'll discuss it.
__________________
|
|
|
02-17-2005, 07:12 AM
|
#13 (permalink)
|
|
Learning C++
Join Date: Feb 2005
Location: Ottawa
Posts: 10
|
will do as soon as i finish.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:51 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|