You could write a program where a robot can find the shortest way out of a maze. If that's too easy, you could make it so the map isn't tiled and the robot can move freely, rather than to predesignated blocks.
Well for a singly linked list you need a Node class (you should use a struct for simplicity).
1 2 3 4 5
struct Node
{
int data; // actual data
Node *next; // link to next node
};
Then you have the List class, which contains a pointer to a single Node.
You don't have to use a pointer but it's easier that way: if it's NULL (nullptr in C++11) then the list is empty. Also you can set the destructor for Node to start a deletion cascade, so you don't have to manually delete the nodes in a loop.
struct Node
{
int data; // actual data
Node *next; // link to next node
Node(): next(nullptr)
{
}
~Node()
{
delete next;
}
};
class List
{
public:
List(): start(nullptr)
{
}
~List()
{
delete start; // chain reaction from here
}
private:
Node *start;
};
Now all that's left is writing the List::insert(data, position) and List::remove(position) functions. Throw an exception when the requested position is bogus. Link the nodes by using next.
Can't help you from here without writing the code for you.
You could write a program where a robot can find the shortest way out of a maze. If that's too easy, you could make it so the map isn't tiled and the robot can move freely, rather than to predesignated blocks.
I found this website quite fun awhile ago. Its kind of a programming competition website that has a section dedicated to AI and pathfinding. This might interest you if you like the A* algorithm and pathfinding fredbill
Btw, if you don't know what you're talking about, please don't give advice to people asking for it. If you can't be bothered to test your code, please don't bother posting it. You have nowhere near the experience you need to off-the-cuff code and have it reasonably bug free.
#include <iostream>
#ifndef _DYNARRAY_H_
#define _DYNARRAY_H_
template<class T>
class MyVector
{
public:
MyVector()
{
// Initialize the variables
value = new T[0];
length = 0;
cap = 0;
}
void push_back(T val)
{
// Check if the array has enough allocated memory
// to add another element to it.
if((cap) >= (length + 1))
{
// If it does, add 1 on the length variable, and then
// add the element to the array
length++;
value[length - 1] = val;
}
else
{
// If there isn't enough allocated memory, resize the array
resize(length + 1);
push_back(val); // After the array is resized, call the function again
}
//std::cout << "Length : " << length << "\nCapacity : " << cap << "\n";
}
void pop_back()
{
// Check if the vector has any elements
if(length > 0)
{
length--; // Decrease the length
T* tmp = value; // Create a temporary array
delete[] value; // Free the class array
value = new T[length]; // Allocate a new array with one less element
for(unsignedint len = 0; len < length; len++)
{
// Now, copy over the values besides the last
value[len] = tmp[len];
}
// delete the temporary value
delete[] tmp;
}
else
{
// If the array has no elements, we display a message
std::cout << "Vector is already equal to zero\n";
}
}
unsignedint size()
{
return length; // Return the length of the vector
}
unsignedint capacity()
{
return cap; // Return the capacity of the vector
}
void reserve(unsignedint amount)
{
delete[] value; // Delete the vector
value = new T[amount]; // Reserve the requested amount of memory
cap = amount; // Update the capacity as required
}
void resize(unsignedint amount)
{
T* tmp = value; // Allocate a pointer that holds the value
delete[] value; // Delete the class pointer
value = new T[amount]; // Allocate a new amount
cap = amount; // Update
for(unsignedint v = 0; v < cap; v++)
{
value[v] = tmp[v]; // Copy the vector elements
}
delete[] tmp;
}
// * The [] operator
T &operator[](unsignedint index)
{
if(index > length)
{
// If the index points to an element out of the vector's range...
std::cout << "Vector index out of range.\n";
}
else
{
// Else, we return a reference to the array's element
return value[index];
}
}
~MyVector()
{
// In the destructor we free allocated memory
delete[] value;
}
private:
T* value; // The pointer that's used as the
unsignedint length; // The length of the variable
unsignedint cap; // Capacity of the array
};
#endif//_DYNARRAY_H_
It's great that you've fixed many of the bugs in it. It was innaccurate to say "I got a basic working version in like 5 minutes literally." Because you literally did not.
It's great that you've fixed many of the bugs in it. It was innaccurate to say "I got a basic working version in like 5 minutes literally." Because you literally did not.
Please be honest when you post.
I admit I didn't test EVERYTHING, but it actually did work the second time I compiled no lie.
It only tested the push_back() thingy and the [] operator. Nothn' else.
look at this code, is there anything wrong with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
int cap = 10;
int* value = newint[cap];
int* tmp = value;
int amount = 20;
delete [] value; //free value == free tmp. Still accessing tmp after this line?
value = newint[amount];
cap = amount; //cap = 20
for(unsignedint v = 0; v < cap; v++)
value[v] = tmp[v]; //are you sure tmp has length 20?
delete [] tmp; //free tmp twice
}
then look back at your void resize(unsignedint amount) method
Groovey! You are advised to use initialization lists, though.
10 11 12 13 14 15 16
MyVector()
{
// Initialize the variables
value = new T[0];
length = 0;
cap = 0;
}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
void resize(unsignedint amount)
{
T* tmp = value; // Allocate a pointer that holds the value
delete[] value; // Delete the class pointer
value = new T[amount]; // Allocate a new amount
cap = amount; // Update
for(unsignedint v = 0; v < cap; v++)
{
value[v] = tmp[v]; // Copy the vector elements
}
delete[] tmp;
}
The noobism of using a for() loop instead of std::copy() is dwarfed by the major mistake of delete[]'ing value then copying from it... then deleting it again!
Ever heard the phrase "in recent years"? Recent can be 24 hours ago or longer.
cire wrote:
Compiling code is not testing it. Code which compiles does not necessarily work.
Exactly. There are compile errors, linking errors, and runtime errors. If a program compiles and links that doesn't mean there are no errors. The point of testing is to try and find bugs, like obvious wrong data or wrong behavior in the program.
I just push backed and displayed my values through cout...
Well if I can't do that, what CAN I do?
BHX, I never got any linking/compile/runtime errors.
Catfish4 wrote:
The noobism of using a for() loop instead of std::copy() is dwarfed by the major mistake of delete[]'ing value then copying from it... then deleting it again!
I had to resize the array, so I deleted it and set it to point to a larger amount.