daviddoria 334 Posting Virtuoso Featured Poster

Not very efficiently, you could simply push the front() element onto the back of the queue, then pop it from the front. Do this until you get to the last element, but simply pop it instead of pushing and popping it. Because of the 'remark' I take it that this is an assignment and therefore you aren't worried about efficiency, just getting it work. Let me know if that's an acceptable solution.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I did some googling and from this same question on several other forums it seems like there is no way to retrieve this information. The reason seems to be that the stream may not even be a file, hence the file name would be ill-defined. The recommendations were to create a class that created and returned the stream, but saved the filename which could be accessed with a getFilename() function, but it sounds like you never saw the construction of the stream, but you know it is writing to a file and want the name. I can't say personally, but it sounded, from other responses, like it is impossible.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

you asked the same question 10 minutes apart.... this forum is for helping with programming, not doing your homework.

daviddoria 334 Posting Virtuoso Featured Poster

You are generating the number, and then outputting it 6 times!

You need to move the random generation into the loop

for (int count = 0; count < 6 ; count++)
{
number_back = rand() % 53 + 1;
cout<<number_back<<endl;
}
daviddoria 334 Posting Virtuoso Featured Poster

Can you please ask a specific question rather than just posting code? And also in the response - what seems to be added was the foreign() definition?

daviddoria 334 Posting Virtuoso Featured Poster

There seem to be random *'s all over the code...

daviddoria 334 Posting Virtuoso Featured Poster

Ah, I though I had read somewhere that the friendship was mutual - ie. if you make A a friend of B, then B is automatically a friend of A, but I guess that is just wrong, as this example shows haha.

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

There are a lot of

cannot access private member

That means you are trying to do

YourClass A;
cout << A.x;

but x is a private member of A. Either 1) make x public, or 2) make an accessor

class YourClass
{
private:
double x;
public:
double getX() {return x;}
};

The errors like "function does not accept 2 arguments" means you are calling

YourFunction(param1, param2);

But it is expecting a different number of parameters.

That should get you about half way through the list. My recommendation is to make a simpler, abstracted example of a problem you are having so we can look at ~20 lines of code and help you with concepts, rather than operating directly on your huge project.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Please post the code for the constructor - you have posted only declaration, no definitions, so we can't see what is going on!

daviddoria 334 Posting Virtuoso Featured Poster

It seems it depends which order you declare the classes matters? For example, this does not compile:

class Point
{
	private:
		//friend class PointFriend;
		double x,y,z;

	public:
		Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
};

class PointFriend
{
	private:
		friend class Point;
	public:
		PointFriend()
		{
			Point P(1.2, 2.3, 3.4);
			cout << P.x << endl;
		}
};

But if you uncomment the "friend class PointFriend;" and comment "friend class Point", it works correctly.

Why is that?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Ok, I implemented it using a distance matrix, and updating the distance matrix row and column that correspond the the cluster that was deleted. It worked pretty well, and I ran it with 7000 points and it took less than an hour. However, it turns out a better cluster distance function for my application is to merge the clusters with the closest distance between any of their points. Ie find the distance from all the points in A to all the points in B. The minimum of those distances is considered the cluster to cluster distance. The clusters with the closest cluster to cluster distances are merged. This makes updating the row and column of the distance matrix horribly horribly slow (ie. its down to 6000 clusters from 7000 points in an hour, and as the size of the clusters grow the speed decreases, so I bet its looking at 20 hours or so.

I don't see any shortcut to computing all of these distances (I already put the points in one cluster in a KDTree and then found the distance from each point in cluster B to the tree, rather than brute force finding the distances between each point in A to all the points in B). That helped a bit, but its still wayyyy too slow.

Sorry, I'm kind of rambling, but I don't know where to go from here!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Please ask a specific question. Where is the code broken? Does it compile? If no, where/what are the errors?

daviddoria 334 Posting Virtuoso Featured Poster

see previous post :)

daviddoria 334 Posting Virtuoso Featured Poster

Ah I remember now, someone had shown me this once before:

typename set<T>::iterator it;

But we never understood why you had to do this? Can anyone explain?

daviddoria 334 Posting Virtuoso Featured Poster

Do not just post your assignments. Try it yourself and let us know if you have any problems.

daviddoria 334 Posting Virtuoso Featured Poster

I think what you want is a friend class. I've never used it, but I bet google knows about it :)

daviddoria 334 Posting Virtuoso Featured Poster

On the line

set<T>::iterator iter;

I am getting "expected ';' before 'iter'"

#include <vector>
#include <set>
using namespace std;
	template <typename T>
	vector<T> UniqueElements(const vector<T> &V)
	{
		set<T> s;
		s.insert(V.begin(), V.end());
		vector<T> Elements;
		
		set<T>::iterator iter;

		return Elements;
		
	}

Can anyone see why?

It compiles fine if I do

set<int>::iterator iter;

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Right, that part is pretty straight forward, but how to keep track of these Cluster objects is the first question, and how to keep track of the distances between the clusters is the second.

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to write a pretty straight forward algorithm (agglomerative clustering), but the details are getting pretty hairy. (A cluster is simply a group of points. The idea is to take a list of points and decide which are "grouped" or "clustered" close together.)

Here is the outline:

Input: A vector of N Points (x,y,z coordinates)
Algorithm:
- Create N clusters (each input point is it's own cluster with only the one point)
- Merge the two closest clusters (ie. delete either cluster, move all of the points from the deleted cluster to the other cluster). Set the "cluster center" to be the center of mass of all of the points in the new cluster. These cluster centers are what are compared to decide which clusters are closest to each other.
- rinse and repeat until the two closest clusters are farther apart than a threshold

I got it working with a really naive algorithm, but then I tried it with an input of size 30,000 and of course that didn't fly ! :)

The choices seem to be
1) Do you actually store Points in a vector in a Cluster class and move them from cluster to cluster? Or do you just have a vector of labels that you update when points should be "moved" to a new cluster?
2) I was considering storing objects that contained "Cluster 1 ID", "Cluster 2 ID", "Distance". But then each iteration …

daviddoria 334 Posting Virtuoso Featured Poster

if you have a very simple program that just has to run a zillion times, you can use OpenMP parallel for loops. You just put a #pragma right before the loop and it magically splits it across all available cores.

You can do more complicated things with openmp, but this is what I use it for.

Dave

Ancient Dragon commented: Excellent :) +36
tux4life commented: Nice to know, I didn't know it ... +1
daviddoria 334 Posting Virtuoso Featured Poster

The real function is

double AverageDistance(const vector<Point> &A, const vector<Point> &B)
{
assert(A.size() == B.size());
double Total = 0;
for(unsigned int i = 0; i < A.size(); i++)
Total+= Distance(A[i], B[i]);

return Total/A.size();
}

as you can see, it doesn't make sense to get the average distance of two vectors of correspondences with different lengths! So this should indeed be handled with an assert, while some things may not.

Narue: What did you mean by "log/trace"?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

ah good idea with asser() instead of exit(0), I always wish that it didn't exit (even though I told it to exit !? haha) for exactly that reason.

Thanks for the comments.

daviddoria 334 Posting Virtuoso Featured Poster

For a while now I've been doing this when I want to check the input of a function (in this example, to make sure they are not equal).

void foo(int a, int b)
{
assert(a != b);
}

But when the assert fails, I always find my self changing it to

void foo(int a, int b)
{
if(a != b)
{
cout << "a: " << a << " b: " << b << endl;
exit(0);
}
}

Is there a reason that I shouldn't just do this in the first place?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

So when would you use find() then? The only thing I would know how to do with the result of find is get the value of the thing you found by dereferencing the iterator, but you already know that value because it is what you were searching for! Is the reason to use find() only to see IF something exists, not WHERE it is in the vector?

daviddoria 334 Posting Virtuoso Featured Poster

I was using a library recently and I was getting a "undefined reference" linker error when I tried to call a template function with a type that they did not plan on me using. I asked on the mailing list and they said to call

VSL_VECTOR_IO_INSTANTIATE(vnl_matrix_fixed<double,3,3>);

I guess it is a macro to tell the compiler to create the function for this type. It worked fine - but they also said there should be a compiler setting to "auto instantiate" this type of thing. Does anyone know how to do this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I found this:

Use std::vector<T>().swap( c ) to free vector memory

it seems to work!

daviddoria 334 Posting Virtuoso Featured Poster

This simple example fills a vector with numbers 0-9. Then I use find() to find where the 7 is. Then I saw online you can get the index by simply subtracting V.begin() from the iterator that find() returns. Has - been overloaded for iterators to do some magic and return the offset? Or are iterators simply these offsets?

unsigned int num = 10;
	vector<unsigned int> V(num);
	for(unsigned int i = 0; i < num; i++)
	{
		V[i] = i;
	}

	vector<unsigned int>::iterator it = find(V.begin(), V.end(), 7);
	if(it == V.end())
	{
		cout << "Could not find 7 in the vector"  << endl;
	}
	else
	{
		cout  << "Have found 7 in the vector"  << endl;

////////// THE LINE IN QUESTION //////////////
		cout << "The number 7 is located at index " << it - V.begin() << endl;
	}

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Interesting, cool though - thanks all!

daviddoria 334 Posting Virtuoso Featured Poster

Cool, that'll do it. I sorted the points and then used unique.

sort(Points.begin(), Points.end());
	vector<Point>::iterator new_end = unique(Points.begin(), Points.end());
	Points.erase(new_end, Points.end());

Thanks all.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Ok so Puzzle is a public member variable of Sudoku. To access it in main, you have to first create an instance of your Sudoku class

Sudoku MySudoku;

Then you can access the Puzzle variable like this

MySudoku.Puzzle[x][y];

You mentioned that Puzzle was filled, but that code must be in your Sudoku.cpp file. Also, why call the file doth.h when it is a Sudoku class?

daviddoria 334 Posting Virtuoso Featured Poster

Is puzzle a member of a class? Or just a global variable that was created in the .h file? Can you post your .h file?

daviddoria 334 Posting Virtuoso Featured Poster

You say

if (Puzzle[ptx]...

but you haven't defined Puzzle anywhere!

daviddoria 334 Posting Virtuoso Featured Poster

To remove duplicates from a vector, my first thought was to insert them all into a set, it would take care of the uniqueness of the elements, and then read them back out into my vector. However, I have a vector<Point>, where Point is a point in 3d space (ie. x,y,z). So the < operator is ill-defined. How else can I remove duplicate points without looping through the entire vector for each element and testing equality?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

This is taken directly from here:

http://en.wikipedia.org/wiki/C%2B%2B0x#Object_construction_improvement

class SomeClass
{
public:
  SomeClass() {}
  explicit SomeClass(int iNewValue) : iValue(iNewValue) {}
 
private:
  int iValue = 5;
};

I don't see range loops at all on that list you sent - is it called something else?

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

I was reading about the new "tuple" type coming in c++0x, and I decided to try it. I saw that if you give a compiler flag -std=c++0x it will work. So I did it, and then #include <tuple> works and everything was good. Then I decided to try the default member initialization.

class Point
{
	public:
		int a = 4;
};

but it doesn't work. Does anyone know where I can find a list of which features are available using -std=c++0x and which are not yet available? I am using gcc.

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

It turns out this is an added feature in c++0x. I'm glad the experts agree that this would be helpful!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Is there a built in type to store UNordered pairs?

ie. I want these

pair<double, double> a(4.0, 5.0);
pair<double, double> a(5.0, 4.0);

to be equal. Do I have to make a wrapper and override == ?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Is pthreads still the way to go to detach a process from the main thread in linux? It seems seems kind of convoluted/old/c-style from looking at some examples - is there a more "c++" way?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

There is no reason to post all of that code to ask that question. I suppose you need to handle/trap a keypress event and manually output a *, but I'm not sure how to do that.

daviddoria 334 Posting Virtuoso Featured Poster

That seems like a good outline, but you have to attempt to write the key functions and then we can help you debug it.

daviddoria 334 Posting Virtuoso Featured Poster

Currently, my index.html is like this

<html>
<head>
<title>EngineeringNotes.net</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>


<div class="header">
	<div id="logo">
		<h1>EngineeringNotes.net</h1>
	</div>
	<div class="menu">
		<ul>
			<li><a href="mailto:daviddoria@gmail.com">Contact Me</a></li>
			<li><a href="phpBB3">Forums</a></li>
			<li><a href="Ideology.html">Ideology</a></li>
		</ul>
	</div><!-- end menu -->
</div><!-- end header -->


<div class="page"><!-- start page -->
	<div class="sidebar"><!-- start sidebar -->
		<h2>EE Topics</h2> 
		<ul>
			<li><a href="Notes/EE/PatternRecognition.pdf">Pattern Recognition</a> </li>
			<li><a href="Notes/EE/ImageProcessing.pdf">Image Processing</a> </li>
			<li><a href="Notes/EE/ComputerVision.pdf">Computer Vision</a> </li>
			<li><a href="Notes/EE/ComputerGraphics.pdf">Computer Graphics (stub)</a> </li>
			<li><a href="Notes/EE/SignalsAndSystems.pdf">Signals and Systems</a> </li>
			<li><a href="Notes/EE/Communications.pdf">Communications</a> </li>
		</ul>
		

	</div><!-- end sidebar -->
	
	<div class="content"><!-- start content -->
		<h2>About this Site</h2>
		<p>
		This site is inteded to provide a "crash course" in many subject areas
		related to electrical engineering. The primers are not at all designed
		to
		be a replacement for a formal course in these topics. The target
		audience is a serious student who feels that it is worth their time
		outside of class to make sure they are getting the "big picture" and
		seeing the value of the subject rather than simply learning the mechanics of "doing problems". 


	</div><!-- end content -->
	
</div><!-- end page -->

</body></html>

If I want to change the content in the "page" section, but leave the title header and sidebar there, how would I do this? I can't imagine I have to have the header and sidebar on every page, or when I have to make a change to either of those, I have to change it on every page!

Any suggestions?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

It does work - but I didn't really want to do that anyway lol, so let me rephrase - is there any way to do this that doesn't involve having to add those two lines (the declaration with specific type and the include) to the .cpp? I've seen people use .txx files - is this what those are for?

daviddoria 334 Posting Virtuoso Featured Poster
////// file: Point.h
#include "Tools.h"
class Point
{
public: 
double x,y,z;
void OutputSum()
{
// do something using a function from Tools here
}
};

Then if I include Point in Tools is that not a problem?

daviddoria 334 Posting Virtuoso Featured Poster

According to this:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13

One way to keep only the function declaration in the .h file is to do this

////////// file: Tools.h
#include <iostream>
#include <vector>

using namespace std;

template <typename T> T Sum(vector<T> &V);
///////// file: Tools.cpp
#include "Tools.h"

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
T Sum(vector<T> &V)
{
	T sum = static_cast<T>(0.0);
	for(unsigned int i = 0; i < V.size(); i++)
		sum += V[i];

	return sum;
		
}

//this is the key line to tell the compiler to actually make this function for unsigned int
template unsigned int Sum<unsigned int>(vector<unsigned int> &V);

However, what if you want this function for a type that Tools does not know about? ie

template Point Sum<Point>(vector<Point> &V);

will not work because Point has not been defined. If you make Tools depend on Point, but Point already depends on Tools, then there is a circular problem.

Any suggestions?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

Which line is it having a problem with?

daviddoria 334 Posting Virtuoso Featured Poster

Which lines is it complaining about?

Also, in general, you cannot/should not do this:

char stuNames[stu][21];
    double stuGrades[stu][num];

The numbers in [ ] need be determined at compile time, or you need to dynamically allocate the array using "new".

daviddoria 334 Posting Virtuoso Featured Poster

That worked. Why do you have to do that?

daviddoria 334 Posting Virtuoso Featured Poster

ah cool. thanks.

daviddoria 334 Posting Virtuoso Featured Poster

I tried to make a template out of this:

template <typename T>
	T VectorMax(const vector<T> &a)
	{
		vector<T>::const_iterator pos;
		pos = max_element (a.begin(), a.end());
		return *pos;
	}

but now I get "error: expected ; before 'pos' "