Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:That algorithm is unrelated. It creates random numbers which are normally distributed with a certain mean and standard deviation. I just made it today. It uses the central limit theory, a theory my maths teacher said was practically useless. :lol:
Probably why he used the qualifier practically :P
Matthew wrote:Checking the size of an array? You should store the array in a structure with an integer keeping the size.
It's C++, we call them classes :lol: As for the suggestion, I fail to see how the complexities of a class have entered a sorting method.

I have decided to give up (in a way) on quicksort as the standardized sorting method is very fast as it is, and if any efficiency is honestly obtained, it won't be worth the time it took to develop the function. I will keep it as a side project though :)
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Structures aren't complex. They are like classes but have different syntax and less features (No methods). They simply combine different piece of data together. To store the size of an array with an array, a structure is required.

I should have said useless practically. Aka. There is no practical use for it.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:Structures aren't complex. They are like classes but have different syntax and less features (No methods). They simply combine different piece of data together. To store the size of an array with an array, a structure is required.
I meant more a less that I wasn't expecting them to come into play for a simple function. I finally got the hang of pointers/free store/ect so that should be less trouble.

I spent today playing with virtual functions and came up with this incredibly useless piece of code:

Code: Select all

#include <iostream>
#include <string>
using namespace std;

class Animal
{
    public:
        Animal() {cout << "Animal Constructor...\n";}
        virtual ~Animal() {cout << "Animal Destructor...\n";}
        virtual void Speak() {cout << "I am an animal\n";}
        virtual void Feed() {cout << "Thank you for the food\n";}
        virtual void Pet() {cout << "Thank you for the petting\n";}
};

class Pig : public Animal
{
    public:
        Pig() {cout << "Pig Constructor...\n";}
        virtual ~Pig() {cout << "Pig Destructor...\n";}
        virtual void Speak() {cout << "Oink!\n";}
        virtual void Feed() {cout << "*Crunch* *Crunch* *Crunch*...\n";}
        virtual void Pet() {cout << "*Grumble*\n";}
};

class Horse : public Animal
{
    public:
        Horse() {cout << "Horse Constructor...\n";}
        virtual ~Horse() {cout << "Horse Destructor...\n";}
        virtual void Speak() {cout << "Naaay!\n";}
        virtual void Feed() {cout << "*Russle* *Russle*...\n";}
        virtual void Pet() {cout << "*Shwishhhh*\n";}
};

class Chicken : public Animal
{
    public:
        Chicken() {cout << "Chicken Constructor...\n";}
        virtual ~Chicken() {cout << "Chicken Destructor...\n";}
        virtual void Speak() {cout << "Bawkak!\n";}
        virtual void Feed() {cout << "*Peck* *Peck* *Peck* *Peck*...\n";}
        virtual void Pet() {cout << "*Crrrr*\n";}
};

class SMALL_SIZE
{
    public:
        SMALL_SIZE() {cout << "SMALL_SIZE Constructor...\n";}
        ~SMALL_SIZE() {cout << "SMALL_SIZE Destructor...\n";}
};

int main()
{
    Animal *ptr;
    SMALL_SIZE useless;
    int choice;
    string animal;

    cout << "\nAnimal = " << sizeof(Animal) << " bytes\n";
    cout << "Pig = " << sizeof(Pig) << " bytes\n";
    cout << "Horse = " << sizeof(Horse) << " bytes\n";
    cout << "Chicken = " << sizeof(Chicken) << " bytes\n";
    cout << "SMALL_SIZE = " << sizeof(SMALL_SIZE) << " bytes\n\n";

    cout << "What type of animal would you like?\n";
    cout << "Pig<1>\nHorse<2>\nChicken<3>";
    cout << "\n> ";
    cin >> choice;

    switch (choice)
    {
        case 1:
            cout << "\n";
            ptr = new Pig;
            animal = "pig";
            break;
        case 2:
            cout << "\n";
            ptr = new Horse;
            animal = "horse";
            break;
        case 3:
            cout << "\n";
            ptr = new Chicken;
            animal = "chicken";
            break;
        default:
            cout << "\n";
            ptr = new Animal;
            animal = "animal";
            break;
    }

    cout << "\nWhat would you like to do with your " << animal << "?\n";
    cout << "Listen to it<1>\nFeed it<2>\nPet it<3>";
    cout << "\n> ";
    cin >> choice;

    switch (choice)
    {
        case 1:
            cout << "\n";
            ptr->Speak();
            break;
        case 2:
            cout << "\n";
            ptr->Feed();
            break;
        case 3:
            cout << "\n";
            ptr->Pet();
            break;
        default:
            cout << "\nWrong input!\n";
    }
    cout << "\n";
    delete ptr;
    return 0;
}
Then later made worked on the algorithm for a program that draws names for buying Christmas gifts for our family:

Code: Select all

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <vector>

int main()
{
    using namespace std;

    string familyNamesA[7] = {"Scott", "Derek", "Tanya", "Lindsay", "Jesse", "Sarah", "Tyler"};
    vector <string> drawnNames;
    int randName;

    for (int i = 0; i < 7; i++)
    drawnNames.push_back(familyNamesA[i]);

    srand( (unsigned)time(0) );

    for (int i = 0; i < 7; i++)
    {
        randName = (rand() % drawnNames.size());
        if (drawnNames.size() == 0)
        cout << "\nNo more names left\n";
        else
        {
            cout << familyNamesA[i] << " buys for ";

            if (familyNamesA[i] == drawnNames[randName] && drawnNames.size() == 1)
            {
                cout << string(100, '\n');
                cout << "NEW LIST\n\n";

                for (int j = 0; j < 7; j++)
                drawnNames.push_back(familyNamesA[j]);

                i = 0;
            }
            else
            {
                while (familyNamesA[i] == drawnNames[randName])
                randName = (rand() % drawnNames.size());

                cout << drawnNames[randName] << "\n";
                drawnNames.erase(drawnNames.begin()+(randName));
            }
        }
    }

    cout << "\nPress ENTER to exit\n";
    cin.get();
    return 0;
}
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Structures come into many things in C. I use them all the time.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Upon attempting to make the lists (for Christmas names) more random, I have another strange error I can't seem to pinpoint. Always problems... :)-
Image
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Do you know how to overload the << operator as to be able to print out an object on screen?
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

I don't know. :(

Once again I don't do much C++.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:I don't know. :(

Once again I don't do much C++.
No problem. I have to read up on streams some more as that is the solution to the problem. What is your main weapon? Python? Do you think it is business appropriate/suitable? I am actually against the higher level languages as I find that they don't give enough control but they also can do some tasks in short amounts of code and quicker coding sessions...
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Many people are now doing performance critical tasks in C/C++ or Java and are doing the rest in other, high-level languages.

Python is too slow though. I think it's best to get a really good library in C and deal with the annoying syntax (Stupid semi-colons that aren't needed is the most obvious example).

I wont be using Python for my applications anymore. I will use it for doing scripts for quick tasks and that's it. Trying to make a game with it was a mistake. Luckily I can mix Python and C.

In other news I'm on the edge of submitting my iPhone game to Apple.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:(Stupid semi-colons that aren't needed is the most obvious example).
What :O How else would you define inline functions?

Code: Select all

dArray(int length) {itsLength = length; pArray = new int[length];} 
That is something I wouldn't want to lose and only works because of semi-colons defining each statement.
Matthew wrote:I wont be using Python for my applications anymore. I will use it for doing scripts for quick tasks and that's it. Trying to make a game with it was a mistake. Luckily I can mix Python and C.
Ya, Python is more for scripting is it not?
Matthew wrote:In other news I'm on the edge of submitting my iPhone game to Apple.
Congratulations. Hope it goes smooth. What's the cost? It's the Monkey Curling game correct?
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Monkey Curling, yes. The game will cost £1.79.

Python uses no semi-colons. In standard C inline-functions aren't allowed. They are like python lambda are they?

>>> (lambda x: x-1)(2)
1
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:Monkey Curling, yes. The game will cost £1.79.
Tell me when it's up. I may give it a look :)
Matthew wrote:Python uses no semi-colons. In standard C inline-functions aren't allowed. They are like python lambda are they?

>>> (lambda x: x-1)(2)
1
I'm not sure what your getting at? Inline functions are simply the declaration followed by the statements on one line.

On a separate note, I have recently been working on a new vector class (mainly for fun but who knows) that I am fairly proud of:

Code: Select all

class myVector
{
    public:
        myVector(){itsLength = 0; pVector = new int[itsLength];}
        ~myVector(){;}

        int& operator [] (int location)
        {
            if (itsLength > 0)
                return pVector[location];
            else
                std::cout << "Error. This vector does not have more than 0 elements\n";
        }

        void add_on (int newElement)
        {
            tempVector = new int[itsLength];

            for (int i = 0; i < itsLength; i++)
                tempVector[i] = pVector[i];

            delete pVector;
            itsLength++;
            pVector = new int[itsLength];

            for (int i = 0; i < itsLength; i++)
                pVector[i] = tempVector[i];

            pVector[itsLength-1] = newElement;

            delete tempVector;
        }

        void take_off(int location)
        {
            itsLength--;
            tempVector = new int[itsLength];

            int j = 0;
            for (int i = 0; i < itsLength+1; i++)
            {
                if (i != location)
                {
                    tempVector[j] = pVector[i];
                    j++;
                }
            }

            delete pVector;
            pVector = new int[itsLength];

            for (int i = 0; i < itsLength; i++)
                pVector[i] = tempVector[i];
        }

        void take_off()
        {
            itsLength--;
            tempVector = new int[itsLength];

            int j = 0;
            for (int i = 0; i < itsLength+1; i++)
            {
                if (i != itsLength+1)
                {
                    tempVector[j] = pVector[i];
                    j++;
                }
            }

            delete pVector;
            pVector = new int[itsLength];

            for (int i = 0; i < itsLength; i++)
                pVector[i] = tempVector[i];
        }

        int len() {return itsLength;} //This comment is just to remind me of .len()'s existence

        void clean()
        {
            delete pVector;
            delete tempVector;
            itsLength = 0;
        }

    private:
        int itsLength;
        int *pVector;
        int *tempVector;
};
It's pretty versatile although I am missing quite a few functions of the STL and am interested in adding more functionality that the STL version does not have. Unfortunately it is 12 bytes just as std::vector.
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Ah, very good. Nice to know you are getting to learn dynamic memory management, even if it's new/delete and not malloc/free.
Administrator
Administrator
Posts: 3307
Joined: Thu Dec 24, 2009 2:06 am
Allegiance:: Space Rome
Location: ON, Canada
User avatar
Administrator
Administrator
Re: C++/C programming discussion

Post by Scott »

Matthew wrote:Ah, very good. Nice to know you are getting to learn dynamic memory management, even if it's new/delete and not malloc/free.
Haha, thanks :) There's an entire chapter on dynamic memory in my book that I haven't reached yet (I'm on type casting at the moment).

Oh, if you have any suggestions of useful functionality to add, let me know.

EDIT:

I edited out my long message to this link: http://www.cplusplus.com/forum/general/30063/
Image
Jedi Master
Jedi Master
Posts: 336
Joined: Sun Aug 09, 2009 9:16 am
Allegiance:: Jedi
User avatar
Jedi Master
Jedi Master
Re: C++/C programming discussion

Post by Matthew »

Is this why you wanted to know a sorting algorithm?
Post Reply