Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Stealthgerbil
Dec 16, 2004


I have an assignment where I had to make a queue (without using the STL :( ) using a linked list. The queue class I made is supposed to represent an airport in the assignment and the nodes are the planes, ect. That part works fine and I can remove and add 'planes' perfectly.

However the second part is driving me nuts. Pretty much for the second part of the assignment I need to turn the three queues into a single queue, based on whichever queue has the highest 'passenger' integer. The story is that the airport got snowed in but they have a reserve runway to use and are letting planes from the other three runways go, depending on which has the highest amount of passengers.

I was messing around trying to figure something out and I did manage to make a function that calls the three queues by reference and then compares the queues to see which one has the highest 'passenger' integer. It also returns a value of 1, 2, or 3, depending on which queue it came from. Then in the main, I have some if/else statements which calls the remove function for whichever queue that the integer returned refers to.

However here is my problem. Once all of the nodes in the queue are removed, I get an exception error because I am still calling the three queues by reference in my function that checks to see which queue has the highest 'passenger' integer. Because of that, it tries to read information from a null queue and crashes.

Is there a way to get the function to ignore an empty queue? The program would work fine if it wasn't for this (it does function perfectly until one of the queues is empty). I can definitely post (on pastebin) my terrible mess of code if anyone is interested.

edit: or any other suggestions are welcome. I dont mind completely redoing the code if it makes sense.

Stealthgerbil fucked around with this message at 09:30 on Oct 28, 2009

Adbot
ADBOT LOVES YOU

raminasi
Jan 25, 2005

a last drink with no ice

Stealthgerbil posted:

I have an assignment where I had to make a queue (without using the STL :( ) using a linked list. The queue class I made is supposed to represent an airport in the assignment and the nodes are the planes, ect. That part works fine and I can remove and add 'planes' perfectly.

However the second part is driving me nuts. Pretty much for the second part of the assignment I need to turn the three queues into a single queue, based on whichever queue has the highest 'passenger' integer. The story is that the airport got snowed in but they have a reserve runway to use and are letting planes from the other three runways go, depending on which has the highest amount of passengers.

I was messing around trying to figure something out and I did manage to make a function that calls the three queues by reference and then compares the queues to see which one has the highest 'passenger' integer. It also returns a value of 1, 2, or 3, depending on which queue it came from. Then in the main, I have some if/else statements which calls the remove function for whichever queue that the integer returned refers to.

However here is my problem. Once all of the nodes in the queue are removed, I get an exception error because I am still calling the three queues by reference in my function that checks to see which queue has the highest 'passenger' integer. Because of that, it tries to read information from a null queue and crashes.

Is there a way to get the function to ignore an empty queue? The program would work fine if it wasn't for this (it does function perfectly until one of the queues is empty). I can definitely post (on pastebin) my terrible mess of code if anyone is interested.

edit: or any other suggestions are welcome. I dont mind completely redoing the code if it makes sense.

You might as well post some code - either your design is muddled or just your explanation is.

Lexical Unit
Sep 16, 2003

Stealthgerbil posted:

edit: or any other suggestions are welcome. I dont mind completely redoing the code if it makes sense.
So you have some function that inspects the front element of three queues and returns 1, 2, or 3 depending on which of the queues has the element you want. Right?

Well why not have a way of asking the queue if it's empty or not. Then you have something like:
code:
if (queue1 is not empty)
  inspect queue1

if (queue2 is not empty)
  inspect queue2

if (queue3 is not empty)
  inspect queue3

return the results of the inspections you've made
So now if queue2 empties first, the function will simply not inspect it and will at that point only be returning either 1 or 3. Once all queues are empty return something else, like -1 to indicate that there's nothing left.

I'm not saying this design is particularly good, but from the code you're describing it sounds like it might be the most simple to implement.

kingcrimbud
Mar 1, 2007
Oh, Great. Now what?

Nomnom Cookie posted:

Your code is using O(n*log(n)) space. Mergesort is an O(n) space algorithm.

Well that's Interesting. And what is space? Is that the memory i'm using? Last i checked big O dealt with time and not space. I may not know c++ but please don't question my algorithms.

Speaking of not knowing c++, i'm getting an unhandled exception here and i'm not sure where. It should be a problem with memory/pointers but my debugger isn't pointing anywhere in the code when it breaks, just shows me a memory location:

code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/* Partition Variables */
float x;
int i2;
int j;
float temp2;
float temp3;
printf("Partition Variables");

/* randomPartition Variables */
int i;
float temp;

/* Quicksort Variables */
int q;

/* Partition Procedure */
int Partition(float A[], int p, int r)
{
	printf("Partition Procedure");
	x=A[r];
	i2=p-1;
	for(j=p;j=r-1;j++)
	{
		if(A[j] <= x)
		{
			i2=i2+1;
			temp2=A[i2];
			A[i2]=A[j];
			A[j]=temp2;
		}
	}
	temp3=A[i2+1];
	A[i2+1]=A[r];
	A[r]=temp3;
	return i2+1;
}

/* randomPartition Procedure */
int randomPartition(float A[], int p, int r)
{
	srand(time(NULL));
	i = A[rand() % sizeof(A)];
	temp=A[i];
	A[i]=A[r];
	A[r]=temp;
	return Partition(A,p,r);
}

/* Quicksort Procedure */
void Quicksort(float A[], int p, int r)
{
	if (p < r)
	{
		q=randomPartition(A,p,r);
		Quicksort(A,p,q-1);
		Quicksort(A,q+1,r);
	}
}

int main(int argc, char *argv[])
{
	/* Extract value for # of elements in array */
	int d;
	sscanf (argv[2], "%d", &d);	
	printf("1\n");


	
	/* Create Input and Output Streams */
	ifstream fin;
	ofstream fout;
		printf("2\n");

	/* Open Input Stream */
	fin.open("numlist.dat");


	/* Tests to ensure files opened */
	if(fin.fail())
	{
		cerr << "Input did not open\n";
		exit(2);
	}
		printf("3\n");
	 /* Construct New Array */
	float *floatArray= new float[d];

	/* Put numbers from list into array */
	float item;
	// printf("Put Numbers from list into array\n"); 
	for (j=0; j<d;j++)
	{	
		fin >> item;
		// printf("item is %f\n", item);
		floatArray[j]=item;
		printf("floatArray[%d] is %f\n", j, floatArray[j]);
	}
	
	/* Call to Quicksort */
	Quicksort(floatArray, 1, d);

	/* Display Array After Sort */
	for (j=0; j<d;j++)
	{	
		printf("floatArray[%d] is %f\n", j, floatArray[j]);
	}
	/* Close file for Reading */
	fin.close();

	/* Open Output Stream */
	fout.open("numlist.dat.srt");

	/* Tests to ensure files opened */
	if(fout.fail())
	{
		cerr << "Input did not open\n";
		exit(2);
	}

	/* Write Output */
	for (i=0;i<d;i++)
	{
		fout << floatArray[i] << endl;
	}
	 /* Close File */
	fout.close();

	getchar();
}
First-chance exception at 0x6812f8bc (msvcr90d.dll) in qsort.exe: 0xC0000005: Access violation reading location 0x555c3a63.
Unhandled exception at 0x6812f8bc (msvcr90d.dll) in qsort.exe: 0xC0000005: Access violation reading location 0x555c3a63.
The program '[4684] qsort.exe: Native' has exited with code 0 (0x0).

BigRedDot
Mar 6, 2008

kingcrimbud posted:

Last i checked big O dealt with time and not space.
Then apparently you've never actually checked at all.

Edit: That occurred to me too
VVVV

BigRedDot fucked around with this message at 17:51 on Oct 28, 2009

Lexical Unit
Sep 16, 2003

kingcrimbud posted:

Well that's Interesting. And what is space? Is that the memory i'm using? Last i checked big O dealt with time and not space. I may not know c++ but please don't question my algorithms.
We are getting trolled so hard right now.

GeneralZod
May 28, 2003

Kneel before Zod!
Grimey Drawer

kingcrimbud posted:

Well that's Interesting. And what is space? Is that the memory i'm using? Last i checked big O dealt with time and not space. I may not know c++ but please don't question my algorithms.

From your own link:

quote:

Worst case space complexity: Θ(n)

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Lexical Unit posted:

We are getting trolled so hard right now.
I'm starting to get a major "hexadecimal" vibe.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

kingcrimbud posted:

Well that's Interesting. And what is space? Is that the memory i'm using? Last i checked big O dealt with time and not space. I may not know c++ but please don't question my algorithms.

Well, I think we finally found the problem here: you're illiterate.

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD

kingcrimbud posted:

code:
int randomPartition(float A[], int p, int r)
{
	...
	i = A[rand() % sizeof(A)];
	...
}
1) sizeof returns the total size of the array, so sizeof(float[10]) is going to be 40 for a 4 byte float. Better approaches are to use sizeof(array)/sizeof(<type>), or better yet, keep track of a given array's size in an integer and just use that to grab a random element, etc.

2) sizeof (to my knowledge) is calculated at compile time and I was pretty sure a dynamically allocated array being sizeof'd throws a compiler error. Maybe I'm wrong on that but either way that's the first thing I'd look at re: crashing.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Bhaal posted:

1) sizeof returns the total size of the array, so sizeof(float[10]) is going to be 40 for a 4 byte float. Better approaches are to use sizeof(array)/sizeof(<type>), or better yet, keep track of a given array's size in an integer and just use that to grab a random element, etc.

2) sizeof (to my knowledge) is calculated at compile time and I was pretty sure a dynamically allocated array being sizeof'd throws a compiler error. Maybe I'm wrong on that but either way that's the first thing I'd look at re: crashing.

Incorrect on both counts*. However, as I'm not apt to do people's homework for them, I will leave the correct answer as an exercise to the reader.

* Well the first one is technically correct but not applicable to the situation.

UberJumper
May 20, 2007
woop
A question about time. Basically i am working on my game and want to store the current hour/minute/second of the ingame time. I have a timer that currently returns the timedelta(ms) between updates.

E.g:

m_CurrentMilliseconds = (Timer::GetTimeDelta() * m_Rate);

Is there something better then me doing the conversions manually by myself?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Dammit, guys, I thought we all had a tacet agreement to ignore kingcrimbud's whining, it was going so well too :mad:

Contero
Mar 28, 2004

UberJumper posted:

A question about time. Basically i am working on my game and want to store the current hour/minute/second of the ingame time. I have a timer that currently returns the timedelta(ms) between updates.

E.g:

m_CurrentMilliseconds = (Timer::GetTimeDelta() * m_Rate);

Is there something better then me doing the conversions manually by myself?

Why don't you just add a new member to your Timer class that does this exact functionality? Somehow you're keeping track of the number of ms from one frame to the next, it shouldn't be hard to add a function that just returns the current ms since startup or whatever (add up all the deltas?).

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
There's a solution that involves templates here... :)

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

kingcrimbud posted:

code:
/* Partition Procedure */
int Partition(float A[], int p, int r)
{
	printf("Partition Procedure");
	x=A[r];
	i2=p-1;
	for(j=p;j=r-1;j++)
	{
		if(A[j] <= x)
		{
			i2=i2+1;
			temp2=A[i2];
			A[i2]=A[j];
			A[j]=temp2;
		}
	}
	temp3=A[i2+1];
	A[i2+1]=A[r];
	A[r]=temp3;
	return i2+1;
}
i know there's some kind of tacit agreement to ignore you going on here, but you are like some kind of reverse genius

ctz
Feb 6, 2003

Veritron posted:

i know there's some kind of tacit agreement to ignore you going on here, but you are like some kind of reverse genius

please don't question my algorithms

yatagan
Aug 31, 2009

by Ozma

Veritron posted:

i know there's some kind of tacit agreement to ignore you going on here, but you are like some kind of reverse genius

His code reads like poetry, specifically limericks.

covener
Jan 10, 2004

You know, for kids!

ctz posted:

please don't question my algorithms

or our commitment to sparklemotion.

HauntedRobot
Jun 22, 2002

an excellent mod
a simple map to my heart
now give me tilt shift
I keep expecting to see a screenshot of this thread in the "things you're working on" thread. "I created an AI to write a C++ program using Markhov Chains".

Lexical Unit
Sep 16, 2003

I'm trying to track down a viral #define someone introduced into the nightmare that is our codebase. What trick can I use to figure out where the #define is located?

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

Lexical Unit posted:

I'm trying to track down a viral #define someone introduced into the nightmare that is our codebase. What trick can I use to figure out where the #define is located?

grep -R \#define | grep FOO

covener
Jan 10, 2004

You know, for kids!

Lexical Unit posted:

I'm trying to track down a viral #define someone introduced into the nightmare that is our codebase. What trick can I use to figure out where the #define is located?

cpp -DYOURMACRO someaffectedfile.c | grep redefined

(or make CPPFLAGS=-DFOO | grep redefined)

Lexical Unit
Sep 16, 2003

Zakalwe posted:

grep -R \#define | grep FOO
Yeah... you haven't seen our codebase. I'm not even sure I could track down what all files are getting pulled in and from where.

Edit: Nevermind :downs: Got the list of included files by parsing it out of the preprocessor output.

Lexical Unit fucked around with this message at 13:55 on Oct 30, 2009

BigRedDot
Mar 6, 2008

Lexical Unit posted:

Our build system sucks so much oh god.
Stop candy-coating the truth! It's nowhere near as good as you imply!

mincepiesupper
Apr 9, 2009
I have a question about debugging vectors: I recently had to change stl header files in order to make the use of the [] operator behave more like at() and tell me when indices went beyond the size of the vector. Is there a way I could have done this without changing the header files? I'm using gcc 4.1.

Also, I wasn't able to get any output which told me where exactly the error was as any assert I added reported the error in the stl header file not the code that was using an invalid index.

pseudorandom name
May 6, 2007

mincepiesupper posted:

I have a question about debugging vectors: I recently had to change stl header files in order to make the use of the [] operator behave more like at() and tell me when indices went beyond the size of the vector. Is there a way I could have done this without changing the header files? I'm using gcc 4.1.

Also, I wasn't able to get any output which told me where exactly the error was as any assert I added reported the error in the stl header file not the code that was using an invalid index.

#define _GLIBCXX_DEBUG

mincepiesupper
Apr 9, 2009
Will this work with a release build? The code I am having the problem with is not mine, what I've been given to build it with is for release builds only. I've also been told that debug builds don't work but I've not yet tried this to see exactly what 'don't work' means.

pseudorandom name
May 6, 2007

mincepiesupper posted:

Will this work with a release build? The code I am having the problem with is not mine, what I've been given to build it with is for release builds only. I've also been told that debug builds don't work but I've not yet tried this to see exactly what 'don't work' means.

If you can change <vector> to hack in your own debugging, you have enough to build with the debug version of libstdc++ instead.

RussianManiac
Dec 27, 2005

by Ozmaugh
Is there a good library for C++ that does immutable strings? That is I want Java type strings where underlying char array doesn't change. The reason is that I am working with very large strings, and in course of my computations a lot of substrings are returned. The big text however, never changes. Right now the library I am forced to use uses STL strings, which I believe to not be immutable. So if my data structure needs to return a substring from the large source text it would need to do copy every time. With immutable strings, the returned string object would only have to contain reference to underlying char array and two indecies.

Am I better off implementing my own immutable string class?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
const char *

RussianManiac
Dec 27, 2005

by Ozmaugh

Avenging Dentist posted:

const char *

That is not what I want. I want a wrapper around char that will act like normal std::string in most ways except that it would share underlying char * with other strings and substr operation would be really fast on it.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

RussianManiac posted:

That is not what I want. I want a wrapper around char that will act like normal std::string in most ways except that it would share underlying char * with other strings and substr operation would be really fast on it.

code:
struct some_string
{
    char *data;
    size_t length;
};

Contero
Mar 28, 2004

Is there a sign function? i.e. sign(-2.3) -> -1

I've always just done val/abs(val), but this is a little gross / bad if val == 0.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Contero posted:

Is there a sign function? i.e. sign(-2.3) -> -1

I've always just done val/abs(val), but this is a little gross / bad if val == 0.

1) It's usually called sgn
2) It's not there, but that's because it's trivial to write an efficient version: (x>0) - (x<0)

OddObserver
Apr 3, 2009

Contero posted:

Is there a sign function? i.e. sign(-2.3) -> -1

I've always just done val/abs(val), but this is a little gross / bad if val == 0.

There is a signbit(). Seems like C99 if I am reading this right:
http://www.opengroup.org/onlinepubs/000095399/functions/signbit.html

... Note that I am sure of what it does for negative 0. Maybe that's negative, and positive 0 is positive?

Vanadium
Jan 8, 2005

pre:
DESCRIPTION
       signbit()  is a generic macro which can work on all real floating-point
       types.  It returns a non-zero value if the value of x has its sign  bit
       set.

       This is not the same as x < 0.0, because IEEE 754 floating point allows
       zero to be signed.  The comparison -0.0  <  0.0  is  false,  but  sign‐
       bit(-0.0) will return a non-zero value.

tef
May 30, 2004

-> some l-system crap ->

RussianManiac posted:

:words:

http://en.wikipedia.org/wiki/Rope_(computer_science)

If you have large strings you may find ropes to be a useful representation of them


quote:

Am I better off implementing my own immutable string class?

Signs point to no.

tef fucked around with this message at 16:12 on Nov 1, 2009

RussianManiac
Dec 27, 2005

by Ozmaugh

tef posted:

http://en.wikipedia.org/wiki/Rope_(computer_science)

If you have large strings you may find ropes to be a useful representation of them


Signs point to no.

This would be a lot worse for my problem, as substring takes logarithmic time using ropes and using immutable string it will be constant. Oh well, I guess I will just implement my own very simple immutable string class. All I have is just a giant string that never changes, and a bunch of operations that work and/or return with substrings of this immutable string.

Adbot
ADBOT LOVES YOU

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

RussianManiac posted:

This would be a lot worse for my problem, as substring takes logarithmic time using ropes and using immutable string it will be constant. Oh well, I guess I will just implement my own very simple immutable string class. All I have is just a giant string that never changes, and a bunch of operations that work and/or return with substrings of this immutable string.
I think you need to remind yourself of how C-style strings work. (hint: you're not gonna be able to get substrings out of your big string in constant time, unless you're okay with overwriting your original array with '\0's.)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply