Your "effect" variable is null. Where is that variable defined? It isn't mentioned anywhere else in the code you posted.
I have some knowledge in Java so would it be possible to use this [url]http://code.google.com/p/kryonet/[/url] library to make a little console chat applet?
[QUOTE=Blueridge;34301779]What's a clear way of removing multiple elements in an array?
For a school assignment I have to make a class that holds an array of integers, and add methods that add to it, remove etc. Kinda like an ArrayList. The problem, however, is they want me to make a removeAll(int val) method that removes all occurrences of val from the list. For example, removeAll(4) would change
{1, 2, 4, 6, 3, 4, 4, 6, 5, 6, 4} to
{1, 2, 6, 3, 6, 5, 6, 0, 0, 0, 0}.
For my regular remove() method, I'm using Apache Common's Lang ArrayUtils class to remove the element. Removing all elements with it doesn't work fully, for some reason. My current plan of attack is to find out how many times val appears in the array, and then call ArrayUtils.remove() that many times. But it gives me weird results, for instance shrinking the array. Any other way to attack this? I can't use ArrayList supposedly, and I doubt they'll check if I am or not, but it would be nice to figure out how to do this the way they want me to.[/QUOTE]
This is what I've come up with:
[CODE]
public void removeAll(int val) {
int read = 0, write = 0, loop = 0;
boolean hasNum = false;
while(read <list.length) {
if(list[read] == val)
{
read++;
list[write] = list[read];
hasNum = true;
}
read++;
write++;
}
if(hasNum && read >= list.length) {
while(write < list.length) {
list[write] = 0;
write++;
}
}
}
public class IntegerListTest
{
static IntegerList list;
public static void main(String[]args)
{
list = new IntegerList(10);
int[] test = {1, 4, 6, 4, 2, 6, 4, 6, 7, 6};
//expected: {1, 6, 2, 6, 6, 7, 6, 0, 0, 0}
list.add(test);
list.removeAll(4);
list.print();
}
}
[/CODE]
And here's what I got instead of what was expected:
0: 1
1: 6
2: 2
3: 4
4: 6
5: 6
6: 4
7: 6
8: 0
9: 0
This time it gets 1 of the 4s(the first one), but not all of them. Not sure what I'm getting wrong here.
[QUOTE=Blueridge;34341890]This is what I've come up with:
[CODE]
public void removeAll(int val) {
int read = 0, write = 0, loop = 0;
boolean hasNum = false;
while(read <list.length) {
if(list[read] == val)
{
read++;
list[write] = list[read];
hasNum = true;
}
read++;
write++;
}
if(hasNum && read >= list.length) {
while(write < list.length) {
list[write] = 0;
write++;
}
}
}
public class IntegerListTest
{
static IntegerList list;
public static void main(String[]args)
{
list = new IntegerList(10);
int[] test = {1, 4, 6, 4, 2, 6, 4, 6, 7, 6};
//expected: {1, 6, 2, 6, 6, 7, 6, 0, 0, 0}
list.add(test);
list.removeAll(4);
list.print();
}
}
[/CODE]
And here's what I got instead of what was expected:
0: 1
1: 6
2: 2
3: 4
4: 6
5: 6
6: 4
7: 6
8: 0
9: 0
This time it gets 1 of the 4s(the first one), but not all of them. Not sure what I'm getting wrong here.[/QUOTE]
[cpp]void remove_array(int *list, int count, int value)
{
int read = 0, write = 0;
while (read < count)
{
while ((read < count) && (list[read] == value)) read++;
if (read < count)
list[write] = list[read];
write++;
read++;
}
while (write < count)
{
list[write] = 0;
write++;
}
}[/cpp]
[editline]22nd January 2012[/editline]
Your idea of having two running indices, one for reading and one for writing was a good one. I imagine my snippet is more like you intended it in your head
[QUOTE=ThePuska;34342023][cpp]void remove_array(int *list, int count, int value)
{
int read = 0, write = 0;
while (read < count)
{
while ((read < count) && (list[read] == value)) read++;
if (read < count)
list[write] = list[read];
write++;
read++;
}
while (write < count)
{
list[write] = 0;
write++;
}
}[/cpp]
[editline]22nd January 2012[/editline]
Your idea of having two running indices, one for reading and one for writing was a good one. I imagine my snippet is more like you intended it in your head[/QUOTE]
Converted it to Java. It sets the first appearance of 4 to 0, and then keept every other element the same.
[QUOTE=Wyzard;34340688]Your "effect" variable is null. Where is that variable defined? It isn't mentioned anywhere else in the code you posted.[/QUOTE]
Whups, sorry. It's defined at the top as
[csharp] Effect effect;[/csharp]
Can a programmer be liable for the program he makes?
I'm making a program that checks a auction site every 5 min and emails a list of people in a text file all auctions with trucks
[QUOTE=Mr. Smartass;34345263]Whups, sorry. It's defined at the top as
[csharp] Effect effect;[/csharp][/QUOTE]
That'll default to null since it's not initialized with a value. Chances are, you're using it without having assigned a value to it yet.
[QUOTE=Wyzard;34346166]That'll default to null since it's not initialized with a value. Chances are, you're using it without having assigned a value to it yet.[/QUOTE]
Nope, before the model draw calls I have this
[csharp]
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
device = graphics.GraphicsDevice;
shipModel = loadModel("grenadelauncher", out grenadeLauncherTextures);
effect = Content.Load<Effect>("effects");
starboxModel = loadModel("starbox", out starBoxTextures);
font = Content.Load<SpriteFont>("font");
}
[/csharp]
[QUOTE=Mr. Smartass;34346432]Nope, before the model draw calls I have this
[csharp]
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
device = graphics.GraphicsDevice;
shipModel = loadModel("grenadelauncher", out grenadeLauncherTextures);
effect = Content.Load<Effect>("effects");
starboxModel = loadModel("starbox", out starBoxTextures);
font = Content.Load<SpriteFont>("font");
}
[/csharp][/QUOTE]
Put a break point on that line and ensure that Content.Load<EFfect>("effects"); isn't returning null.
[QUOTE=Lord Ned;34346448]Put a break point on that line and ensure that Content.Load<EFfect>("effects"); isn't returning null.[/QUOTE]
I did, and it isn't;
The thing is, the error started after I changed the second model's effect type to textured from colored, so I'm 100% sure that that's where the error is coming from;
[csharp] private void drawWeapon()
{
SamplerState ss = new SamplerState();
ss.AddressU = TextureAddressMode.Clamp;
ss.AddressV = TextureAddressMode.Clamp;
device.SamplerStates[0] = ss;
DepthStencilState dss = new DepthStencilState();
dss.DepthBufferEnable = true;
device.DepthStencilState = dss;
//Matrix worldMatrix = weaponPositionMatrix;
Matrix[] weaponTransforms = new Matrix[shipModel.Bones.Count];
shipModel.CopyAbsoluteBoneTransformsTo(weaponTransforms);
int i = 0;
foreach (ModelMesh mesh in shipModel.Meshes)
{
//sets the effects for all meshes in the model
foreach (Effect currentEffect in mesh.Effects)
{
worldMatrix = weaponTransforms[mesh.ParentBone.Index];
currentEffect.CurrentTechnique = currentEffect.Techniques["Textured"];
currentEffect.Parameters["xWorld"].SetValue(weaponTransforms[mesh.ParentBone.Index] * worldMatrix);
currentEffect.Parameters["xView"].SetValue(viewMatrix);
currentEffect.Parameters["xProjection"].SetValue(projectionMatrix);
currentEffect.Parameters["xEnableLighting"].SetValue(true);
currentEffect.Parameters["xLightDirection"].SetValue(sunAngle);
currentEffect.Parameters["xTexture"].SetValue(grenadeLauncherTextures[i++]);
currentEffect.Parameters["xAmbient"].SetValue(0.5f);
}
mesh.Draw();
}
}
[/csharp]
I have a more conceptual question about pseudocode and planning the actual structure. How thoroughly do you prepare a relatively serious project theoretically before actually starting to code it? As far as I am aware, it is the best idea to create a language independent pseudocode structure before actually starting to create it.
If it's a complex enough program I'll spend some time thinking about overall architecture first, but I don't see much value in creating a pseudocode mockup of a whole program before starting to write the "real" code. To the extent that your real code matches what you wrote in pseudocode, you're just doing the same work twice. And you'll probably discover problems and design considerations while working on the real code that you didn't foresee while writing pseudocode, so you'll end up deviating from your plans anyway.
Some people like to use [url=https://en.wikipedia.org/wiki/Unified_Modeling_Language]UML[/url] to design a program in advance. That can be useful for high-level planning, but IMO trying to plan out all the details (like every method's argument list) is a waste,
Pseudocode is more useful for smaller things, like figuring out an algorithm without having to worry about details like memory management.
I can't actually start something.
I just got an idea, but I can't make myself open Code::Blocks and start programming. It's kind of a...laziness?
Really need help on this, I don't understand why I can't get myself to do any programming.
[QUOTE=Bumrang;34352475]I can't actually start something.
I just got an idea, but I can't make myself open Code::Blocks and start programming. It's kind of a...laziness?
Really need help on this, I don't understand why I can't get myself to do any programming.[/QUOTE]
I find just talking to other programmers [about programming] motivating
[QUOTE=ief014;34352898]I find just talking to other programmers [about programming] motivating[/QUOTE]
Or, if making a game, having an artist to create sprites for you. It helps me want to continue working when I can keep expanding instead of being limited in assets.
Obj-C
passing a mutable array from the appDelegate to the view controller. It's a big long array of restaurants that are tagged on a map. The appDelegate pulls the data from a plist and makes a nice array and then passes it to the viewController to add the info to the map.
The appDelegate does it's job right, the NSLogs prove it. But once the viewController gets it... it's a null array.
App Delegate code
self.viewController.restaurants = restaurants;
And in View Controller
NSLog(@"THIS IS, %@",restaurants);
returns "THIS IS (null)"
What's going on and why doesn't this work?
I don't really know if anyone would be willing to help with this, as (at least to me) it seems possibly pretty complicated, but here goes:
I have a rectangle with known coordinates. Inside this rectangle is another, smaller polygon; its coordinates are also known. I then have a large set of coordinates that represent another polygon, which may or may not be concave.
I'm hoping to come up with a way of finding the best way to fit said curve so that all its points are inside the outer rectangle, but the inner polygon must be inside this curve. The inner polygon must remain in a fixed position, and the set of points representing the curve can be translated, rotated and scaled (as long as the scaling is done in both x and y simultaneously and by an equal factor).
So uh, yeah. I have absolutely no idea as to where I should even begin with this. :suicide:
If anyone could even just point me in the vaguely right direction regarding to where the hell I should start, I would be very, very grateful.
also i hope this is in the right section, as the problem seems to be leaning more towards a maths problem than a programming problem, i guess
[QUOTE=ned_ballad;34358033]Obj-C
passing a mutable array from the appDelegate to the view controller. It's a big long array of restaurants that are tagged on a map. The appDelegate pulls the data from a plist and makes a nice array and then passes it to the viewController to add the info to the map.
The appDelegate does it's job right, the NSLogs prove it. But once the viewController gets it... it's a null array.
App Delegate code
self.viewController.restaurants = restaurants;
And in View Controller
NSLog(@"THIS IS, %@",restaurants);
returns "THIS IS (null)"
What's going on and why doesn't this work?[/QUOTE]
If the View Controller's array is a property try [self restaurants] or self.restaurants in NSLog.
it ended up being that I was passing the array before the view controller was created
6 hours...
SIX HOURS
and it was the wrong order messing me up
:(
Hey guys, more recursion...
[cpp]#include <iostream>
using namespace std;
double findLargestElement(double arr[], int, int);
int main()
{
const int SIZE = 50;
double arr[SIZE];
double largest;
int n;
cout << "How many elements are in the array, arr? ";
cin >> n;
cout << "Please enter the numbers for the array, arr:\n";
for (int i = 0; i < n; i++)
{
cout << "Enter arr[" << i << "]: ";
cin >> arr[i];
}
largest = findLargestElement(arr, SIZE, n);
cout << "The largest value of array, arr is " << largest << ".\n";
return 0;
}
double findLargestElement(double arr[], int SIZE, int n)
{
if (n == 0)
return arr[0];
return findLargestElement(arr, SIZE, n - 1);
}[/cpp]
I seriously can not figure this out, I am suppose to find the largest element of the array recursively, but I have no idea how to do it.
My professor gave me this hint.
[I]I could find the largest among arr[0] through arr[j] if I could find the largest among arr[0] through arr[j - 1]. I would compare that answer with ....[/I]
[QUOTE=Leonmyster;34368743]
My professor gave me this hint.
[I]I could find the largest among arr[0] through arr[j] if I could find the largest among arr[0] through arr[j - 1]. I would compare that answer with ....[/I][/QUOTE]
What your professor is saying is he can find the largest element in the interval\range 0 to j, by comparing that value with the largest element in the interval 0 to j-1. That's how the recursion works.
If you're trying to find the largest element in the interval 0 to 0, it's gotta be whatever value you have, you can't recurse further.
This should be more than enough help.
[QUOTE=Leonmyster;34368743]Hey guys, more recursion...
..code..
I seriously can not figure this out, I am suppose to find the largest element of the array recursively, but I have no idea how to do it.
My professor gave me this hint.
[I]I could find the largest among arr[0] through arr[j] if I could find the largest among arr[0] through arr[j - 1]. I would compare that answer with ....[/I][/QUOTE]
The max of a list with one element in it is that element.
The max of a list with two elements in it is the max of the first and the second. You can take this rule and generalize it to any list by looking at the list like this:
[1, 2, 3, 4] => [1, [2, [3, [4]]]]
So to get the max of a list, you compare the first item with the max of the list in the second slot:
[img]http://i.imgur.com/pB82x.png[/img]
It's a long shot, but does anyone have any experience with networking on source? I need to pass an entity handle to the client (and read networked values from the entity) through a usermessage.
When encoding it for a usermessage, it encodes it as a long (through WRITE_EHANDLE). I can't find any examples of how to get the entity back from this long value on the client. Anyone able to point me in the right direction? Struggling to find any examples of it being used anywhere in the code so I'm a bit confused, I'm probably going about it the totally wrong way.
[QUOTE=Jimbomcb;34371214]It's a long shot, but does anyone have any experience with networking on source? I need to pass an entity handle to the client (and read networked values from the entity) through a usermessage.
When encoding it for a usermessage, it encodes it as a long (through WRITE_EHANDLE). I can't find any examples of how to get the entity back from this long value on the client. Anyone able to point me in the right direction? Struggling to find any examples of it being used anywhere in the code so I'm a bit confused, I'm probably going about it the totally wrong way.[/QUOTE]
IClientEntityList provides methods for getting entity from the handle.
[cpp]abstract_class IClientEntityList
{
public:
// Get IClientNetworkable interface for specified entity
virtual IClientNetworkable* GetClientNetworkable( int entnum ) = 0;
virtual IClientNetworkable* GetClientNetworkableFromHandle( CBaseHandle hEnt ) = 0;
virtual IClientUnknown* GetClientUnknownFromHandle( CBaseHandle hEnt ) = 0;
// NOTE: This function is only a convenience wrapper.
// It returns GetClientNetworkable( entnum )->GetIClientEntity().
virtual IClientEntity* GetClientEntity( int entnum ) = 0;
virtual IClientEntity* GetClientEntityFromHandle( CBaseHandle hEnt ) = 0;
//...
};[/cpp]
For example:
[cpp]C_BaseEntity *ent = static_cast<C_BaseEntity*>(source::g_interfaces->EntityList->GetClientEntityFromHandle(hnd));
if (!ent) return nullptr;[/cpp]
e: EHANDLE is a typedef of CHandle<CBaseEntity>, which in turn is derived from CBaseHandle, so it should work with this.
[QUOTE=raBBish;34371325]IClientEntityList provides methods for getting entity from the handle.
[cpp]abstract_class IClientEntityList
{
public:
// Get IClientNetworkable interface for specified entity
virtual IClientNetworkable* GetClientNetworkable( int entnum ) = 0;
virtual IClientNetworkable* GetClientNetworkableFromHandle( CBaseHandle hEnt ) = 0;
virtual IClientUnknown* GetClientUnknownFromHandle( CBaseHandle hEnt ) = 0;
// NOTE: This function is only a convenience wrapper.
// It returns GetClientNetworkable( entnum )->GetIClientEntity().
virtual IClientEntity* GetClientEntity( int entnum ) = 0;
virtual IClientEntity* GetClientEntityFromHandle( CBaseHandle hEnt ) = 0;
//...
};[/cpp]
For example:
[cpp]C_BaseEntity *ent = static_cast<C_BaseEntity*>(source::g_interfaces->EntityList->GetClientEntityFromHandle(hnd));
if (!ent) return nullptr;[/cpp]
e: EHANDLE is a typedef of CHandle<CBaseEntity>, which in turn is derived from CBaseHandle, so it should work with this.[/QUOTE]
Thanks for the help. I must be messing up somewhere else along the line. The handle I'm getting on the receiving end of the usermessage isn't valid according to "ClientEntityList().IsHandleValid" and it just returns a null pointer using the code you suggested, time to have a dig around and see why it's doing this.
[editline]24th January 2012[/editline]
sorted!
Hey guys! I have been fascinated by the mandelbrot and other fractals for a while now, but I never expected the maths behind them to be so simple. Anyway, i tried programming the mandelbrot, but apparently it is not quite as simple as I thought:
[code]
int xPix = 0, yPix = 0;
float x0, y0 ,x, y, x2, y2;
int iteration;
int max_iteration = 10;
for(xPix = 0; xPix < 800; xPix++)
{
for(yPix = 0; yPix < 600; yPix++)
{
x0 = xPix / 228.5714f - 2.5f;
y0 = yPix / 300 -1.0f;
x = x0;
y = y0;
x2 = x*x;
y2 = y*y;
iteration = 0;
while (x2 + y2 < 4 && iteration < max_iteration)
{
y = 2*x*y + y0;
x = x2 - y2 + x0;
x2 = x*x;
y2 = y*y;
iteration++;
}
if(iteration <= 0)
{
mandelbrot.SetPixel(xPix, yPix, sf::Color(0,0,0));
}
else
{
mandelbrot.SetPixel(xPix, yPix, sf::Color(255*sin((float)iteration), 255*cos((float)iteration), 255*-sin((float)iteration)));
}
}
}
[/code]
This is what I get, I call it the Mandelnot:
[img]http://i.imgur.com/KEuQe.png[/img]
I have tweeked so many things, that I am sure I have done something fundamentally wrong. I just don't know where. If anyone can help me I would be quite happy!
[code]y0 = yPix / 300 -1.0f;[/code]
Put a .0f at the end of 300.
Oh.. My.. God.....
[QUOTE=swift and shift;34370389][img]http://i.imgur.com/pB82x.png[/img][/QUOTE]
A simpler way to write the second definition is:
[code]my_max (x:xs) = max x $ my_max xs[/code]
Sorry, you need to Log In to post a reply to this thread.