jaskij 45 Junior Poster in Training

@triumphost try Orwell's Dev-C++, that guy picked it up after all those years.
And YES, the compiler is more-or-less up to date.

jaskij 45 Junior Poster in Training

Hope I don't start a holy war ;)

This question rose to my mind after starting a project with a couple of friends (my very first team project to be honest). We all agreed on CamelCase, but one friend suggested somethin unnatural to me, that is, starting names of all classes and variables of composit data types with capital and of base types with a small letter.
So far I named all classes starting with capitals and variables starting with a small letter.
Although the pros of this he gave were strong (so that it was adopted), it still doesn't sit right with me.

Another disagreement was with braces - where to put them? Since I scan the code more like it is for script languages (like Python), that is ignoring braces and looking only at indentation, I went for the more compact style of putting the opening brace on the same line as the control statement.
Closing one goes - as I see almost everywhere, on it's own line.

Wanted to know what's your take on this guys. How do you do things? How do you indent? How do you name your classes/functions and variables? Why that way?

If anyone is interested, something I found linked on Stroustrup's (I think) page a month or two back, a coding style guidebook from Lockheed Martin: http://www2.research.att.com/~bs/JSF-AV-rules.pdf

jaskij 45 Junior Poster in Training

If a non-portable way is enough for you, try the system() function from cstdlib, which works as though you have just typed the command given as a parameter in the command line.

jaskij 45 Junior Poster in Training

Try Orwell Dev-C++ - someone finally picked up Dev-C++ after all those years.
http://sourceforge.net/projects/orwelldevcpp/

jaskij 45 Junior Poster in Training

Depending on the game, there are all kinds of answers. If it's a Uni assignment, try Space Invaders - I've seen it written in under 500 lines of code, in a nasty way though.

jaskij 45 Junior Poster in Training

First, a lil bit of history and theory: since C++ is based on C, and C is a pretty low-level language, many things are closer to how computer does them, then they are to human's way.

Arrays (or container) indexing is one such case.
They are indexed beginning at 0 (so you used them properly in the cout statements), so a statement like yours a[1]=a[2]; actually assigns the second element the same value as the third one.

jaskij 45 Junior Poster in Training

@rubberman: passing by value... not a good idea, unless the arrays are extremly small...

Since someone already post their code, I'll post mine too. Since an array is essentially a pointer, an array of arrays is an array of pointers, so a pointer to pointer (as far, as types are concerned) AFAIK the standard way for passing a two-dimensional arrays is using a pointer to pointer, like this:

void function(int **arr,int width, int height){
    for (int i=0; i<width; i++)
        for(int j=0; j<height; j++)
            cout<<arr[i][j];
}

then, in your code you can call the function it like this:

const int width=5;
const int height=7;
int arr[width][height];
function(arr,width,height);

although I never actually did it that way, so I might be wrong. There's a ton of better things, like vector of vectors, or a single vector and offsetting it, so you call it likearr[y*width+x].
Should you use vectors, remember to pass it by reference.

Anyways, remember, that passing by value anything of any decent size is not a good idea. We don't want our program to allocate (and free, calling destructors in the case of classes) additional 100kB of data every time the function is called, simply because you didn't bother to pass an argument as a reference.

jaskij 45 Junior Poster in Training

WaltP isn't that OS-dependant? Anyways, Windows (at least W7) supporst slashes too.

Another thing is, AFAIK if you supply the flags, you HAVE to specify the write flag too, so:

walletfile.open("C:\accounts\wallet.acnt", ios::trunc | ios::out);

should help too

jaskij 45 Junior Poster in Training

As for IDEs, you might try Orwell Dev-Cpp ( http://sourceforge.net/projects/orwelldevcpp/ )- this is the one most of my friends started out with, and it has seen some updates recently, like un up-to-date compiler ;P

If you need some pre-made tasks, try some online solving system, like http://www.spoj.pl/

DeanMSands3 commented: Bravo, sir. It's as if an old friend was dead and has returned to me. +4
jaskij 45 Junior Poster in Training

If you insist on using MS Excel spreadsheet, you could try exporting the data to csv and then re-importing it if your program changes that. That is IF Excel supports csv.

jaskij 45 Junior Poster in Training

IMO, you could just go by every single value and check if they're equal. Like when comparing strings. It doesn't really matter how you go through them, if the matrices are equal you WILL go through every single value anyways. And what you do just asks for bugs.
Why not do something like

// 262144 is 512^2
bool equal=true;
for(i=0;i<262144;i++){
	if(a[i]!=b[i]){
		equal=false;
		cout<<different;
		break;
	}
}
if(equal)cout<<equal;

Also, you could think of pre-computing some cheap (?) metric of those matrices and using it as a precondition when checking for equality.

jaskij 45 Junior Poster in Training

1. #include <typeinfo> and typeid() http://en.wikipedia.org/wiki/Typeid

3. I tend to be wrong recently, but IMO an empty template parameter would incur a compile error.

triumphost commented: You're right! +6
jaskij 45 Junior Poster in Training

Yeah, but then I wanted to have the reading routine as a method, since it can be so nicely written recursively, but maybe that's just me being stubborn.

jaskij 45 Junior Poster in Training

The expressions are numbered, so a normal vector of pointers will do as well :)
The problem is where to put that vector - outside the class, which destroys my object approach, or inside as a static member, which seems ugly to me.

jaskij 45 Junior Poster in Training

Tutorials (like http://edn.embarcadero.com/article/31863 ) are an obvious reply, but since I don't know much about UML, I can't tell whether they are good.

The second part is nice software. Personally, I like the free Modelio, which is IMO quite strict about UML (don't know if this is what you're looking for). One of it's drawbacks would be minimal keyboard support (lack of hotkeys, little support for tab and so on).

jaskij 45 Junior Poster in Training

I have to write an interpreter for tristate logic as a Uni assignment.
I got most of the things down, like expression tree and such, there is just one thing that's bugging me, that is input, or a particular part of it.

The thing is, in the expression there can be a reference to another expression (they are numbered). My problem is, how does one expression know about the address of another?

I can see two approaches to this, neither satisfactory.
One thing is to keep a vector of pointers as a static class member, second is to move parsing outside of the class, which incurs writing much more code.

If I have been unclear, consider this sample input. S followed by a number is an expression or a reference to one.
Oh yeah, forgot to mention, this is Polish (prefix) notation.

S1 = OR T x1
S0 = AND S2 S1
print S0
S1 = AND F x0
S3 = ARG2 S0
S4 = OR S1 x0
jaskij 45 Junior Poster in Training

My bad then :(
Sorry about that. A lib it is then.

jaskij 45 Junior Poster in Training

I'm not that good with streams and such, but you could try using istream::get or getc if you are using cstdio .

jaskij 45 Junior Poster in Training

You give too little info here, but my guess is one of the two: you have an invalid or non-standard declaration of int main() or you are trying to compile your own lib as an application (this can be changed in project settings).

jaskij 45 Junior Poster in Training

Maybe it is not really the case, but IMO the whole class should be a template, not just one function or member...
Like:

template <class T>
class Array{
public:
	T* m_array;
}

Although it's 3:30 am and I might've gotten this wrong.

jaskij 45 Junior Poster in Training

You do not populate the people vector.

This should work correctly, if memory serves:

std::sort(seznam_student,seznam_student+st_studentov,sort_by_name)

Or just use a vector from the beginning:

std::vector<student> seznam_student;

if(meni==1)
{
	seznam_student.push_back(vpis(st_studentov));
	st_studentov++;
}
if(meni==2)
{
for(int a=0;a<st_studentov;a++)
{
	izpis(seznam_student[a]);
}
}
if(meni==3)
{
	std::vector<student> people;
	std::sort( people.begin(), people.end(), sort_by_name );
	std::sort( people.begin(), people.end(), sort_by_age );
}
jaskij 45 Junior Poster in Training

According to Marshall Cline's C++ FAQ, which I really like, it is not if the code should be human readable.
http://www.parashift.com/c++-faq-lite/new/convert-to-c.html

jaskij 45 Junior Poster in Training

Ancient Dragon: there is actually a new version of Dev-C++, although not by Bloodshed. It contains MinGW GCC 4.6.2, so it should support most of the features even for C++11 ;)

http://sourceforge.net/projects/orwelldevcpp/

Ancient Dragon commented: Thanks. I didn't know that. +17
jaskij 45 Junior Poster in Training

Also, a word of warning if you are using MS Visual Studio:

VS has a lot of runtime checks built in their debug implementation of STL, so if you run it under debug it will most likely feel terribly slow.

In my case it came out when I wrote a simple app to render Mandelbrot and Julia sets. The difference was immense. In debug, it would take a fair amount of time to render with around 16 iterations, while in release it would be faster even at around 60-70 iterations.

jaskij 45 Junior Poster in Training

It's a problem of context.
The pointer node* head is local to the constructor.

Depending on what it is you are trying to do, maybe a static node *head member will be the right choice for you?

jaskij 45 Junior Poster in Training

Sorry if this has been posted before:

SPOJ especially tutorial problems.

It can a bit challenging, since most problems are demanding algorithmically and SPOJ judges (yes, software ones) are pretty strict on I/O, but it is also pretty much fun.

jaskij 45 Junior Poster in Training

Maybe something along these lines, although I'm not sure if it really works.
Probably the loop is wrong and will skip the last character...

But you have something to work with :)

const int len=sizeof(search);
char* tmp=new char[len];
int i=0;
while(const int len=sizeof(search)
char* tmp=new char[len]
int i=0;
while(!fileIn.get(&tmp[i]).eof()){
	if(tmp[i]!=search[i])
		i=0
	else if(i==len-1)
		//	output replace
		//	i=0
}
jaskij 45 Junior Poster in Training

TBH, xerces still gives me the feeling of WTH, maybe other lib would be better?

Anyways, do you have a header for that class?

jaskij 45 Junior Poster in Training

Damn you and my topology exam.

Debugging it right now. Or at least trying to.

jaskij 45 Junior Poster in Training

+1 for that WaltP

By comparing maximal values (using Mathematica) uint32 will work up to 12!, uint64 up to 20!.

Looking at IEEE 754-2008 and again comparing the values, long double (binary128 in IEEE 754) should work somewhere to the upper thirties, didn't check the exact value.

But still, that's not the point of vegaseats code, is it?

jaskij 45 Junior Poster in Training

The thing is, insertProc() accepts argument of type el_t , not PCB.
The way this is now, you would do something like

myLL.insertProc(arr[0].id)
jaskij 45 Junior Poster in Training

Sorry for that.

Looking at some code samples and tutos I get a strong feeling of WTH, but maybe I'll be able to help you out some, just post your code so far.

jaskij 45 Junior Poster in Training

There's a ton of good XML libraries out there, like the GNOME libxml or boost, most of them are in the repos ;)

Also, as much a PITA it can be, you could wrute your own XML-schema and validate the files against it, to ensure you don't make mistakes in the XML ;) there are many websites and some IDEs that do it, you don't need to do it yourself ;)

As for tutorials, it really depends on the library of your choice.
I didn't read much into it, but this one using boost seems legit ;)

jaskij 45 Junior Poster in Training

Seems legit, just one thing: make count static, so that the value is shared across all instances of LL.
Further reading on static members:
http://cplusplus.com/doc/tutorial/classes2/

jaskij 45 Junior Poster in Training

Dynamic allocation of memory here is a must, since the user determines the size of the arrays.

Theoretically you could approximate the maximum size of the array and statically declare it with that size, but that is not a good idea, since it wastes memory and such.

jaskij 45 Junior Poster in Training

Constructor is just a normal method, just with no return type, so declare and define it like you would do with any other method (usually declaration in header file and definition in cpp file).

I just wrote it like that to make the code put in the forum as short as possible.
Of course don't forget to declare members of PCB ;)

jaskij 45 Junior Poster in Training

Please use code or icode tags. arr[j*cols+i] is just addressing an element in the array.

Consider the example:

1 2 3
4 5 6
7 8 9

Becomes 1 2 3 4 5 6 7 8 9 in this representation. I suppose you can figure out how it works from here. int *arr=new int[rows*cols] just allocates the memory for the array. How else did you want to do it?

jaskij 45 Junior Poster in Training

First, consider writing a constructor, like

struct PCB{
	PCB(el_t id_,string state_){
		id=id_;
		state=state_;
	}
};

Second, consider making the state an enum - that would be much better IMO, but it depends on you.

Finally, an array of structs is nothing special, just use operator.() , like:

int main(){
	PCB arr[50];
	for(int i=0;i<49;i++)
		arr[i].id=1;
}

Finally, are you sure you can assign an int to type el_t ?

jaskij 45 Junior Poster in Training

Learn pointers. Just do it.
Loads of more advanced problems require them.

The solution provided by Behi John seems legit, so if you HAVE TO use a 2-d array, use that, but that's a stupid thing IMO.
Or you can represent the 2d array as a 1d one in the memory, see the code below.
Iterating over it may get a little tricky, but it's nothing you can't debug :)

#include <iostream>
#include <new>

void func(int *arr,int cols,int rows){
	for(int i=0;i<cols;i++)
		for(int j=0;j<rows;j++)
			cout<<arr[j*cols+i];
}

int main(){
	int rows,cols;
	cin>>rows;
	cin>>cols;
	int *arr=new int[rows*cols]
	func(arr);
}

@Behi John: either you somehow have the cutting edge of c++ (c++ 0x standard was approved 12th of August 2011) in your compiler or it's the non-standard Microsoft way, not sure which.

jaskij 45 Junior Poster in Training

This

#include <iostream>

using namespace std;

template<class T>
bool logicalAnd(T x, T y){
	return !(x == y);
}

int main(){
	int x = 3, y = 5;
	cout << logicalAnd(x,y) << endl;
}

compiled without any errors or warnings using g++ -Wall , so if it doesn't compile under VS it's most likely something with your installation, or something VS-specific.

jaskij 45 Junior Poster in Training

Arrays are EVIL. Use a vector of vectors.
They work similarly to arrays, but have resizing and a bunch of other things, like checking if you don't call for elements outside of the array.
Like this:

#include <vector>
#include <iostream>

using namespace std;

void display(vector<vector<int> > &arr){
	//	do your stuff here
}

int main(){
	vector<vector<int > > arr;
	int w,h;
	cout<<"Width: ";
	cin>>w;
	cout<<"Height: ";
	cin>>h;
	arr.resize(w);
	for(int i=0;i<w;i++)
		arr[i].resize(h);
	//	get the array from the user
	display(arr);
}

That was the preferred solution.
Another would be to make a 1-dimension array of size w*h, then refer to items like arr[y*cols+h] . But still, it's not recommended, as dynamic allocation is prone to erros, although unaviodable.

Actually, making it one dimension (instead of two) is desirable with vectors too, since with two-dimensional arrays (or array-like classes) there is no assurance that all the numbers will be in one block.

@Behi Jon:
The >> for template class declaration was added in c++ OX, there should be a space in there, like > >

jaskij 45 Junior Poster in Training

Look at the STL algorithm header, there is a whole load of functions using this concept, like std::count_if() .

Also, if you want to compute an integral over a range, I found it preferable to pass the integrated function as a pointer, but that's a whole another case and may not be true in general.

jaskij 45 Junior Poster in Training

http://cplusplus.com/doc/tutorial/polymorphism/ - a pretty nice explanation IMO

jaskij 45 Junior Poster in Training

Maybe something went wrong with ampersands or something?
It displays the same way for me :/

jaskij 45 Junior Poster in Training

This here is a code of my recent uni assignment to render Mandelbrot, Julia and Burning Ship sets, using SDL.
The code works properly, using MS Visual Studio 2010 Premium, but displays some errors (either numerical or type casting differences, I couldn't find them) after porting to Linux and compiling using g++.
If you compile it under VS and it runs slowly, try compiling it in release configuration instead of debug, as VS has quite a bit of run-time checks built into debugging versions of STL.
Also, the first idea was to store the rendered SDL_Surface internally, but I ran out of time to implement that :/
And I suppose you could optimize it somehow too, anyways I am putting it here mostly to ask other members of DaniWeb to evaluate it, and if someone finds it helpful, all the better :)

#include <vector>
#include <algorithm>
#include <complex>
#include <cmath>
#include "SDL/SDL.h"

namespace fractal{
	//	typedefs
	typedef enum {MANDEL=0, JULIA=1, SHIP=2} fractType;

	//	constants
	const int MODE_COUNT=3;

	//	a bit ugly, but shortens the code and such
	//	order as in modeType
	const int RES_W[MODE_COUNT]={400,300,400};
	const int RES_H[MODE_COUNT]={250,300,250};
	const double MIN_RE[MODE_COUNT]={-2.5,-2,-2.5};
	const double MAX_RE[MODE_COUNT]={1.5,2,1.5};
	const double MIN_IM[MODE_COUNT]={-1.25,-2,-1.75};
	const double MAX_IM[MODE_COUNT]={1.25,2,0.75};

	//	histogram
	class Histogram{
	private:
		static const int BPP=32;
		static const unsigned char COLOR_R=0x24;
		static const unsigned char COLOR_G=0xb0;
		static const unsigned char COLOR_B=0x15;
		fractType type;
		static int iters;
		int width,height;
		long double top, left,bottom, right;
		std::vector<std::vector<long double> > hist;
		bool validHist,validSurf;
		void init();
		void …
DeanMSands3 commented: Rock on! +3
jaskij 45 Junior Poster in Training

Guess I made a fool out of myself there. Thanks for pointing those out WaltP :)

And what I suggested at the beginning was mostly because OP used those, although I knew that :/ damn my laziness :/
What I meant by "catching" the EOL is that it stays in the buffer and that is the cause of the unintended behavior.

jaskij 45 Junior Poster in Training

Just go from the head to the tail, copying every member inside a vector (sorry, array would work here, but you would have to allocate it dynamically, which is easier with vectors).
Although these causes quite a memory overhead.

jaskij 45 Junior Poster in Training

Dynamic array== vector , http://cplusplus.com/reference/stl/vector/

There are at least two ways to do what you want, depending on the specifics of your problem.

One is to make three vectors, or make your own struct/class to hold all three in a single place.

The other is make a struct with name and those two floats and then make a vector of that struct.

jaskij 45 Junior Poster in Training

You could do a bubble sort, it's fairly simple, but works in [tex]O(n^2)[/tex].
If I am not wrong, you could also use a quicksort, since it iterates through all the elements in each recurring call anyways. But not the in-place version, since requires random access.

Or you could just copy the whole list into a temporary array, sort it (even using std::sort() from the algorithm header) and then put it back into the list.

jaskij 45 Junior Poster in Training

There are many algorithms.

Unless your numbers are ridiculously large, I suggest using Eratosthenes' sieve and then checking up to the square root of the number (for every divisor of a number under it's square root there is a corresponding one above it).

Or you could just hard-code a list of primes and then check against it. There are many lists of primes on the net, google it.

@DeanMSands3: even better then using squaring, you could just precompute the square root.