Any good challenges for intermediate level C++?

Pages: 1234
Dijkstra's algorithm was the best suggestion.

Or it's heuristic big brother, A*.

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.
Seems a bit complicated.

I'm not as skilled as you guys. :P
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.
Fredbill30 wrote:
Seems a bit complicated.

If it's not complicated then it's not a challenge.
Wow, thanks. That made thinks things a lot more easier.
Last edited on by Fredbill30
closed account (3qX21hU5)
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

https://www.hackerrank.com/#categories/ai/astar-search

I believe you have to create a account to do the problems though.
Last edited on
I honestly hate making accounts for stuff. I'll make one when it isn't so early.
I'm already doing a dynamic array thing, I got a basic working version in like 5 minutes literally without counting time to get the compiler set up.


If the code in http://www.cplusplus.com/forum/general/107004/#msg580320 and a little further down in the post is part of your "basic working version" you've seriously overestimated the quality of your code.

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.
Well that isn't even a recent version. I fixed many of the bugs in that one.

Now I have this version, which I'm sure has a few problems, but I didn't find 'em.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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(unsigned int 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";
			}
		}
		
		unsigned int size()
		{
			return length; // Return the length of the vector
		}
		
		unsigned int capacity()
		{
			return cap; // Return the capacity of the vector
		}
		
		void reserve(unsigned int 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(unsigned int 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(unsigned int v = 0; v < cap; v++)
			{
				value[v] = tmp[v]; // Copy the vector elements
			}
			delete[] tmp;
		}
		
		// * The [] operator
		T &operator[](unsigned int 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 
		
		unsigned int length; // The length of the variable
		unsigned int cap;    // Capacity of the array
};

#endif//_DYNARRAY_H_ 
Last edited on by Fredbill30
Well that isn't even a recent version.

That was posted less than 24 hours ago.

I fixed many of the bugs in that one.

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.
Last edited on
That was posted less than 24 hours ago.
Still not recent ;)

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.
Last edited on by Fredbill30
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 = new int[cap];
  int* tmp = value;
  int amount = 20;

  delete [] value;  //free value == free tmp. Still accessing tmp after this line?

  value = new int[amount];
  cap = amount;  //cap = 20

  for(unsigned int 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(unsigned int 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(unsigned int 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(unsigned int 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!
I admit I didn't test EVERYTHING, but it actually did work the second time I compiled no lie.

Compiling code is not testing it. Code which compiles does not necessarily work.

Your "recent" version contains almost all of the bugs your "not-so-recent" version exhibited.


It only tested the push_back() thingy and the [] operator. Nothn' else.

I'd like to see how you tested them.

http://ideone.com/Dy1bfE
Last edited on
Fredbill30 wrote:
Still not recent ;)

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.
@Fredbill30
T* tmp = value;This copies the pointer, not the array it points to.
How do I copy the array it points to then?
closed account (3qX21hU5)
Fredbill30 wrote:
I have been programming for 3 years I think I have experience.


Sorry I just had to. feel free to shame me all you want ;p

Last edited on
You're acting like I only know C++.
Pages: 1234